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

Dependencies:   Milkcocoa-os

Committer:
jksoft
Date:
Thu Jun 01 00:25:37 2017 +0000
Revision:
5:103da32c92c7
Parent:
2:727e3e0fcb81
????????????????

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jksoft 0:885b53a42035 1 #include "mbed.h"
jksoft 0:885b53a42035 2 #include "Milkcocoa.h"
jksoft 0:885b53a42035 3 #include "EthernetInterface.h"
jksoft 0:885b53a42035 4
jksoft 0:885b53a42035 5 EthernetInterface eth;
jksoft 0:885b53a42035 6 RawSerial pc(USBTX,USBRX);
jksoft 0:885b53a42035 7
jksoft 0:885b53a42035 8 /************************* Your Milkcocoa Setup *********************************/
jksoft 0:885b53a42035 9
jksoft 1:1ab9d9f92764 10 #define MILKCOCOA_APP_ID "...YOUR_MILKCOCOA_APP_ID..."
jksoft 0:885b53a42035 11 #define MILKCOCOA_DATASTORE "mbed_lan"
jksoft 0:885b53a42035 12
jksoft 0:885b53a42035 13 /************* Milkcocoa Setup (you don't need to change this!) ******************/
jksoft 0:885b53a42035 14
jksoft 0:885b53a42035 15 #define MILKCOCOA_SERVERPORT 1883
jksoft 0:885b53a42035 16
jksoft 0:885b53a42035 17 /************ Global State (you don't need to change this!) ******************/
jksoft 0:885b53a42035 18 const char MQTT_SERVER[] = MILKCOCOA_APP_ID ".mlkcca.com";
jksoft 0:885b53a42035 19 const char MQTT_CLIENTID[] = __TIME__ MILKCOCOA_APP_ID;
jksoft 0:885b53a42035 20
jksoft 0:885b53a42035 21 extern void onpush(MQTT::MessageData& md);
jksoft 0:885b53a42035 22
jksoft 0:885b53a42035 23 int main() {
jksoft 0:885b53a42035 24
jksoft 0:885b53a42035 25 pc.baud(9600);
jksoft 0:885b53a42035 26 pc.printf("Milkcocoa mbed os ver demo\n\r\n\r\n\r");
jksoft 0:885b53a42035 27
jksoft 0:885b53a42035 28 int ret = eth.connect();
jksoft 0:885b53a42035 29 if (ret != 0) {
jksoft 0:885b53a42035 30 printf("\r\nConnection error\r\n");
jksoft 0:885b53a42035 31 return -1;
jksoft 0:885b53a42035 32 }
jksoft 0:885b53a42035 33 pc.printf("\n\rEthernet connected\n\r");
jksoft 0:885b53a42035 34
jksoft 0:885b53a42035 35 Milkcocoa* milkcocoa = new Milkcocoa(&eth, MQTT_SERVER, MILKCOCOA_SERVERPORT, MILKCOCOA_APP_ID, MQTT_CLIENTID);
jksoft 0:885b53a42035 36
jksoft 0:885b53a42035 37 milkcocoa->connect();
jksoft 0:885b53a42035 38
jksoft 0:885b53a42035 39 pc.printf("%d\n\r",milkcocoa->on(MILKCOCOA_DATASTORE, "push", onpush));
jksoft 0:885b53a42035 40
jksoft 5:103da32c92c7 41 #ifdef __MILKCOCOA_THREAD
jksoft 0:885b53a42035 42 milkcocoa->setLoopCycle(5000);
jksoft 0:885b53a42035 43 milkcocoa->start();
jksoft 5:103da32c92c7 44 #endif
jksoft 0:885b53a42035 45 while(1) {
jksoft 0:885b53a42035 46 DataElement elem = DataElement();
jksoft 0:885b53a42035 47 elem.setValue("v", 1);
jksoft 0:885b53a42035 48
jksoft 0:885b53a42035 49 milkcocoa->push(MILKCOCOA_DATASTORE, elem);
jksoft 0:885b53a42035 50
jksoft 5:103da32c92c7 51 #ifndef __MILKCOCOA_THREAD
jksoft 5:103da32c92c7 52 milkcocoa->loop();
jksoft 5:103da32c92c7 53 #endif
jksoft 0:885b53a42035 54 Thread::wait(7000);
jksoft 0:885b53a42035 55
jksoft 0:885b53a42035 56 }
jksoft 0:885b53a42035 57 }
jksoft 0:885b53a42035 58
jksoft 0:885b53a42035 59 void onpush(MQTT::MessageData& md)
jksoft 0:885b53a42035 60 {
jksoft 0:885b53a42035 61 MQTT::Message &message = md.message;
jksoft 0:885b53a42035 62 DataElement de = DataElement((char*)message.payload);
jksoft 0:885b53a42035 63 pc.printf("onpush\n\r");
jksoft 0:885b53a42035 64 pc.printf("%d\n\r",de.getInt("v"));
jksoft 0:885b53a42035 65 }