My ThingSpeak doesn't receive particular data regularly

9 views (last 30 days)
I apllied codes on Arduino and sends data regularly.
In Serial Monitor there's no problem here. But ThingSpeak doesn't receive humid and CO2 data regularly.
setting update value is 2 second, so It can recieve light and temperature data every 2 second.
Only humidity and CO2 doesn't record every 2 second. I need help.
Is there anything wrong?
#if 0
#include <SoftwareSerial.h>
SoftwareSerial esp8266(2, 3); //esp8266 시리얼 통신 설정
#define BAUDRATE 9600
#else
#define esp8266 Serial3 //esp8266선을 RX,TX3으로 수정
#define BAUDRATE 9600
#endif
#include <Wire.h> //BH1750 조도센서 라이브러리 포함
#include <BH1750FVI.h>
#include <DHT.h> //DHT22 라이브러리 포함
// 핀 설정 및 센서 기본 변수 설정
#define DEBUG true //esp 설정
#define DHTPIN A0 //DHT22 온습도 센서 핀 설정
#define DHTTYPE DHT22
#define MG_PIN A1 //MG811 CO2센서 핀 설정
#define BOOL_PIN 2
#define DC_GAIN 8.5
#define READ_SAMPLE_INTERVAL 5
#define READ_SAMPLE_TIMES 5
#define ZERO_POINT_VOLTAGE 0.220
#define REACTION_VOLTAGE 0.030
//라이브러리별 설정
DHT dht(DHTPIN, DHTTYPE);
BH1750FVI::eDeviceMode_t DEVICEMODE = BH1750FVI::k_DevModeContHighRes;
BH1750FVI LightSensor(DEVICEMODE);
//전역변수 설정
float hum;
float temp;
float CO2Curve[3] = {2.602, ZERO_POINT_VOLTAGE, (REACTION_VOLTAGE / (2.602 - 3))};
float volts;
//String형태로 입출력 변수 설정
String AP = "bime0304_2.5G"; //Wifi설정
String PASS = "bime0304";
String WApiKey = "CU8634SH66Q1MLO9"; //ThingSpeak Write API
String RApiKey = "XMP6OYHXR8RYX55A"; //ThingSpeak Read API
String HOST = "api.thingspeak.com";
String PORT = "80";
String fieldTemp = "field1"; //Air Temperature 필드1 설정(최대 8)
String fieldHum = "field2"; //Air humidity 필드2 설정
String fieldLight = "field3"; //Light Intensity 필드3 설정
String fieldCO2 = "field4"; //CO2 센서 필드 4 설정
int countTrueCommand; //ESP8266 커맨드 카운트 함수
int countTimeCommand;
boolean found = false;
int percentage; //CO2센서 전역변수 설정
//CO2 계산 함수
int MGGetPercentage(float volts, float * pcurve) //MGGetPercentage 변수
{
if ((volts / DC_GAIN ) >= ZERO_POINT_VOLTAGE) {
return -1;
} else {
return pow(10, ((volts / DC_GAIN) - pcurve[1]) / pcurve[2] + pcurve[0]);
}
}
float MGRead(int mg_pin) //MGRead변수
{
int i;
float v = 0;
for (i = 0; i < READ_SAMPLE_TIMES; i++) {
v += analogRead(mg_pin);
delay(READ_SAMPLE_INTERVAL);
}
v = (v / READ_SAMPLE_TIMES) * 5 / 1024 ;
return v;
}
void setup()
{
unsigned int i = 0;
// Open Serial1 communications and wait for port to open:
// 시리얼 1 통신 시작 및 열릴때까지 대기
esp8266.begin(BAUDRATE); //ESP8266 보드레이트 설정
esp8266.setTimeout(5000);
dht.begin(); //온습도 시작
LightSensor.begin(); //조도 센서 시작
Serial.begin(9600); //시리얼 통신 시작
pinMode(BOOL_PIN, INPUT);
digitalWrite(BOOL_PIN, HIGH);
Serial.println("Thingspeak with ESP-01");
//ESPWiFi 셋팅
sendCommand("AT", 5, "OK");
sendCommand("AT+CWMODE=1", 5, "OK");
sendCommand("AT+CWJAP=\"" + AP + "\",\"" + PASS + "\"", 20, "OK");
//IP획득 및 표시
sendCommand("AT+CIFSR", 2, "OK");
delay(2000);
}
//반복 조건
void loop()
{
hum = dht.readHumidity(); //습도 읽는 함수
temp = dht.readTemperature(); //온도 읽는 함수
uint16_t lux = LightSensor.GetLightIntensity(); //조도 읽는 함수
volts = MGRead(MG_PIN);
percentage = MGGetPercentage(volts, CO2Curve);
Serial.print("\n");
if (digitalRead(BOOL_PIN) ) {
Serial.print( "HIGH " );
} else {
Serial.print( "LOW " );
}
//시리얼 표시
Serial.print("Humidity: ");
Serial.print(hum, 1); //습도 소수점 첫째 표시
Serial.print(" %, Temp: ");
Serial.print(temp, 1); //온도 소수점 첫째 표시
Serial.print(" Celsius, ");
Serial.print(lux); //조도 표시
Serial.print(" lux ");
Serial.print(volts); //CO2볼트 표시
Serial.print("V ");
if (percentage == -1) { //CO2 값 표시
Serial.print( "<400" );
}
else {
Serial.print(percentage);
}
Serial.print( "ppm " );
if (digitalRead(BOOL_PIN)) //CO2 BoolPin 상태 보고
{
Serial.println( "HIGH" );
}
else
{
Serial.println( "LOW" );
}
//Thing Speak 서버에 업로드
sendCommand("AT+CIPMUX=1", 5, "OK");
//온도
String getData1 = "GET /update?api_key=" + WApiKey + "&" + fieldTemp + "=" + String(temp);
sendCommand("AT+CIPSTART=0,\"TCP\",\"" + HOST + "\"," + PORT, 15, "OK");
sendCommand("AT+CIPSEND=0," + String(getData1.length() + 4), 4, ">");
esp8266.println(getData1); countTrueCommand++;
sendCommand("AT+CIPCLOSE=0", 5, "OK");
//습도
String getData2 = "GET /update?api_key=" + WApiKey + "&" + fieldHum + "=" + String(hum);
sendCommand("AT+CIPSTART=0,\"TCP\",\"" + HOST + "\"," + PORT, 15, "OK");
sendCommand("AT+CIPSEND=0," + String(getData2.length() + 4), 4, ">");
esp8266.println(getData2); countTrueCommand++;
sendCommand("AT+CIPCLOSE=0", 5, "OK");
//조도
String getData3 = "GET /update?api_key=" + WApiKey + "&" + fieldLight + "=" + String(lux);
sendCommand("AT+CIPSTART=0,\"TCP\",\"" + HOST + "\"," + PORT, 15, "OK");
sendCommand("AT+CIPSEND=0," + String(getData3.length() + 4), 4, ">");
esp8266.println(getData3); countTrueCommand++;
sendCommand("AT+CIPCLOSE=0", 5, "OK");
//CO2
String getData4 = "GET /update?api_key=" + WApiKey + "&" + fieldCO2 + "=" + String(percentage);
sendCommand("AT+CIPSTART=0,\"TCP\",\"" + HOST + "\"," + PORT, 15, "OK");
sendCommand("AT+CIPSEND=0," + String(getData4.length() + 4), 4, ">");
esp8266.println(getData4); countTrueCommand++;
sendCommand("AT+CIPCLOSE=0", 5, "OK");
delay(1000); //딜레이 1
}
// ESP 커맨드 입력 및 전송상태 수신
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;
}

Accepted Answer

Christopher Stapels
Christopher Stapels on 24 Oct 2022
Edited: Christopher Stapels on 25 Oct 2022
Now I see the issue. You should send one update to ThingSpeak instead of building them seperately for each field.
You care actually sending four rapid fire requests in much less than 1 second. The second through fourth requests are almost always being ignored by the server since they come so fast.
Fortunately you can append them all into one request. have a look at the write data API page.
You can append the data from multiple fields using the '&' , like this
https://api.thingspeak.com/update.json?api_key=<write_api_key>&field1=123&field2=321&field3=111&field4=22
...etc up to 8 fields, and status, and location information.
Then you can just send one request instead of four. You will also use fewer messages.

More Answers (1)

Christopher Stapels
Christopher Stapels on 24 Oct 2022
Do you have a paid thingSpeak license? The update rate for the free account is only once per 15 seconds. If you have a free account and are sending requests every 2 seconds, the server will reject most of them and you will be wasting some resources.
Are you seeing any data in your channel at all?
  3 Comments
Jun Hyuk Jeon
Jun Hyuk Jeon on 25 Oct 2022
Thanks for your proper answer. It works perfectly as i want. :)

Sign in to comment.

Communities

More Answers in the  ThingSpeak Community

Categories

Find more on Configure Accounts and Channels in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!