【 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

Files at this revision

API Documentation at this revision

Comitter:
Okoshi
Date:
Tue Apr 18 07:22:04 2017 +0000
Parent:
16:b4edf1f8ab70
Commit message:
? IoT???? [ODIN-W2(WiFi) + ??? + milkcocoa ] ?; ODIN-W2 ?mbed ApplicationShiled ??????; ???????Milkcocoa??????????????;

Changed in this revision

LM75B.lib Show annotated file Show diff for this revision Revisions of this file
Milkcocoa-os.lib Show annotated file Show diff for this revision Revisions of this file
README.md Show annotated file Show diff for this revision Revisions of this file
esp8266-driver.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed_app.json Show annotated file Show diff for this revision Revisions of this file
diff -r b4edf1f8ab70 -r 79fa29aa6724 LM75B.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LM75B.lib	Tue Apr 18 07:22:04 2017 +0000
@@ -0,0 +1,1 @@
+https://mbed.org/users/chris/code/LM75B/#6a70c9303bbe
diff -r b4edf1f8ab70 -r 79fa29aa6724 Milkcocoa-os.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Milkcocoa-os.lib	Tue Apr 18 07:22:04 2017 +0000
@@ -0,0 +1,1 @@
+https://mbed.org/users/jksoft/code/Milkcocoa-os/#8e4149b53a8a
diff -r b4edf1f8ab70 -r 79fa29aa6724 README.md
--- a/README.md	Mon Apr 10 16:30:03 2017 +0100
+++ b/README.md	Tue Apr 18 07:22:04 2017 +0000
@@ -1,4 +1,4 @@
-# mbed-os-example-wifi #
+# mbed-os-example-wifi with Milkcocoa #
 
 WiFi example for mbed OS
 
@@ -17,6 +17,9 @@
 
 ESP2866 is a fallback option and will be used if the build is for unsupported platform.
 
+with 
+* [mbed Application Shield](https://developer.mbed.org/components/mbed-Application-Shield/)
+
 #### Connecting the ESP2866 ####
 
 ESP module needs to be connected to RX and TX UART pins (+ power and ground) on your target board. That can be achieved using Grove shield or connected directly using jumper wires, please note that not all Arduino form factor boards have UART compatible with the Grove shiled.
@@ -36,18 +39,32 @@
 2. Configure the WiFi credentials
 
   Edit ```mbed_app.json``` to include correct SSID and Password:
+  and MilkCocoa item.
 
   ```
-      "config": {
-          "wifi-ssid": {
-              "help": "WiFi SSID",
-              "value": "\"SSID\""
-          },
-          "wifi-password": {
-              "help": "WiFi Password",
-              "value": "\"Password\""
-          }
-      },
+        "milkcocoa_app_id": {
+            "help": "MilkCocoa Application ID",
+            "value": "\"xxxxxxxxxxxx\"",
+            "macro_name": "CONFIG_MILKCOCOA_APP_ID"
+        },        
+        "milkcocoa_datastore": {
+            "help": "Milkcocoa Datastore",
+            "value": "\"xxxxxxxxx\"",
+            "macro_name": "CONFIG_MILKCOCOA_DATASTORE"
+        },   
+        "serial_console_speed": {
+            "help": "Baud rate of the serial console",
+            "value": 115200,
+            "macro_name": "MBED_SERIAL_UART_SPEED"
+        },
+        "wifi-ssid": {
+            "help": "WiFi SSID",
+            "value": "\"xxxxxxxxxxxxx\""
+        },
+        "wifi-password": {
+            "help": "WiFi Password",
+            "value": "\"xxxxxxxxxxx\""
+        },
   ```
 
 3. Compile and generate binary
diff -r b4edf1f8ab70 -r 79fa29aa6724 esp8266-driver.lib
--- a/esp8266-driver.lib	Mon Apr 10 16:30:03 2017 +0100
+++ b/esp8266-driver.lib	Tue Apr 18 07:22:04 2017 +0000
@@ -1,1 +1,1 @@
-https://github.com/armmbed/esp8266-driver/#dc37b65ca877d969aa492f348626df6e1b0b1df0
+https://github.com/armmbed/esp8266-driver/#abd45947366baae35035dc7850e9541be05ba05d
diff -r b4edf1f8ab70 -r 79fa29aa6724 main.cpp
--- a/main.cpp	Mon Apr 10 16:30:03 2017 +0100
+++ b/main.cpp	Tue Apr 18 07:22:04 2017 +0000
@@ -31,6 +31,26 @@
 
 #endif
 
+#include "Milkcocoa.h"
+/************************* Your Milkcocoa Setup *********************************/
+#define MILKCOCOA_APP_ID      CONFIG_MILKCOCOA_APP_ID
+#define MILKCOCOA_DATASTORE   CONFIG_MILKCOCOA_DATASTORE
+/************* Milkcocoa Setup (you don't need to change this!) ******************/
+
+#define MILKCOCOA_SERVERPORT  1883
+
+/************ Global State (you don't need to change this!) ******************/
+const char MQTT_SERVER[]  = MILKCOCOA_APP_ID ".mlkcca.com";
+const char MQTT_CLIENTID[] = __TIME__ MILKCOCOA_APP_ID;
+
+extern void onpush(MQTT::MessageData& md);
+
+RawSerial pc(USBTX,USBRX);
+
+#include "LM75B.h"              //  温度センサー機能追加
+LM75B sensor(D14,D15);
+DigitalOut led1(LED1);
+
 const char *sec2str(nsapi_security_t sec)
 {
     switch (sec) {
@@ -74,33 +94,10 @@
     delete[] ap;
 }
 
-void http_demo(NetworkInterface *net)
-{
-    TCPSocket socket;
-
-    printf("Sending HTTP request to www.arm.com...\r\n");
-
-    // Open a socket on the network interface, and create a TCP connection to www.arm.com
-    socket.open(net);
-    socket.connect("www.arm.com", 80);
-
-    // Send a simple http request
-    char sbuffer[] = "GET / HTTP/1.1\r\nHost: www.arm.com\r\n\r\n";
-    int scount = socket.send(sbuffer, sizeof sbuffer);
-    printf("sent %d [%.*s]\r\n", scount, strstr(sbuffer, "\r\n")-sbuffer, sbuffer);
-
-    // Recieve a simple http response and print out the response line
-    char rbuffer[64];
-    int rcount = socket.recv(rbuffer, sizeof rbuffer);
-    printf("recv %d [%.*s]\r\n", rcount, strstr(rbuffer, "\r\n")-rbuffer, rbuffer);
-
-    // Close the socket to return its memory and bring down the network interface
-    socket.close();
-}
-
 int main()
 {
-    printf("WiFi example\r\n\r\n");
+    pc.baud(MBED_SERIAL_UART_SPEED);
+    pc.printf("Milkcocoa mbed os ver demo\n\r\n\r\n\r");
 
     scan_demo(&wifi);
 
@@ -117,10 +114,34 @@
     printf("Netmask: %s\r\n", wifi.get_netmask());
     printf("Gateway: %s\r\n", wifi.get_gateway());
     printf("RSSI: %d\r\n\r\n", wifi.get_rssi());
+    
+    Milkcocoa* milkcocoa = new Milkcocoa(&wifi, MQTT_SERVER, MILKCOCOA_SERVERPORT, MILKCOCOA_APP_ID, MQTT_CLIENTID);
+    
+    milkcocoa->connect();
+    
+    printf("%d\n\r",milkcocoa->on(MILKCOCOA_DATASTORE, "push", onpush));
+    
+    milkcocoa->setLoopCycle(5000);
+    milkcocoa->start();
 
-    http_demo(&wifi);
+    for (int i=0 ; i<1000 ; i++) {
+        float temperature = sensor.read();  // 温度情報取得
+        DataElement elem = DataElement();
+        elem.setValue("temp", temperature);
+        milkcocoa->push(MILKCOCOA_DATASTORE, elem);
+        Thread::wait(60000);    // 60秒に一回1000個アップする。
+    }
 
     wifi.disconnect();
 
     printf("\r\nDone\r\n");
 }
+
+void onpush(MQTT::MessageData& md)
+{
+    MQTT::Message &message = md.message;
+    DataElement de = DataElement((char*)message.payload);
+    pc.printf("onpush\n\r");
+    pc.printf("%d\n\r",de.getInt("temp"));
+}
+
diff -r b4edf1f8ab70 -r 79fa29aa6724 mbed_app.json
--- a/mbed_app.json	Mon Apr 10 16:30:03 2017 +0100
+++ b/mbed_app.json	Tue Apr 18 07:22:04 2017 +0000
@@ -1,12 +1,27 @@
 {
     "config": {
+        "milkcocoa_app_id": {
+            "help": "MilkCocoa Application ID",
+            "value": "\"xxxxxxxxxxxx\"",
+            "macro_name": "CONFIG_MILKCOCOA_APP_ID"
+        },        
+        "milkcocoa_datastore": {
+            "help": "Milkcocoa Datastore",
+            "value": "\"xxxxxxxxx\"",
+            "macro_name": "CONFIG_MILKCOCOA_DATASTORE"
+        },   
+        "serial_console_speed": {
+            "help": "Baud rate of the serial console",
+            "value": 115200,
+            "macro_name": "MBED_SERIAL_UART_SPEED"
+        },
         "wifi-ssid": {
             "help": "WiFi SSID",
-            "value": "\"SSID\""
+            "value": "\"xxxxxxxxxxxxx\""
         },
         "wifi-password": {
             "help": "WiFi Password",
-            "value": "\"PASSWORD\""
+            "value": "\"xxxxxxxxxxx\""
         },
         "wifi-tx": {
             "help": "TX pin for serial connection to external device",