【 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

Committer:
Okoshi
Date:
Tue Apr 18 07:22:04 2017 +0000
Revision:
17:79fa29aa6724
Parent:
10:5b5beb106156
? IoT???? [ODIN-W2(WiFi) + ??? + milkcocoa ] ?; ODIN-W2 ?mbed ApplicationShiled ??????; ???????Milkcocoa??????????????;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 0:857719181846 1 /* WiFi Example
mbed_official 0:857719181846 2 * Copyright (c) 2016 ARM Limited
mbed_official 0:857719181846 3 *
mbed_official 0:857719181846 4 * Licensed under the Apache License, Version 2.0 (the "License");
mbed_official 0:857719181846 5 * you may not use this file except in compliance with the License.
mbed_official 0:857719181846 6 * You may obtain a copy of the License at
mbed_official 0:857719181846 7 *
mbed_official 0:857719181846 8 * http://www.apache.org/licenses/LICENSE-2.0
mbed_official 0:857719181846 9 *
mbed_official 0:857719181846 10 * Unless required by applicable law or agreed to in writing, software
mbed_official 0:857719181846 11 * distributed under the License is distributed on an "AS IS" BASIS,
mbed_official 0:857719181846 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mbed_official 0:857719181846 13 * See the License for the specific language governing permissions and
mbed_official 0:857719181846 14 * limitations under the License.
mbed_official 0:857719181846 15 */
mbed_official 0:857719181846 16
mbed_official 0:857719181846 17 #include "mbed.h"
mbed_official 0:857719181846 18 #include "TCPSocket.h"
mbed_official 0:857719181846 19
mbed_official 0:857719181846 20 #if TARGET_UBLOX_EVK_ODIN_W2
mbed_official 1:aea78e21a7da 21 #include "OdinWiFiInterface.h"
mbed_official 1:aea78e21a7da 22 OdinWiFiInterface wifi;
mbed_official 10:5b5beb106156 23
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 10:5b5beb106156 29
mbed_official 10:5b5beb106156 30 ESP8266Interface wifi(MBED_CONF_APP_WIFI_TX, MBED_CONF_APP_WIFI_RX);
mbed_official 10:5b5beb106156 31
mbed_official 0:857719181846 32 #endif
mbed_official 0:857719181846 33
Okoshi 17:79fa29aa6724 34 #include "Milkcocoa.h"
Okoshi 17:79fa29aa6724 35 /************************* Your Milkcocoa Setup *********************************/
Okoshi 17:79fa29aa6724 36 #define MILKCOCOA_APP_ID CONFIG_MILKCOCOA_APP_ID
Okoshi 17:79fa29aa6724 37 #define MILKCOCOA_DATASTORE CONFIG_MILKCOCOA_DATASTORE
Okoshi 17:79fa29aa6724 38 /************* Milkcocoa Setup (you don't need to change this!) ******************/
Okoshi 17:79fa29aa6724 39
Okoshi 17:79fa29aa6724 40 #define MILKCOCOA_SERVERPORT 1883
Okoshi 17:79fa29aa6724 41
Okoshi 17:79fa29aa6724 42 /************ Global State (you don't need to change this!) ******************/
Okoshi 17:79fa29aa6724 43 const char MQTT_SERVER[] = MILKCOCOA_APP_ID ".mlkcca.com";
Okoshi 17:79fa29aa6724 44 const char MQTT_CLIENTID[] = __TIME__ MILKCOCOA_APP_ID;
Okoshi 17:79fa29aa6724 45
Okoshi 17:79fa29aa6724 46 extern void onpush(MQTT::MessageData& md);
Okoshi 17:79fa29aa6724 47
Okoshi 17:79fa29aa6724 48 RawSerial pc(USBTX,USBRX);
Okoshi 17:79fa29aa6724 49
Okoshi 17:79fa29aa6724 50 #include "LM75B.h" // 温度センサー機能追加
Okoshi 17:79fa29aa6724 51 LM75B sensor(D14,D15);
Okoshi 17:79fa29aa6724 52 DigitalOut led1(LED1);
Okoshi 17:79fa29aa6724 53
mbed_official 0:857719181846 54 const char *sec2str(nsapi_security_t sec)
mbed_official 0:857719181846 55 {
mbed_official 0:857719181846 56 switch (sec) {
mbed_official 0:857719181846 57 case NSAPI_SECURITY_NONE:
mbed_official 0:857719181846 58 return "None";
mbed_official 0:857719181846 59 case NSAPI_SECURITY_WEP:
mbed_official 0:857719181846 60 return "WEP";
mbed_official 0:857719181846 61 case NSAPI_SECURITY_WPA:
mbed_official 0:857719181846 62 return "WPA";
mbed_official 0:857719181846 63 case NSAPI_SECURITY_WPA2:
mbed_official 0:857719181846 64 return "WPA2";
mbed_official 0:857719181846 65 case NSAPI_SECURITY_WPA_WPA2:
mbed_official 0:857719181846 66 return "WPA/WPA2";
mbed_official 0:857719181846 67 case NSAPI_SECURITY_UNKNOWN:
mbed_official 0:857719181846 68 default:
mbed_official 0:857719181846 69 return "Unknown";
mbed_official 0:857719181846 70 }
mbed_official 0:857719181846 71 }
mbed_official 0:857719181846 72
mbed_official 0:857719181846 73 void scan_demo(WiFiInterface *wifi)
mbed_official 0:857719181846 74 {
mbed_official 0:857719181846 75 WiFiAccessPoint *ap;
mbed_official 0:857719181846 76
mbed_official 0:857719181846 77 printf("Scan:\r\n");
mbed_official 0:857719181846 78
mbed_official 0:857719181846 79 int count = wifi->scan(NULL,0);
mbed_official 0:857719181846 80
mbed_official 0:857719181846 81 /* Limit number of network arbitrary to 15 */
mbed_official 0:857719181846 82 count = count < 15 ? count : 15;
mbed_official 0:857719181846 83
mbed_official 0:857719181846 84 ap = new WiFiAccessPoint[count];
mbed_official 0:857719181846 85 count = wifi->scan(ap, count);
mbed_official 0:857719181846 86 for (int i = 0; i < count; i++)
mbed_official 0:857719181846 87 {
mbed_official 0:857719181846 88 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 89 sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2],
mbed_official 0:857719181846 90 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 91 }
mbed_official 0:857719181846 92 printf("%d networks available.\r\n", count);
mbed_official 0:857719181846 93
mbed_official 0:857719181846 94 delete[] ap;
mbed_official 0:857719181846 95 }
mbed_official 0:857719181846 96
mbed_official 0:857719181846 97 int main()
mbed_official 0:857719181846 98 {
Okoshi 17:79fa29aa6724 99 pc.baud(MBED_SERIAL_UART_SPEED);
Okoshi 17:79fa29aa6724 100 pc.printf("Milkcocoa mbed os ver demo\n\r\n\r\n\r");
mbed_official 0:857719181846 101
mbed_official 0:857719181846 102 scan_demo(&wifi);
mbed_official 0:857719181846 103
mbed_official 0:857719181846 104 printf("\r\nConnecting...\r\n");
mbed_official 0:857719181846 105 int ret = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
mbed_official 0:857719181846 106 if (ret != 0) {
mbed_official 0:857719181846 107 printf("\r\nConnection error\r\n");
mbed_official 0:857719181846 108 return -1;
mbed_official 0:857719181846 109 }
mbed_official 0:857719181846 110
mbed_official 0:857719181846 111 printf("Success\r\n\r\n");
mbed_official 0:857719181846 112 printf("MAC: %s\r\n", wifi.get_mac_address());
mbed_official 0:857719181846 113 printf("IP: %s\r\n", wifi.get_ip_address());
mbed_official 0:857719181846 114 printf("Netmask: %s\r\n", wifi.get_netmask());
mbed_official 0:857719181846 115 printf("Gateway: %s\r\n", wifi.get_gateway());
mbed_official 0:857719181846 116 printf("RSSI: %d\r\n\r\n", wifi.get_rssi());
Okoshi 17:79fa29aa6724 117
Okoshi 17:79fa29aa6724 118 Milkcocoa* milkcocoa = new Milkcocoa(&wifi, MQTT_SERVER, MILKCOCOA_SERVERPORT, MILKCOCOA_APP_ID, MQTT_CLIENTID);
Okoshi 17:79fa29aa6724 119
Okoshi 17:79fa29aa6724 120 milkcocoa->connect();
Okoshi 17:79fa29aa6724 121
Okoshi 17:79fa29aa6724 122 printf("%d\n\r",milkcocoa->on(MILKCOCOA_DATASTORE, "push", onpush));
Okoshi 17:79fa29aa6724 123
Okoshi 17:79fa29aa6724 124 milkcocoa->setLoopCycle(5000);
Okoshi 17:79fa29aa6724 125 milkcocoa->start();
mbed_official 0:857719181846 126
Okoshi 17:79fa29aa6724 127 for (int i=0 ; i<1000 ; i++) {
Okoshi 17:79fa29aa6724 128 float temperature = sensor.read(); // 温度情報取得
Okoshi 17:79fa29aa6724 129 DataElement elem = DataElement();
Okoshi 17:79fa29aa6724 130 elem.setValue("temp", temperature);
Okoshi 17:79fa29aa6724 131 milkcocoa->push(MILKCOCOA_DATASTORE, elem);
Okoshi 17:79fa29aa6724 132 Thread::wait(60000); // 60秒に一回1000個アップする。
Okoshi 17:79fa29aa6724 133 }
mbed_official 0:857719181846 134
mbed_official 0:857719181846 135 wifi.disconnect();
mbed_official 0:857719181846 136
mbed_official 0:857719181846 137 printf("\r\nDone\r\n");
mbed_official 0:857719181846 138 }
Okoshi 17:79fa29aa6724 139
Okoshi 17:79fa29aa6724 140 void onpush(MQTT::MessageData& md)
Okoshi 17:79fa29aa6724 141 {
Okoshi 17:79fa29aa6724 142 MQTT::Message &message = md.message;
Okoshi 17:79fa29aa6724 143 DataElement de = DataElement((char*)message.payload);
Okoshi 17:79fa29aa6724 144 pc.printf("onpush\n\r");
Okoshi 17:79fa29aa6724 145 pc.printf("%d\n\r",de.getInt("temp"));
Okoshi 17:79fa29aa6724 146 }
Okoshi 17:79fa29aa6724 147