Ryan Feeney / MBED-MQTT

Working example code TCP 1883.

Dependencies:   EthernetInterface MQTT mbed-rtos mbed

Fork of K64F-RTOS-MQTT-Example by Mike Baylis

Files at this revision

API Documentation at this revision

Comitter:
ryanqfeeney
Date:
Mon Mar 20 17:10:21 2017 +0000
Parent:
1:9e3ca45e51b5
Commit message:
Working with board and node server.; Change the host to the local; ip of the computer running the server

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Sat Mar 18 19:54:58 2017 +0000
+++ b/main.cpp	Mon Mar 20 17:10:21 2017 +0000
@@ -3,9 +3,8 @@
 #include "MQTTEthernet.h"
 #include "rtos.h"
 #include "k64f.h"
-
 // connect options for MQTT broker
-#define BROKER "broker.hivemq.com"   // MQTT broker URL
+#define BROKER "192.168.0.29"   // MQTT broker URL
 #define PORT 1883                           // MQTT broker port number
 #define CLIENTID "0002F7F000"      // use K64F MAC address without colons
 #define USERNAME ""                         // not required for MQTT Dashboard public broker 
@@ -13,7 +12,8 @@
 #define TOPIC "testeroo"                    // MQTT topic
 
 Queue<uint32_t, 6> messageQ;
-
+bool flash = false;
+int thresh = 3;
 // LED color control function
 void controlLED(color_t led_color) {
     switch(led_color) {
@@ -50,19 +50,37 @@
     MQTT::Message &message = md.message;
     pc.printf("Receiving MQTT message:  %.*s\r\n", message.payloadlen, (char*)message.payload);
     
-    if (message.payloadlen == 3) {
+   // if (message.payloadlen == 3) {
         if (strncmp((char*)message.payload, "red", 3) == 0)
             controlLED(red);
         
-        else if(strncmp((char*)message.payload, "grn", 3) == 0)
+        else if(strncmp((char*)message.payload, "add", 3) == 0)
+            {controlLED(off);
             controlLED(green);
+            wait(.1);
+            controlLED(off);}
         
         else if(strncmp((char*)message.payload, "blu", 3) == 0)
             controlLED(blue);
         
         else if(strncmp((char*)message.payload, "off", 3) == 0)
             controlLED(off);
-    }        
+            
+        else if(strncmp((char*)message.payload, "b1o", 3) == 0)
+            flash = true;
+        else if(strncmp((char*)message.payload, "b1u", 3) == 0)
+            {flash = false;
+            controlLED(off);
+            controlLED(blue);
+            wait(.1);
+            controlLED(off);}
+        else if(strncmp((char*)message.payload, "new", 3)==0)
+            {controlLED(off);
+            controlLED(blue);
+            wait(.1);
+            controlLED(off);}
+        
+  //  }        
 }
 
 int main() {
@@ -103,6 +121,8 @@
     else
         pc.printf("success\r\n");
     
+    
+    //client starts building connection request to the broker
     MQTTPacket_connectData data = MQTTPacket_connectData_initializer;       
     data.MQTTVersion = 3;
     data.clientID.cstring = CLIENTID;
@@ -135,26 +155,43 @@
     message.dup = false;
     message.payload = (void*)buf;
     message.payloadlen = strlen(buf)+1;
+    int counter = 0;
     
     while(true) {
         osEvent switchEvent = messageQ.get(100);
         
+
+        if(flash){
+            controlLED(red);
+            wait(.25);
+            controlLED(off);
+            wait(.25);
+        }
+        
         if (switchEvent.value.v == 22 || switchEvent.value.v == 33) {
             switch(switchEvent.value.v) {
                 case 22 :
-                    sprintf(buf, "sw2");
+                    if(counter == thresh){sprintf(buf, "b1o");}
+                    else{sprintf(buf, "add");}
+                    counter++;
                     break;
                 case 33 :
-                    sprintf(buf, "sw3");
+                    
+                    if(counter == (thresh+1)){sprintf(buf, "b1u");}
+                    else{sprintf(buf, "new");}
+                    if(counter > 0){counter--;}
                     break;
             }
+
+            
             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);
         }           
     }
-}
+}
\ No newline at end of file