【mbed OS5対応バージョン】データの保存、更新、取得ができるWebサービス「milkcocoa」に接続し、データのプッシュ、送信、取得ができるライブラリを使ったサンプルです。 ESP8266版 https://mlkcca.com/

Dependencies:   Milkcocoa-os esp8266-driver

Files at this revision

API Documentation at this revision

Comitter:
jksoft
Date:
Fri Feb 10 01:30:14 2017 +0000
Child:
1:fa058d6fb4f8
Commit message:
the first

Changed in this revision

Milkcocoa-os.lib 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-os.lib Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Milkcocoa-os.lib	Fri Feb 10 01:30:14 2017 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/jksoft/code/Milkcocoa-os/#0a2f634d3324
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/esp8266-driver.lib	Fri Feb 10 01:30:14 2017 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/teams/ESP8266/code/esp8266-driver/#a45ebbd86564
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Fri Feb 10 01:30:14 2017 +0000
@@ -0,0 +1,71 @@
+#include "mbed.h"
+#include "MQTTInterface.h"
+#include "MQTTClient.h"
+#include "Milkcocoa.h"
+#include "MClient.h"
+#include "EthernetInterface.h"
+
+#include "ESP8266Interface.h"
+ESP8266Interface wifi(D1,D0);
+RawSerial pc(USBTX,USBRX);
+
+/************************* WiFi Access Point *********************************/
+
+#define WLAN_SSID       "...SSID..."
+#define WLAN_PASS       "...PASS..."
+
+/************************* Your Milkcocoa Setup *********************************/
+
+#define MILKCOCOA_APP_ID      "...YOUR_MILKCOCOA_APP_ID..."
+#define MILKCOCOA_DATASTORE   "mbed_esp8266"
+
+/************* 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);
+
+int main() {
+    
+    pc.baud(9600);
+    pc.printf("Milkcocoa mbed os ver demo\n\r\n\r\n\r");
+    pc.printf("Connecting to %s\n\r",WLAN_SSID);
+    
+    int ret = wifi.connect(WLAN_SSID, WLAN_PASS, NSAPI_SECURITY_WPA_WPA2);
+    if (ret != 0) {
+        printf("\r\nConnection error\r\n");
+        return -1;
+    }
+    pc.printf("\n\rWiFi connected\n\r");
+    
+    Milkcocoa* milkcocoa = new Milkcocoa(&wifi, MQTT_SERVER, MILKCOCOA_SERVERPORT, MILKCOCOA_APP_ID, MQTT_CLIENTID);
+    
+    milkcocoa->connect();
+    
+    pc.printf("%d\n\r",milkcocoa->on(MILKCOCOA_DATASTORE, "push", onpush));
+    
+    milkcocoa->setLoopCycle(5000);
+    milkcocoa->start();
+
+    while(1) {
+        DataElement elem = DataElement();
+        elem.setValue("v", 1);
+        
+        milkcocoa->push(MILKCOCOA_DATASTORE, elem);
+
+        Thread::wait(7000);
+        
+    }
+}
+
+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("v"));
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-os.lib	Fri Feb 10 01:30:14 2017 +0000
@@ -0,0 +1,1 @@
+https://github.com/ARMmbed/mbed-os/#34c1facf42a174f47fdf9002cd8c6bf10ac41744