Skip to content

Instantly share code, notes, and snippets.

@alanzhaonys
Forked from joseftsch/wemos_usbhost.md
Created June 5, 2021 22:56
Show Gist options
  • Save alanzhaonys/8f3236d99ae4425283ba28ff78b28038 to your computer and use it in GitHub Desktop.
Save alanzhaonys/8f3236d99ae4425283ba28ff78b28038 to your computer and use it in GitHub Desktop.
wemos usb host

Today I connected my USB host board (see "hardware" section below) to my Wemos 1D device. It receives data from an outdoor "weather station receiver" and prints in to serial console in Arduino IDE.

Hardware:

Arduino libraries:

Connection diagram an pins used:

WEMOS 1D | USB HOST Board
5V   +-->  5V
GND  +-->  0V
D1   +-->  RX
D2   +-->  TX

Arduino code:

#include <SoftwareSerial.h>  //https://github.com/plerup/espsoftwareserial
SoftwareSerial swSer(D2, D1, false, 64); // D2 = TX; D1 = RX;

String inputString = "";         // string, for incoming data from swserial
boolean stringComplete = false;  // is string completed?

void setup() {
  Serial.begin(115200); // Serial com to PC
  swSer.begin(9600); // Serial com to usbhost device
  Serial.println("Startup!"); // Print startup message
}

void loop() {
  serialEvent(); //Call serialEvent function to get data from serial device
  if (stringComplete) {
      if (inputString.startsWith("$")) { // print inputString only if it starts with $ as we dont need debug messages
        Serial.print(inputString); // as the string contains a newline at the end we dont need a "println"
      } 
      inputString = "";
      stringComplete = false;
    }
}

void serialEvent() {
  while (swSer.available()>0) {
    char inChar = (char)swSer.read(); // get data from swSer 
    inputString += inChar; // concatenate received data
    if (inChar == '\n') { // if we receive a newline char the string is finished
      stringComplete = true;
    }
  }
}

Arduino console output:

Startup!
$1;1;;;23,4;;;;;;;;51;;;;;;;12,0;46;0,0;9;0;0
$1;1;;;23,3;;;;;;;;51;;;;;;;12,0;46;0,0;9;0;0
$1;1;;;23,3;;;;;;;;51;;;;;;;11,9;46;0,0;9;0;0

Several snippets of code used - main sources:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment