Field 2 not updating

Joseph Varani le 19 Déc 2023 (modifié(e) le 19 Déc 2023)
Dernière activité Réponse par Christopher Stapels le 20 Déc 2023

Hello, I have followed the tutorial from https://www.halvorsen.blog/documents/technology/iot/arduino/resources/Arduino%20UNO%20R4%20WiFi%20and%20ThingSpeak.pdf to use an arduino Uno R4 wifi board to write sensor data to thingspeak, and the field 1 of random data is updating properly, but when I try to send data from analog pin A0 it will not update to field 2. I've been speaking with ChatGPT to try to resolve this issue, and it has offered several solutions that have simplified the code dramatically but the same problem persists. I wonder if this could be an error with the channel settings in my thingspeak channel? I cannot find any errors myself. ChatGPT suggested looking for the http responses to trouble shoot and both fields return -210, which it says means the API key is missing, but I have stored this key in the seperate arduino_secrets.h tab in the code, so both fields must be using the same API key, and since field 1 is working properly I don't think this is the issue. This is why I suspect there might be a deeper issue with my channel. I set it up about a year ago with a different experimental project, so maybe it has been corrupted over time. Any help is much appreciated! My current loop code reads as follows:
void loop() {
// Update field 1
float temperature = random(2000, 3000) / 100.0;
Serial.println(temperature);
ThingSpeak.setField(1, temperature);
ThingSpeak.writeFields(SECRET_CH_ID, SECRET_WRITE_APIKEY);
int response = ThingSpeak.writeFields(SECRET_CH_ID, SECRET_WRITE_APIKEY);
Serial.print("ThingSpeak write response code: ");
Serial.println(response);
delay(1000); // Add a delay to separate updates
// Update field 2
float button = analogRead(A0);
int buttonInt = static_cast<int>(button);
Serial.println(buttonInt);
ThingSpeak.setField(2, buttonInt);
ThingSpeak.writeFields(SECRET_CH_ID, SECRET_WRITE_APIKEY);
int response2 = ThingSpeak.writeFields(SECRET_CH_ID, SECRET_WRITE_APIKEY);
Serial.print("ThingSpeak write response code: ");
Serial.println(response2);
delay(19000); // Adjust the delay to maintain the total delay period
}
Christopher Stapels
Christopher Stapels le 19 Déc 2023 (modifié(e) le 19 Déc 2023)
You are allowing no time between subsequent writes to the two fields (in the same channel), when 15 seconds are required if you have a free account. See this post for the full explanation and solution.
Joseph Varani
Joseph Varani le 19 Déc 2023 (modifié(e) le 20 Déc 2023)
Thank you for your quick response! I have updated my code to send both fields in a single writeFields as your other post showed, but I still had the same issue. Field 1 updated just fine but nothing was happening with field 2. However, I had also been thinking that maybe there was an issue with the settings for field 2 in thingspeak, so as an experiment I added a 3rd field of another random number, and this time field 3 updated, and all of the data from field 2 this morning also showed up! So this seems to be fixed, thank you again!
The only strange thing that I am now noticing is that field 2 still does not update as consistently as fields 1 or 3. I think this is because the range for field 2 is between 0 and 1023, whereas fields 1 and 3 are all between 20 and 30, so could it be that this is just easier to graph for thingspeak? Occasionally field 2 will give me error code 504, saying something about timeout, I believe. This hasnt happened since this morning, but I still need to update the page to see the newest data rather than it being displayed in real time.
My new loop looks like below:
void loop() {
// Update field 1
float temperature = random(2000, 3000) / 100.0;
Serial.println(temperature);
// Update field 2
float button = analogRead(A0);
int buttonInt = static_cast<int>(button);
Serial.println(buttonInt);
float humidity = random(2000, 3000) / 100.0;
Serial.println(humidity);
ThingSpeak.setField(1, temperature);
ThingSpeak.setField(2, buttonInt);
ThingSpeak.setField(3, humidity);
ThingSpeak.writeFields(SECRET_CH_ID, SECRET_WRITE_APIKEY);
int response = ThingSpeak.writeFields(SECRET_CH_ID, SECRET_WRITE_APIKEY);
Serial.print("ThingSpeak write response code: ");
Serial.println(response);
delay(20000); // Add a delay to separate updates]
}
Christopher Stapels
Christopher Stapels le 20 Déc 2023
Code looks much better, except you are still writing twice with no delay in between.
Both of these lines will cause a write, but the second will faile every time becasue its too fast.
ThingSpeak.writeFields(SECRET_CH_ID, SECRET_WRITE_APIKEY);
int response = ThingSpeak.writeFields(SECRET_CH_ID, SECRET_WRITE_APIKEY);
.
Fields take up to 255 characters of string data per feed so I dont think your theory about the size of the data is correct, unless it is causing some kind of error on the device side. To be sure, I would write your program with just an update to field 2 and try that for a while to understand where it is failing.
Joseph Varani
1
Publication
1
Répondre

Tags

Aucun tag saisi pour le moment.