Arduino uno + esp8266 as wifi with 2 sensor code failed to send data to Channel

30 views (last 30 days)
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);
}

Answers (1)

Vinod
Vinod on 18 Nov 2020
Take a look at my answer to this question.

Communities

More Answers in the  ThingSpeak Community

Categories

Find more on Write Data to Channel 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!