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

Dependencies:   Milkcocoa mbed

Committer:
jksoft
Date:
Tue Dec 15 10:04:22 2015 +0000
Revision:
0:e86fd348cd96
Child:
1:e2ca99ac317b
??

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jksoft 0:e86fd348cd96 1 #include "mbed.h"
jksoft 0:e86fd348cd96 2 #include "MQTTESP8266.h"
jksoft 0:e86fd348cd96 3 #include "MQTTClient.h"
jksoft 0:e86fd348cd96 4 #include "SoftSerialSendOnry.h"
jksoft 0:e86fd348cd96 5 #include "Milkcocoa.h"
jksoft 0:e86fd348cd96 6
jksoft 0:e86fd348cd96 7 // The default setting is for the Simple IoT Board(mbed LPC1114FN28)
jksoft 0:e86fd348cd96 8 // Please change to fit the platform
jksoft 0:e86fd348cd96 9 SoftSerialSendOnry pc(dp10); // tx
jksoft 0:e86fd348cd96 10 DigitalOut myled(dp18);
jksoft 0:e86fd348cd96 11
jksoft 0:e86fd348cd96 12 /************************* WiFi Access Point *********************************/
jksoft 0:e86fd348cd96 13
jksoft 0:e86fd348cd96 14 #define WLAN_SSID "...SSID..."
jksoft 0:e86fd348cd96 15 #define WLAN_PASS "...PASS..."
jksoft 0:e86fd348cd96 16
jksoft 0:e86fd348cd96 17 /************************* Your Milkcocoa Setup *********************************/
jksoft 0:e86fd348cd96 18
jksoft 0:e86fd348cd96 19 #define MILKCOCOA_APP_ID "...YOUR_MILKCOCOA_APP_ID..."
jksoft 0:e86fd348cd96 20 #define MILKCOCOA_DATASTORE "esp8266"
jksoft 0:e86fd348cd96 21
jksoft 0:e86fd348cd96 22 /************* Milkcocoa Setup (you don't need to change this!) ******************/
jksoft 0:e86fd348cd96 23
jksoft 0:e86fd348cd96 24 #define MILKCOCOA_SERVERPORT 1883
jksoft 0:e86fd348cd96 25
jksoft 0:e86fd348cd96 26 /************ Global State (you don't need to change this!) ******************/
jksoft 0:e86fd348cd96 27
jksoft 0:e86fd348cd96 28 // Create an ESP8266 WiFiClient class to connect to the MQTT server.
jksoft 0:e86fd348cd96 29 // The default setting is for the Simple IoT Board(mbed LPC1114FN28)
jksoft 0:e86fd348cd96 30 // Please change to fit the platform
jksoft 0:e86fd348cd96 31 MQTTESP8266 ipstack(dp16,dp15,dp26,WLAN_SSID,WLAN_PASS); // TX,RX,Reset,SSID,Password,Baud
jksoft 0:e86fd348cd96 32
jksoft 0:e86fd348cd96 33 const char MQTT_SERVER[] = MILKCOCOA_APP_ID ".mlkcca.com";
jksoft 0:e86fd348cd96 34 const char MQTT_CLIENTID[] = __TIME__ MILKCOCOA_APP_ID;
jksoft 0:e86fd348cd96 35
jksoft 0:e86fd348cd96 36 Milkcocoa milkcocoa = Milkcocoa(&ipstack, MQTT_SERVER, MILKCOCOA_SERVERPORT, MILKCOCOA_APP_ID, MQTT_CLIENTID);
jksoft 0:e86fd348cd96 37
jksoft 0:e86fd348cd96 38 extern void onpush(MQTT::MessageData& md);
jksoft 0:e86fd348cd96 39
jksoft 0:e86fd348cd96 40 int main() {
jksoft 0:e86fd348cd96 41 // void setup() {
jksoft 0:e86fd348cd96 42 pc.baud(9600);
jksoft 0:e86fd348cd96 43 pc.printf("Milkcocoa mbed ver demo\n\r\n\r\n\r");
jksoft 0:e86fd348cd96 44 pc.printf("Connecting to %s\n\r",WLAN_SSID);
jksoft 0:e86fd348cd96 45
jksoft 0:e86fd348cd96 46 milkcocoa.connect();
jksoft 0:e86fd348cd96 47 pc.printf("\n\rWiFi connected\n\r");
jksoft 0:e86fd348cd96 48 //pc.printf("IP address: %s\n\r",ipstack.getInterface().getIPAddress());
jksoft 0:e86fd348cd96 49
jksoft 0:e86fd348cd96 50 pc.printf("%d\n\r",milkcocoa.on(MILKCOCOA_DATASTORE, "push", onpush));
jksoft 0:e86fd348cd96 51
jksoft 0:e86fd348cd96 52 // }
jksoft 0:e86fd348cd96 53 while(1) {
jksoft 0:e86fd348cd96 54 // void loop() {
jksoft 0:e86fd348cd96 55 milkcocoa.loop();
jksoft 0:e86fd348cd96 56
jksoft 0:e86fd348cd96 57 DataElement elem = DataElement();
jksoft 0:e86fd348cd96 58 elem.setValue("v", 1);
jksoft 0:e86fd348cd96 59
jksoft 0:e86fd348cd96 60 milkcocoa.push(MILKCOCOA_DATASTORE, elem);
jksoft 0:e86fd348cd96 61 wait(7.0);
jksoft 0:e86fd348cd96 62 }
jksoft 0:e86fd348cd96 63 }
jksoft 0:e86fd348cd96 64
jksoft 0:e86fd348cd96 65 void onpush(MQTT::MessageData& md)
jksoft 0:e86fd348cd96 66 {
jksoft 0:e86fd348cd96 67 MQTT::Message &message = md.message;
jksoft 0:e86fd348cd96 68 DataElement de = DataElement((char*)message.payload);
jksoft 0:e86fd348cd96 69 pc.printf("onpush\n\r");
jksoft 0:e86fd348cd96 70 pc.printf("%d\n\r",de.getInt("v"));
jksoft 0:e86fd348cd96 71 }