Guilhem Saurel
/
AV_MQTT_example
Example for AV_MQTT
main.cpp@2:b9b08c7d6e12, 2013-08-12 (annotated)
- Committer:
- Nim65s
- Date:
- Mon Aug 12 09:37:05 2013 +0000
- Revision:
- 2:b9b08c7d6e12
- Parent:
- 1:3bb5708ae2b9
- Child:
- 6:761a452a8a77
Callback & JSON
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Nim65s | 0:ab341e0e5a21 | 1 | #include "mbed.h" |
Nim65s | 0:ab341e0e5a21 | 2 | #include "rtos.h" |
Nim65s | 0:ab341e0e5a21 | 3 | #include "AV_MQTT.h" |
Nim65s | 0:ab341e0e5a21 | 4 | #include "EthernetInterface.h" |
Nim65s | 2:b9b08c7d6e12 | 5 | #include "picojson.h" |
Nim65s | 0:ab341e0e5a21 | 6 | |
Nim65s | 0:ab341e0e5a21 | 7 | EthernetInterface eth; |
Nim65s | 0:ab341e0e5a21 | 8 | |
Nim65s | 2:b9b08c7d6e12 | 9 | DigitalIn jdown(p12), jleft(p13), jcenter(p14), jup(p15), jright(p16); |
Nim65s | 2:b9b08c7d6e12 | 10 | DigitalOut l1(LED1), l2(LED2), l3(LED3), l4(LED4); |
Nim65s | 0:ab341e0e5a21 | 11 | |
Nim65s | 0:ab341e0e5a21 | 12 | void callback(char *topic, char *message) { |
Nim65s | 2:b9b08c7d6e12 | 13 | picojson::value v; |
Nim65s | 2:b9b08c7d6e12 | 14 | picojson::parse(v, message, message + strlen(message)); |
Nim65s | 2:b9b08c7d6e12 | 15 | |
Nim65s | 2:b9b08c7d6e12 | 16 | int led = atoi(v.get<picojson::array>()[0].get("write").get<picojson::array>()[0].get("mbed_mqtt_example.led_settings").to_str().c_str()); |
Nim65s | 2:b9b08c7d6e12 | 17 | l2 = (led & 4) >> 2; |
Nim65s | 2:b9b08c7d6e12 | 18 | l3 = (led & 2) >> 1; |
Nim65s | 2:b9b08c7d6e12 | 19 | l4 = led & 1; |
Nim65s | 0:ab341e0e5a21 | 20 | } |
Nim65s | 0:ab341e0e5a21 | 21 | |
Nim65s | 0:ab341e0e5a21 | 22 | int main() { |
Nim65s | 0:ab341e0e5a21 | 23 | printf("\r\n====================================================\r\n"); |
Nim65s | 0:ab341e0e5a21 | 24 | eth.init(); |
Nim65s | 0:ab341e0e5a21 | 25 | do printf("Connection...\r\n"); while (eth.connect() != 0); |
Nim65s | 0:ab341e0e5a21 | 26 | |
Nim65s | 2:b9b08c7d6e12 | 27 | AV_MQTT client("10.41.240.6", callback, "MBED_MQTT_EXAMPLE", "mbed_password", "id", 1883, false); |
Nim65s | 0:ab341e0e5a21 | 28 | |
Nim65s | 0:ab341e0e5a21 | 29 | while(true) { |
Nim65s | 0:ab341e0e5a21 | 30 | |
Nim65s | 2:b9b08c7d6e12 | 31 | if (jdown) client.pub("button", "1"); |
Nim65s | 2:b9b08c7d6e12 | 32 | else if (jleft) client.pub("button", "2"); |
Nim65s | 2:b9b08c7d6e12 | 33 | else if (jcenter) client.pub("button", "3"); |
Nim65s | 2:b9b08c7d6e12 | 34 | else if (jup) client.pub("button", "4"); |
Nim65s | 2:b9b08c7d6e12 | 35 | else if (jright) client.pub("button", "5"); |
Nim65s | 0:ab341e0e5a21 | 36 | |
Nim65s | 2:b9b08c7d6e12 | 37 | l1 = 0; |
Nim65s | 0:ab341e0e5a21 | 38 | wait(1); |
Nim65s | 2:b9b08c7d6e12 | 39 | l1 = 1; |
Nim65s | 0:ab341e0e5a21 | 40 | wait(1); |
Nim65s | 0:ab341e0e5a21 | 41 | } |
Nim65s | 1:3bb5708ae2b9 | 42 | } |