i can't see the data updating in thingspeak but only 1 field can be update in thingspeak and other 3 or not updationg only once it has been update 4 fields. In serialmonitor s

10 views (last 30 days)
#include <DHT.h>
#include <MQ135.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ThingSpeak.h>
// Air Quality Monitoring System
#define DHTPIN 2 // GPIO2 on ESP8266
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
MQ135 mq135(A0);
// Soil Moisture Sensor
const int soilMoisturePin = A0; // Analog pin for soil moisture sensor
// WiFi and ThingSpeak settings
const char *ssid = "xxxxxx";
const char *password = "xxxxxx";
const char *thingSpeakApiKey = "WL790UNYFNUNVMV2";
const unsigned long airQualityChannel = 2365781;
WiFiClient client;
void setup() {
Serial.begin(115200);
delay(10);
// Connect to WiFi
Serial.println("Connecting to WiFi");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
ThingSpeak.begin(client);
}
void loop() {
// Air Quality Monitoring
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
float airQuality = mq135.getPPM();
// Soil Moisture Reading
int soilMoisture = analogRead(soilMoisturePin);
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" °C\t");
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print(" %\t");
Serial.print("Air Quality: ");
Serial.print(airQuality);
Serial.print(" ppm\t");
Serial.print("Soil Moisture: ");
Serial.print(soilMoisture);
Serial.println(" (0-1023)");
updateAirQuality(temperature, humidity, airQuality, soilMoisture);
delay(15000);
}
void updateAirQuality(float temperature, float humidity, float airQuality, int soilMoisture) {
ThingSpeak.writeField(airQualityChannel, 1, temperature, thingSpeakApiKey);
ThingSpeak.writeField(airQualityChannel, 2, humidity, thingSpeakApiKey);
ThingSpeak.writeField(airQualityChannel, 3, airQuality, thingSpeakApiKey);
ThingSpeak.writeField(airQualityChannel, 4, soilMoisture, thingSpeakApiKey);
int writeSuccess = ThingSpeak.writeFields(airQualityChannel, thingSpeakApiKey);
if (writeSuccess) {
Serial.println("ThingSpeak update successful");
} else {
Serial.println("Error updating ThingSpeak");
}
}
this is my code i can't see the data is updating but in serial monitor i can see the total 4 fields updating in thingspeak i can only see the field 1 is updating and remaining 3 or updated only once and not again so please help me in this please

Accepted Answer

Christopher Stapels
Christopher Stapels on 4 Dec 2023
You are using the single write function, you should use the multiple fields write syntax.
In your code, you are writing four seperate times(one field each) to ThingSpeak with no delay. Requests must be at least 1 second or 15 seconds apart depending on your license type.
Fortunately since you are already using the ThingSpeak library, its an easy fix. Look at the write multiple fields examples that come with the library.
The pattern is to use the SetField function for each field, and then one copy of the writeFields() function. Then you will be able to write all four fields in a single request, and then there will be a nice 15 second delay(which you already have) between each request.
  12 Comments

Sign in to comment.

More Answers (0)

Communities

More Answers in the  ThingSpeak Community

Categories

Find more on Read Data from Channel in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!