reading json objects from thingspeak

Dependencies:   ESP8266 mbed picojson

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002  * File: main.cpp
00003  * Program: iac-audit
00004  * Developer: Connor Horne
00005  * Description: Sensor reading and sending to cloud program made for the IAC. Built for use with the FRDM-K64F and attahced systems.
00006 */
00007 
00008 
00009 // mbed headers
00010 #include "mbed.h"
00011 #include "ESP8266.h"
00012 #include "picojson.h"
00013 #include <string>
00014 
00015 bool setup(ESP8266 * esp, Serial * output);
00016 bool receive(ESP8266 * esp, Serial * output);
00017 
00018 picojson::value v;
00019 char * json;
00020 char resp[1024]; //buffer for the responce
00021 double field;
00022 bool pass = false;
00023 
00024 // entry point
00025 int main()
00026 {   
00027     // system wide vars
00028     mbed::Serial output(USBTX, USBRX);  
00029     ESP8266 esp(PTC17, PTC16, 115200);
00030     
00031     output.printf("starting esp8266\r\n");
00032     
00033     esp.Reset();
00034     wait(5);
00035     //esp.baud(115200);
00036     wait(5);
00037     
00038     output.printf("starting setup\r\n");    
00039     
00040     pass = setup(&esp, &output);
00041     
00042     output.printf("sending value\r\n");
00043     
00044     pass = receive(&esp, &output);
00045     
00046 } // end main
00047 
00048 bool setup(ESP8266 * esp, Serial * comm)
00049 {
00050     char buffer[1024];
00051     char ip[256];
00052     char status[1024];
00053     
00054     comm->printf("!setting mode\r\n");
00055     esp->SetMode(3); //'1'
00056     esp->RcvReply(buffer, 9000);
00057     comm->printf("response: %s\r\n", buffer);
00058     
00059     comm->printf("!setting connection\r\n");
00060     esp->SetSingle();
00061     esp->RcvReply(buffer, 5000);
00062     comm->printf("response: %s\r\n", buffer);
00063     
00064     /*comm->printf("!getting list\r\n");    
00065     esp->GetList(buffer);    
00066     comm->printf("got list\r\n");
00067     comm->printf("%s\r\n", buffer);*/
00068   
00069     comm->printf("!joining AP\r\n");    
00070     esp->Join("SSID", "password"); //change to SSID and Password
00071     esp->RcvReply(buffer, 5000);
00072     comm->printf("response: %s\r\n", buffer);
00073     
00074     comm->printf("!getting IP\r\n");    
00075     esp->GetIP(ip);
00076     esp->RcvReply(buffer, 5000);
00077     comm->printf("response: %s\r\n", buffer);
00078     comm->printf("ip: %s\r\n", ip);
00079     
00080     comm->printf("!getting connection status\r\n");    
00081     esp->GetConnStatus(status);
00082     esp->RcvReply(buffer, 5000);
00083     comm->printf("response: %s\r\n", buffer);
00084     comm->printf("stats: %s\r\n", status);
00085     
00086     return true;
00087 }
00088 
00089 bool receive(ESP8266 * esp, Serial * comm)
00090 {
00091     char buffer[2048];
00092     char cmd[64];
00093         
00094     comm->printf("!setting up TCP connection\r\n");   
00095     strcpy(cmd,"AT+CIPSTART=\"TCP\",\"184.106.153.149\",80");
00096     esp->SendCMD(cmd);
00097     esp->RcvReply(buffer, 5000);
00098     comm->printf("response: %s\r\n", buffer);
00099     
00100     char message[512];
00101     sprintf(resp,"GET http://api.thingspeak.com/channels/246738/feeds.json?metadata=true&results=0\r\n"); //GET https://api.thingspeak.com/channels/215748/feeds/last\r\n
00102     comm->printf("!sending packet length \r\n");   
00103     sprintf(cmd, "AT+CIPSEND=%d", strlen(resp)+2);
00104     esp->SendCMD(cmd);
00105     esp->RcvReply(buffer, 5000);
00106     comm->printf("response: %s\r\n", buffer);
00107     
00108     comm->printf("!sending data packet\r\n");
00109     esp->SendCMD(resp);
00110     esp->RcvReply(buffer, 10000);
00111     comm->printf("response: %s\r\n", buffer);
00112     if(strlen(buffer)!=0)
00113     {
00114     comm->printf("response: %s\r\n", buffer);
00115     strcpy(json, buffer);
00116     char * json=(char*) malloc(strlen(buffer)+1);
00117           comm->printf("parsing\r\n");
00118           string err = picojson::parse(v, json, json + strlen(json));
00119           if(err.empty())
00120           {
00121            field=v.get("field1").get<double>();
00122           // comm->printf("field1 value: %f", field);
00123            }
00124           else
00125           {
00126               comm->printf("error parsing\r\n");
00127           }     
00128         }
00129     comm->printf("field1 value: %f", field);
00130     comm->printf("!closing TCP connection\r\n");   
00131     strcpy(cmd, "AT+CIPCLOSE");
00132     esp->SendCMD(cmd);
00133     esp->RcvReply(buffer, 7000);
00134     comm->printf("response: %s\r\n", buffer);
00135     
00136     return true;
00137 }