Arduino uno + esp8266 as wifi with 2 sensor code failed to send data to Channel
조회 수: 12 (최근 30일)
이전 댓글 표시
I have this code at the bottom and it always shows me a message that read data failed, what is the problem? "Tinker CAD simulator"
// Tinkercad Arduino Tutorials: To Interface ESP8266+ThingSpeak+Arduino in Tinkercad Simulator
String ssid = "Simulator Wifi"; // SSID to connect to WIFI
String password = "Our virtual wifi has no password"; //
String host = "https://api.thingspeak.com";
const int httpPort = 80;
String uri = "/update?api_key=Q63ZZ2IFAYQ6I6HV";
String field1 = "field1";
String field2 = "field2";
int setupESP8266(void){
// Start our ESP8266 Serial Communication
Serial.begin(115200); // Serial connection over USB to computer
Serial.println("AT"); // Serial connection on Tx / Rx port to ESP8266
delay(10); // Wait a little for the ESP to respond
if (!Serial.find("OK")) return 1;
Serial.println("AT+CWJAP=\"" + ssid + "\",\"" + password + "\"");
delay(10); // Wait a little for the ESP to respond
if (!Serial.find("OK")) return 2;
// Open TCP connection to the host:
Serial.println("AT+CIPSTART=\"TCP\",\"" + host + "\"," + httpPort);
delay(50); // Wait a little for the ESP to respond
if (!Serial.find("OK")) return 3;
return 0;
}
void sensors(void){
int tempSensor1 = map(analogRead(A0),20,350,-40,125);
int gasSensor2 = map(analogRead(A1),10,350,0,100);
// Construct our HTTP call
String httpPacket= "GET "+ uri +"&"+field1 +"="+ String(tempSensor1)+"&"+ field2 +"="+String(gasSensor2)+ " HTTP/1.1\r\nHost: " + host + "\r\n\r\n";
int length = httpPacket.length();
Serial.println("tempSensorvalue: "+ String(tempSensor1)+"\n\n");
Serial.println("gasSensorvalue: "+ String(gasSensor2)+"\n\n");
Serial.print("AT+CIPSEND=");
Serial.println(length);
delay(10); // Wait a little for the ESP to respond if (!Serial.find(">")) return -1;
// Send our http request
Serial.print(httpPacket);
delay(10); // Wait a little for the ESP to respond
if (!Serial.find("SEND OK\r\n")) return;
}
void setup() {
setupESP8266();
}
void loop() {
sensors();
delay(10000);
}
댓글 수: 0
답변 (1개)
커뮤니티
더 많은 답변 보기: ThingSpeak 커뮤니티
참고 항목
카테고리
Help Center 및 File Exchange에서 REST API에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!