reading json objects from thingspeak

Dependencies:   ESP8266 mbed picojson

Committer:
siddharthp
Date:
Fri Apr 21 15:50:57 2017 +0000
Revision:
3:b71e184f0c95
Parent:
2:986bafeeb5ab
buffer size changed

Who changed what in which revision?

UserRevisionLine numberNew contents of line
siddharthp 0:1ef95b520410 1 /*
siddharthp 0:1ef95b520410 2 * File: main.cpp
siddharthp 0:1ef95b520410 3 * Program: iac-audit
siddharthp 0:1ef95b520410 4 * Developer: Connor Horne
siddharthp 0:1ef95b520410 5 * Description: Sensor reading and sending to cloud program made for the IAC. Built for use with the FRDM-K64F and attahced systems.
siddharthp 0:1ef95b520410 6 */
siddharthp 0:1ef95b520410 7
siddharthp 0:1ef95b520410 8
siddharthp 0:1ef95b520410 9 // mbed headers
siddharthp 0:1ef95b520410 10 #include "mbed.h"
siddharthp 0:1ef95b520410 11 #include "ESP8266.h"
siddharthp 0:1ef95b520410 12 #include "picojson.h"
siddharthp 0:1ef95b520410 13 #include <string>
siddharthp 0:1ef95b520410 14
siddharthp 0:1ef95b520410 15 bool setup(ESP8266 * esp, Serial * output);
siddharthp 0:1ef95b520410 16 bool receive(ESP8266 * esp, Serial * output);
siddharthp 0:1ef95b520410 17
siddharthp 0:1ef95b520410 18 picojson::value v;
siddharthp 0:1ef95b520410 19 char * json;
siddharthp 0:1ef95b520410 20 char resp[1024]; //buffer for the responce
siddharthp 0:1ef95b520410 21 double field;
siddharthp 0:1ef95b520410 22 bool pass = false;
siddharthp 0:1ef95b520410 23
siddharthp 0:1ef95b520410 24 // entry point
siddharthp 0:1ef95b520410 25 int main()
siddharthp 0:1ef95b520410 26 {
siddharthp 0:1ef95b520410 27 // system wide vars
siddharthp 0:1ef95b520410 28 mbed::Serial output(USBTX, USBRX);
siddharthp 0:1ef95b520410 29 ESP8266 esp(PTC17, PTC16, 115200);
siddharthp 0:1ef95b520410 30
siddharthp 0:1ef95b520410 31 output.printf("starting esp8266\r\n");
siddharthp 0:1ef95b520410 32
siddharthp 0:1ef95b520410 33 esp.Reset();
siddharthp 0:1ef95b520410 34 wait(5);
siddharthp 0:1ef95b520410 35 //esp.baud(115200);
siddharthp 0:1ef95b520410 36 wait(5);
siddharthp 0:1ef95b520410 37
siddharthp 0:1ef95b520410 38 output.printf("starting setup\r\n");
siddharthp 0:1ef95b520410 39
siddharthp 0:1ef95b520410 40 pass = setup(&esp, &output);
siddharthp 0:1ef95b520410 41
siddharthp 0:1ef95b520410 42 output.printf("sending value\r\n");
siddharthp 0:1ef95b520410 43
siddharthp 0:1ef95b520410 44 pass = receive(&esp, &output);
siddharthp 0:1ef95b520410 45
siddharthp 0:1ef95b520410 46 } // end main
siddharthp 0:1ef95b520410 47
siddharthp 0:1ef95b520410 48 bool setup(ESP8266 * esp, Serial * comm)
siddharthp 0:1ef95b520410 49 {
siddharthp 0:1ef95b520410 50 char buffer[1024];
siddharthp 0:1ef95b520410 51 char ip[256];
siddharthp 0:1ef95b520410 52 char status[1024];
siddharthp 0:1ef95b520410 53
siddharthp 0:1ef95b520410 54 comm->printf("!setting mode\r\n");
siddharthp 0:1ef95b520410 55 esp->SetMode(3); //'1'
siddharthp 0:1ef95b520410 56 esp->RcvReply(buffer, 9000);
siddharthp 0:1ef95b520410 57 comm->printf("response: %s\r\n", buffer);
siddharthp 0:1ef95b520410 58
siddharthp 0:1ef95b520410 59 comm->printf("!setting connection\r\n");
siddharthp 0:1ef95b520410 60 esp->SetSingle();
siddharthp 0:1ef95b520410 61 esp->RcvReply(buffer, 5000);
siddharthp 0:1ef95b520410 62 comm->printf("response: %s\r\n", buffer);
siddharthp 0:1ef95b520410 63
siddharthp 0:1ef95b520410 64 /*comm->printf("!getting list\r\n");
siddharthp 0:1ef95b520410 65 esp->GetList(buffer);
siddharthp 0:1ef95b520410 66 comm->printf("got list\r\n");
siddharthp 0:1ef95b520410 67 comm->printf("%s\r\n", buffer);*/
siddharthp 0:1ef95b520410 68
siddharthp 0:1ef95b520410 69 comm->printf("!joining AP\r\n");
siddharthp 1:b9ca2db84b72 70 esp->Join("SSID", "password"); //change to SSID and Password
siddharthp 0:1ef95b520410 71 esp->RcvReply(buffer, 5000);
siddharthp 0:1ef95b520410 72 comm->printf("response: %s\r\n", buffer);
siddharthp 0:1ef95b520410 73
siddharthp 0:1ef95b520410 74 comm->printf("!getting IP\r\n");
siddharthp 0:1ef95b520410 75 esp->GetIP(ip);
siddharthp 0:1ef95b520410 76 esp->RcvReply(buffer, 5000);
siddharthp 0:1ef95b520410 77 comm->printf("response: %s\r\n", buffer);
siddharthp 0:1ef95b520410 78 comm->printf("ip: %s\r\n", ip);
siddharthp 0:1ef95b520410 79
siddharthp 0:1ef95b520410 80 comm->printf("!getting connection status\r\n");
siddharthp 0:1ef95b520410 81 esp->GetConnStatus(status);
siddharthp 0:1ef95b520410 82 esp->RcvReply(buffer, 5000);
siddharthp 0:1ef95b520410 83 comm->printf("response: %s\r\n", buffer);
siddharthp 0:1ef95b520410 84 comm->printf("stats: %s\r\n", status);
siddharthp 0:1ef95b520410 85
siddharthp 0:1ef95b520410 86 return true;
siddharthp 0:1ef95b520410 87 }
siddharthp 0:1ef95b520410 88
siddharthp 0:1ef95b520410 89 bool receive(ESP8266 * esp, Serial * comm)
siddharthp 0:1ef95b520410 90 {
siddharthp 3:b71e184f0c95 91 char buffer[2048];
siddharthp 0:1ef95b520410 92 char cmd[64];
siddharthp 0:1ef95b520410 93
siddharthp 0:1ef95b520410 94 comm->printf("!setting up TCP connection\r\n");
siddharthp 0:1ef95b520410 95 strcpy(cmd,"AT+CIPSTART=\"TCP\",\"184.106.153.149\",80");
siddharthp 0:1ef95b520410 96 esp->SendCMD(cmd);
siddharthp 0:1ef95b520410 97 esp->RcvReply(buffer, 5000);
siddharthp 0:1ef95b520410 98 comm->printf("response: %s\r\n", buffer);
siddharthp 0:1ef95b520410 99
siddharthp 0:1ef95b520410 100 char message[512];
siddharthp 0:1ef95b520410 101 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
siddharthp 0:1ef95b520410 102 comm->printf("!sending packet length \r\n");
siddharthp 0:1ef95b520410 103 sprintf(cmd, "AT+CIPSEND=%d", strlen(resp)+2);
siddharthp 0:1ef95b520410 104 esp->SendCMD(cmd);
siddharthp 0:1ef95b520410 105 esp->RcvReply(buffer, 5000);
siddharthp 0:1ef95b520410 106 comm->printf("response: %s\r\n", buffer);
siddharthp 0:1ef95b520410 107
siddharthp 0:1ef95b520410 108 comm->printf("!sending data packet\r\n");
siddharthp 0:1ef95b520410 109 esp->SendCMD(resp);
siddharthp 2:986bafeeb5ab 110 esp->RcvReply(buffer, 10000);
siddharthp 0:1ef95b520410 111 comm->printf("response: %s\r\n", buffer);
siddharthp 0:1ef95b520410 112 if(strlen(buffer)!=0)
siddharthp 0:1ef95b520410 113 {
siddharthp 0:1ef95b520410 114 comm->printf("response: %s\r\n", buffer);
siddharthp 0:1ef95b520410 115 strcpy(json, buffer);
siddharthp 0:1ef95b520410 116 char * json=(char*) malloc(strlen(buffer)+1);
siddharthp 0:1ef95b520410 117 comm->printf("parsing\r\n");
siddharthp 0:1ef95b520410 118 string err = picojson::parse(v, json, json + strlen(json));
siddharthp 0:1ef95b520410 119 if(err.empty())
siddharthp 0:1ef95b520410 120 {
siddharthp 0:1ef95b520410 121 field=v.get("field1").get<double>();
siddharthp 0:1ef95b520410 122 // comm->printf("field1 value: %f", field);
siddharthp 0:1ef95b520410 123 }
siddharthp 0:1ef95b520410 124 else
siddharthp 0:1ef95b520410 125 {
siddharthp 0:1ef95b520410 126 comm->printf("error parsing\r\n");
siddharthp 0:1ef95b520410 127 }
siddharthp 0:1ef95b520410 128 }
siddharthp 0:1ef95b520410 129 comm->printf("field1 value: %f", field);
siddharthp 0:1ef95b520410 130 comm->printf("!closing TCP connection\r\n");
siddharthp 0:1ef95b520410 131 strcpy(cmd, "AT+CIPCLOSE");
siddharthp 0:1ef95b520410 132 esp->SendCMD(cmd);
siddharthp 0:1ef95b520410 133 esp->RcvReply(buffer, 7000);
siddharthp 0:1ef95b520410 134 comm->printf("response: %s\r\n", buffer);
siddharthp 0:1ef95b520410 135
siddharthp 0:1ef95b520410 136 return true;
siddharthp 0:1ef95b520410 137 }