Multiple sensor data not uploading simultaneously on the Thingspeak fields of the same channel

35 views (last 30 days)
I have created 3 fields for 3 of my sensor datas (gsr sensor, dht22 temperature and humidity sensor) . But the values are not updated into their respective fields simultaneously. I have written the code as follows, it would be very kind if someone could help me out. Thank you.
#include <dht.h>
#include <SoftwareSerial.h>
dht DHT;
#define DHT11_PIN 12
#define RX 2
#define TX 3
const int GSR=A2;
int sensorValue=0;
int gsr_average=0;
String AP = "XXXXXXXXXXX"; // AP NAME
String PASS = "XXXXXXXXXXXXX"; // AP PASSWORD
String API = "YYYYYYYYYYYY"; // Write API KEY
String HOST = "api.thingspeak.com";
String PORT = "80";
//String field = "field1";
int countTrueCommand;
int countTimeCommand;
boolean found = false;
//int valSensor=1;
SoftwareSerial esp8266(RX,TX);
void setup() {
Serial.begin(9600);
esp8266.begin(115200);
sendCommand("AT",5,"OK");
sendCommand("AT+CWMODE=1",5,"OK");
sendCommand("AT+CWJAP=\""+ AP +"\",\""+ PASS +"\"",20,"OK");
}
void loop() {
//valSensor = getSensorData();
sendtoServer();
}
String getTemperatureValue(){
int chk = DHT.read22(DHT11_PIN);
float t= DHT.temperature;
Serial.print(" Temperature(C)= ");
Serial.println(DHT.temperature);
delay(50);
return String (t);
}
String getHumidityValue(){
// int chk = DHT.read22(DHT11_PIN);
float h= DHT.humidity;
Serial.print(" Humidity in %= ");
Serial.println(DHT.humidity);
delay(50);
return String (h);
}
String getGSRValue(){
long sum=0;
for(int i=0;i<10;i++) //Average the 10 measurements to remove the glitch
{
sensorValue=analogRead(GSR);
sum += sensorValue;
delay(5);
}
gsr_average = sum/10;
Serial.println(gsr_average);
return String(gsr_average);
}
void sendtoServer(){
String getData = "GET /update?api_key="+ API +"&field1="+ getGSRValue();
sendCommand("AT+CIPMUX=1",5,"OK");
sendCommand("AT+CIPSTART=0,\"TCP\",\""+ HOST +"\","+ PORT,15,"OK");
sendCommand("AT+CIPSEND=0," +String(getData.length()+4),4,">");
esp8266.println(getData);delay(1000);countTrueCommand++;
sendCommand("AT+CIPCLOSE=0",5,"OK");
String getData2 = "GET /update?api_key="+ API +"&field2="+ getTemperatureValue();
sendCommand("AT+CIPMUX=1",5,"OK");
sendCommand("AT+CIPSTART=0,\"TCP\",\""+ HOST +"\","+ PORT,15,"OK");
sendCommand("AT+CIPSEND=0," +String(getData2.length()+4),4,">");
esp8266.println(getData2);delay(500);countTrueCommand++;
sendCommand("AT+CIPCLOSE=0",5,"OK");
String getData3 = "GET /update?api_key="+ API +"&field3="+ getHumidityValue();
sendCommand("AT+CIPMUX=1",5,"OK");
sendCommand("AT+CIPSTART=0,\"TCP\",\""+ HOST +"\","+ PORT,15,"OK");
sendCommand("AT+CIPSEND=0," +String(getData3.length()+4),4,">");
esp8266.println(getData3);delay(250);countTrueCommand++;
sendCommand("AT+CIPCLOSE=0",5,"OK");
}
void sendCommand(String command, int maxTime, char readReplay[]) {
Serial.print(countTrueCommand);
Serial.print(". at command => ");
Serial.print(command);
Serial.print(" ");
while(countTimeCommand < (maxTime*1))
{
esp8266.println(command);//at+cipsend
if(esp8266.find(readReplay))//ok
{
found = true;
break;
}
countTimeCommand++;
}
if(found == true)
{
Serial.println("OYI");
countTrueCommand++;
countTimeCommand = 0;
}
if(found == false)
{
Serial.println("Fail");
countTrueCommand = 0;
countTimeCommand = 0;
}
found = false;
}

Answers (3)

Vinod
Vinod on 23 Aug 2021
Edited: Vinod on 24 Aug 2021
That is likely because you are trying to update the same channel from multiple sensors and running afoul of how frequently a channel can be updated. Please use this example as a starting point: https://github.com/mathworks/thingspeak-arduino/tree/master/examples/ESP8266/via%20AT%20commands/WriteMultipleFields and use a separate channel per device.
  1 Comment
Christopher Stapels
Christopher Stapels on 30 Aug 2021
You can update three sensors attached to the same device simultaneously, but it is not reccomended to use three devices to one channel. Also it is not reccomended to send three sensors to the same channel seperately instead of at the same time.

Sign in to comment.


Megha Patil
Megha Patil on 25 May 2022
Can i send the three sensor data (functions are connected on each other in project) on same channel as field 1, field 2, field 3 ? how it is possible? Thank you.

Megha Patil
Megha Patil on 26 May 2022
Hello Sir,
I am tring to end DHT11 sensor data to ThingSpeak. At starting data is send on cloud and i can visivalize it properly, after 68 reading I am getting following error:
Humidity in %= 32
Temperature(C)= 27
0. at command => AT+CIPMUX=1 OYI
1. at command => AT+CIPSTART=0,"TCP","api.thingspeak.com",80 OYI
2. at command => AT+CIPSEND=0,60 Fail
1. at command => AT+CIPCLOSE=0 Fail
Is there any limit of sending data per day. Thank you.

Communities

More Answers in the  ThingSpeak Community

Categories

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

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!