Tinkercad ESP8266 simulator posting "null" values to ThingSpeak | ESP8266 Wi-Fi Module | Forum

Avatar

Please consider registering
Guest

Search

— Forum Scope —






— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

Register Lost password?
sp_Feed sp_TopicIcon
Tinkercad ESP8266 simulator posting "null" values to ThingSpeak
Avatar
sgmustadio
New Member
Forum Posts: 3
sp_UserOfflineSmall Offline
1
July 9, 2017 - 11:55 am
sp_Permalink sp_Print sp_EditHistory

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);
}
Avatar
piajola

Gold
Forum Posts: 71
sp_UserOfflineSmall Offline
2
July 10, 2017 - 1:00 am
sp_Permalink sp_Print sp_EditHistory

I do not know if this can help you

56 // Construct HTTP request

57 String req = "GET " + uri + " HTTP/1.1

58 " + "Host: " + hostname + "

59 " +    "Connection: close

60 " +    "

61 ";

62 int len = req.length();

63  

64 // Send our request length

65 Serial.print("AT+CIPSEND=");

66 Serial.println(len);

67 delay(10);

68 if ( Serial.find(">") == 0 ) {

69  soft.println("Error");

70 }

71

72 // Send our http request

73 Serial.print(req);

74 delay(10);

75 if (!Serial.find("SEND OK

76 ")) {

77  soft.println("Error");

78 }

my way in my real life (nothing against simulators)  600000+ records and counting ...

  // TCP connection

  String cmd = "AT+CIPSTART=\"TCP\",\"";

  cmd += "184.106.153.149"; // api.thingspeak.com -- not working

  cmd += "\",80";

  altSer.println(cmd);

  //  if (altSer.find("Error")) {     one "AT commands" version

  if (altSer.find("ERROR")) {      another "AT commands" version

    Serial.println("AT+CIPSTART error");

    delay(4000);

    return;

  }

  // prepare GET string

  String getStr = "GET /update?api_key=";

  getStr += apiKey;

  getStr += "&field1=";

  getStr += String(strTemp);           number to string

  getStr += "&field2=";

  getStr += String(strHume);           number to string

  getStr += "\ r \ n \ r \ n";      must be \ r  and  \ n  without the space between so 2 CRs and 2 NLs

  // send data length

  cmd = "AT+CIPSEND=";

  cmd += String(getStr.length());    number to string

  altSer.println(cmd);

  if (altSer.find(">")) {

    altSer.print(getStr);

  } else {

    altSer.println("AT+CIPCLOSE");

    // alert user

    Serial.println("AT+CIPCLOSE");

  }
  // thingspeak needs 15 sec delay between updates

  Serial.println("- * - * - * - * - * - * - * -");

  delay(59800);     almost 60 points/hour -- Better to use millis()

}

** altSer to ESP8266

** Serial to console

Maybe number 0 is received as NULL in your code (thinking aloud)

Also there are more than one "AT commands" versions

Avatar
cstapels

Gold
Forum Posts: 30
sp_UserOfflineSmall Offline
3
July 10, 2017 - 9:30 am
sp_Permalink sp_Print

sgmustadio,

From what I see in your code, you aren't sending any values in your GET request.  You can see this link for sample code.  This code uses the bulk post method, but it allows you to see the formatting.  Also try this link.

 In lines 56-62, you need to include the api key and field 1 info, as in the request you listed "api.thingspeak.com/update?api_key=XXXXXXXXXXXXXXXX&field1=0)"

56  // Construct HTTP request
57  String req = "GET " + uri + " HTTP/1.1
58 " + "Host: " + hostname + "
59 " +    "Connection: close
60 " +    "
61 ";
62  int len = req.length();

Avatar
sgmustadio
New Member
Forum Posts: 3
sp_UserOfflineSmall Offline
4
July 12, 2017 - 9:07 pm
sp_Permalink sp_Print sp_EditHistory

Thanks for the suggestions, piajola and cstapels. Still no luck on my end. The weird thing is that the GET request (with exactly the same formatting) successfully posts to ThingSpeak from a real ESP8266 (SparkFun Thing Dev), so it must be something with the Tinkercad ESP8266. My request does have a value for field1, as per the URI: "/update?api_key=I72PI7HCKYI1MD9Q&field1=0" (don't worry, I've since changed my Write API key, so this one is pretty useless).

I've even tried a POST request (replace lines 56-62 with the following):

// POST Test (no spaces for newline/return carriage symbols)
String body = "api_key=I72PI7HCKYI1MD9Q&field1=0"; 
String req = String("POST /update HTTP/1.1\ r \ n") +
                  "Host: " + hostname + "\ r \ n" +    
                  "User-Agent: Arduino/1.0\ r \ n" +    
                  "Content-Type: application/x-www-form-urlencoded;\ r \ n" +
                  "Content-Length: " + body.length() + "\ r \ n" +    
                  "Connection: close\ r \ n" +    
                  "\ r \ n" +
                  body + "\ r \ n";  
int len = req.length();  

Once again, it works great in real hardware as well as from something like hurl.it, but gives me a 404 Not Found in the simulator.

Avatar
cstapels

Gold
Forum Posts: 30
sp_UserOfflineSmall Offline
5
July 13, 2017 - 6:41 pm
sp_Permalink sp_Print

You had the right information in the uri variable (I missed that above).  Since you are getting a 404, perhaps its your line endings? There is a space between them, perhaps your encoder (in the simulator) is changing that space to a %20?

Avatar
sgmustadio
New Member
Forum Posts: 3
sp_UserOfflineSmall Offline
6
July 14, 2017 - 8:52 pm
sp_Permalink sp_Print

Oops, forgot to mention (sorry!): I added spaces to the slash-r-slash-n line endings because this forum tool actually tries to parse the newline-return carriage characters, and the normal \\ escape character doesn't work. There are no spaces there in the code.

Forum Timezone: America/New_York

Most Users Ever Online: 114

Currently Online:
27 Guest(s)

Currently Browsing this Page:
2 Guest(s)

Top Posters:

rw950431: 241

Vinod: 137

piajola: 71

vespapierre: 63

Adarsh_Murthy: 59

chrisjmears: 54

Newest Members:

Murad

MikefromMichigan

mariofel

jbeale1

GauthamPughaz

hellan

Forum Stats:

Groups: 4

Forums: 17

Topics: 1183

Posts: 4169

 

Member Stats:

Guest Posters: 1

Members: 5443

Moderators: 0

Admins: 2

Administrators: Hans, lee