Cambridge Hackathon / Mbed OS MQTT-Python-Demo

Dependencies:   C12832 MQTT

Files at this revision

API Documentation at this revision

Comitter:
Jenny Plunkett
Date:
Fri Nov 17 15:59:59 2017 -0600
Parent:
1:bc10688f7226
Child:
3:fa6eba727624
Commit message:
Blank main for tutorial, and working main

Changed in this revision

broker/broker.py Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
working/main_working.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/broker/broker.py	Fri Nov 17 15:59:59 2017 -0600
@@ -0,0 +1,41 @@
+import paho.mqtt.client as paho
+import socket
+
+# https://os.mbed.com/teams/mqtt/wiki/Using-MQTT#python-client
+
+# MQTT broker hosted on local machine
+mqttc = paho.Client()
+
+# Settings for connection
+host = "IP_ADDRESS_HERE"
+topic= "Mbed/#"
+
+# Callbacks
+def on_connect(self, mosq, obj, rc):
+    print("Connected rc: " + str(rc))
+
+def on_message(mosq, obj, msg):
+    print("[Received] Topic: " + msg.topic + ", Message: " + str(msg.payload) + "\n");
+
+def on_subscribe(mosq, obj, mid, granted_qos):
+    print("Subscribed OK")
+
+def on_unsubscribe(mosq, obj, mid, granted_qos):
+    print("Unsubscribed OK")
+
+# Set callbacks
+mqttc.on_message = on_message
+mqttc.on_connect = on_connect
+mqttc.on_subscribe = on_subscribe
+mqttc.on_unsubscribe = on_unsubscribe
+
+# Connect and subscribe
+print "Your IP address is:", socket.gethostbyname(socket.gethostname())
+print("Connecting to " + host + "/" + topic)
+mqttc.connect(host, port=1883, keepalive=60)
+mqttc.subscribe(topic, 0)
+
+# Loop forever, receiving messages
+mqttc.loop_forever()
+
+print("rc: " + str(rc))
--- a/main.cpp	Tue Nov 14 15:17:55 2017 -0600
+++ b/main.cpp	Fri Nov 17 15:59:59 2017 -0600
@@ -21,7 +21,7 @@
 // GLOBAL VARIABLES HERE
 
 
-// FUNCTION DEFINTIONS HERE
+// FUNCTION DEFINITIONS HERE
 
 
 int main() {
--- a/working/main_working.h	Tue Nov 14 15:17:55 2017 -0600
+++ b/working/main_working.h	Fri Nov 17 15:59:59 2017 -0600
@@ -22,8 +22,11 @@
 
 C12832  lcd(PE_14, PE_12, PD_12, PD_11, PE_9);
 OdinWiFiInterface wifi;
+InterruptIn button(PF_2);
+volatile bool publish = false;
+volatile int message_num = 0;
 
-// FUNCTION DEFINTIONS HERE
+// FUNCTION DEFINITIONS HERE
 
 void lcd_print(const char* message) {
     lcd.cls();
@@ -31,6 +34,21 @@
     lcd.printf(message);
 }
 
+void messageArrived(MQTT::MessageData& md) {
+    MQTT::Message &message = md.message;
+    char msg[300];
+    sprintf(msg, "Message arrived: QoS%d, retained %d, dup %d, packetID %d\r\n", message.qos, message.retained, message.dup, message.id);
+    lcd_print(msg);
+    wait_ms(1000);
+    char payload[300];
+    sprintf(payload, "Payload %.*s\r\n", message.payloadlen, (char*)message.payload);
+    lcd_print(payload);
+}
+
+void publish_message() {
+    publish = true;
+}
+
 int main() {
 
     // MAIN CODE HERE
@@ -41,13 +59,47 @@
         lcd_print("Connection error.");
         return -1;
     }
+
+    NetworkInterface* net = &wifi;
+    MQTTNetwork mqttNetwork(net);
+    MQTT::Client<MQTTNetwork, Countdown> client(mqttNetwork);
+
+    const char* host = "IP_ADDRESS_HERE";
+    const char* topic = "Mbed";
+    lcd_print("Connecting to MQTT network...");
+    int rc = mqttNetwork.connect(host, 1883);
+    if (rc != 0) {
+        lcd_print("Connection error.");
+        return -1;
+    }
     lcd_print("Successfully connected!");
 
+    MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
+    data.MQTTVersion = 3;
+    data.clientID.cstring = "connect-cloud-board";
+    client.connect(data);
+    client.subscribe(topic, MQTT::QOS0, messageArrived);
+
+    button.fall(&publish_message);
+
     while (true) {
 
         // WHILE LOOP CODE HERE
 
-        
+        if (publish) {
+            message_num++;
+            MQTT::Message message;
+            char buff[100];
+            sprintf(buff, "QoS0 Hello, Python! #%d", message_num);
+            message.qos = MQTT::QOS0;
+            message.retained = false;
+            message.dup = false;
+            message.payload = (void*) buff;
+            message.payloadlen = strlen(buff) + 1;
+            rc = client.publish(topic, message);
+            client.yield(100);
+            publish = false;
+        }
 
     }