Error sending data to ThingSpeak - Data Intervals??

77 次查看(过去 30 天)
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 个评论
Christopher Stapels
Christopher Stapels 2024-7-1,13:14
@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 2024-7-1,18:12
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.

请先登录,再进行评论。

回答(2 个)

Christopher Stapels
Christopher Stapels 2024-6-30,21:13
编辑:Christopher Stapels 2024-7-1,18:28
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 个评论
Edward
Edward 2024-7-3,13:01
编辑:Christopher Stapels 2024-7-3,15:29
#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 2024-7-3,15:28
编辑:Christopher Stapels 2024-7-3,15:34
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?

请先登录,再进行评论。


Edward
Edward 2024-7-3,12:02
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

社区

更多回答在  ThingSpeak Community

类别

Help CenterFile Exchange 中查找有关 REST API 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by