【 IoTを試そう [ODIN-W2(WiFi) + 温度計 + milkcocoa ] 】 ODIN-W2 にmbed ApplicationShiled をつないで、 https://developer.mbed.org/components/mbed-Application-Shield/ 温度のデータをMilkcocoaにアップするプログラムです。

Dependencies:   LM75B Milkcocoa-os

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
00002  * Copyright (c) 2016 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include "mbed.h"
00018 #include "TCPSocket.h"
00019 
00020 #if TARGET_UBLOX_EVK_ODIN_W2
00021 #include "OdinWiFiInterface.h"
00022 OdinWiFiInterface wifi;
00023 
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 
00030 ESP8266Interface wifi(MBED_CONF_APP_WIFI_TX, MBED_CONF_APP_WIFI_RX);
00031 
00032 #endif
00033 
00034 #include "Milkcocoa.h"
00035 /************************* Your Milkcocoa Setup *********************************/
00036 #define MILKCOCOA_APP_ID      CONFIG_MILKCOCOA_APP_ID
00037 #define MILKCOCOA_DATASTORE   CONFIG_MILKCOCOA_DATASTORE
00038 /************* Milkcocoa Setup (you don't need to change this!) ******************/
00039 
00040 #define MILKCOCOA_SERVERPORT  1883
00041 
00042 /************ Global State (you don't need to change this!) ******************/
00043 const char MQTT_SERVER[]  = MILKCOCOA_APP_ID ".mlkcca.com";
00044 const char MQTT_CLIENTID[] = __TIME__ MILKCOCOA_APP_ID;
00045 
00046 extern void onpush(MQTT::MessageData& md);
00047 
00048 RawSerial pc(USBTX,USBRX);
00049 
00050 #include "LM75B.h"              //  温度センサー機能追加
00051 LM75B sensor(D14,D15);
00052 DigitalOut led1(LED1);
00053 
00054 const char *sec2str(nsapi_security_t sec)
00055 {
00056     switch (sec) {
00057         case NSAPI_SECURITY_NONE:
00058             return "None";
00059         case NSAPI_SECURITY_WEP:
00060             return "WEP";
00061         case NSAPI_SECURITY_WPA:
00062             return "WPA";
00063         case NSAPI_SECURITY_WPA2:
00064             return "WPA2";
00065         case NSAPI_SECURITY_WPA_WPA2:
00066             return "WPA/WPA2";
00067         case NSAPI_SECURITY_UNKNOWN:
00068         default:
00069             return "Unknown";
00070     }
00071 }
00072 
00073 void scan_demo(WiFiInterface *wifi)
00074 {
00075     WiFiAccessPoint *ap;
00076 
00077     printf("Scan:\r\n");
00078 
00079     int count = wifi->scan(NULL,0);
00080 
00081     /* Limit number of network arbitrary to 15 */
00082     count = count < 15 ? count : 15;
00083 
00084     ap = new WiFiAccessPoint[count];
00085     count = wifi->scan(ap, count);
00086     for (int i = 0; i < count; i++)
00087     {
00088         printf("Network: %s secured: %s BSSID: %hhX:%hhX:%hhX:%hhx:%hhx:%hhx RSSI: %hhd Ch: %hhd\r\n", ap[i].get_ssid(),
00089                sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2],
00090                ap[i].get_bssid()[3], ap[i].get_bssid()[4], ap[i].get_bssid()[5], ap[i].get_rssi(), ap[i].get_channel());
00091     }
00092     printf("%d networks available.\r\n", count);
00093 
00094     delete[] ap;
00095 }
00096 
00097 int main()
00098 {
00099     pc.baud(MBED_SERIAL_UART_SPEED);
00100     pc.printf("Milkcocoa mbed os ver demo\n\r\n\r\n\r");
00101 
00102     scan_demo(&wifi);
00103 
00104     printf("\r\nConnecting...\r\n");
00105     int ret = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
00106     if (ret != 0) {
00107         printf("\r\nConnection error\r\n");
00108         return -1;
00109     }
00110 
00111     printf("Success\r\n\r\n");
00112     printf("MAC: %s\r\n", wifi.get_mac_address());
00113     printf("IP: %s\r\n", wifi.get_ip_address());
00114     printf("Netmask: %s\r\n", wifi.get_netmask());
00115     printf("Gateway: %s\r\n", wifi.get_gateway());
00116     printf("RSSI: %d\r\n\r\n", wifi.get_rssi());
00117     
00118     Milkcocoa* milkcocoa = new Milkcocoa(&wifi, MQTT_SERVER, MILKCOCOA_SERVERPORT, MILKCOCOA_APP_ID, MQTT_CLIENTID);
00119     
00120     milkcocoa->connect();
00121     
00122     printf("%d\n\r",milkcocoa->on(MILKCOCOA_DATASTORE, "push", onpush));
00123     
00124     milkcocoa->setLoopCycle(5000);
00125     milkcocoa->start();
00126 
00127     for (int i=0 ; i<1000 ; i++) {
00128         float temperature = sensor.read();  // 温度情報取得
00129         DataElement elem = DataElement();
00130         elem.setValue("temp", temperature);
00131         milkcocoa->push(MILKCOCOA_DATASTORE, elem);
00132         Thread::wait(60000);    // 60秒に一回1000個アップする。
00133     }
00134 
00135     wifi.disconnect();
00136 
00137     printf("\r\nDone\r\n");
00138 }
00139 
00140 void onpush(MQTT::MessageData& md)
00141 {
00142     MQTT::Message &message = md.message;
00143     DataElement de = DataElement((char*)message.payload);
00144     pc.printf("onpush\n\r");
00145     pc.printf("%d\n\r",de.getInt("temp"));
00146 }
00147