arduino USB条码扫描器不发送帖子json

问题描述

arduino uno + USB收益+ ecn28j60

我合并了两个代码

[此发送帖子]和[此usb扫描条形码]

我无法在服务器端接收POST数据, 如果我注释了 Usb.Task();,我将无法使用$ _POST访问帖子数据。 然后开始工作。

#include <usbhid.h>
#include <usbhub.h>
#include <hiduniversal.h>
#include <hidboot.h>
#include <SPI.h>
#include <EtherCard.h>

String barcode = "";


#define PATH    "api/v2/control"


// ethernet interface mac address,must be unique on the LAN
byte mymac[] = { 0x74,0x69,0x2D,0x30,0x31 };

const char website[] PROGMEM = "pevchee.jeck.com.ua";

byte Ethernet::buffer[700];
uint32_t timer;
Stash stash;








class MyParser : public HIDReportParser {
  public:
    MyParser();
    void Parse(USBHID *hid,bool is_rpt_id,uint8_t len,uint8_t *buf);
  protected:
    uint8_t KeyToAscii(bool upper,uint8_t mod,uint8_t key);
    virtual void OnKeyScanned(bool upper,uint8_t key);
    virtual void OnScanFinished();
};

MyParser::MyParser() {}

void MyParser::Parse(USBHID *hid,uint8_t *buf) {
  // If error or empty,return
  if (buf[2] == 1 || buf[2] == 0) return;

  for (uint8_t i = 7; i >= 2; i--) {
    // If empty,skip
    if (buf[i] == 0) continue;

    // If enter signal emitted,scan finished
    if (buf[i] == UHS_HID_BOOT_KEY_ENTER) {
      OnScanFinished();
    }

    // If not,continue normally
    else {
      // If bit position not in 2,it's uppercase words
      OnKeyScanned(i > 2,buf,buf[i]);
    }

    return;
  }
}

uint8_t MyParser::KeyToAscii(bool upper,uint8_t key) {
  // Letters
  if (VALUE_WITHIN(key,0x04,0x1d)) {
    if (upper) return (key - 4 + 'A');
    else return (key - 4 + 'a');
  }

  // Numbers
  else if (VALUE_WITHIN(key,0x1e,0x27)) {
    return ((key == UHS_HID_BOOT_KEY_ZERO) ? '0' : key - 0x1e + '1');
  }

  return 0;
}

void MyParser::OnKeyScanned(bool upper,uint8_t key) {
  uint8_t ascii = KeyToAscii(upper,mod,key);
  barcode = barcode + ((char)ascii);
}

void MyParser::OnScanFinished() {
Serial.println("SCAN + " + barcode);
}

USB          Usb;
USBHub       Hub(&Usb);
HIDUniversal Hid(&Usb);
MyParser     Parser;

void setup() {
  Serial.begin( 115200 );

////////////////////////////////////////////////////////////////////////////////////
  Serial.println("\n[webClient]");

  if (ether.begin(sizeof Ethernet::buffer,mymac,8) == 0) 
    Serial.println( "Failed to access Ethernet controller");
  if (!ether.dhcpsetup())
    Serial.println("DHCP Failed");

  ether.printIp("IP:  ",ether.myip);
  ether.printIp("GW:  ",ether.gwip);  
  ether.printIp("DNS: ",ether.dnsip);  

  if (!ether.dnsLookup(website))
    Serial.println("DNS Failed");

  ether.printIp("SRV: ",ether.hisip);

///////////////////////////////////////////////////////////////////////////////////////  
  Serial.println("Start SCAN");

  if (Usb.Init() == -1) {
    Serial.println("OSC did not start.");
  }

  delay( 200 );

  Hid.SetReportParser(0,&Parser);
}

void loop() {
  Usb.Task();
  
   
  ether.packetLoop(ether.packetReceive());

  if (millis() > timer) {
    timer = millis() + 10000;

    byte sd = stash.create();
    stash.print("{\"device_id\":73,\"barcode\":\"");
    stash.print("rtyry");
    stash.print("\"}");
    stash.save();

    // generate the header with payload - note that the stash size is used,// and that a "stash descriptor" is passed in as argument using "$H"
    Stash::prepare(PSTR("POST http://$F/$F HTTP/1.0" "\r\n"
                "Host: $F" "\r\n"
                "Content-Length: $D" "\r\n"
                "Content-Type: application/json" "\r\n"
                "\r\n"
                "$H"),website,PSTR(PATH),stash.size(),sd);

    // send the packet - this also releases all stash buffers once done
    ether.tcpsend();
  }
  
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)