I'm trying to send information from an Arduino to a NodeMCU board via the software serial library so i can then send that data to a local server i have running via post request, the data does seem to arrive to the NodeMCU but all it does is take over the the serial monitor and print itself over and over while i can't access it through serial.read( ) or serial.readString( ), the post request is being sent but with an empty string and if i hard code it to send over a string to the server it does send it succesfully so it looks like it's a problem with the serial transmission that i'm not able to get that data over to the string i'm sending over to the server, i can't figure out what i'm doing wrong.
Arduino UNO code:
#include <SoftwareSerial.h>
SoftwareSerial espSerial(6, 7); //6 to RX, 7 to TX
String str;
void setup(){
Serial.begin(115200);
espSerial.begin(9600);
delay(2000);
}
void loop(){
delay(5000);
float chairNum = 1, data = 34; //placeholder data
//begin byte
espSerial.write(0x01);
str = "";
str += ("chairNum=");
str += String(chairNum, 1);
str += ("&data=");
str += String(data, 1);
//string data being sent
espSerial.print(str);
//end byte
espSerial.write(0x02);
}
ESP-12E code:
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
//wifi credentials
const char* ssid = "WiFi__EXT";
const char* password = "sdfdfse";
//Your Domain name with URL path or IP address with path
const char* serverName = "http://192.168.0.12:3000/chairTest";
// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastTime = 0;
// Timer set to 10 minutes (600000)
//unsigned long timerDelay = 600000;
// Set timer to 5 seconds (5000)
unsigned long timerDelay = 5000;
//variables for the byte transfer logic
int i = 0;
char dataArray[50] = "";
bool flag1 = false;
void setup() {
Serial.begin(9600);
WiFi.begin(ssid, password);
Serial.println("Connecting");
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());
Serial.println("Timer set to 5 seconds (timerDelay variable), it will take 5 seconds before publishing the first reading.");
}
void loop() {
//Send an HTTP POST request every x seconds
if ((millis() - lastTime) > timerDelay) {
//Check WiFi connection status
if(WiFi.status()== WL_CONNECTED){
WiFiClient client;
HTTPClient http;
// Your Domain name with URL path or IP address with path
http.begin(client, serverName);
// Specify content-type header
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
// Data to send with HTTP POST
String message = "";
byte n = Serial.available();
if (n != 0){
if (flag1 == false){
byte x = Serial.read();
if (x == 0x01){
Serial.print("Received start.");
flag1 = true;
}
}else{
byte x1 = Serial.read();
if (x1 != 0x02){
dataArray[i] = x1;
i++;
}else{
dataArray[i] = 0x00; //nul-byte
Serial.println(dataArray);
message = dataArray;
i = 0;
flag1 = false;
}
}
}
String httpRequestData = message;
Serial.println(httpRequestData);
// Send HTTP POST request
int httpResponseCode = http.POST(httpRequestData);
Serial.print("HTTP Response code: ");
Serial.println(String(httpResponseCode));
// Free resources
http.end();
}
else {
Serial.println("WiFi Disconnected");
}
lastTime = millis();
}
}
I've copied the byte receiving logic from another topic to see if it would fix my problem but it's the same result even if i just try to get the data using a single serial.readString( ) and i'm using the latest version to date on IDE and all the libraries.