PGO6

Dependencies:   MQTT

Files at this revision

API Documentation at this revision

Comitter:
s0130594
Date:
Thu Nov 14 15:07:12 2019 +0100
Parent:
5:2ce5049b9c14
Commit message:
Added MQTT functionality

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Thu Oct 31 15:20:07 2019 +0100
+++ b/main.cpp	Thu Nov 14 15:07:12 2019 +0100
@@ -169,9 +169,8 @@
     led3=1;
     InterruptIn button(USER_BUTTON);
     button.fall(callback(&button1_onpressed_cb));
-    //multiclick_state = 0;
     pc.printf("Print print \n");
-    topic = "IGNISVOTESPOSTING";
+    topic = "IGNISVOTESPOSTINGBETA";
     NetworkInterface* network = easy_connect(true);
     if (!network) {
         pc.printf("Je netwerk is kapot");
@@ -188,34 +187,45 @@
     int rc = mqttNetwork.connect(hostname, port);
     if (rc != 0)
         pc.printf("rc from TCP connect is %d\r\n", rc);
+    pc.printf("result van connecten %d\r\n", rc);
 
-    // QoS 2
-    MQTT::Message message;
-    char buf[100];
-    sprintf(buf, "Hello World!  QoS 2 message from app version %f\r\n", 1.0);
-    message.qos = MQTT::QOS2;
-    message.payload = (void*)buf;
-    message.payloadlen = strlen(buf)+1;
-    rc = client.publish(topic, message);
-    while (arrivedcount < 1)
-        client.yield(100);
+    MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
+    data.MQTTVersion = 3;
+    data.clientID.cstring = "mbed-sample";
+    data.username.cstring = "testuser";
+    data.password.cstring = "testpassword";
+    if ((rc = client.connect(data)) != 0)
+        pc.printf("rc from MQTT connect is %d\r\n", rc);
  
-    if ((rc = client.unsubscribe(topic)) != 0)
-        pc.printf("rc from unsubscribe was %d\r\n", rc);
- 
-    if ((rc = client.disconnect()) != 0)
-        pc.printf("rc from disconnect was %d\r\n", rc);
- 
-    mqttNetwork.disconnect();
-    pc.printf("Version %.2f: finish %d msgs\r\n", 1111, arrivedcount);
+    pc.printf("rc from MQTT connect is %d\r\n", rc);
 
     while(true){
         if(disp){
             // Toon eindtotaal
             pc.printf("Er is zoveel keer geklikt: %d \n",button_count);
             disp=false;
-            
+
+            // QoS 2
+            MQTT::Message message;
+            char buf[100];
+            sprintf(buf, "Er is %d keer geklikt",button_count);
+            message.qos = MQTT::QOS1;
+            message.retained = false;
+            message.dup = false;
+            message.payload = (void*)buf;
+            message.payloadlen = strlen(buf)+1;
+            pc.printf("ik ga nu publishen");
+            rc = client.publish(topic, message);
         }
     }
+
+    pc.printf("result van publishen %d\r\n", rc);
+    if ((rc = client.unsubscribe(topic)) != 0)
+        pc.printf("rc from unsubscribe was %d\r\n", rc);
+
+    if ((rc = client.disconnect()) != 0)
+        pc.printf("rc from disconnect was %d\r\n", rc);
+    pc.printf("Version %.2f: finish %d msgs\r\n", 1111, arrivedcount);
+    mqttNetwork.disconnect();
     return 0;
 }