Example application using MQTT for a thermostat application.
Dependencies: C12832 EthernetInterface LM75B MQTT MbedJSONValue RGB-fun mbed-rtos mbed
Fork of HelloMQTT_FRDMK64F by
main.cpp@19:852fa5912f91, 2017-04-25 (annotated)
- Committer:
- sillevl
- Date:
- Tue Apr 25 12:42:01 2017 +0000
- Revision:
- 19:852fa5912f91
- Parent:
- 18:da0ba00e21ab
update led color using json
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sam_grove | 5:4a257f6ac09a | 1 | #include "C12832.h" |
sillevl | 18:da0ba00e21ab | 2 | #include "LM75B.h" |
icraggs | 8:a3e3113054a1 | 3 | #include "MQTTEthernet.h" |
icraggs | 2:638c854c0695 | 4 | #include "MQTTClient.h" |
sillevl | 18:da0ba00e21ab | 5 | #include "rtos.h" |
sillevl | 18:da0ba00e21ab | 6 | #include "MbedJSONValue.h" |
sillevl | 18:da0ba00e21ab | 7 | #include "RGB.h" |
icraggs | 2:638c854c0695 | 8 | |
sillevl | 18:da0ba00e21ab | 9 | /*** IMPORTANT !!! |
sillevl | 18:da0ba00e21ab | 10 | * Change the following values |
sillevl | 18:da0ba00e21ab | 11 | */ |
sillevl | 18:da0ba00e21ab | 12 | |
sillevl | 18:da0ba00e21ab | 13 | char* hostname = "mqtt.labict.be"; |
sillevl | 18:da0ba00e21ab | 14 | char* temperature_topic = "softwareengineering/thermostat/YOURSENSORID/temperature"; |
sillevl | 18:da0ba00e21ab | 15 | char* led_topic = "softwareengineering/thermostat/YOURSENSORID/led"; |
sillevl | 18:da0ba00e21ab | 16 | int port = 1883; |
sillevl | 18:da0ba00e21ab | 17 | char* clientId = "myrandomstringclientid"; |
sillevl | 18:da0ba00e21ab | 18 | |
sillevl | 18:da0ba00e21ab | 19 | //#define K64F 1 |
icraggs | 2:638c854c0695 | 20 | |
sillevl | 18:da0ba00e21ab | 21 | #ifdef K64F |
sillevl | 18:da0ba00e21ab | 22 | C12832 lcd(D11, D13, D12, D7, D10); |
sillevl | 18:da0ba00e21ab | 23 | LM75B sensor(D14,D15); |
sillevl | 18:da0ba00e21ab | 24 | RGB led(p23,p24,p25); |
sillevl | 18:da0ba00e21ab | 25 | #else //LPC1768 |
sillevl | 18:da0ba00e21ab | 26 | C12832 lcd(p5, p7, p6, p8, p11); |
sillevl | 18:da0ba00e21ab | 27 | LM75B sensor(p28,p27); |
sillevl | 18:da0ba00e21ab | 28 | RGB led(p23,p24,p25); |
sillevl | 18:da0ba00e21ab | 29 | #endif |
icraggs | 8:a3e3113054a1 | 30 | |
sillevl | 18:da0ba00e21ab | 31 | MQTT::Client<MQTTEthernet, Countdown>* client; |
sillevl | 18:da0ba00e21ab | 32 | |
sillevl | 18:da0ba00e21ab | 33 | void send_temperature(void const * arg) |
sillevl | 18:da0ba00e21ab | 34 | { |
sillevl | 18:da0ba00e21ab | 35 | while(true){ |
sillevl | 18:da0ba00e21ab | 36 | char buf[100]; |
sillevl | 18:da0ba00e21ab | 37 | sprintf(buf, "{\"temperature\": %.2f}", sensor.read()); |
sillevl | 18:da0ba00e21ab | 38 | |
sillevl | 18:da0ba00e21ab | 39 | MQTT::Message message; |
sillevl | 18:da0ba00e21ab | 40 | message.qos = MQTT::QOS0; |
sillevl | 18:da0ba00e21ab | 41 | message.retained = false; |
sillevl | 18:da0ba00e21ab | 42 | message.dup = false; |
sillevl | 18:da0ba00e21ab | 43 | message.payload = (void*)buf; |
sillevl | 18:da0ba00e21ab | 44 | message.payloadlen = strlen(buf); |
sillevl | 18:da0ba00e21ab | 45 | client->publish(temperature_topic, message); |
sillevl | 18:da0ba00e21ab | 46 | |
sillevl | 18:da0ba00e21ab | 47 | Thread::wait(5000); |
sillevl | 18:da0ba00e21ab | 48 | } |
sillevl | 18:da0ba00e21ab | 49 | } |
sillevl | 18:da0ba00e21ab | 50 | |
sillevl | 18:da0ba00e21ab | 51 | void process_rgb(MQTT::MessageData& md) |
icraggs | 2:638c854c0695 | 52 | { |
icraggs | 9:5beb8609e9f7 | 53 | MQTT::Message &message = md.message; |
sam_grove | 5:4a257f6ac09a | 54 | lcd.cls(); |
sam_grove | 5:4a257f6ac09a | 55 | lcd.locate(0,3); |
icraggs | 9:5beb8609e9f7 | 56 | printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\n", message.qos, message.retained, message.dup, message.id); |
icraggs | 9:5beb8609e9f7 | 57 | printf("Payload %.*s\n", message.payloadlen, (char*)message.payload); |
sillevl | 19:852fa5912f91 | 58 | lcd.puts((char*)message.payload); |
sillevl | 18:da0ba00e21ab | 59 | |
sillevl | 19:852fa5912f91 | 60 | char buffer[32] = {0}; |
sillevl | 19:852fa5912f91 | 61 | memcpy(buffer, message.payload, message.payloadlen); |
sillevl | 18:da0ba00e21ab | 62 | |
sillevl | 18:da0ba00e21ab | 63 | MbedJSONValue payload; |
sillevl | 19:852fa5912f91 | 64 | parse(payload, buffer); |
sillevl | 18:da0ba00e21ab | 65 | |
sillevl | 19:852fa5912f91 | 66 | std::string color_string = payload["color"].get<std::string>(); |
sillevl | 19:852fa5912f91 | 67 | printf("Color: %s\n", color_string.c_str()); |
sillevl | 18:da0ba00e21ab | 68 | |
sillevl | 19:852fa5912f91 | 69 | Color color((int) std::strtol(color_string.c_str(), 0, 16)); |
sillevl | 19:852fa5912f91 | 70 | led.setColor(&color); |
icraggs | 2:638c854c0695 | 71 | } |
icraggs | 0:0cae29831d01 | 72 | |
icraggs | 2:638c854c0695 | 73 | int main(int argc, char* argv[]) |
icraggs | 2:638c854c0695 | 74 | { |
icraggs | 8:a3e3113054a1 | 75 | MQTTEthernet ipstack = MQTTEthernet(); |
icraggs | 2:638c854c0695 | 76 | |
sillevl | 18:da0ba00e21ab | 77 | client = new MQTT::Client<MQTTEthernet, Countdown>(ipstack); |
icraggs | 3:7a6a899de7cc | 78 | |
icraggs | 6:e4c690c45021 | 79 | lcd.printf("Connecting to %s:%d\n", hostname, port); |
icraggs | 6:e4c690c45021 | 80 | int rc = ipstack.connect(hostname, port); |
icraggs | 6:e4c690c45021 | 81 | if (rc != 0) |
icraggs | 6:e4c690c45021 | 82 | lcd.printf("rc from TCP connect is %d\n", rc); |
icraggs | 6:e4c690c45021 | 83 | |
icraggs | 6:e4c690c45021 | 84 | MQTTPacket_connectData data = MQTTPacket_connectData_initializer; |
icraggs | 6:e4c690c45021 | 85 | data.MQTTVersion = 3; |
sillevl | 18:da0ba00e21ab | 86 | data.clientID.cstring = clientId; |
icraggs | 0:0cae29831d01 | 87 | |
sillevl | 18:da0ba00e21ab | 88 | if ((rc = client->connect(data)) != 0) |
sillevl | 18:da0ba00e21ab | 89 | lcd.printf("rc from MQTT connect is %d\n", rc); |
icraggs | 2:638c854c0695 | 90 | |
sillevl | 18:da0ba00e21ab | 91 | if ((rc = client->subscribe(led_topic, MQTT::QOS1, process_rgb)) != 0) |
sillevl | 18:da0ba00e21ab | 92 | lcd.printf("rc from MQTT subscribe is %d\n", rc); |
sillevl | 18:da0ba00e21ab | 93 | |
sillevl | 18:da0ba00e21ab | 94 | |
sillevl | 18:da0ba00e21ab | 95 | Thread send_thread(send_temperature); |
sillevl | 18:da0ba00e21ab | 96 | |
sillevl | 18:da0ba00e21ab | 97 | |
sillevl | 18:da0ba00e21ab | 98 | while(true){ |
sillevl | 18:da0ba00e21ab | 99 | client->yield(100); |
icraggs | 12:086a9314e8a5 | 100 | } |
sillevl | 18:da0ba00e21ab | 101 | |
icraggs | 2:638c854c0695 | 102 | |
icraggs | 0:0cae29831d01 | 103 | return 0; |
icraggs | 0:0cae29831d01 | 104 | } |