温度センサの値をMilkcocoaへPushするプログラム
Dependencies: EthernetInterface Milkcocoa_EthernetIF mbed-rtos mbed
Fork of MilkcocoaSample_Eth by
main.cpp@3:fce801478ba1, 2016-02-24 (annotated)
- Committer:
- jksoft
- Date:
- Wed Feb 24 17:04:31 2016 +0000
- Revision:
- 3:fce801478ba1
- Parent:
- 0:92848fdd9379
- Child:
- 4:764c7ce9b80d
??
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:92848fdd9379 | 10 | DigitalOut myled(LED1); |
jksoft | 0:92848fdd9379 | 11 | |
jksoft | 0:92848fdd9379 | 12 | /************************* Your Milkcocoa Setup *********************************/ |
jksoft | 3:fce801478ba1 | 13 | |
jksoft | 0:92848fdd9379 | 14 | #define MILKCOCOA_APP_ID "...YOUR_MILKCOCOA_APP_ID..." |
jksoft | 0:92848fdd9379 | 15 | #define MILKCOCOA_DATASTORE "esp8266" |
jksoft | 0:92848fdd9379 | 16 | |
jksoft | 0:92848fdd9379 | 17 | /************* Milkcocoa Setup (you don't need to change this!) ******************/ |
jksoft | 0:92848fdd9379 | 18 | |
jksoft | 0:92848fdd9379 | 19 | #define MILKCOCOA_SERVERPORT 1883 |
jksoft | 0:92848fdd9379 | 20 | |
jksoft | 0:92848fdd9379 | 21 | /************ Global State (you don't need to change this!) ******************/ |
jksoft | 0:92848fdd9379 | 22 | |
jksoft | 0:92848fdd9379 | 23 | const char MQTT_SERVER[] = MILKCOCOA_APP_ID ".mlkcca.com"; |
jksoft | 0:92848fdd9379 | 24 | const char MQTT_CLIENTID[] = __TIME__ MILKCOCOA_APP_ID; |
jksoft | 0:92848fdd9379 | 25 | |
jksoft | 0:92848fdd9379 | 26 | extern void onpush(MQTT::MessageData& md); |
jksoft | 0:92848fdd9379 | 27 | |
jksoft | 0:92848fdd9379 | 28 | int main() { |
jksoft | 0:92848fdd9379 | 29 | // void setup() { |
jksoft | 0:92848fdd9379 | 30 | pc.baud(9600); |
jksoft | 0:92848fdd9379 | 31 | |
jksoft | 0:92848fdd9379 | 32 | MQTTEthernet *ipstack = new MQTTEthernet(); |
jksoft | 0:92848fdd9379 | 33 | MClient *client = new MClient(ipstack); |
jksoft | 0:92848fdd9379 | 34 | Milkcocoa *milkcocoa = new Milkcocoa(client, MQTT_SERVER, MILKCOCOA_SERVERPORT, MILKCOCOA_APP_ID, MQTT_CLIENTID); |
jksoft | 0:92848fdd9379 | 35 | |
jksoft | 0:92848fdd9379 | 36 | pc.printf("Milkcocoa mbed ver demo\n\r\n\r\n\r"); |
jksoft | 0:92848fdd9379 | 37 | |
jksoft | 0:92848fdd9379 | 38 | milkcocoa->connect(); |
jksoft | 0:92848fdd9379 | 39 | pc.printf("\n\rEther connected\n\r"); |
jksoft | 0:92848fdd9379 | 40 | |
jksoft | 0:92848fdd9379 | 41 | pc.printf("%d\n\r",milkcocoa->on(MILKCOCOA_DATASTORE, "push", onpush)); |
jksoft | 0:92848fdd9379 | 42 | |
jksoft | 0:92848fdd9379 | 43 | // } |
jksoft | 0:92848fdd9379 | 44 | while(1) { |
jksoft | 0:92848fdd9379 | 45 | // void loop() { |
jksoft | 0:92848fdd9379 | 46 | milkcocoa->loop(); |
jksoft | 0:92848fdd9379 | 47 | |
jksoft | 0:92848fdd9379 | 48 | DataElement elem = DataElement(); |
jksoft | 0:92848fdd9379 | 49 | elem.setValue("v", 1); |
jksoft | 0:92848fdd9379 | 50 | |
jksoft | 0:92848fdd9379 | 51 | milkcocoa->push(MILKCOCOA_DATASTORE, elem); |
jksoft | 0:92848fdd9379 | 52 | wait(7.0); |
jksoft | 0:92848fdd9379 | 53 | } |
jksoft | 0:92848fdd9379 | 54 | } |
jksoft | 0:92848fdd9379 | 55 | |
jksoft | 0:92848fdd9379 | 56 | void onpush(MQTT::MessageData& md) |
jksoft | 0:92848fdd9379 | 57 | { |
jksoft | 0:92848fdd9379 | 58 | MQTT::Message &message = md.message; |
jksoft | 0:92848fdd9379 | 59 | DataElement de = DataElement((char*)message.payload); |
jksoft | 0:92848fdd9379 | 60 | pc.printf("onpush\n\r"); |
jksoft | 0:92848fdd9379 | 61 | pc.printf("%d\n\r",de.getInt("v")); |
jksoft | 0:92848fdd9379 | 62 | } |