random ESP 8266 data

Ignacio on 23 May 2024
Latest activity Edit by Christopher Stapels on 28 May 2024

I have an Arduino Uno R3 with an integrated ESP 8266. With the Arduino Uno, I measure some capacitive humidity sensors and a DHT 22 temperature and humidity sensor. The measurements are sent to the serial port and the ESP 8266 picks them up and uploads them to ThingSpeak. My problem is that it does this randomly and not in the assigned fields. Could someone help me? Thank you very much
Ignacio
Ignacio on 27 May 2024
I've already found the error; we need to remove the serialprint section
Christopher Stapels
Christopher Stapels on 28 May 2024 (Edited on 28 May 2024)
I am surprised that would make a differece, but Im glad to hear you found an error to fix. Can you post the working code for others who find this topic?
I would imagine that the way you are finding commas in your input data is potentially fragile. I would consider printing the input and output data to check that part in case you are still seeing any issues.
Also, I have had times where I need to wait longer than 5 seconds to conenct, especially on an esp8266 in a weak WiFi network. You may want to increase the startAttempTime comparison constant.
Christopher Stapels
Christopher Stapels on 23 May 2024
Are you using the ThingSpeak library? Have you compared your code to that code or to the examples code in the documentation?
After you have compared, please share abbreviated parts of your code that are responsible for writing data and connecting to the server.
Ignacio
Ignacio on 24 May 2024
I show you the graphs where you can see the response, the sensors through the serial port and the LCD give correct readings, but when passing to thingspeak they come out random
Ignacio
Ignacio on 24 May 2024 (Edited on 28 May 2024)
Thank you very much Christopher, the code I have used is:
#include <ESP8266WiFi.h>
#include <ThingSpeak.h>
const char* ssid = "xxxxxx";
const char* password = "xxxxx";
char thingSpeakAddress[] = "api.thingspeak.com";
String APIKey = "xxxxxx";
unsigned long channelNumber = xxxxx;
WiFiClient client;
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
unsigned long startAttemptTime = millis();
while (WiFi.status() != WL_CONNECTED && millis() - startAttemptTime < 5000) {
delay(500);
Serial.print(".");
}
if(WiFi.status() != WL_CONNECTED) {
Serial.println("Failed to connect to WiFi");
}
ThingSpeak.begin(client);
}
void loop() {
if (Serial.available()) {
String data = Serial.readStringUntil('\n');
int commaIndex[6];
commaIndex[0] = data.indexOf(',');
for(int i=1; i<6; i++){
commaIndex[i] = data.indexOf(',', commaIndex[i-1] + 1);
}
if(commaIndex[5] > 0) {
String humidity1 = data.substring(0, commaIndex[0]);
String humidity2 = data.substring(commaIndex[0] + 1, commaIndex[1]);
String humDHT = data.substring(commaIndex[1] + 1, commaIndex[2]);
String tempDHT = data.substring(commaIndex[2] + 1, commaIndex[3]);
String tempC = data.substring(commaIndex[3] + 1, commaIndex[4]);
String relay1Count = data.substring(commaIndex[4] + 1, commaIndex[5]);
String relay2Count = data.substring(commaIndex[5] + 1);
Serial.println("humidity1: " + humidity1);
Serial.println("humidity2: " + humidity2);
Serial.println("humDHT: " + humDHT);
Serial.println("tempDHT: " + tempDHT);
Serial.println("tempC: " + tempC);
Serial.println("relay1Count: " + relay1Count);
Serial.println("relay2Count: " + relay2Count);
ThingSpeak.setField(1, humidity1.toInt());
ThingSpeak.setField(2, humidity2.toInt());
ThingSpeak.setField(3, humDHT.toFloat());
ThingSpeak.setField(4, tempDHT.toFloat());
ThingSpeak.setField(5, tempC.toFloat());
ThingSpeak.setField(6, relay1Count.toInt());
ThingSpeak.setField(7, relay2Count.toInt());
ThingSpeak.writeFields(channelNumber, APIKey.c_str());
} else {
Serial.println("Invalid data format");
}
}
delay(20000);
}