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

조회 수: 33 (최근 30일)
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;
}

답변 (3개)

Vinod
Vinod 2021년 8월 23일
편집: Vinod 2021년 8월 24일
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
Christopher Stapels
Christopher Stapels 2021년 8월 30일
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.

댓글을 달려면 로그인하십시오.


Megha Patil
Megha Patil 2022년 5월 25일
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.
  댓글 수: 2
Christopher Stapels
Christopher Stapels 2022년 5월 25일
편집: Christopher Stapels 2022년 5월 25일
Yes it is definitely possible, I can share how. Can you start a new thread for this question please? It will be harder for someone else to find here at the bottom of this one.

댓글을 달려면 로그인하십시오.


Megha Patil
Megha Patil 2022년 5월 26일
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.
  댓글 수: 1
Christopher Stapels
Christopher Stapels 2022년 5월 26일
Can you start a new thread for this question please? It will be harder for someone else to find here at the bottom of this one.

댓글을 달려면 로그인하십시오.

커뮤니티

더 많은 답변 보기:  ThingSpeak 커뮤니티

카테고리

Help CenterFile Exchange에서 Read Data from Channel에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by