Error sending data to ThingSpeak - Data Intervals??

33 views (last 30 days)
I am sending data from sensors on a XIAO esp32 S3 - there are 6 fields of data - I have timed to send every 5 minutes. When I initiate the code it sends 2 or 3 sets, but then I get an error message saying 'Error sending data to ThingSpeak' - and it doesnt send any more data unless I reset the esp , yet the data is being collected as shown on the serial monitor. I have played around witht the time intervals but no joy. Is it a function of thinkspeak or should I investigate the code?
  4 Comments
Christopher Stapels
Christopher Stapels on 1 Jul 2024
@Umar I really want you to stay and be part of the community. But your answer sounds very much like it was generated by an LLM. The community guidelines allow this, but you are supposed to indicate that you are using AI for your answer.
For example, you said "Review ThingSpeak limits" even though the OP said they send data every 5 minutes, At present, the slowest limit for ThingSpeak is 15 seconds. I've definitely seen times when an LLM will offer that kind of generic advice, but humans who dot know the actual information usually don't bother to write such generalities.
And the "Hope following these steps will help resolve your problem." has never been posted by a human.
So please continue to be part of the community, and feel free to use AI to help you answer, but please indicate you used AI. And even better, reread the AI response and write it in the kind of language that would help you solve the problem if you were reading it. MATLAB answers need people like you that are willing to read questions and offer help. But please answer with a little more care.
Umar
Umar on 1 Jul 2024
Dear Chris,
Thank you for your feedback and for expressing your desire for me to continue being part of the community. I don’t know if such tool does exist because it will not help you understand the meaning behind code and definitely not help you gain knowledge about Mathworks, Simulink. You are an essential part of community as well. Our goal should be helping out people struggling with problems and help make difference in their lives.
I appreciate your guidance on how to improve my responses, and I will strive to provide more specific and helpful advice in line with the community guidelines. Your input is valuable, and I am committed to contributing in a way that adds value to the discussions.
Again, thank you for encouraging me to continue being part of the community.

Sign in to comment.

Answers (2)

Christopher Stapels
Christopher Stapels on 30 Jun 2024
Edited: Christopher Stapels on 1 Jul 2024
Generally I'd look at the code. ThingSpeak will only reject your data if you send it too fast or format it wrong, but it seems you are not sending data too fast. Are you using the thingSpeak communication library? Are you getting a specific error code? Wi fi strength is often an issue.
  2 Comments
Edward
Edward on 3 Jul 2024
Edited: Christopher Stapels on 3 Jul 2024
#include <WiFi.h>
#include <WebServer.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <Adafruit_INA219.h>
#include <BH1750.h>
#include <HTTPClient.h>
#include <time.h>
#define I2C_SDA D4
#define I2C_SCL D5
const char* ssid = "BTB-5HAQXQ";
const char* password = "xxxxxxx";
BH1750 lightMeter;
WebServer server(80);
Adafruit_BME280 bme;
Adafruit_INA219 ina219;
const char* apiKey = "xxxxxxxxxxxxxxxx";
struct SensorData {
float temperature;
float humidity;
float pressure;
float voltage;
float current;
float soc; // State of Charge
float light;
time_t timeStamp;
};
const int maxDataPoints = 60;
SensorData data[maxDataPoints];
int dataIndex = 0;
unsigned long lastCollectionTime = 0;
const long collectionInterval = 120000; // Adjusted to 2 minutes for more relaxed timing
float initialVoltage = 0.0; // To be used for SoC calculation
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi connected");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
Wire.begin(I2C_SDA, I2C_SCL);
lightMeter.begin();
Serial.println(F("BH1750 Test begin"));
Serial.println("Measuring voltage and current with INA219 ...");
if (!bme.begin(0x76)) {
Serial.println("Could not find a valid BME280 sensor, check wiring!");
}
ina219.begin();
initialVoltage = ina219.getBusVoltage_V();
configTime(0, 3600, "pool.ntp.org", "time.nist.gov");
setenv("TZ", "GMT0BST,M3.5.0/1,M10.5.0", 1);
tzset();
server.on("/trigger", HTTP_GET, handleTrigger);
server.begin();
Serial.println("Web server started, ready to collect data.");
}
void loop() {
server.handleClient();
unsigned long currentMillis = millis();
if (currentMillis - lastCollectionTime >= collectionInterval) {
lastCollectionTime = currentMillis;
collectData();
}
checkMemoryAndTrigger();
}
void collectData() {
float lux = lightMeter.readLightLevel();
if (dataIndex < maxDataPoints) {
data[dataIndex].temperature = bme.readTemperature();
data[dataIndex].humidity = bme.readHumidity();
data[dataIndex].pressure = bme.readPressure() / 100.0F;
data[dataIndex].voltage = ina219.getBusVoltage_V();
data[dataIndex].current = ina219.getCurrent_mA();
data[dataIndex].soc = calculateSoC(data[dataIndex].voltage);
data[dataIndex].timeStamp = time(nullptr);
data[dataIndex].light = lux;
dataIndex++;
} else {
Serial.println("Memory full. Data not collected.");
}
}
float calculateSoC(float currentVoltage) {
if (initialVoltage == 0) return 0;
float soc = (currentVoltage / initialVoltage) * 100.0;
return soc > 100.0 ? 100.0 : soc;
}
void handleTrigger() {
Serial.println("Trigger function called.");
String currentTime = getTimeString();
String httpResponse = "Trigger received at: " + currentTime + "\n";
httpResponse += "Number of data points to be sent: " + String(dataIndex) + "\n";
httpResponse += "Data sending process initiated in the background...";
server.send(200, "text/plain", httpResponse);
sendDataToThingSpeak();
}
void sendDataToThingSpeak() {
if (WiFi.status() != WL_CONNECTED) {
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi reconnected for data transmission.");
}
WiFiClient client;
HTTPClient http;
for (int i = 0; i < dataIndex; i++) {
String isoTimestamp = convertToIso8601(data[i].timeStamp);
String url = "http://api.thingspeak.com/update?api_key=" + String(apiKey) +
"&field1=" + String(data[i].temperature) +
"&field2=" + String(data[i].humidity) +
"&field3=" + String(data[i].light) +
"&field4=" + String(data[i].voltage) +
"&field5=" + String(data[i].current) +
"&field6=" + String(data[i].soc) +
"&field7=" + String(data[i].pressure) +
"&created_at=" + isoTimestamp;
http.begin(client, url);
int httpCode = http.GET();
if (httpCode == 200) {
Serial.println("Data sent to ThingSpeak.");
} else {
Serial.println("Failed to send data. HTTP Code: " + String(httpCode));
break; // Exit loop if any transmission fails
}
http.end();
delay(2000); // Short delay to prevent rate limit issues
}
dataIndex = 0; // Clear data index after attempting to send all data
WiFi.disconnect();
Serial.println("Disconnected from WiFi to save power.");
goToDeepSleep();
}
void goToDeepSleep() {
Serial.println("Going to deep sleep for 2 minutes...");
esp_sleep_enable_timer_wakeup(120 * 1000000); // Sleep for 2 minutes
esp_deep_sleep_start();
}
String convertToIso8601(time_t timeStamp) {
struct tm *timeinfo;
char buffer[80];
timeinfo = localtime(&timeStamp);
strftime(buffer, sizeof(buffer), "%Y-%m-%dT%H:%M:%SZ", timeinfo);
return String(buffer);
}
String getTimeString() {
time_t now;
struct tm *timeinfo;
time(&now);
timeinfo = localtime(&now);
char buffer[80];
strftime(buffer, sizeof(buffer), "%d/%m/%Y %H:%M:%S", timeinfo);
return String(buffer);
}
void checkMemoryAndTrigger() {
float memoryUsage = (float)dataIndex / maxDataPoints;
if (memoryUsage >= 0.01) {
Serial.println("Memory usage nearing 1%. Triggering data send.");
handleTrigger();
}
}
Christopher Stapels
Christopher Stapels on 3 Jul 2024
Edited: Christopher Stapels on 3 Jul 2024
I see in your code that you send
"Failed to send data. HTTP Code:" is this the error you get? if so, what http code do you get?
A couple more comments: You may not really need to generate a timestamp - if you dont provide one, ThingSpeak will use the time you write to the server.
You can record RSSI in filed 8 to see how strong the network signal is.
I usually make the wifi client global, but this may not be necessary.
You say you get 2-3 sets of data in your channel. How far apart are the timestamps for these 2-3 sets of data?

Sign in to comment.


Edward
Edward on 3 Jul 2024
I have played around with it changed rates and code but still geting
12:55:03.198 -> WiFi connected
12:55:03.198 -> IP Address: 192.168.1.126
12:55:03.198 -> BH1750 Test begin
12:55:03.198 -> Measuring voltage and current with INA219 ...
12:55:03.198 -> Web server started, ready to collect data.
12:57:02.119 -> Memory usage nearing 1%. Triggering data send.
12:57:02.119 -> Trigger function called.
12:57:14.097 -> Failed to send data. HTTP Code: -1
12:57:14.097 -> Disconnected from WiFi to save power.
12:57:14.097 -> Going to deep sleep for 2 minutes...
I have a similar setup for esp8266 and that works ok
Now I am using an XIAOesp32S3 but but similar code adapted wont work
I didhave some data points uploaded overnight but not in a patterntc which represented the data collection and send
like you I feel the code is not liking Thingspeak process but not sure
Thanks
  1 Comment
Christopher Stapels
Christopher Stapels on 3 Jul 2024
Can you share a few choice segments of the code so we can see how you are writing?

Sign in to comment.

Communities

More Answers in the  ThingSpeak Community

Categories

Find more on Write Data to Channel in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!