Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: EthernetInterface WebSocketClient mbed-rtos mbed
main.cpp
00001 #include "mbed.h" 00002 #include "EthernetInterface.h" 00003 #include "Websocket.h" 00004 00005 DigitalOut myled(LED1); 00006 AnalogIn light_in(p20); 00007 AnalogIn temp_in(p19); 00008 AnalogIn humid_in(p18); 00009 00010 int main() 00011 { 00012 const char apikey[] = "xxxxxxxxxxxxxxxxxxxxxxx"; // your apikey 00013 const char feed[]= "/feeds/00000"; // //your feed URL 00014 char cms[512]; 00015 char recv[512]; 00016 int token = 0; 00017 EthernetInterface eth; 00018 eth.init(); 00019 eth.connect(); 00020 wait(5); 00021 printf("IP Address is %s\n\r", eth.getIPAddress()); 00022 00023 while (1) { 00024 Websocket ws("ws://api.cosm.com:8080/feeds/"); 00025 ws.connect(); 00026 if(ws.read(recv)) { 00027 printf("rcv: %s\r\n", recv); 00028 } 00029 00030 float r_light, r_temp, r_humid; 00031 float light, temp, humid; 00032 00033 light = light_in; 00034 temp = temp_in; 00035 humid = humid_in; 00036 00037 r_light = light * 3.3 * 5 / 0.013; 00038 r_temp = temp * 3.3 * 100 - 60; 00039 r_humid = humid * 3.3 * 100; 00040 00041 myled = 1; 00042 sprintf (cms,"{\"method\" : \"put\",\"resource\" : \"%s\",\"params\" : {},\"headers\" : {\"X-ApiKey\":\"%s\"},\"body\" :{\"version\" : \"1.0.0\",\"datastreams\" : [{\"id\" : \"0\",\"current_value\" : \"%4.0f\"},{\"id\" : \"1\",\"current_value\" : \"%3.0f\"},{\"id\" : \"2\",\"current_value\" : \"%3.0f\"}]},\"token\" : \"0x%d\"}\r\n",feed,apikey,r_light,r_temp,r_humid,token); 00043 //set your ID and value 00044 printf ("%s\r\n",cms); 00045 token++; 00046 int res = ws.send(cms); 00047 myled= 0 ; 00048 wait(5); 00049 00050 00051 if(ws.read(recv)) { 00052 printf("rcv: %s\r\n", recv); 00053 wait(1); 00054 } 00055 00056 ws.close(); 00057 wait(300); 00058 00059 } 00060 }
Generated on Wed Jul 13 2022 22:36:08 by
1.7.2