Reading back data from 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
Reading back data from Thingspeak
Avatar
astrotutor
New Member
Forum Posts: 3
sp_UserOfflineSmall Offline
1
July 30, 2017 - 8:15 am
sp_Permalink sp_Print

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

Avatar
rw950431

Forum Posts: 252
sp_UserOfflineSmall Offline
2
July 31, 2017 - 6:37 am
sp_Permalink sp_Print sp_EditHistory

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

Avatar
astrotutor
New Member
Forum Posts: 3
sp_UserOfflineSmall Offline
3
August 1, 2017 - 8:48 am
sp_Permalink sp_Print

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...

Avatar
rw950431

Forum Posts: 252
sp_UserOfflineSmall Offline
4
August 2, 2017 - 3:08 am
sp_Permalink sp_Print

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).

Avatar
astrotutor
New Member
Forum Posts: 3
sp_UserOfflineSmall Offline
5
August 3, 2017 - 4:19 am
sp_Permalink sp_Print

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

Avatar
2SEAN2
New Member
Forum Posts: 2
sp_UserOfflineSmall Offline
6
October 30, 2017 - 5:48 am
sp_Permalink sp_Print

Dear Friends,

I have tried the above code and the output value is getting printed on the serial monitor from thingspeak. Please help me to display the value on the LED Matrix display with max7219 and fonts.h libraries.

Thanks in adavnce.

Avatar
piajola

Gold
Forum Posts: 76
sp_UserOfflineSmall Offline
7
October 30, 2017 - 2:29 pm
sp_Permalink sp_Print

Hi 2SEAN2,

From Andreas Spiess in video #86 MAX7219 Dot Matrix Display, Thingspeak, ESP8266 ...

and read the description and comments

I think there is enough info for your cause Wink

Good luck

Avatar
2SEAN2
New Member
Forum Posts: 2
sp_UserOfflineSmall Offline
8
October 31, 2017 - 2:40 am
sp_Permalink sp_Print

Thanks for the quick response piajola.

Will check the same and update.

 

Thanks again.

Forum Timezone: America/New_York

Most Users Ever Online: 114

Currently Online:
25 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

rw950431: 252

Vinod: 164

cstapels: 96

piajola: 75

vespapierre: 63

Adarsh_Murthy: 59

Newest Members:

epicpaymentsny

DanielBobby

mercedeszw18

ujuxoketate

olihfez

winnieeq69

Forum Stats:

Groups: 4

Forums: 17

Topics: 1313

Posts: 4565

 

Member Stats:

Guest Posters: 1

Members: 5703

Moderators: 0

Admins: 2

Administrators: Hans, lee