0

I am using an Arduino Uno and a USB Host shield for Barcode Scanner. The whole scenario is that when I scan the barcode through USB Barcode Scanner, it scans and gives a code. Now the question is how to connect the ESP8266 with the USB Host Shield to transfer data to the web server. I have set the pins to 2, 3 for receiving data from server. Send Data and Receive data with ESP8266.

The Arduino Code:

#include <SoftwareSerial.h>
#include <hid.h>
#include <hiduniversal.h>
#include <LiquidCrystal.h>
#include <avr/pgmspace.h>
#include <Usb.h>
#include <usbhub.h>
#include <avr/pgmspace.h>
#include <hidboot.h>
String result = "";
#define DISPLAY_WIDTH 16


// initialize the LCD library with the numbers of the interface pins
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
SoftwareSerial esp(3, 2);

USB     Usb;
USBHub     Hub(&Usb);
HIDUniversal Hid(&Usb);
HIDBoot<HID_PROTOCOL_KEYBOARD>    Keyboard(&Usb);

class KbdRptParser : public KeyboardReportParser
{
 void PrintKey(uint8_t mod, uint8_t key);
protected:
  virtual void OnKeyDown  (uint8_t mod, uint8_t key);
  virtual void OnKeyPressed(uint8_t key);
};

void KbdRptParser::OnKeyDown(uint8_t mod, uint8_t key)  
{


    uint8_t c = OemToAscii(mod, key);

    if (c)
        delay(100);
       OnKeyPressed(c);


}

/* what to do when symbol arrives */
void KbdRptParser::OnKeyPressed(uint8_t key)  
{
static uint32_t next_time = 0;      //watchdog
static uint8_t current_cursor = 0;  //tracks current cursor position  

    if( millis() > next_time ) {
      lcd.clear();
      result = "";
      current_cursor = 0;
      delay( 5 );  //LCD-specific 
      lcd.setCursor( 0,0 );
    }//if( millis() > next_time ...

    next_time = millis() + 200;  //reset watchdog

    if( current_cursor++ == ( DISPLAY_WIDTH + 1 )) {  //switch to second line if cursor outside the screen
      lcd.setCursor( 0,1 );
    }


 result += (char)key;//Serial.print("Result "+result+"\n");
 delay( 50 );
    //Serial.print((char)key );
    //lcd.print((char)key );
};

KbdRptParser Prs;

void setup()
{
    Serial.begin(9600);
    Serial.println("Start");
    esp.begin(115200);
    if (Usb.Init() == -1) {
        Serial.println("OSC did not start.");
    }

    delay( 200 );

    Hid.SetReportParser(0, (HIDReportParser*)&Prs);
    // set up the LCD's number of columns and rows: 
    lcd.begin(DISPLAY_WIDTH, 2);
    lcd.clear();
    lcd.noAutoscroll();
    lcd.print("Ready");
    delay( 200 );

}

void loop()
{
  String content = "";
  char character;
  Usb.Task();  
  if(result != ""){
    delay(10);
    Serial.println(result);
    } 
   /*while(Serial.available()){
    character = Serial.read();
    content.concat(character);
    Serial.print("Serial Result : "+content);

    }*/
    if (esp.available())
   {

    char c = esp.read();
    if(isDigit(c)){
      lcd.clear();
      lcd.print(c);  
        Serial.print(c);

       }
    } 

}

The ESP8266 Code is:

#include <ESP8266WiFi.h>

const char* ssid     = "stormfiber"; //Wi-Fi SSID
const char* password = "11223344"; //Wi-Fi Password

//Server IP or domain name
const char* host = "192.168.100.228";
void setup()
{
  Serial.begin(115200);
  Serial1.begin(9600);
  WiFi.begin(ssid, password);
  Serial.println(ssid);
  while (WiFi.status() != WL_CONNECTED) { //Wait till connects
    delay(500);
    Serial.print(".");
  }


}

void loop()
{
  char character;
  String content = "";
  delay(5000);

  //Serial.print("connecting to ");
  //Serial.println(host);
  while(Serial1.available()){
    character = Serial1.read();
    content.concat(character);
    }
    if(content == ""){
      content = "123";
     }
  WiFiClient client; //Client to handle TCP Connection
  const int httpPort = 2124;
  if (!client.connect(host, httpPort)) { //Connect to server using port httpPort
    Serial.println("connection failed");
    return;
  }

  // We now create a URI for the request
  String url = "/test/index.php?code="+content; //File or Server page you want to communicate with. along with data

  // This will send the request to the server 
  client.print(String("GET ") + url + "\r\n"); 

  unsigned long timeout = millis();
  while (client.available() == 0) {
    if (millis() - timeout > 25000) { //Try to fetch response for 25 seconds
      Serial.println(">>> Client Timeout !");
      client.stop();
      return;
    }
  }

  // Read all the lines of the reply from server and print them to Serial
  while(client.available()){
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }
  Serial.println();
  //Serial.println("closing connection");
  client.stop(); //Close Connection
  /*static unsigned long timer = 0;
  unsigned long interval = 5000;
  static unsigned int index = 0;
  if (millis() - timer >= interval)
  {    
    timer = millis();
    Serial.print("The index ");
    Serial.println(index);
    index++;
  }*/
}

Please help how to connect the ESP8266 with the host shield. Although it works fine with Arduino only. Any help regarding this will be appreciated. Thanks

  • the USB host shield doesn't use pins 2 and 3 but your LCD setup does – Juraj Mar 8 at 12:19
  • Thanks for the answer but how we can connect esp8266 communicating with USB Host Shield. Is there any possibility? – Muhammad Adil Khan Mar 8 at 12:21
  • Can we read data from 2,3 pin by esp8266? – Muhammad Adil Khan Mar 8 at 12:22
  • what problem do you have with the USB shield? it changes nothing for the SoftwareSerial on pins 2 an 3 – Juraj Mar 8 at 12:22
  • yes it shows nothing – Muhammad Adil Khan Mar 8 at 12:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.