My ThingSpeak doesn't receive particular data regularly
4 次查看(过去 30 天)
显示 更早的评论
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;
}
0 个评论
采纳的回答
Christopher Stapels
2022-10-24
编辑:Christopher Stapels
2022-10-25
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.
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.
0 个评论
更多回答(1 个)
Christopher Stapels
2022-10-24
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?
社区
更多回答在 ThingSpeak Community
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Read Data from Channel 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!