![](/media/cache/profiles/5f14615696649541a025d3d0f8e0447f.jpg.50x50_q85.jpg)
データの保存、更新、取得ができるWebサービス「milkcocoa」に接続し、データのプッシュ、送信、取得ができるライブラリを使ったサンプルです。 EthernetIF版 https://mlkcca.com/
Dependencies: EthernetInterface Milkcocoa_EthernetIF mbed-rtos mbed
main.cpp@4:764c7ce9b80d, 2016-02-25 (annotated)
- Committer:
- jksoft
- Date:
- Thu Feb 25 04:49:46 2016 +0000
- Revision:
- 4:764c7ce9b80d
- Parent:
- 3:fce801478ba1
- Child:
- 5:7082396c1aa7
??????
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 | 0:92848fdd9379 | 13 | #define MILKCOCOA_APP_ID "...YOUR_MILKCOCOA_APP_ID..." |
jksoft | 4:764c7ce9b80d | 14 | #define MILKCOCOA_DATASTORE "mbed" |
jksoft | 0:92848fdd9379 | 15 | |
jksoft | 0:92848fdd9379 | 16 | /************* Milkcocoa Setup (you don't need to change this!) ******************/ |
jksoft | 0:92848fdd9379 | 17 | |
jksoft | 0:92848fdd9379 | 18 | #define MILKCOCOA_SERVERPORT 1883 |
jksoft | 0:92848fdd9379 | 19 | |
jksoft | 0:92848fdd9379 | 20 | /************ Global State (you don't need to change this!) ******************/ |
jksoft | 0:92848fdd9379 | 21 | |
jksoft | 0:92848fdd9379 | 22 | const char MQTT_SERVER[] = MILKCOCOA_APP_ID ".mlkcca.com"; |
jksoft | 0:92848fdd9379 | 23 | const char MQTT_CLIENTID[] = __TIME__ MILKCOCOA_APP_ID; |
jksoft | 0:92848fdd9379 | 24 | |
jksoft | 0:92848fdd9379 | 25 | extern void onpush(MQTT::MessageData& md); |
jksoft | 0:92848fdd9379 | 26 | |
jksoft | 0:92848fdd9379 | 27 | int main() { |
jksoft | 0:92848fdd9379 | 28 | // void setup() { |
jksoft | 0:92848fdd9379 | 29 | pc.baud(9600); |
jksoft | 0:92848fdd9379 | 30 | |
jksoft | 0:92848fdd9379 | 31 | MQTTEthernet *ipstack = new MQTTEthernet(); |
jksoft | 0:92848fdd9379 | 32 | MClient *client = new MClient(ipstack); |
jksoft | 0:92848fdd9379 | 33 | Milkcocoa *milkcocoa = new Milkcocoa(client, MQTT_SERVER, MILKCOCOA_SERVERPORT, MILKCOCOA_APP_ID, MQTT_CLIENTID); |
jksoft | 0:92848fdd9379 | 34 | |
jksoft | 0:92848fdd9379 | 35 | pc.printf("Milkcocoa mbed ver demo\n\r\n\r\n\r"); |
jksoft | 0:92848fdd9379 | 36 | |
jksoft | 0:92848fdd9379 | 37 | milkcocoa->connect(); |
jksoft | 0:92848fdd9379 | 38 | pc.printf("\n\rEther connected\n\r"); |
jksoft | 0:92848fdd9379 | 39 | |
jksoft | 0:92848fdd9379 | 40 | pc.printf("%d\n\r",milkcocoa->on(MILKCOCOA_DATASTORE, "push", onpush)); |
jksoft | 0:92848fdd9379 | 41 | |
jksoft | 0:92848fdd9379 | 42 | // } |
jksoft | 0:92848fdd9379 | 43 | while(1) { |
jksoft | 0:92848fdd9379 | 44 | // void loop() { |
jksoft | 0:92848fdd9379 | 45 | milkcocoa->loop(); |
jksoft | 0:92848fdd9379 | 46 | |
jksoft | 0:92848fdd9379 | 47 | DataElement elem = DataElement(); |
jksoft | 0:92848fdd9379 | 48 | elem.setValue("v", 1); |
jksoft | 0:92848fdd9379 | 49 | |
jksoft | 0:92848fdd9379 | 50 | milkcocoa->push(MILKCOCOA_DATASTORE, elem); |
jksoft | 0:92848fdd9379 | 51 | wait(7.0); |
jksoft | 0:92848fdd9379 | 52 | } |
jksoft | 0:92848fdd9379 | 53 | } |
jksoft | 0:92848fdd9379 | 54 | |
jksoft | 0:92848fdd9379 | 55 | void onpush(MQTT::MessageData& md) |
jksoft | 0:92848fdd9379 | 56 | { |
jksoft | 0:92848fdd9379 | 57 | MQTT::Message &message = md.message; |
jksoft | 0:92848fdd9379 | 58 | DataElement de = DataElement((char*)message.payload); |
jksoft | 0:92848fdd9379 | 59 | pc.printf("onpush\n\r"); |
jksoft | 0:92848fdd9379 | 60 | pc.printf("%d\n\r",de.getInt("v")); |
jksoft | 0:92848fdd9379 | 61 | } |