【mbed OS5対応バージョン】データの保存、更新、取得ができるWebサービス「milkcocoa」に接続し、データのプッシュ、送信、取得ができるライブラリを使ったサンプルです。 EthernetIF版 https://mlkcca.com/
main.cpp@2:727e3e0fcb81, 2017-02-15 (annotated)
- Committer:
- jksoft
- Date:
- Wed Feb 15 02:19:04 2017 +0000
- Revision:
- 2:727e3e0fcb81
- Parent:
- 1:1ab9d9f92764
- Child:
- 5:103da32c92c7
Milkcocoa-os????????????
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jksoft | 0:885b53a42035 | 1 | #include "mbed.h" |
jksoft | 0:885b53a42035 | 2 | #include "Milkcocoa.h" |
jksoft | 0:885b53a42035 | 3 | #include "EthernetInterface.h" |
jksoft | 0:885b53a42035 | 4 | |
jksoft | 0:885b53a42035 | 5 | EthernetInterface eth; |
jksoft | 0:885b53a42035 | 6 | RawSerial pc(USBTX,USBRX); |
jksoft | 0:885b53a42035 | 7 | |
jksoft | 0:885b53a42035 | 8 | /************************* Your Milkcocoa Setup *********************************/ |
jksoft | 0:885b53a42035 | 9 | |
jksoft | 1:1ab9d9f92764 | 10 | #define MILKCOCOA_APP_ID "...YOUR_MILKCOCOA_APP_ID..." |
jksoft | 0:885b53a42035 | 11 | #define MILKCOCOA_DATASTORE "mbed_lan" |
jksoft | 0:885b53a42035 | 12 | |
jksoft | 0:885b53a42035 | 13 | /************* Milkcocoa Setup (you don't need to change this!) ******************/ |
jksoft | 0:885b53a42035 | 14 | |
jksoft | 0:885b53a42035 | 15 | #define MILKCOCOA_SERVERPORT 1883 |
jksoft | 0:885b53a42035 | 16 | |
jksoft | 0:885b53a42035 | 17 | /************ Global State (you don't need to change this!) ******************/ |
jksoft | 0:885b53a42035 | 18 | const char MQTT_SERVER[] = MILKCOCOA_APP_ID ".mlkcca.com"; |
jksoft | 0:885b53a42035 | 19 | const char MQTT_CLIENTID[] = __TIME__ MILKCOCOA_APP_ID; |
jksoft | 0:885b53a42035 | 20 | |
jksoft | 0:885b53a42035 | 21 | extern void onpush(MQTT::MessageData& md); |
jksoft | 0:885b53a42035 | 22 | |
jksoft | 0:885b53a42035 | 23 | int main() { |
jksoft | 0:885b53a42035 | 24 | |
jksoft | 0:885b53a42035 | 25 | pc.baud(9600); |
jksoft | 0:885b53a42035 | 26 | pc.printf("Milkcocoa mbed os ver demo\n\r\n\r\n\r"); |
jksoft | 0:885b53a42035 | 27 | |
jksoft | 0:885b53a42035 | 28 | int ret = eth.connect(); |
jksoft | 0:885b53a42035 | 29 | if (ret != 0) { |
jksoft | 0:885b53a42035 | 30 | printf("\r\nConnection error\r\n"); |
jksoft | 0:885b53a42035 | 31 | return -1; |
jksoft | 0:885b53a42035 | 32 | } |
jksoft | 0:885b53a42035 | 33 | pc.printf("\n\rEthernet connected\n\r"); |
jksoft | 0:885b53a42035 | 34 | |
jksoft | 0:885b53a42035 | 35 | Milkcocoa* milkcocoa = new Milkcocoa(ð, MQTT_SERVER, MILKCOCOA_SERVERPORT, MILKCOCOA_APP_ID, MQTT_CLIENTID); |
jksoft | 0:885b53a42035 | 36 | |
jksoft | 0:885b53a42035 | 37 | milkcocoa->connect(); |
jksoft | 0:885b53a42035 | 38 | |
jksoft | 0:885b53a42035 | 39 | pc.printf("%d\n\r",milkcocoa->on(MILKCOCOA_DATASTORE, "push", onpush)); |
jksoft | 0:885b53a42035 | 40 | |
jksoft | 0:885b53a42035 | 41 | milkcocoa->setLoopCycle(5000); |
jksoft | 0:885b53a42035 | 42 | milkcocoa->start(); |
jksoft | 0:885b53a42035 | 43 | |
jksoft | 0:885b53a42035 | 44 | while(1) { |
jksoft | 0:885b53a42035 | 45 | DataElement elem = DataElement(); |
jksoft | 0:885b53a42035 | 46 | elem.setValue("v", 1); |
jksoft | 0:885b53a42035 | 47 | |
jksoft | 0:885b53a42035 | 48 | milkcocoa->push(MILKCOCOA_DATASTORE, elem); |
jksoft | 0:885b53a42035 | 49 | |
jksoft | 0:885b53a42035 | 50 | Thread::wait(7000); |
jksoft | 0:885b53a42035 | 51 | |
jksoft | 0:885b53a42035 | 52 | } |
jksoft | 0:885b53a42035 | 53 | } |
jksoft | 0:885b53a42035 | 54 | |
jksoft | 0:885b53a42035 | 55 | void onpush(MQTT::MessageData& md) |
jksoft | 0:885b53a42035 | 56 | { |
jksoft | 0:885b53a42035 | 57 | MQTT::Message &message = md.message; |
jksoft | 0:885b53a42035 | 58 | DataElement de = DataElement((char*)message.payload); |
jksoft | 0:885b53a42035 | 59 | pc.printf("onpush\n\r"); |
jksoft | 0:885b53a42035 | 60 | pc.printf("%d\n\r",de.getInt("v")); |
jksoft | 0:885b53a42035 | 61 | } |