// csvESP8266SoftwareParser.ino // download SafeString V4.1.5+ library from the Arduino Library manager or from // https://www.forward.com.au/pfod/ArduinoProgramming/SafeString/index.html #include "SafeString.h" #include "SoftwareSerial.h" const int RX_pin = 13; // for ESP8266 use 13 D7 on NodeMCU/wemos-d1-esp8266 const int TX_pin = 12; // for ESP8266 use 12 D6 on NodeMCU/wemos-d1-esp8266 // else you need to define your own RX_pin, TX_pin SoftwareSerial softSerial(RX_pin, TX_pin); // RX, TX float longitudeIn; float latitudeIn; cSF(sfInput, 60); // enough for 60char input line // ESP8266 software serial has 64byte RX buffer bool decodeCSV(SafeString &sfCSV, float &longitude, float &latitude) { if (!sfCSV.endsWith('\n')) { // input filled up but no \n Serial.print(F(" missing \\n terminator ")); Serial.println(sfCSV); return false; } Serial.print("sfCSV: "); Serial.print(sfCSV); float lng = 0, lat = 0; cSF(field, 20); // to hold numbers sfCSV.firstToken(field, ',', true); // return empty fields to detect missing data if (!field.toFloat(lng)) { // ignores leading and trailing whitespace Serial.print(F("longitude not a valid float '")); Serial.print(field); Serial.println("'"); return false; } sfCSV.nextToken(field, ',', true); if (!field.toFloat(lat)) { // ignores leading and trailing whitespace Serial.print(F("latitude not a valid float '")); Serial.print(field); Serial.println("'"); return false; } // else check no extra fields if (!sfCSV.isEmpty()) { Serial.print(F("More than two fields. Remaining data:")); Serial.print(sfCSV); return false; } // else all OK update returns longitude = lng; latitude = lat; return true; } void setup() { Serial.begin(115200); for (int i = 10; i > 0; i--) { Serial.print(' '); Serial.print(i); delay(500); } Serial.println(); SafeString::setOutput(Serial); Serial.println(F("Receiver started")); softSerial.begin(9600); } void loop() { if (sfInput.readUntil(softSerial, '\n')) { // returns true if found \n OR reached sfInput limit // if \n found it is returned. if (decodeCSV(sfInput, longitudeIn, latitudeIn)) { // got new valid inputs update Serial.print(F(" new longitude:")); Serial.print(longitudeIn); Serial.print(F(" new latitude:")); Serial.print(latitudeIn); Serial.println(); } else { // error in CSV nothing updated } sfInput.clear(); // for next line } }