Quantcast
Channel: ThingSpeak IoT Community - Forum: ESP8266 Wi-Fi Module
Viewing all 167 articles
Browse latest View live

sgmustadio on Tinkercad ESP8266 simulator posting "null" values to ThingSpeak

$
0
0

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

piajola on Export all data to the computer

$
0
0

Sommoh

In my case with windows 10, having one free account in Thimgspeak channels (not the forum one) signed in and the webpage changes a little showing

CHANNEL NAME
Channel ID: 000000
Author: xxxxxxx
Access: Public (or Private)

 -------------- -------------- ------------------- ----------- --------------------------
Private View|Public View|Channel Setting|API Keys|Data Import / Export|  (5 tabs)

-----------------------   ------------------------------------
|Add Visualizations| |Data Export MATLAB Analysis| (2 buttons)
-----------------------   ------------------------------------

then press the last tab (5th) Data Import / Export NOT any button

will change to next screen, you will see 2 zones

Import

 ---------
|Upload| ( green button)
 ---------

Export

 ------------
|Download| ( green button)
 ------------

press Download green button then it will shows a dialogBox asking for place & name for the .csv file that will be in your machine

In my case:

start 18:03  --  end  18:27

31,0 MB (32.518.144 bytes)
665907 lines of csv data

created_at,                       entry_id,field1,field2,field3,field4,field5,field6,field7,field8
2016-02-03 00:19:58 UTC,1,         5,10,15,15,,,,
2016-02-03 00:20:15 UTC,2,         5,10,15,15,,,,
2016-02-03 00:20:33 UTC,3,         5,10,15,15,,,,
2016-02-03 00:20:50 UTC,4,         5,10,15,15,,,,
.
.
.
2017-07-09 23:24:36 UTC,665904,21.59,66.86,,,,,,
2017-07-09 23:25:37 UTC,665905,21.53,66.84,,,,,,
2017-07-09 23:26:38 UTC,665906,21.56,67.06,,,,,,

Hope this is some help

You are welcome in advance 🙂

piajola on Retrieve value 30 writes ago

rw950431 on Retrieve value 30 writes ago

$
0
0

Thanks piajola- its right at the bottom under the heading "Get specific entry in channel" for anyone else who overlooked it.

piajola on Tinkercad ESP8266 simulator posting "null" values to ThingSpeak

$
0
0

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

cstapels on Tinkercad ESP8266 simulator posting "null" values to ThingSpeak

$
0
0

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();

Sommoh on Export all data to the computer

$
0
0

piajola said
Sommoh

In my case with windows 10, having one free account in Thimgspeak channels (not the forum one) signed in and the webpage changes a little showing

CHANNEL NAME
Channel ID: 000000
Author: xxxxxxx
Access: Public (or Private)

 -------------- -------------- ------------------- ----------- --------------------------
Private View|Public View|Channel Setting|API Keys|Data Import / Export|  (5 tabs)

-----------------------   ------------------------------------
|Add Visualizations| |Data Export MATLAB Analysis| (2 buttons)
-----------------------   ------------------------------------

then press the last tab (5th) Data Import / Export NOT any button

will change to next screen, you will see 2 zones

Import

 ---------
|Upload| ( green button)
 ---------

Export

 ------------
|Download| ( green button)
 ------------

press Download green button then it will shows a dialogBox asking for place & name for the .csv file that will be in your machine

In my case:

start 18:03  --  end  18:27

31,0 MB (32.518.144 bytes)
665907 lines of csv data

created_at,                       entry_id,field1,field2,field3,field4,field5,field6,field7,field8
2016-02-03 00:19:58 UTC,1,         5,10,15,15,,,,
2016-02-03 00:20:15 UTC,2,         5,10,15,15,,,,
2016-02-03 00:20:33 UTC,3,         5,10,15,15,,,,
2016-02-03 00:20:50 UTC,4,         5,10,15,15,,,,
.
.
.
2017-07-09 23:24:36 UTC,665904,21.59,66.86,,,,,,
2017-07-09 23:25:37 UTC,665905,21.53,66.84,,,,,,
2017-07-09 23:26:38 UTC,665906,21.56,67.06,,,,,,

Hope this is some help

You are welcome in advance 🙂  


Thank you very much. The Export Data button in Private View won't do it, but this button does the trick.
Very niceSmile

cstapels on Export all data to the computer

$
0
0

The export data button in Private view will export  what you see in your private view.  If you want to export more data, try changing the number of data points on your plot (using the pencil icon on top of the chart), and then the number of data points exported should change accordingly.  Or you can use the export tab as suggested above.


sgmustadio on Tinkercad ESP8266 simulator posting "null" values to ThingSpeak

$
0
0

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.

cstapels on Tinkercad ESP8266 simulator posting "null" values to ThingSpeak

$
0
0

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?

sgmustadio on Tinkercad ESP8266 simulator posting "null" values to ThingSpeak

$
0
0

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.

astrotutor on Reading back data from Thingspeak

$
0
0

 Hi All.

I have set up  a Thingspeak channel with the purpose of logging how many times a tweet hashtag is used. This is OK.

I have also set up a ThingHTTP with a TweetControl to do the above and log it to field 1 of the channel. This is OK. I make a Tweet and the number of events increases. I can also achieve the same by using the ThingHTTP code in a browser too. All happy.

I have also set up another ThingHTTP to read just the entry_id of the channel to obtain just the number of events. I'm not interested in what else is in the tweet just the number of times the hashtag is utilised at an event I will be running. This works OK.

I can use the Get command give from the last ThingHTTP in a browser and it gives me on the page just the number of events. Great.

I have also found some code to GET the ThingHTTP data from the channel into the serial of my ESP8266. 

But. What the serial shows is a load of preceding information before the actual event_id info I'm after. I only what the event_id number with which I can then use to activate some lights or something. Not sure what at the moment but something eye catching!

The information on the serial is this:

Connecting to api.thingspeak.com
Requesting URL: api.thingspeak.com/apps/thinghttp/send_request?api_key=XXXXXXXXXXXXXX
TRY: 2.
HTTP/1.1 200 OK

Content-Type: text/html; charset=utf-8

Content-Length: 1

Connection: close

Status: 200 OK

ETag: "eccbc87e4b5ce2fe28308fd9f2a7baf3"

Cache-Control: max-age=0, private, must-revalidate

X-Request-Id: 6fb2b657-ab0a-4d4d-a63b-bf5515d7c827

X-Runtime: 0.272793

X-Powered-By: Phusion Passenger 4.0.57

Date: Sun, 30 Jul 2017 12:09:09 GMT

Server: nginx/1.9.3 + Phusion Passenger 4.0.57

 

3

 

It is just the number 3 at the end that I want. How do I do this? I've been looking around for hours but have not found anything that can help that I can understand. Any help would be appreciated.

The code I have to GET is this:

// We now create a URI for the request
String url = "/apps/thinghttp/send_request?api_key=XXXXXXXXXXXXX";
Serial.print("Requesting URL: ");
Serial.println(host + url);
Serial.println(String("TRY: ") + value + ".");

// This will send the request to the server
Client.print(String("GET ") + url + "&headers=false" + " HTTP/1.1
" + "Host: " + host + "
" + "Connection: close

");
delay(500);

 

// Read all the lines of the reply from server and print them to Serial
while(Client.available()){
String line = Client.readStringUntil('
');
Serial.println(line);
}

rw950431 on Reading back data from Thingspeak

$
0
0

Unfortunately, as you have found, the Arduino web client lacks many of the sophisticated features of more advanced programming environments so you are left to deal with the raw strings by yourself.

 

If you are certain that the line you want is the only one that starts with a number then the procedure isnt too hard.

 

 

while(Client.available()){
String line = Client.readStringUntil('
');

  if ( line.charAt(0)>='0' and line.charAt(0)<='9') {
    Serial.println(line);

  };

};

See https://www.arduino.cc/en/Reference/StringObject particularly https://www.arduino.cc/en/Tutorial/StringCharacters

You may also wish to convert your string to a useable number instead - see https://www.arduino.cc/en/Reference/StringToInt

jteindl on Sensor data in js as an "IF/ELSE"

$
0
0
In my JavaScript I have a number of variables as shown:
 
stop = new google.maps.LatLng(59.093914487013, -122.71569423377514)
waypts.push({
location: stop,
stopover: true
 
The line in Bold is what I need help with. I have data transmitted to Thingspeak. I have an API that allows me to select the exact value to query; what I need to know is how to use a GET statement (Or anything that would work) to have that Stopover line be True or False based on the result. So IF the result queried by the API is over 8, it's TRUE, if it's under it is false.
 
The API line looks like this: 
 
GET https://api.thingspeak.com/channels/****/fields/1.json?results=2
So How do I set that Bold field to query this and either be TRUE or FALSE based upon the result of this query?
 
If you need any more info please reply and I can fill in any blanks, but I'm reallllly needing some help here.
 
Thank you! 

astrotutor on Reading back data from Thingspeak

$
0
0

Hi rw950431

Thanks for the info. The result it gives is as below. I'm thinking is the answer to get the length of the string and read the last characters backwards until a blank space is reached? It's odd that a web browser only prints the 3 at the bottom that is the entry_id and nothing else.

Connecting to api.thingspeak.com
Requesting URL: api.thingspeak.com/apps/thinghttp/send_request?api_key=xxxxxxxxxxxxxx
TRY: 2.
200
1
Connection:
200
0.035260
X-Powered-By:
4.0.57
Date:
01
2017
12:43:37
4.0.57

3

Try nr. 2 is finished.
Waiting for next try...


rw950431 on Reading back data from Thingspeak

$
0
0

Not sure why its doing that.  Are you sure your code is reading the complete line and not just one character at a time?  (Maybe you need readStringUtil('
')  instead).

astrotutor on Reading back data from Thingspeak

$
0
0

Thanks for the help but I found this video:

which demonstrated an Example from Arduino which does exactly what I want. I have also now managed to convert the result from a string to an integer. So hopefully I can now get the code to do something more interesting with the result than just multiply it.

/**
* BasicHTTPClient.ino
*
* Created on: 24.05.2015
*
*/

#include <Arduino.h>

#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>

#include <ESP8266HTTPClient.h>

#define USE_SERIAL Serial

ESP8266WiFiMulti WiFiMulti;

void setup() {

USE_SERIAL.begin(115200);
// USE_SERIAL.setDebugOutput(true);

USE_SERIAL.println();
USE_SERIAL.println();
USE_SERIAL.println();

for(uint8_t t = 4; t > 0; t--) {
USE_SERIAL.printf("[SETUP] WAIT %d...
", t);
USE_SERIAL.flush();
delay(1000);
}

WiFiMulti.addAP("ssis", "password");

}

void loop() {
// wait for WiFi connection
if((WiFiMulti.run() == WL_CONNECTED)) {

HTTPClient http;

USE_SERIAL.print("[HTTP] begin...
");
// configure traged server and url
// get the entry_id from Thingspeak from the ThingHTTP TweetControl
http.begin("api.thingspeak.com", 80, "/apps/thinghttp/send_request?api_key=XXXXXXXXXXXXXXX"); //HTTP

USE_SERIAL.print("[HTTP] GET...
");
// start connection and send HTTP header
int httpCode = http.GET();

// httpCode will be negative on error
if(httpCode > 0) {
// HTTP header has been send and Server response header has been handled
USE_SERIAL.printf("[HTTP] GET... code: %d
", httpCode);

// file found at server
if(httpCode == HTTP_CODE_OK) {
String payload = http.getString();
// Print the string value from Thingspeak
USE_SERIAL.println(payload);
// Convert string to integer so that we can utilise the number
int number_tweets = payload.toInt();
USE_SERIAL.println(2*number_tweets);
}
} else {
USE_SERIAL.printf("[HTTP] GET... failed, error: %s
", http.errorToString(httpCode).c_str());
}

http.end();
}

delay(10000);
}

SteveMcC on Wemos D1 mini - Vodafone New Zealand 4G

$
0
0

Hello All

I thought it would be of some help to this community to share my experiences with getting a thingspeak device working here in New Zealand.

I am using a wemos D1 mini to upload my temperature data to thingspeak for a year or so before it “stopped” working.  My original project used LUA and worked fine.

I thought the problem was that the code had become corrupted (I found a few people who mentioned the same problems as I was having) so I set about converting the LUA code to Arduino.

I could not for the life of me get any of the example code on the web working at all.  I could connect to the wifi no problem; it would allocate my wemos an IP address.  When the code ran I could even see the IP address of the thingspeak servers, and it connected to them fine.

The problem was when the code got to the following line, it would time out and fail.

client.print("POST /update HTTP/1.1
");

I took the device to work(a fibre connection to the internet), and the code work perfectly, but I could not get it to work at my home, or even through a wifi hotspot from my cellphone.

I discovered that the problem was with my internet connection at my home.  My internet connection is through Vodafone using their wireless broadband 4G network.  It appears that Vodafone (I won’t say blocks) but doesn’t allow the particular ports used by the WiFiClient connection through.

I changed my code so that it uses the HTTP client instead.  This worked well, and I have been successfully using this for a few months now.

Relevant Snippets of the code I used is below.

I am sending two strings of information to thing speak.  The temperature outside my house, and the voltage of my batteries, thus the two values being passed to the function.

 

#include <ESP8266HTTPClient.h>  // instead of #include <ESP8266WiFi.h>

..

..

..

void PostToThingSpeak(float celsius, float voltage){

String updateURL = "http://";

updateURL += String(server);

updateURL +="/update?api_key=";

updateURL += String(apiKey);

updateURL +="&field1=";

updateURL += String(celsius);

updateURL +="&field2=";

updateURL += String(voltage);

client.begin(updateURL);

int httpCode = client.GET();  // I guess I could check that I get a “1” as confirmation but I implemented that yet

client.end();

}

 

For those of us who like a  “TLDR” Summary:

Could not connect using normal "client" code through vodafones network here in new Zealand, so had to adjust the code to use the httpclient method.

rw950431 on Wemos D1 mini - Vodafone New Zealand 4G

$
0
0

For the record what port was your original code connecting to?

SteveMcC on Wemos D1 mini - Vodafone New Zealand 4G

$
0
0

I was connecting using port 80...

for example the code below was one of the examples I tried to use.....and I could not get it to work....

(I have since then spoken to a friend who works in IT, and apparently there are different APN's that can be used with vodafone NZ network, and some of them allow different ports access through their firewalls, but I am not sure I can confirm or deny this)

if (client.connect(server,80)) {
String postStr = apiKey;
postStr +="&field1=";
postStr += String(t);
postStr +="&field2=";
postStr += String(h);
postStr += "



";
 
client.print("POST /update HTTP/1.1
");
client.print("Host: api.thingspeak.com
");
client.print("Connection: close
");
client.print("X-THINGSPEAKAPIKEY: "+apiKey+"
");
client.print("Content-Type: application/x-www-form-urlencoded
");
client.print("Content-Length: ");
client.print(postStr.length());
client.print("

");
client.print(postStr);
 
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" degrees Celsius Humidity: ");
Serial.print(h);
Serial.println("Sending data to Thingspeak");
}
Viewing all 167 articles
Browse latest View live