Mega+WiFi board not sending correct sensor data

All,

I am having a mega+wifi controller, which is a great board. I want to capture analog sensor data on pin A0 and post this value to a webserver through the ESP8266 module.

I got nearly eveything working except of the value that I capture on A0 is not being recognized/sent correctly through/to the ESP8266, instead I get some other values that do not seem to make sense.

On the mega module I have this code and it is running well, on serial I get good values and if I play with the sensor (moisture sensor) I see how values go up or down.

#define SensorPin A0

float sensorValue = 0;

String postmessage = "";
void setup(){
  Serial.begin(9600);
  Serial3.begin(115200);
}

void loop(){
 for (int i = 0; i <= 100; i++) 
 { 
   sensorValue = sensorValue + analogRead(SensorPin); 
   delay(1); 
 } 
 sensorValue = sensorValue/100.0; 
/// Serial.println(sensorValue); 
 delay(30); 

  postmessage = "";
  postmessage += sensorValue;

  Serial3.println(postmessage);

  Serial.println(postmessage);
  
  delay(1000);
}

The mega serial monitor output on BAUD 9600 should be based on Serial.println(postmessage), this looks good, I get a continuous flow of values (checked in mode 1: off, 2: off, 3: on, 4: on, 5: off, 6: off, 7: off; RXD/TXD switch on RXD3/TXD3) BAUD 9600:

848.94
849.07
849.11
849.52
848.98
849.17
849.40
849.10

As the switch on the board stating RXD0/TXD0 and RXD3/TXD3 is put on RXD3/TXD3, my understanding is, this should make the ESP8266 feed based on Serial3.println(postmessage).

Now to the ESP8266 code:

Need to post as an “answer”, I got beyond 9000 characters with the post, see below for the code.

At the bottom you see the reference from where I got this. Btw, the whole sending to server/php part works great but when I look at what the ESP8266 captures and posts is the following (checked in mode 1: off, 2: off, 3: off, 4: off, 5: on, 6: on, 7: off; RXD/TXD switch on RXD3/TXD3), BAUD 115200:

⸮⸮⸮rlConnecting

.
Connected to WiFi network with IP Address: x.x.x.x
httpRequestData: api_key=A&sensor=moist01&location=loc01&value1=126
HTTP Response code: 200
httpRequestData: api_key=A&sensor=moist01&location=loc01&value1=151
HTTP Response code: 200
httpRequestData: api_key=A&sensor=moist01&location=loc01&value1=103
HTTP Response code: 200

So, the problem that I have is that the sensor values I get from the mega are not ending up correctly at the ESP8266. They should be something around 850 but instead they are somewhere between 100 and 170 with huge variations.

I tested in following modes:
CH340 connect to Mega COM3 connect to ESP8266
1: on, 2: on, 3: on, 4: on, 5: off, 6: off, 7: off; RXD/TXD switch on RXD3/TXD3
This mode allows me only to see the mega values on serial with BAUD 9600, all other BAUDs show garbage values, on the server I see that the ESP8266 posts the bad values that I could see when checking the Serial of the ESP8266 on BAUD 115200 (i.e. ranging from 90-100).

Mega+ESP8266
1: on, 2: on, 3: off, 4: off, 5: off, 6: off, 7: off; RXD/TXD switch on RXD3/TXD3
As the USB is off here I can only see that on the server the values posted are still garbage ranging 115-155.
When, for testing, I disconnect the sensor pin from A0, I get values of around 100.

Here is an extract of what is being tracked on the server:

ID Sensor Location Value 1 Timestamp
581 moist01 loc01 102 2020-12-30 18:02:30
580 moist01 loc01 102 2020-12-30 18:02:00
579 moist01 loc01 106 2020-12-30 18:01:30
578 moist01 loc01 161 2020-12-30 18:01:00
577 moist01 loc01 151 2020-12-30 18:00:30

I read through several discussion on this or similar topics and could not find a solution or guidance in the right direction.

Appreciate if you could provide guidance and help me getting this last piece to work properly.

Cheers!

Here is the ESP8266 code…

Now to the ESP8266 code:

#ifdef ESP32
  #include <WiFi.h>
  #include <HTTPClient.h>
#else
  #include <ESP8266WiFi.h>
  #include <ESP8266HTTPClient.h>
  #include <WiFiClient.h>
#endif


// Replace with your network credentials
const char* ssid     = "ssid"; 
const char* password = "password";

float postmessage;

// REPLACE with your Domain name and URL path or IP address with path
const char* serverName = "aaa.aa/post-esp-data.php";

// Keep this API Key value to be compatible with the PHP code provided in the project page. 
// If you change the apiKeyValue value, the PHP file /post-esp-data.php also needs to have the same key 
String apiKeyValue = "A";

String sensorName = "moist01";
String sensorLocation = "loc01";


void setup() {
  Serial.begin(115200);
  
  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());

}

void loop() {
  postmessage = String().toFloat();
  
  //Check WiFi connection status
  if(WiFi.status()== WL_CONNECTED){
    HTTPClient http;
    
    // Your Domain name with URL path or IP address with path
    http.begin(serverName);
    
    // Specify content-type header
    http.addHeader("Content-Type", "application/x-www-form-urlencoded");
    
    // Prepare your HTTP POST request data
    String httpRequestData = "api_key=" + apiKeyValue + "&sensor=" + sensorName
                          + "&location=" + sensorLocation + "&value1=" + analogRead(A0)
                          + "";
    Serial.print("httpRequestData: ");
    Serial.println(httpRequestData);
    
    // You can comment the httpRequestData variable above
    // then, use the httpRequestData variable below (for testing purposes without the BME280 sensor)
    //String httpRequestData = "api_key=tPmAT5Ab3j7F9&sensor=BME280&location=Office&value1=24.75&value2=49.54&value3=1005.14";

    // Send HTTP POST request
    int httpResponseCode = http.POST(httpRequestData);
     
    // If you need an HTTP request with a content type: text/plain
    //http.addHeader("Content-Type", "text/plain");
    //int httpResponseCode = http.POST("Hello, World!");
    
    // If you need an HTTP request with a content type: application/json, use the following:
    //http.addHeader("Content-Type", "application/json");
    //int httpResponseCode = http.POST("{\"value1\":\"19\",\"value2\":\"67\",\"value3\":\"78\"}");
        
    if (httpResponseCode>0) {
      Serial.print("HTTP Response code: ");
      Serial.println(httpResponseCode);
    }
    else {
      Serial.print("Error code: ");
      Serial.println(httpResponseCode);
    }
    // Free resources
    http.end();
  }
  else {
    Serial.println("WiFi Disconnected");
  }
  //Send an HTTP POST request every 30 seconds
  delay(30000);  
}

/*
  Rui Santos
  Complete project details at https://RandomNerdTutorials.com/esp32-esp8266-mysql-database-php/
  
  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files.
  
  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.

*/

For completeness in case the board reference is not clear enough, I got such a board:

In the described step 4 are the modes as an overview to which I referred to with the on and off info.

you don't read Serial in esp8266

Juraj,

Thanks for pointing out a potential issue, but what would be a solution?

Based on your hint/findings I did some research and tried the following addings to the already existing ESP8266 code:

int incomingByte = 0;

....Setup...

void loop() {

  if (Serial.available() > 0) {

  incomingByte = Serial.read(); // read the incoming byte:

  Serial.print(" I received:");

  Serial.println(incomingByte);
  }

Mainly to try out the reading but it does not do any change and in my serial monitor I don't get anything like "I received:" etc. as it appears still not to be able to read anything.

Appreciate if you could guide me on top of the root cause, also to a potential solution.

Cheers

I got a bit further but am still not at my goal.

First, the RXD/TXD switch on my board seems not work well or something is wrong with the mega's pins. Anyways, instead of going through RXD3/TXD3 with the switch, I am now using RXD1/TXD1 and wired the ESP pins to the right mega pins.

As I do "Serial1.println(postmessage);" on the mega side, it is actually posting now in the serial monitor of the ESP8266 on its BAUD! So, the values are flowing and I see them posting in the ESP.

Even though I see them in the serial, it does not mean that I could manage to "read" them into the ESP8266 code.

New on below is that I kept this incomingByte piece that should validate the availability of the serial, I am not getting anything there as a response, similarly the postmessage1 variable is in place to read the serial but it does not work as the captured value is -1.00. Underneath the code is also the serial output.

void loop() {

if (Serial.available() > 0) {

  incomingByte = Serial.read(); // read the incoming byte:

  Serial.print(" I received:");

  Serial.println(incomingByte);
  }

  postmessage1 = Serial.read(); // read the incoming byte:

//  postmessage = String().toFloat();
  
  //Check WiFi connection status
  if(WiFi.status()== WL_CONNECTED){
    HTTPClient http;
    
    // Your Domain name with URL path or IP address with path
    http.begin(serverName);
    
    // Specify content-type header
    http.addHeader("Content-Type", "application/x-www-form-urlencoded");
    
    // Prepare your HTTP POST request data
    String httpRequestData = "api_key=" + apiKeyValue + "&sensor=" + sensorName
                          + "&location=" + sensorLocation + "&value1=" + postmessage1
                          + "";
    Serial.print("httpRequestData: ");
    Serial.println(httpRequestData);

ESP8266 serial output:

httpRequestData: api_key=A&sensor=moist01&location=loc01&value1=-1.00
HTTP Response code: 200
839.37
847.78
848.39
854.65
854.21
850.28
849.68
848.26

You can see here that only the mega values are being printed through the serial linkage. But the serial connectivity check is not showing anything and the serial print of httpRequestData is not showing in the serial as well.

On the server side I get data posted and the sensor value is -1.00

Appreciate any further guidance and support.

Cheers!

what is so hard for people to understand this board?

you have 3 chips on board: USB, ATmega2560, esp8266. USB chip is the gate to computer of course

esp8266 Serial is on-board connected to USB chip, to Serial of 2560 and to Serial3 of 2560, but Serial/Serial3 is switched with the switch

2560 Serial is connected to USB and to esp8266 Serial
2560 Serial3 is connected to esp8266 Serial

the DIP switches and the RX/TX1/3 switch enable connections between the chips and this determines how the Serial communication is routed. if it goes to the other chip or to USB or to both

Juraj, do you see any issues/constraints with the code that I posted?

All, I found a solution, but prior explaining it, I just would like to mention something that I have observed on forums and which caught my attention.
A forum is a place to exchange, for people to seek new insights (let's call them learner) but also for people to share their knowledge (let's call them teacher). There is mostly a big difference between how each group perceives a question, as well as an answer.
In my case being here a learner, I again experience that a knowledgable answer might be still be beyond the level of understanding of a learner as some bits and pieces might still be missing. Usually leading to frustration on both sides.

Anyways, just wanted to share to hopefully create this way a bit more understanding and patience on both sides...

And now to the found root cause and solution, first of all, Juraj, thanks for looking into this case, your remark on the reading aspect was not the solution but it made me focus on the right direction. Also your remarks regarding the board and its modes, not necessarily what I was looking for but after further playing with the board I understand the point you wanted to make and it also supports my understanding of how the board works in the modes, especially the way serial communication is designed and which different paths can be taken with the switches or with the cables.

The issue I faced is that the serial "data" was read in a wrong way or better to say in a wrong format. I read several forum entries which were about reading bytes. And this part of it is meant to test or validate it:

if (Serial.available() > 0) 

  incomingByte = Serial.read(); // read the incoming byte:

  Serial.print(" I received:");

  Serial.println(incomingByte);
 

  postmessage1 = Serial.read(); // read the incoming byte:

But in my case I don't want bytes, I actually want float formated data.

So, the trick is instead of using Serial.read() it is needed in my case to use Serial.parseFloat():

void loop() {

if (Serial.available() > 0) {

//  postmessage = Serial.read(); // read the incoming byte:
    postmessage = Serial.parseFloat();

Now I can also see on the server the values that the Mega is creating and posted to the web-server:

ID Sensor Location Value 1 Timestamp
2427 moist01 loc01 852.10 2020-12-31 12:02:11
2426 moist01 loc01 847.68 2020-12-31 12:02:01
2425 moist01 loc01 849.18 2020-12-31 12:01:51
2424 moist01 loc01 845.29 2020-12-31 12:01:41
2423 moist01 loc01 834.57 2020-12-31 12:01:30
2422 moist01 loc01 52.00 2020-12-31 11:59:13
2421 moist01 loc01 56.00 2020-12-31 11:59:03
2420 moist01 loc01 52.00 2020-12-31 11:58:53
2419 moist01 loc01 56.00 2020-12-31 11:58:42

Anyways, thanks again for the support I received in this forum and I am still happy with the board and am looking forward to experiment further with much more sensors on this board!

Cheers

the best teaching method: wait a little. let them time to process

I've been processing quite a while before deciding to write a post :stuck_out_tongue: :stuck_out_tongue_closed_eyes: