![](/media/cache/profiles/be6b2f6b6e7d72e528c6123f2cd5568e.jpg.50x50_q85.jpg)
Example for AV_MQTT
Diff: main.cpp
- Revision:
- 2:b9b08c7d6e12
- Parent:
- 1:3bb5708ae2b9
- Child:
- 6:761a452a8a77
--- a/main.cpp Thu Aug 08 16:14:26 2013 +0000 +++ b/main.cpp Mon Aug 12 09:37:05 2013 +0000 @@ -2,19 +2,21 @@ #include "rtos.h" #include "AV_MQTT.h" #include "EthernetInterface.h" +#include "picojson.h" EthernetInterface eth; -DigitalOut led(LED1); - -DigitalIn down(p12), left(p13), center(p14), up(p15), right(p16); +DigitalIn jdown(p12), jleft(p13), jcenter(p14), jup(p15), jright(p16); +DigitalOut l1(LED1), l2(LED2), l3(LED3), l4(LED4); void callback(char *topic, char *message) { - printf("==== New PUBLISH received ====\r\n"); - printf(topic); - printf("\r\n------------------------------\r\n"); - printf(message); - printf("\r\n------------ end -------------\r\n"); + picojson::value v; + picojson::parse(v, message, message + strlen(message)); + + int led = atoi(v.get<picojson::array>()[0].get("write").get<picojson::array>()[0].get("mbed_mqtt_example.led_settings").to_str().c_str()); + l2 = (led & 4) >> 2; + l3 = (led & 2) >> 1; + l4 = led & 1; } int main() { @@ -22,19 +24,19 @@ eth.init(); do printf("Connection...\r\n"); while (eth.connect() != 0); - AV_MQTT client("10.41.240.6", callback, "MBED_MQTT_TEST", "mbed_password"); + AV_MQTT client("10.41.240.6", callback, "MBED_MQTT_EXAMPLE", "mbed_password", "id", 1883, false); while(true) { - if (down) client.pub("button", "1"); - else if (left) client.pub("button", "2"); - else if (center) client.pub("button", "3"); - else if (up) client.pub("button", "4"); - else if (right) client.pub("button", "5"); + if (jdown) client.pub("button", "1"); + else if (jleft) client.pub("button", "2"); + else if (jcenter) client.pub("button", "3"); + else if (jup) client.pub("button", "4"); + else if (jright) client.pub("button", "5"); - led = 0; + l1 = 0; wait(1); - led = 1; + l1 = 1; wait(1); } } \ No newline at end of file