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

Committer:
Okoshi
Date:
Thu Dec 22 10:24:22 2016 +0000
Revision:
10:0db5d718029c
Parent:
8:72d49a084297
I tried ThinkSpeak  functionality to the WiFi sample program.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Okoshi 8:72d49a084297 1 /* WiFi Example with ThingSpeak
mbed_official 0:857719181846 2 * Copyright (c) 2016 ARM Limited
Okoshi 8:72d49a084297 3 * Fuji Electronics Hiroaki Okoshi 2016.12.22 Modify.
mbed_official 0:857719181846 4 *
mbed_official 0:857719181846 5 * Licensed under the Apache License, Version 2.0 (the "License");
mbed_official 0:857719181846 6 * you may not use this file except in compliance with the License.
mbed_official 0:857719181846 7 * You may obtain a copy of the License at
mbed_official 0:857719181846 8 *
mbed_official 0:857719181846 9 * http://www.apache.org/licenses/LICENSE-2.0
mbed_official 0:857719181846 10 *
mbed_official 0:857719181846 11 * Unless required by applicable law or agreed to in writing, software
mbed_official 0:857719181846 12 * distributed under the License is distributed on an "AS IS" BASIS,
mbed_official 0:857719181846 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mbed_official 0:857719181846 14 * See the License for the specific language governing permissions and
mbed_official 0:857719181846 15 * limitations under the License.
mbed_official 0:857719181846 16 */
mbed_official 0:857719181846 17
mbed_official 0:857719181846 18 #include "mbed.h"
mbed_official 0:857719181846 19 #include "TCPSocket.h"
mbed_official 0:857719181846 20
mbed_official 0:857719181846 21 #if TARGET_UBLOX_EVK_ODIN_W2
mbed_official 1:aea78e21a7da 22 #include "OdinWiFiInterface.h"
mbed_official 1:aea78e21a7da 23 OdinWiFiInterface wifi;
mbed_official 0:857719181846 24 #else
mbed_official 1:aea78e21a7da 25 #if !TARGET_FF_ARDUINO
mbed_official 1:aea78e21a7da 26 #error [NOT_SUPPORTED] Only Arduino form factor devices are supported at this time
mbed_official 1:aea78e21a7da 27 #endif
mbed_official 1:aea78e21a7da 28 #include "ESP8266Interface.h"
mbed_official 1:aea78e21a7da 29 ESP8266Interface wifi(D1, D0);
mbed_official 0:857719181846 30 #endif
mbed_official 0:857719181846 31
Okoshi 8:72d49a084297 32 #include "LM75B.h" // 温度センサー
Okoshi 8:72d49a084297 33 LM75B sensor(D14,D15);
Okoshi 8:72d49a084297 34
Okoshi 8:72d49a084297 35 DigitalOut led1(LED1);
Okoshi 8:72d49a084297 36
mbed_official 0:857719181846 37 const char *sec2str(nsapi_security_t sec)
mbed_official 0:857719181846 38 {
mbed_official 0:857719181846 39 switch (sec) {
mbed_official 0:857719181846 40 case NSAPI_SECURITY_NONE:
mbed_official 0:857719181846 41 return "None";
mbed_official 0:857719181846 42 case NSAPI_SECURITY_WEP:
mbed_official 0:857719181846 43 return "WEP";
mbed_official 0:857719181846 44 case NSAPI_SECURITY_WPA:
mbed_official 0:857719181846 45 return "WPA";
mbed_official 0:857719181846 46 case NSAPI_SECURITY_WPA2:
mbed_official 0:857719181846 47 return "WPA2";
mbed_official 0:857719181846 48 case NSAPI_SECURITY_WPA_WPA2:
mbed_official 0:857719181846 49 return "WPA/WPA2";
mbed_official 0:857719181846 50 case NSAPI_SECURITY_UNKNOWN:
mbed_official 0:857719181846 51 default:
mbed_official 0:857719181846 52 return "Unknown";
mbed_official 0:857719181846 53 }
mbed_official 0:857719181846 54 }
mbed_official 0:857719181846 55
mbed_official 0:857719181846 56 void scan_demo(WiFiInterface *wifi)
mbed_official 0:857719181846 57 {
mbed_official 0:857719181846 58 WiFiAccessPoint *ap;
mbed_official 0:857719181846 59
mbed_official 0:857719181846 60 printf("Scan:\r\n");
mbed_official 0:857719181846 61
mbed_official 0:857719181846 62 int count = wifi->scan(NULL,0);
mbed_official 0:857719181846 63
mbed_official 0:857719181846 64 /* Limit number of network arbitrary to 15 */
mbed_official 0:857719181846 65 count = count < 15 ? count : 15;
mbed_official 0:857719181846 66
mbed_official 0:857719181846 67 ap = new WiFiAccessPoint[count];
mbed_official 0:857719181846 68 count = wifi->scan(ap, count);
Okoshi 8:72d49a084297 69 for (int i = 0; i < count; i++) {
mbed_official 0:857719181846 70 printf("Network: %s secured: %s BSSID: %hhX:%hhX:%hhX:%hhx:%hhx:%hhx RSSI: %hhd Ch: %hhd\r\n", ap[i].get_ssid(),
mbed_official 0:857719181846 71 sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2],
mbed_official 0:857719181846 72 ap[i].get_bssid()[3], ap[i].get_bssid()[4], ap[i].get_bssid()[5], ap[i].get_rssi(), ap[i].get_channel());
mbed_official 0:857719181846 73 }
mbed_official 0:857719181846 74 printf("%d networks available.\r\n", count);
mbed_official 0:857719181846 75
mbed_official 0:857719181846 76 delete[] ap;
mbed_official 0:857719181846 77 }
mbed_official 0:857719181846 78
Okoshi 8:72d49a084297 79 /*
Okoshi 8:72d49a084297 80 * ThingSpeak Demo
Okoshi 8:72d49a084297 81 */
Okoshi 8:72d49a084297 82
Okoshi 8:72d49a084297 83 void ThingSpeak_demo(NetworkInterface *net)
mbed_official 0:857719181846 84 {
mbed_official 0:857719181846 85 TCPSocket socket;
Okoshi 8:72d49a084297 86 socket.open(net); // ソケットオープン
mbed_official 0:857719181846 87
Okoshi 8:72d49a084297 88 socket.connect("api.thingspeak.com", 80);
Okoshi 8:72d49a084297 89 char urlBuffer[128];
mbed_official 0:857719181846 90
Okoshi 8:72d49a084297 91 // 温度情報取得
Okoshi 8:72d49a084297 92 float temperature = sensor.read();
Okoshi 8:72d49a084297 93
Okoshi 8:72d49a084297 94 // メッセージ用テキストを生成
Okoshi 8:72d49a084297 95 urlBuffer[0]=0;
Okoshi 8:72d49a084297 96 sprintf(urlBuffer, "GET /update?api_key=%s&field1=%f\r\n\r\n", MBED_CONF_APP_THINGSPEAK_KEY, temperature);
mbed_official 0:857719181846 97
Okoshi 8:72d49a084297 98 // ThingSpeed にメッセージを送信
Okoshi 8:72d49a084297 99 int BuffreLen = strlen(urlBuffer);
Okoshi 8:72d49a084297 100 int scount = socket.send(urlBuffer, BuffreLen+1);
Okoshi 8:72d49a084297 101 printf ("strlen=%d,urlBuffer=%.*s",BuffreLen,BuffreLen-2,urlBuffer);
mbed_official 0:857719181846 102
Okoshi 8:72d49a084297 103 // for debug // デバグ時に受信内容を確認する。
Okoshi 8:72d49a084297 104 // char rbuffer[64];
Okoshi 8:72d49a084297 105 // int rcount = socket.recv(rbuffer, sizeof rbuffer);
Okoshi 8:72d49a084297 106 // printf("recv %d [%.*s]\r\n", rcount, strstr(rbuffer, "\r\n")-rbuffer, rbuffer);
mbed_official 0:857719181846 107
Okoshi 8:72d49a084297 108 socket.close(); //ソケットクローズ
mbed_official 0:857719181846 109 }
mbed_official 0:857719181846 110
mbed_official 0:857719181846 111 int main()
mbed_official 0:857719181846 112 {
mbed_official 0:857719181846 113 printf("WiFi example\r\n\r\n");
mbed_official 0:857719181846 114
mbed_official 0:857719181846 115 scan_demo(&wifi);
mbed_official 0:857719181846 116
mbed_official 0:857719181846 117 printf("\r\nConnecting...\r\n");
mbed_official 0:857719181846 118 int ret = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
mbed_official 0:857719181846 119 if (ret != 0) {
mbed_official 0:857719181846 120 printf("\r\nConnection error\r\n");
mbed_official 0:857719181846 121 return -1;
mbed_official 0:857719181846 122 }
mbed_official 0:857719181846 123
mbed_official 0:857719181846 124 printf("Success\r\n\r\n");
mbed_official 0:857719181846 125 printf("MAC: %s\r\n", wifi.get_mac_address());
mbed_official 0:857719181846 126 printf("IP: %s\r\n", wifi.get_ip_address());
mbed_official 0:857719181846 127 printf("Netmask: %s\r\n", wifi.get_netmask());
mbed_official 0:857719181846 128 printf("Gateway: %s\r\n", wifi.get_gateway());
mbed_official 0:857719181846 129 printf("RSSI: %d\r\n\r\n", wifi.get_rssi());
mbed_official 0:857719181846 130
Okoshi 8:72d49a084297 131 printf("ThingSpeak Demo\r\n");
Okoshi 8:72d49a084297 132
Okoshi 8:72d49a084297 133 // 60秒に1回メッセージを送信する。
Okoshi 8:72d49a084297 134 for (int i=0 ; i<1000 ; i++) {
Okoshi 8:72d49a084297 135 led1 = !led1;
Okoshi 8:72d49a084297 136 printf ("%d:",i);
Okoshi 8:72d49a084297 137 ThingSpeak_demo(&wifi); //ThingSpeed に
Okoshi 8:72d49a084297 138 Thread::wait(60000); //60秒に一回送信
Okoshi 8:72d49a084297 139 }
mbed_official 0:857719181846 140
mbed_official 0:857719181846 141 wifi.disconnect();
mbed_official 0:857719181846 142
Okoshi 8:72d49a084297 143 printf ("Done\r\n");
mbed_official 0:857719181846 144 }