modular-2 access china mobile's OneNet By Http
Dependencies: MbedJSONValue mbed-http
main.cpp@0:19ff9eff4dc2, 2018-06-22 (annotated)
- Committer:
- yao6116601
- Date:
- Fri Jun 22 23:19:24 2018 +0000
- Revision:
- 0:19ff9eff4dc2
access OneNet
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yao6116601 | 0:19ff9eff4dc2 | 1 | #include "mbed.h" |
yao6116601 | 0:19ff9eff4dc2 | 2 | #include "EthernetInterface.h" |
yao6116601 | 0:19ff9eff4dc2 | 3 | #include "http_request.h" |
yao6116601 | 0:19ff9eff4dc2 | 4 | #include "http_response.h" |
yao6116601 | 0:19ff9eff4dc2 | 5 | #include "MbedJSONValue.h" |
yao6116601 | 0:19ff9eff4dc2 | 6 | #include <string> |
yao6116601 | 0:19ff9eff4dc2 | 7 | static const char* mbedIp = "192.168.31.28"; //IP |
yao6116601 | 0:19ff9eff4dc2 | 8 | static const char* mbedMask = "255.255.255.0"; // Mask |
yao6116601 | 0:19ff9eff4dc2 | 9 | static const char* mbedGateway = "192.168.31.1"; //Gateway |
yao6116601 | 0:19ff9eff4dc2 | 10 | DigitalOut myled(PF_14); |
yao6116601 | 0:19ff9eff4dc2 | 11 | EthernetInterface eth; |
yao6116601 | 0:19ff9eff4dc2 | 12 | TCPSocket socket; |
yao6116601 | 0:19ff9eff4dc2 | 13 | char uri[256]; |
yao6116601 | 0:19ff9eff4dc2 | 14 | char devid[] = "34485081"; |
yao6116601 | 0:19ff9eff4dc2 | 15 | char api_key[] ="m7ARDyXF2QmO5O7u7UaBO8HHPpU="; |
yao6116601 | 0:19ff9eff4dc2 | 16 | char host[] = "api.heclouds.com"; |
yao6116601 | 0:19ff9eff4dc2 | 17 | int softTime; |
yao6116601 | 0:19ff9eff4dc2 | 18 | int main() { |
yao6116601 | 0:19ff9eff4dc2 | 19 | float temp; |
yao6116601 | 0:19ff9eff4dc2 | 20 | char body[64]; |
yao6116601 | 0:19ff9eff4dc2 | 21 | MbedJSONValue demo; |
yao6116601 | 0:19ff9eff4dc2 | 22 | HttpRequest*request; |
yao6116601 | 0:19ff9eff4dc2 | 23 | HttpResponse* response; |
yao6116601 | 0:19ff9eff4dc2 | 24 | printf("modular-2 Connect to OneNet\n"); |
yao6116601 | 0:19ff9eff4dc2 | 25 | eth.set_network(mbedIp,mbedMask,mbedGateway); |
yao6116601 | 0:19ff9eff4dc2 | 26 | eth.connect(); |
yao6116601 | 0:19ff9eff4dc2 | 27 | socket.open(ð); |
yao6116601 | 0:19ff9eff4dc2 | 28 | socket.connect("api.heclouds.com", 80); |
yao6116601 | 0:19ff9eff4dc2 | 29 | softTime=0; |
yao6116601 | 0:19ff9eff4dc2 | 30 | while(1) { |
yao6116601 | 0:19ff9eff4dc2 | 31 | wait(1.0); // 1 sec |
yao6116601 | 0:19ff9eff4dc2 | 32 | softTime++; |
yao6116601 | 0:19ff9eff4dc2 | 33 | if (softTime>4) |
yao6116601 | 0:19ff9eff4dc2 | 34 | { |
yao6116601 | 0:19ff9eff4dc2 | 35 | softTime=0; |
yao6116601 | 0:19ff9eff4dc2 | 36 | temp=(((double) rand() / (RAND_MAX)) + 1)*100; |
yao6116601 | 0:19ff9eff4dc2 | 37 | sprintf(body,"{\"temperature\":%f}",temp); |
yao6116601 | 0:19ff9eff4dc2 | 38 | uri[0] = 0; |
yao6116601 | 0:19ff9eff4dc2 | 39 | strcat(uri,"http://api.heclouds.com/devices/"); |
yao6116601 | 0:19ff9eff4dc2 | 40 | strcat(uri,devid); |
yao6116601 | 0:19ff9eff4dc2 | 41 | strcat(uri,"/datapoints?type=3"); |
yao6116601 | 0:19ff9eff4dc2 | 42 | request=new HttpRequest(ð,HTTP_POST,uri); |
yao6116601 | 0:19ff9eff4dc2 | 43 | request->set_header("api-key",api_key); |
yao6116601 | 0:19ff9eff4dc2 | 44 | request->set_header("Content-Type", "application/json"); |
yao6116601 | 0:19ff9eff4dc2 | 45 | sprintf(body,"{\"temperature\":%f}",temp); |
yao6116601 | 0:19ff9eff4dc2 | 46 | response= request->send(body, strlen(body)); |
yao6116601 | 0:19ff9eff4dc2 | 47 | printf("status is %d - %s\n", response->get_status_code(), response->get_status_message().c_str()); |
yao6116601 | 0:19ff9eff4dc2 | 48 | // printf("body is:\n%s\n", response->get_body_as_string().c_str()); |
yao6116601 | 0:19ff9eff4dc2 | 49 | delete request; |
yao6116601 | 0:19ff9eff4dc2 | 50 | } |
yao6116601 | 0:19ff9eff4dc2 | 51 | // get switch |
yao6116601 | 0:19ff9eff4dc2 | 52 | uri[0] = 0; |
yao6116601 | 0:19ff9eff4dc2 | 53 | strcat(uri,"http://api.heclouds.com/devices/"); |
yao6116601 | 0:19ff9eff4dc2 | 54 | strcat(uri,devid); |
yao6116601 | 0:19ff9eff4dc2 | 55 | strcat(uri,"/datastreams/switch"); |
yao6116601 | 0:19ff9eff4dc2 | 56 | // strcat(http_cmd,"switch"); |
yao6116601 | 0:19ff9eff4dc2 | 57 | request=new HttpRequest(ð,HTTP_GET,uri); |
yao6116601 | 0:19ff9eff4dc2 | 58 | request->set_header("api-key",api_key); |
yao6116601 | 0:19ff9eff4dc2 | 59 | request->set_header("Content-Type", "application/json"); |
yao6116601 | 0:19ff9eff4dc2 | 60 | |
yao6116601 | 0:19ff9eff4dc2 | 61 | response= request->send(body, strlen(body)); |
yao6116601 | 0:19ff9eff4dc2 | 62 | // printf("body is:\n%s\n", response->get_body_as_string().c_str()); |
yao6116601 | 0:19ff9eff4dc2 | 63 | parse(demo, response->get_body_as_string().c_str()); |
yao6116601 | 0:19ff9eff4dc2 | 64 | int sw; |
yao6116601 | 0:19ff9eff4dc2 | 65 | sw=demo["data"]["current_value"].get<int>(); |
yao6116601 | 0:19ff9eff4dc2 | 66 | printf("sw=%d\n",sw); |
yao6116601 | 0:19ff9eff4dc2 | 67 | myled=sw; |
yao6116601 | 0:19ff9eff4dc2 | 68 | delete request; |
yao6116601 | 0:19ff9eff4dc2 | 69 | } |
yao6116601 | 0:19ff9eff4dc2 | 70 | } |