MQTT from mbed to node with pressure sensor data retrieval
Dependencies: EthernetInterface FSR MQTT Queue mbed-rtos mbed
Fork of K64F-RTOS-MQTT-Example by
Diff: main.cpp
- Revision:
- 1:ddf6a1c98d91
- Parent:
- 0:9796509d718e
diff -r 9796509d718e -r ddf6a1c98d91 main.cpp --- a/main.cpp Wed Feb 10 18:39:26 2016 +0000 +++ b/main.cpp Tue May 02 16:49:13 2017 +0000 @@ -3,16 +3,25 @@ #include "MQTTEthernet.h" #include "rtos.h" #include "k64f.h" +#include "FSR.h" +//#include "queue.h" + // connect options for MQTT broker -#define BROKER "broker.mqttdashboard.com" // MQTT broker URL +#define BROKER "10.136.107.229" // MQTT broker URL #define PORT 1883 // MQTT broker port number -#define CLIENTID "" // use K64F MAC address without colons +#define CLIENTID "MyMbedClient1" // use K64F MAC address without colons #define USERNAME "" // not required for MQTT Dashboard public broker #define PASSWORD "" // not required for MQTT Dashboard public broker -#define TOPIC "" // MQTT topic +#define TOPIC1 "pressure" // MQTT topic +#define TOPIC2 "motion" -Queue<uint32_t, 6> messageQ; +Queue<float, 6> messageQ; +//Queue messageQ( 1, 5); +FSR fsr(PTB2, 10); + +//InterruptIn g_button_tap(SW2); +InterruptIn pressure_in(PTB2); // LED color control function void controlLED(color_t led_color) { @@ -36,15 +45,12 @@ } // Switch 2 interrupt handler -void sw2_ISR(void) { - messageQ.put((uint32_t*)22); +void on_tap(void) { + + messageQ.put(fsr.readFSRResistance()); + } -// Switch3 interrupt handler -void sw3_ISR(void) { - messageQ.put((uint32_t*)33); -} - // MQTT message arrived callback function void messageArrived(MQTT::MessageData& md) { MQTT::Message &message = md.message; @@ -69,9 +75,8 @@ // turn off LED controlLED(off); - // set SW2 and SW3 to generate interrupt on falling edge - switch2.fall(&sw2_ISR); - switch3.fall(&sw3_ISR); + //g_button_tap.fall(&on_tap); + pressure_in.fall(&on_tap); pc.printf("\r\n\r\nWelcome to the K64F MQTT Demo!\r\n"); pc.printf("\r\nAttempting connect to local network...\r\n"); @@ -115,15 +120,15 @@ else pc.printf("success\r\n"); - char* topic = TOPIC; + char* topic = TOPIC1; // subscribe to MQTT topic pc.printf("Subscribing to MQTT topic %s: ", topic); if ((rc = client.subscribe(topic, MQTT::QOS0, messageArrived)) != 0) pc.printf("failed: rc= %d\r\n", rc); - else pc.printf("success\r\n"); + MQTT::Message message; char buf[100]; @@ -134,22 +139,16 @@ message.payloadlen = strlen(buf)+1; while(true) { + osEvent switchEvent = messageQ.get(100); - if (switchEvent.value.v == 22 || switchEvent.value.v == 33) { - switch(switchEvent.value.v) { - case 22 : - sprintf(buf, "sw2"); - break; - case 33 : - sprintf(buf, "sw3"); - break; - } + if (switchEvent.value.v > 3000) + { + sprintf(buf, "message"); pc.printf("Publishing MQTT message: %.*s\r\n", message.payloadlen, (char*)message.payload); rc = client.publish(topic, message); client.yield(100); } - else { client.yield(100); }