Reading data from ThingsSpeak website

Hi,

I’m trying to build temperature monitor where Core 1 measures temp outside and sends data to ThingsSpeak website.
Core 2 is located in my room and has TFT sceen attached to it. This core displays temperature. (I tried using Spark publish/subscribe but results are highly unstable).

What i can’t figure out (since i haven’t found any examples) is how to “read” data from internet. What i would need is to read data from : http://api.thingspeak.com/channels/20611/feed/last.json
And more specifically: “field5”:null,
null has usually value like -2.2 etc.

Can someone help me with simple working example so that i could integrate it to my code?

Hi @qwerty009,

I have built a similar project but without the ThingsSpeak integration. I have had great success with the publish/subscribe route. I am sending data from one core, with a thermocouple breakout and type K thermocouple to another core attached to a 16x32 RGB LED matrix, to display it.

Have a look at this thread and see if it helps. https://community.spark.io/t/spark-variable-and-publish-subscribe-trouble-solved/8978/21

Please could you explain a little more what you mean by

My attempts with Subscribe/publish are in this topic: https://community.spark.io/t/how-to-transfer-data-between-2-cores/9050/15

Hi @qwerty099,

Looks like you have been guided to the thread in which I was helped.

I could recommend ITTT (If this then that). Using the same publishing code I have sent the data to a spread sheet on my google drive and automatically updated a graph in the spreadsheet. It is really easy to do. You can also set high and low points that can send you an email when reached. It worked really well for me.

There are lots of tutorials to do this but if you need help just ask. :smile:

Sorry I can not help with ThingsSpeak :frowning:

I managed to write smth that is sort of working. It works as intended, but core restarted in less then hour running this code.
Can someone take a look, maybe there are some obvious errors from my part?

[code]// This #include statement was automatically added by the Spark IDE.
#include “HttpClient/HttpClient.h”

// This #include statement was automatically added by the Spark IDE.
#include “TimeAlarms/TimeAlarms.h”

// This #include statement was automatically added by the Spark IDE.
#include “Adafruit_mfGFX/Adafruit_mfGFX.h”

// This #include statement was automatically added by the Spark IDE.
#include “Adafruit_ILI9341/Adafruit_ILI9341.h”

char displayText[64];

Adafruit_ILI9341 tft = Adafruit_ILI9341(A2, A1, A0);

float minTemp = 100;
float maxTemp = -100;
float hetkeTemp = 0;
int update = 0;
int i=0;
String timeStr;
String timeMax;
String timeMin;

unsigned long check = 0;
unsigned int uptime = 0;

HttpClient http;

// Headers currently need to be set at init, useful for API keys etc.
http_header_t headers[] = {
{ “Content-Type”, “application/json” },
// { “Accept” , “application/json” },
{ “Accept” , “/”},
{ NULL, NULL } // NOTE: Always terminate headers will NULL
};

http_request_t request;
http_response_t response;

void setup() {
Time.zone(+2);
Alarm.alarmRepeat(01,00,00, MidnightAlarm);
Alarm.timerRepeat(60, Repeats);
Serial.begin(9600);
tft.begin();
tft.fillScreen(ILI9341_WHITE);

}

void loop() {

    if(hetkeTemp>-40 && hetkeTemp <50 && update == 1){
        if (minTemp>hetkeTemp){
           
            minTemp=hetkeTemp;
            timeMin = "";
            if(Time.hour()<10){timeMin +=" ";}
            timeMin += Time.hour();
            timeMin += ":";
            if(Time.minute()<10){timeMin +="0";}
            timeMin += Time.minute();
        }
        if (maxTemp<hetkeTemp){
            
            maxTemp=hetkeTemp;
            timeMax = "";
            if(Time.hour()<10){timeMax +=" ";}
            timeMax += Time.hour();
            timeMax += ":";
            if(Time.minute()<10){timeMax +="0";}
            timeMax += Time.minute();
        }
        
             uptime=millis()/1000/60/60;

        tft.setRotation(1);
        tft.setTextColor(ILI9341_BLACK, ILI9341_WHITE);
        
        tft.setTextSize(2);
        tft.setCursor(160,3);
        tft.print(uptime);
        
        tft.setTextSize(8);
        tft.setCursor(40,30);
        if(hetkeTemp>0){tft.print("+");} 
        tft.print(hetkeTemp,1); 
        
        tft.setTextSize(3);
        tft.setCursor(40,120);
        tft.print("MAX ");
        tft.setTextSize(2);
        tft.setCursor(40,145);
        tft.print(timeMax);
        tft.setCursor(150,120);
        tft.setTextSize(5);
        if(maxTemp>0){tft.print("+");}
        tft.print(maxTemp,1); 
        
        tft.setTextSize(3);
        tft.setCursor(40,170);
        tft.print("MIN ");
        tft.setTextSize(2);
        tft.setCursor(40,195);
        tft.print(timeMin);
        tft.setCursor(150,170);
        tft.setTextSize(5);
        if(minTemp>0){tft.print("+");}
        tft.print(minTemp,1); 
        
        tft.setTextSize(2);
        tft.setCursor(110,222);
        tft.print(timeStr);
        
        update = 0;
    }

    //digitalClockDisplay();

//Serial.println();
Alarm.delay(0);

}

void Repeats(){

// Serial.println();
//Serial.println(“Application>\tStart of Loop.”);
// Request path and body can be set at runtime or at setup.
request.hostname = “api.thingspeak.com”;
request.port = 80;
request.path = “/channels/24877/fields/1/last.json?key=GOKBGDF6332V45P3”;

// The library also supports sending a body with your request:
//request.body = "{\"key\":\"value\"}";

// Get request
http.get(request, response, headers);
//Serial.print("Application>\tResponse status: ");
//Serial.println(response.status);
if(response.status == 200){
    Serial.print("Application>\tHTTP Response Body: ");
    
    String web = response.body;
    String aeg2 = web.substring(28,34);
    String aeg1 = web.substring(26,28);
    int gmt = atof(aeg1.c_str());
    int tund = gmt + 2;
    
    timeStr = "";
    timeStr += tund;
    timeStr += aeg2;
    timeStr += "";

    int last = web.lastIndexOf('"');
    int previous = web.lastIndexOf('"', last - 1 );
    String result = web.substring(previous+1, last); 
    hetkeTemp = atof(result.c_str());
    
  //  Serial.println(timeStr);   
    
   update = 1;
}

}

void MidnightAlarm(){

minTemp = hetkeTemp;
maxTemp = hetkeTemp;
Spark.syncTime();

}[/code]

Fault is most likely in repeats() imo.