Hi All,
I'm running into a weird error where my HTTP GET request will successfully post to a ThingSpeak channel, but the data is "null." Any idea on why this might be occurring? I'm planning to use Tinkercad and ThingSpeak as part of an "Introduction to Arduino" (with an IoT section) class in the next couple of weeks, so any help is appreciated!
The Tinkercad simulation can be found here: https://tinkercad.com/things/iIz3ZdsHG3B
If I paste in the URL (api.thingspeak.com/update?api_key=I72PI7HCKYI1MD9Q&field1=0) into my browser, I can post a "0" to my ThingSpeak channel.
Also, if I send a GET request for example.com/index.html, I successfully get a properly formatted HTML page in the Tinkercad simulator, so I know that the emulated Internet connection is working.
However, when I run the GET request to post something to ThingSpeak, I receive a 200 OK and the channel is updated with a "null" value, which makes me think that something is formatted incorrectly. Here is a JSON of the data after posting 2 values:
{"channel":{"id":299689,"name":"Temperature Logger","latitude":"0.0","longitude":"0.0","field1":"Temperature","created_at":"2017-07-08T17:26:56-06:00","updated_at":"2017-07-09T09:33:53-06:00","last_entry_id":2},"feeds":[{"created_at":"2017-07-09T09:33:07-06:00","entry_id":1,"field1":null},{"created_at":"2017-07-09T09:33:53-06:00","entry_id":2,"field1":null}]}
I've posted my code below. Please note that the ESP8266 is attached to the Arduino's Serial and SoftSerial is being used for debugging/status codes.
#include "SoftwareSerial.h" // WiFi config String ssid = "Simulator Wifi"; String password = ""; // Server, file, and port const String hostname = "api.thingspeak.com"; const String uri = "/update?api_key=I72PI7HCKYI1MD9Q&field1=0"; const int port = 80; // This works: Basic GET of a page /*const String hostname = "example.com"; const String uri = "/index.html"; const int port = 80;*/ // Software serial object SoftwareSerial soft(8, 9); // RX, TX void setup() { // Initialize serial connections Serial.begin(115200); soft.begin(9600); // Talk to ESP8266 soft.println("Init ESP8266..."); Serial.println("AT"); delay(10); if ( Serial.find("OK") == 0 ) { soft.println("ESP8266 not found"); while(1); } // Connect to WiFi soft.println("Connecting to WiFi..."); Serial.println("AT+CWJAP=\"" + ssid + "\",\"" + password + "\""); delay(10); if ( Serial.find("OK") == 0 ) { soft.println("Could not connect to WiFi"); while(1); } else { soft.println("Connected!"); } } void loop() { // Open TCP connection Serial.println("AT+CIPSTART=\"TCP\",\"" + hostname + "\"," + port); delay(50); if ( Serial.find("OK") == 0 ) { soft.println("Error"); } // Construct HTTP request String req = "GET " + uri + " HTTP/1.1 " + "Host: " + hostname + " " + "Connection: close " + " "; int len = req.length(); // Send our request length Serial.print("AT+CIPSEND="); Serial.println(len); delay(10); if ( Serial.find(">") == 0 ) { soft.println("Error"); } // Send our http request Serial.print(req); delay(10); if (!Serial.find("SEND OK ")) { soft.println("Error"); } // Wait for a response from the server while( Serial.available() == 0 ) { delay(5); } // Print reply from server while ( Serial.available() ) { String ln = Serial.readStringUntil(' '); soft.print(ln); } // Close TCP connection Serial.println("AT+CIPCLOSE=0"); delay(50); if ( Serial.find("OK") == 0 ) { soft.println("Error"); } else { soft.println(); soft.println("Connection closed"); } delay(20000); }