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

Dependencies:   Milkcocoa-os

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "Milkcocoa.h"
00003 #include "EthernetInterface.h"
00004 
00005 EthernetInterface eth;
00006 RawSerial pc(USBTX,USBRX);
00007 
00008 /************************* Your Milkcocoa Setup *********************************/
00009 
00010 #define MILKCOCOA_APP_ID      "...YOUR_MILKCOCOA_APP_ID..."
00011 #define MILKCOCOA_DATASTORE   "mbed_lan"
00012 
00013 /************* Milkcocoa Setup (you don't need to change this!) ******************/
00014 
00015 #define MILKCOCOA_SERVERPORT  1883
00016 
00017 /************ Global State (you don't need to change this!) ******************/
00018 const char MQTT_SERVER[]  = MILKCOCOA_APP_ID ".mlkcca.com";
00019 const char MQTT_CLIENTID[] = __TIME__ MILKCOCOA_APP_ID;
00020 
00021 extern void onpush(MQTT::MessageData& md);
00022 
00023 int main() {
00024     
00025     pc.baud(9600);
00026     pc.printf("Milkcocoa mbed os ver demo\n\r\n\r\n\r");
00027     
00028     int ret = eth.connect();
00029     if (ret != 0) {
00030         printf("\r\nConnection error\r\n");
00031         return -1;
00032     }
00033     pc.printf("\n\rEthernet connected\n\r");
00034     
00035     Milkcocoa* milkcocoa = new Milkcocoa(&eth, MQTT_SERVER, MILKCOCOA_SERVERPORT, MILKCOCOA_APP_ID, MQTT_CLIENTID);
00036     
00037     milkcocoa->connect();
00038     
00039     pc.printf("%d\n\r",milkcocoa->on(MILKCOCOA_DATASTORE, "push", onpush));
00040     
00041 #ifdef __MILKCOCOA_THREAD
00042     milkcocoa->setLoopCycle(5000);
00043     milkcocoa->start();
00044 #endif
00045     while(1) {
00046         DataElement elem = DataElement();
00047         elem.setValue("v", 1);
00048         
00049         milkcocoa->push(MILKCOCOA_DATASTORE, elem);
00050 
00051 #ifndef __MILKCOCOA_THREAD
00052         milkcocoa->loop();
00053 #endif
00054         Thread::wait(7000);
00055         
00056     }
00057 }
00058 
00059 void onpush(MQTT::MessageData& md)
00060 {
00061     MQTT::Message &message = md.message;
00062     DataElement de = DataElement((char*)message.payload);
00063     pc.printf("onpush\n\r");
00064     pc.printf("%d\n\r",de.getInt("v"));
00065 }