Sending Serial data from UNO to NodeMCU

Hi all

Background - I know a bit of programming but nothing in depth so apologies in advance for any glaring errors.

I have an issue with my code.

I have an UNO which is relaying joystick(potentionmeter) data that I want to send via serial to a ESP8266. This then sends the pot values to another ESP8266 via wifi to control a motor.

The issue is between the UNO and the serial connected ESP8266.

When I connect the serial ports together and do a simple serial.print with no other code(such as the wifi code) I get the values as expected between the UNO and the ESP8266.

However once I introduce the code to send this data to the second ESP8266 I get fragmented values.

For example

256
126
26
56
1

etc

I think I am introducing a serial read without allowing the serial buffer to finish but I don't know how to implement this into my code.

Please help if possible...this has me really stumped. I've tried serial.flush, writing 0 to the output value etc to no avail.

Thanks in advance.

Controller (UNO)

// //Sensor Assignments

int sensorPinY = A0; //Tank Forward,Reverse
int sensorPinX = A1; //Tank Track Left, Right
int sensorValueY = 0; 
int sensorValueX = 0;
String xVal;
String yVal;


void setup() {
 
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:

sensorValueY = map(analogRead(sensorPinY),0,700,88,166); //Mapped Acceleration to be 0-10
sensorValueX = map(analogRead(sensorPinX),0,700,222,289); //Mapped Turn to be 11-20

//Serial.print("Y: ");
//yVal = String(sensorValueY);
Serial.println(sensorValueY);
Serial.print('\n');
//Serial.print("X: ");
//xVal = String(sensorValueX);
Serial.println(sensorValueX);
Serial.print('\n');
//delay(); //Update 100MS



}

ESP8266 Recieving Serial Data

#include <ESP8266WiFi.h>

const char *ssid = "tankcontrol";
const char *password = "k1a1tank";
int outputValue = 0;
String teststring = "";


void setup() {
  Serial.begin(9600);
  delay(10);

  // //We start by connecting to a WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  // /* Explicitly set the ESP8266 to be a WiFi-client, otherwise, it by default,
  //    would try to act as both a client and an access-point and could cause
  //    network-issues with your other WiFi-devices on your WiFi-network. */
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());


}

void loop() {
  // read the analog in value:



//teststring = Serial.readStringUntil('\n');
 outputValue = Serial.parseInt();
 delay(5);   


  // print the results to the Serial Monitor:


 Serial.println(outputValue);
 // Serial.println(teststring);




  // // Use WiFiClient class to create TCP connections
  WiFiClient client;
  const char * host = "192.168.4.1";
  const int httpPort = 80;

  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  }


//   // We now create a URI for the request
 // outputValue = teststring.toInt();

  String url = "/data/";
  url += "?sensor_reading=";
  url += outputValue;


Serial.print("Requesting URL: ");
Serial.println(url);

//   // // This will send the request to the server
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "Connection: close\r\n\r\n");
  unsigned long timeout = millis();
  while (client.available() == 0) {
    if (millis() - timeout > 5000) {
//       Serial.println(">>> Client Timeout !");
      client.stop();
      return;
    }
  }

  Serial.println();
  Serial.println("Closing connection");
  Serial.println();
  Serial.println();
  Serial.println();

  delay(5);




}

I suggest that you have a look at the serial input basics tutorial. The tutorial shows robust and reliable ways to read serial data.

OK thanks I think I've got things in a better state...instead of directly parsing integers I have used a single char and let a specific car be an integer value. I have then put a return to bin any characters I don't want and start back on the loop.

I haven't fully tested by I'm getting less trash data and will report back.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.