Kaori Watanabe / Mbed OS Project

Dependencies:   DHT11 LPS22HB LSM6DSL VL53L0X

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ThingSpeak.cpp Source File

ThingSpeak.cpp

00001 #include "ThingSpeak.h"
00002 
00003 //#include "Sensors.h"
00004 
00005 
00006 const char *sec2str(nsapi_security_t sec)
00007 {
00008     switch (sec) {
00009         case NSAPI_SECURITY_NONE:
00010             return "None";
00011         case NSAPI_SECURITY_WEP:
00012             return "WEP";
00013         case NSAPI_SECURITY_WPA:
00014             return "WPA";
00015         case NSAPI_SECURITY_WPA2:
00016             return "WPA2";
00017         case NSAPI_SECURITY_WPA_WPA2:
00018             return "WPA/WPA2";
00019         case NSAPI_SECURITY_UNKNOWN:
00020         default:
00021             return "Unknown";
00022     }
00023 }
00024 
00025 int scan_demo(WiFiInterface *wifi)
00026 {
00027     WiFiAccessPoint *ap;
00028 
00029     printf("Scan:\n");
00030 
00031     int count = wifi->scan(NULL,0);
00032     printf("%d networks available.\n", count);
00033 
00034     /* Limit number of network arbitrary to 15 */
00035     count = count < 15 ? count : 15;
00036 
00037     ap = new WiFiAccessPoint[count];
00038     count = wifi->scan(ap, count);
00039     for (int i = 0; i < count; i++)
00040     {
00041         printf("Network: %s secured: %s BSSID: %hhX:%hhX:%hhX:%hhx:%hhx:%hhx RSSI: %hhd Ch: %hhd\n", ap[i].get_ssid(),
00042                sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2],
00043                ap[i].get_bssid()[3], ap[i].get_bssid()[4], ap[i].get_bssid()[5], ap[i].get_rssi(), ap[i].get_channel());
00044     }
00045 
00046     delete[] ap;
00047     return count;
00048 }
00049 
00050 
00051 void send_values(char* urlBuffer)
00052 {
00053   TCPSocket socket;
00054   socket.open(&wifi);
00055   int scount;
00056   int bufLen = strlen(urlBuffer);
00057 
00058   if(socket.connect("api.thingspeak.com", 80) == 0)
00059   {
00060     scount = socket.send(urlBuffer, bufLen+1);
00061     printf ("Message sent: strlen=%d,urlBuffer=%.*s\r\n",bufLen,bufLen-2,urlBuffer);
00062   }
00063   else printf("Failed to connect to ThingSpeak.\r\n");
00064 
00065 
00066   // Recieve a simple http response and print out the response line
00067   char rbuffer[2000];
00068   int response = socket.recv(rbuffer, sizeof rbuffer);
00069   if (response < 0) {
00070       printf("Error receiving data: %d\n\r", response);
00071   } else {
00072       printf("recv %d [%.*s]\n\r", response, strstr(rbuffer, "\r\n")-rbuffer, rbuffer);
00073   }
00074 
00075   socket.close();
00076 
00077 }