Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of mbed-os-example-mbed5-blinky by
main.cpp@24:6ba1245bf049, 2017-01-24 (annotated)
- Committer:
- jksoft
- Date:
- Tue Jan 24 13:41:36 2017 +0000
- Revision:
- 24:6ba1245bf049
- Parent:
- 22:af9dcf379926
??????????
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| Jonathan Austin |
0:2757d7abb7d9 | 1 | #include "mbed.h" |
| jksoft | 24:6ba1245bf049 | 2 | #include "MQTTInterface.h" |
| jksoft | 24:6ba1245bf049 | 3 | #include "MQTTClient.h" |
| jksoft | 24:6ba1245bf049 | 4 | #include "Milkcocoa.h" |
| jksoft | 24:6ba1245bf049 | 5 | #include "MClient.h" |
| jksoft | 24:6ba1245bf049 | 6 | #include "EthernetInterface.h" |
| jksoft | 24:6ba1245bf049 | 7 | |
| jksoft | 24:6ba1245bf049 | 8 | EthernetInterface eth; |
| jksoft | 24:6ba1245bf049 | 9 | Serial pc(USBTX,USBRX); |
| jksoft | 24:6ba1245bf049 | 10 | |
| jksoft | 24:6ba1245bf049 | 11 | /************************* Your Milkcocoa Setup *********************************/ |
| Jonathan Austin |
0:2757d7abb7d9 | 12 | |
| jksoft | 24:6ba1245bf049 | 13 | #define MILKCOCOA_APP_ID "teaidsirehz" |
| jksoft | 24:6ba1245bf049 | 14 | #define MILKCOCOA_DATASTORE "mbed_lan" |
| jksoft | 24:6ba1245bf049 | 15 | |
| jksoft | 24:6ba1245bf049 | 16 | /************* Milkcocoa Setup (you don't need to change this!) ******************/ |
| jksoft | 24:6ba1245bf049 | 17 | |
| jksoft | 24:6ba1245bf049 | 18 | #define MILKCOCOA_SERVERPORT 1883 |
| jksoft | 24:6ba1245bf049 | 19 | |
| jksoft | 24:6ba1245bf049 | 20 | /************ Global State (you don't need to change this!) ******************/ |
| jksoft | 24:6ba1245bf049 | 21 | const char MQTT_SERVER[] = MILKCOCOA_APP_ID ".mlkcca.com"; |
| jksoft | 24:6ba1245bf049 | 22 | const char MQTT_CLIENTID[] = __TIME__ MILKCOCOA_APP_ID; |
| jksoft | 24:6ba1245bf049 | 23 | |
| jksoft | 24:6ba1245bf049 | 24 | extern void onpush(MQTT::MessageData& md); |
| Jonathan Austin |
0:2757d7abb7d9 | 25 | |
| Jonathan Austin |
0:2757d7abb7d9 | 26 | int main() { |
| jksoft | 24:6ba1245bf049 | 27 | |
| jksoft | 24:6ba1245bf049 | 28 | pc.baud(9600); |
| jksoft | 24:6ba1245bf049 | 29 | pc.printf("Milkcocoa mbed os ver demo\n\r\n\r\n\r"); |
| jksoft | 24:6ba1245bf049 | 30 | |
| jksoft | 24:6ba1245bf049 | 31 | int ret = eth.connect(); |
| jksoft | 24:6ba1245bf049 | 32 | if (ret != 0) { |
| jksoft | 24:6ba1245bf049 | 33 | printf("\r\nConnection error\r\n"); |
| jksoft | 24:6ba1245bf049 | 34 | return -1; |
| jksoft | 24:6ba1245bf049 | 35 | } |
| jksoft | 24:6ba1245bf049 | 36 | pc.printf("\n\rEthernet connected\n\r"); |
| jksoft | 24:6ba1245bf049 | 37 | |
| jksoft | 24:6ba1245bf049 | 38 | MQTTInterface* ipstack = new MQTTInterface(ð); |
| jksoft | 24:6ba1245bf049 | 39 | MClient* client = new MClient(ipstack); |
| jksoft | 24:6ba1245bf049 | 40 | Milkcocoa* milkcocoa = new Milkcocoa(client, MQTT_SERVER, MILKCOCOA_SERVERPORT, MILKCOCOA_APP_ID, MQTT_CLIENTID); |
| jksoft | 24:6ba1245bf049 | 41 | |
| jksoft | 24:6ba1245bf049 | 42 | milkcocoa->connect(); |
| jksoft | 24:6ba1245bf049 | 43 | |
| jksoft | 24:6ba1245bf049 | 44 | pc.printf("%d\n\r",milkcocoa->on(MILKCOCOA_DATASTORE, "push", onpush)); |
| jksoft | 24:6ba1245bf049 | 45 | |
| jksoft | 24:6ba1245bf049 | 46 | milkcocoa->start(); |
| jksoft | 24:6ba1245bf049 | 47 | |
| jksoft | 24:6ba1245bf049 | 48 | while(1) { |
| jksoft | 24:6ba1245bf049 | 49 | |
| jksoft | 24:6ba1245bf049 | 50 | DataElement elem = DataElement(); |
| jksoft | 24:6ba1245bf049 | 51 | elem.setValue("v", 1); |
| jksoft | 24:6ba1245bf049 | 52 | |
| jksoft | 24:6ba1245bf049 | 53 | milkcocoa->push(MILKCOCOA_DATASTORE, elem); |
| jksoft | 24:6ba1245bf049 | 54 | pc.printf("PUSH\n\r"); |
| jksoft | 24:6ba1245bf049 | 55 | Thread::wait(500); |
| jksoft | 24:6ba1245bf049 | 56 | |
| Jonathan Austin |
0:2757d7abb7d9 | 57 | } |
| Jonathan Austin |
0:2757d7abb7d9 | 58 | } |
| Jonathan Austin |
1:846c97078558 | 59 | |
| jksoft | 24:6ba1245bf049 | 60 | void onpush(MQTT::MessageData& md) |
| jksoft | 24:6ba1245bf049 | 61 | { |
| jksoft | 24:6ba1245bf049 | 62 | MQTT::Message &message = md.message; |
| jksoft | 24:6ba1245bf049 | 63 | DataElement de = DataElement((char*)message.payload); |
| jksoft | 24:6ba1245bf049 | 64 | pc.printf("onpush\n\r"); |
| jksoft | 24:6ba1245bf049 | 65 | pc.printf("%d\n\r",de.getInt("v")); |
| jksoft | 24:6ba1245bf049 | 66 | } |
