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

Dependencies:   Milkcocoa-os

Committer:
jksoft
Date:
Thu Feb 09 07:31:26 2017 +0000
Revision:
0:885b53a42035
Child:
1:1ab9d9f92764
??

Who changed what in which revision?

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