LEDの点灯状態をMilkcocoaから受け取って変更するテストプログラム

Dependencies:   EthernetInterface Milkcocoa_eth mbed-rtos mbed

Fork of MilkcocoaSample_Eth by Junichi Katsu

Committer:
jksoft
Date:
Wed Feb 24 17:03:29 2016 +0000
Revision:
2:7be8b81dc8ac
Parent:
1:a45427394577
??

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jksoft 0:92848fdd9379 1 #include "mbed.h"
jksoft 0:92848fdd9379 2 #include "MQTTEthernet.h"
jksoft 0:92848fdd9379 3 #include "MQTTClient.h"
jksoft 0:92848fdd9379 4 #include "Milkcocoa.h"
jksoft 0:92848fdd9379 5 #include "MClient.h"
jksoft 0:92848fdd9379 6
jksoft 0:92848fdd9379 7 // The default setting is for the Simple IoT Board(mbed LPC1114FN28)
jksoft 0:92848fdd9379 8 // Please change to fit the platform
jksoft 0:92848fdd9379 9 Serial pc(USBTX, USBRX);
jksoft 1:a45427394577 10 DigitalOut red(LED1);
jksoft 1:a45427394577 11 Ticker flipper;
jksoft 0:92848fdd9379 12
jksoft 0:92848fdd9379 13 /************************* Your Milkcocoa Setup *********************************/
jksoft 0:92848fdd9379 14
jksoft 2:7be8b81dc8ac 15 #define MILKCOCOA_APP_ID "...YOUR_MILKCOCOA_APP_ID..."
jksoft 1:a45427394577 16 #define MILKCOCOA_DATASTORE "LED"
jksoft 0:92848fdd9379 17
jksoft 0:92848fdd9379 18 /************* Milkcocoa Setup (you don't need to change this!) ******************/
jksoft 0:92848fdd9379 19
jksoft 0:92848fdd9379 20 #define MILKCOCOA_SERVERPORT 1883
jksoft 0:92848fdd9379 21
jksoft 0:92848fdd9379 22 /************ Global State (you don't need to change this!) ******************/
jksoft 0:92848fdd9379 23
jksoft 0:92848fdd9379 24 const char MQTT_SERVER[] = MILKCOCOA_APP_ID ".mlkcca.com";
jksoft 0:92848fdd9379 25 const char MQTT_CLIENTID[] = __TIME__ MILKCOCOA_APP_ID;
jksoft 0:92848fdd9379 26
jksoft 0:92848fdd9379 27 extern void onpush(MQTT::MessageData& md);
jksoft 0:92848fdd9379 28
jksoft 1:a45427394577 29 int duty = 0;
jksoft 1:a45427394577 30 int duty_count = 0;
jksoft 1:a45427394577 31
jksoft 1:a45427394577 32 void flip() {
jksoft 1:a45427394577 33 if( duty_count > duty )
jksoft 1:a45427394577 34 {
jksoft 1:a45427394577 35 red = 0;
jksoft 1:a45427394577 36 }
jksoft 1:a45427394577 37 else
jksoft 1:a45427394577 38 {
jksoft 1:a45427394577 39 red = 1;
jksoft 1:a45427394577 40 }
jksoft 1:a45427394577 41
jksoft 1:a45427394577 42 duty_count++;
jksoft 1:a45427394577 43 if( duty_count > 100) duty_count = 0;
jksoft 1:a45427394577 44 }
jksoft 1:a45427394577 45
jksoft 0:92848fdd9379 46 int main() {
jksoft 0:92848fdd9379 47 pc.baud(9600);
jksoft 0:92848fdd9379 48
jksoft 0:92848fdd9379 49 MQTTEthernet *ipstack = new MQTTEthernet();
jksoft 0:92848fdd9379 50 MClient *client = new MClient(ipstack);
jksoft 0:92848fdd9379 51 Milkcocoa *milkcocoa = new Milkcocoa(client, MQTT_SERVER, MILKCOCOA_SERVERPORT, MILKCOCOA_APP_ID, MQTT_CLIENTID);
jksoft 0:92848fdd9379 52
jksoft 0:92848fdd9379 53 pc.printf("Milkcocoa mbed ver demo\n\r\n\r\n\r");
jksoft 0:92848fdd9379 54
jksoft 0:92848fdd9379 55 milkcocoa->connect();
jksoft 0:92848fdd9379 56 pc.printf("\n\rEther connected\n\r");
jksoft 0:92848fdd9379 57
jksoft 0:92848fdd9379 58 pc.printf("%d\n\r",milkcocoa->on(MILKCOCOA_DATASTORE, "push", onpush));
jksoft 0:92848fdd9379 59
jksoft 1:a45427394577 60 flipper.attach(&flip, 0.0001);
jksoft 1:a45427394577 61
jksoft 0:92848fdd9379 62 while(1) {
jksoft 0:92848fdd9379 63 milkcocoa->loop();
jksoft 0:92848fdd9379 64 }
jksoft 0:92848fdd9379 65 }
jksoft 0:92848fdd9379 66
jksoft 0:92848fdd9379 67 void onpush(MQTT::MessageData& md)
jksoft 0:92848fdd9379 68 {
jksoft 0:92848fdd9379 69 MQTT::Message &message = md.message;
jksoft 0:92848fdd9379 70 DataElement de = DataElement((char*)message.payload);
jksoft 1:a45427394577 71 //pc.printf("onpush:%s\n\r",message.payload);
jksoft 1:a45427394577 72 //pc.printf("%d\n\r",de.getInt("RED"));
jksoft 1:a45427394577 73 duty = de.getInt("RED");
jksoft 0:92848fdd9379 74 }