modular-2 access china mobile's OneNet By Http

Dependencies:   MbedJSONValue mbed-http

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "EthernetInterface.h"
00003 #include "http_request.h"
00004 #include "http_response.h"
00005 #include "MbedJSONValue.h"
00006  #include <string>
00007 static const char*          mbedIp       = "192.168.31.28";  //IP
00008 static const char*          mbedMask     = "255.255.255.0";  // Mask
00009 static const char*          mbedGateway  = "192.168.31.1";    //Gateway
00010 DigitalOut myled(PF_14);
00011 EthernetInterface eth;
00012 TCPSocket socket;
00013 char uri[256];
00014 char devid[] = "34485081"; 
00015 char api_key[] ="m7ARDyXF2QmO5O7u7UaBO8HHPpU=";
00016 char host[] = "api.heclouds.com";
00017 int softTime;
00018 int main() {
00019     float temp;
00020     char body[64];
00021      MbedJSONValue demo;
00022      HttpRequest*request;
00023      HttpResponse* response;
00024     printf("modular-2 Connect to OneNet\n");
00025     eth.set_network(mbedIp,mbedMask,mbedGateway);
00026     eth.connect();  
00027     socket.open(&eth);
00028     socket.connect("api.heclouds.com", 80);
00029     softTime=0;
00030     while(1) {
00031         wait(1.0); // 1 sec
00032         softTime++;
00033         if (softTime>4)
00034         { 
00035         softTime=0;
00036         temp=(((double) rand() / (RAND_MAX)) + 1)*100;
00037          sprintf(body,"{\"temperature\":%f}",temp);
00038         uri[0] = 0;
00039         strcat(uri,"http://api.heclouds.com/devices/");
00040         strcat(uri,devid);
00041         strcat(uri,"/datapoints?type=3");
00042        request=new HttpRequest(&eth,HTTP_POST,uri);
00043        request->set_header("api-key",api_key);
00044        request->set_header("Content-Type", "application/json"); 
00045        sprintf(body,"{\"temperature\":%f}",temp); 
00046         response= request->send(body, strlen(body));
00047        printf("status is %d - %s\n", response->get_status_code(), response->get_status_message().c_str());
00048   //  printf("body is:\n%s\n", response->get_body_as_string().c_str());
00049       delete request;
00050       }
00051       // get switch
00052        uri[0] = 0;
00053         strcat(uri,"http://api.heclouds.com/devices/");
00054         strcat(uri,devid);
00055         strcat(uri,"/datastreams/switch");
00056       //   strcat(http_cmd,"switch");
00057       request=new HttpRequest(&eth,HTTP_GET,uri);
00058        request->set_header("api-key",api_key);
00059        request->set_header("Content-Type", "application/json"); 
00060      
00061        response= request->send(body, strlen(body));
00062   //  printf("body is:\n%s\n", response->get_body_as_string().c_str());  
00063      parse(demo, response->get_body_as_string().c_str()); 
00064     int sw;
00065     sw=demo["data"]["current_value"].get<int>();
00066     printf("sw=%d\n",sw);
00067     myled=sw;
00068         delete request;
00069     }
00070 }