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

Dependencies:   Milkcocoa-os

main.cpp

Committer:
jksoft
Date:
2017-09-06
Revision:
7:a0ba4c04a63c
Parent:
5:103da32c92c7

File content as of revision 7:a0ba4c04a63c:

#include "mbed.h"
#include "Milkcocoa.h"
#include "EthernetInterface.h"

EthernetInterface eth;
RawSerial pc(USBTX,USBRX);

/************************* Your Milkcocoa Setup *********************************/

#define MILKCOCOA_APP_ID      "...YOUR_MILKCOCOA_APP_ID..."
#define MILKCOCOA_DATASTORE   "mbed_lan"

/************* 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");
    
    int ret = eth.connect();
    if (ret != 0) {
        printf("\r\nConnection error\r\n");
        return -1;
    }
    pc.printf("\n\rEthernet connected\n\r");
    
    Milkcocoa* milkcocoa = new Milkcocoa(&eth, MQTT_SERVER, MILKCOCOA_SERVERPORT, MILKCOCOA_APP_ID, MQTT_CLIENTID);
    
    milkcocoa->connect();
    
    pc.printf("%d\n\r",milkcocoa->on(MILKCOCOA_DATASTORE, "push", onpush));
    
#ifdef __MILKCOCOA_THREAD
    milkcocoa->setLoopCycle(5000);
    milkcocoa->start();
#endif
    while(1) {
        DataElement elem = DataElement();
        elem.setValue("v", 1);
        
        milkcocoa->push(MILKCOCOA_DATASTORE, elem);

#ifndef __MILKCOCOA_THREAD
        milkcocoa->loop();
#endif
        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"));
}