I tried ThinkSpeak functionality to the WiFi sample program. It is proven with ODIN-W2.

Dependencies:   LM75B

Fork of mbed-os-example-mbed5-wifi by mbed-os-examples

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* WiFi Example with ThingSpeak
00002  * Copyright (c) 2016 ARM Limited
00003  *                    Fuji Electronics Hiroaki Okoshi 2016.12.22 Modify.
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 #include "mbed.h"
00019 #include "TCPSocket.h"
00020 
00021 #if TARGET_UBLOX_EVK_ODIN_W2
00022 #include "OdinWiFiInterface.h"
00023 OdinWiFiInterface wifi;
00024 #else
00025 #if !TARGET_FF_ARDUINO
00026 #error [NOT_SUPPORTED] Only Arduino form factor devices are supported at this time
00027 #endif
00028 #include "ESP8266Interface.h"
00029 ESP8266Interface wifi(D1, D0);
00030 #endif
00031 
00032 #include "LM75B.h"              //  温度センサー
00033 LM75B sensor(D14,D15);
00034 
00035 DigitalOut led1(LED1);
00036 
00037 const char *sec2str(nsapi_security_t sec)
00038 {
00039     switch (sec) {
00040         case NSAPI_SECURITY_NONE:
00041             return "None";
00042         case NSAPI_SECURITY_WEP:
00043             return "WEP";
00044         case NSAPI_SECURITY_WPA:
00045             return "WPA";
00046         case NSAPI_SECURITY_WPA2:
00047             return "WPA2";
00048         case NSAPI_SECURITY_WPA_WPA2:
00049             return "WPA/WPA2";
00050         case NSAPI_SECURITY_UNKNOWN:
00051         default:
00052             return "Unknown";
00053     }
00054 }
00055 
00056 void scan_demo(WiFiInterface *wifi)
00057 {
00058     WiFiAccessPoint *ap;
00059 
00060     printf("Scan:\r\n");
00061 
00062     int count = wifi->scan(NULL,0);
00063 
00064     /* Limit number of network arbitrary to 15 */
00065     count = count < 15 ? count : 15;
00066 
00067     ap = new WiFiAccessPoint[count];
00068     count = wifi->scan(ap, count);
00069     for (int i = 0; i < count; i++) {
00070         printf("Network: %s secured: %s BSSID: %hhX:%hhX:%hhX:%hhx:%hhx:%hhx RSSI: %hhd Ch: %hhd\r\n", ap[i].get_ssid(),
00071                sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2],
00072                ap[i].get_bssid()[3], ap[i].get_bssid()[4], ap[i].get_bssid()[5], ap[i].get_rssi(), ap[i].get_channel());
00073     }
00074     printf("%d networks available.\r\n", count);
00075 
00076     delete[] ap;
00077 }
00078 
00079 /*
00080  * ThingSpeak Demo
00081  */
00082 
00083 void ThingSpeak_demo(NetworkInterface *net)
00084 {
00085     TCPSocket socket;
00086     socket.open(net);   // ソケットオープン
00087 
00088     socket.connect("api.thingspeak.com", 80);
00089     char urlBuffer[128];
00090 
00091     // 温度情報取得
00092     float temperature = sensor.read();
00093 
00094     // メッセージ用テキストを生成
00095     urlBuffer[0]=0;
00096     sprintf(urlBuffer, "GET /update?api_key=%s&field1=%f\r\n\r\n", MBED_CONF_APP_THINGSPEAK_KEY, temperature);
00097 
00098     // ThingSpeed にメッセージを送信
00099     int BuffreLen = strlen(urlBuffer);
00100     int scount = socket.send(urlBuffer, BuffreLen+1);
00101     printf ("strlen=%d,urlBuffer=%.*s",BuffreLen,BuffreLen-2,urlBuffer);
00102 
00103     // for debug    // デバグ時に受信内容を確認する。
00104     // char rbuffer[64];
00105     // int rcount = socket.recv(rbuffer, sizeof rbuffer);
00106     // printf("recv %d [%.*s]\r\n", rcount, strstr(rbuffer, "\r\n")-rbuffer, rbuffer);
00107 
00108     socket.close(); //ソケットクローズ
00109 }
00110 
00111 int main()
00112 {
00113     printf("WiFi example\r\n\r\n");
00114 
00115     scan_demo(&wifi);
00116 
00117     printf("\r\nConnecting...\r\n");
00118     int ret = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
00119     if (ret != 0) {
00120         printf("\r\nConnection error\r\n");
00121         return -1;
00122     }
00123 
00124     printf("Success\r\n\r\n");
00125     printf("MAC: %s\r\n", wifi.get_mac_address());
00126     printf("IP: %s\r\n", wifi.get_ip_address());
00127     printf("Netmask: %s\r\n", wifi.get_netmask());
00128     printf("Gateway: %s\r\n", wifi.get_gateway());
00129     printf("RSSI: %d\r\n\r\n", wifi.get_rssi());
00130 
00131     printf("ThingSpeak Demo\r\n");
00132 
00133     // 60秒に1回メッセージを送信する。
00134     for (int i=0 ; i<1000 ; i++) {
00135         led1 = !led1;
00136         printf ("%d:",i);
00137         ThingSpeak_demo(&wifi); //ThingSpeed に
00138         Thread::wait(60000);    //60秒に一回送信
00139     }
00140 
00141     wifi.disconnect();
00142 
00143     printf ("Done\r\n");
00144 }