Junichi Katsu / Mbed 2 deprecated MilkcocoaSample_Eth_K64F

Dependencies:   EthernetInterface Milkcocoa_eth mbed-rtos mbed

Fork of MilkcocoaSample_Eth by Junichi Katsu

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "MQTTEthernet.h"
00003 #include "MQTTClient.h"
00004 #include "Milkcocoa.h"
00005 #include "MClient.h"
00006 
00007 // The default setting is for the Simple IoT Board(mbed LPC1114FN28)
00008 // Please change to fit the platform
00009 Serial pc(USBTX, USBRX);
00010 DigitalOut red(LED1);
00011 Ticker flipper;
00012 
00013 /************************* Your Milkcocoa Setup *********************************/
00014 
00015 #define MILKCOCOA_APP_ID      "...YOUR_MILKCOCOA_APP_ID..."
00016 #define MILKCOCOA_DATASTORE   "LED"
00017 
00018 /************* Milkcocoa Setup (you don't need to change this!) ******************/
00019 
00020 #define MILKCOCOA_SERVERPORT  1883
00021 
00022 /************ Global State (you don't need to change this!) ******************/
00023 
00024 const char MQTT_SERVER[]  = MILKCOCOA_APP_ID ".mlkcca.com";
00025 const char MQTT_CLIENTID[] = __TIME__ MILKCOCOA_APP_ID;
00026 
00027 extern void onpush(MQTT::MessageData& md);
00028 
00029 int duty = 0;
00030 int duty_count = 0;
00031 
00032 void flip() {
00033     if( duty_count > duty )
00034     {
00035         red = 0;
00036     }
00037     else
00038     {
00039         red = 1;
00040     }
00041     
00042     duty_count++;
00043     if( duty_count > 100) duty_count = 0;
00044 }
00045 
00046 int main() {
00047     pc.baud(9600);
00048 
00049     MQTTEthernet *ipstack = new MQTTEthernet();
00050     MClient *client = new MClient(ipstack);
00051     Milkcocoa *milkcocoa = new Milkcocoa(client, MQTT_SERVER, MILKCOCOA_SERVERPORT, MILKCOCOA_APP_ID, MQTT_CLIENTID);
00052 
00053     pc.printf("Milkcocoa mbed ver demo\n\r\n\r\n\r");
00054     
00055     milkcocoa->connect();
00056     pc.printf("\n\rEther connected\n\r");
00057     
00058     pc.printf("%d\n\r",milkcocoa->on(MILKCOCOA_DATASTORE, "push", onpush));
00059     
00060     flipper.attach(&flip, 0.0001);
00061     
00062     while(1) {
00063         milkcocoa->loop();
00064     }
00065 }
00066 
00067 void onpush(MQTT::MessageData& md)
00068 {
00069     MQTT::Message &message = md.message;
00070     DataElement de = DataElement((char*)message.payload);
00071     //pc.printf("onpush:%s\n\r",message.payload);
00072     //pc.printf("%d\n\r",de.getInt("RED"));
00073     duty = de.getInt("RED");
00074 }