hellomqttt to thingspeak mqtt and ifttt

Dependencies:   Servo MQTTPacket FP

Revision:
25:ca1b1098c77f
Parent:
24:d2e25cdf9084
--- a/main.cpp	Thu Mar 11 13:56:02 2021 +0000
+++ b/main.cpp	Wed May 05 14:48:01 2021 +0000
@@ -28,6 +28,784 @@
 
 //STARTED WITH IMPORT FROM HERE https://os.mbed.com/teams/mqtt/code/HelloMQTT/
 
+#include "mbed.h"
+#include "string"
+
+#define USE_LCD 1// change this to 1 to output messages to LCD instead of serial
+
+#if USE_LCD
+#include "C12832.h"
+C12832 lcd(p5, p7, p6, p8, p11);
+#define logMessage1  wait(2);lcd.cls();lcd.locate(0,3);lcd.printf
+#define logMessage2  wait(2);lcd.cls();lcd.locate(0,15);lcd.printf
+#else  //use serial printf
+#include "C12832.h"
+C12832 lcd(p5, p7, p6, p8, p11);
+#define logMessage1 pc.printf
+#define logMessage2 pc.printf
+#endif
+
+#define MQTTCLIENT_QOS2 1
+
+#define CLIENTID "JASON_BERRY_BOT" 
+//#define USERNAME "testuser"
+//#define PASSWORD "TUBNO9VEWTIJZ63R" //thingspeak
+
+#define USERNAME "token_2F8Fe1gxYIbVpZbr" //beebotte mqtt
+#define PASSWORD "token_2F8Fe1gxYIbVpZbr" //beebotte mqtt
+
+//#define HOSTNAME "54.157.36.210"  //thingspeak mqtt broker 15 sec limit on messages
+//#define HOSTNAME "5.196.95.208"     //mosquitto test broker no limit on messages
+#define HOSTNAME "54.221.205.80"      //beebotte broker, which can be triggered from iffft webhooks
+
+///STUDENT CHANGE YOUR NETWORK WIFI PASSWORD ETC HERE
+#define NETWORK_ID "HUAWEI P30 Pro"
+#define NETWORK_PASSWORD "4f54ed7ab852"
+     
+#include "easy-connect.h"
+#include "MQTTNetwork.h"
+#include "MQTTmbed.h"
+#include "MQTTClient.h"
+#include "MMA7660.h"
+#include "LM75B.h"
+#include "Servo.h"
+
+//funtions prototypes
+int boot_up(void);
+void messageArrived(MQTT::MessageData& md);
+void build_bots_AI_DI_SI_message(char *buf);
+void control_bots_AO_DO_SO_message(char *ser_bt_mqtt_rec_mes);
+
+//hardware mapping
+DigitalIn  up(p16);
+DigitalIn  down(p12);
+DigitalOut reset(p30);
+DigitalOut led1(LED1); //TEST DO
+DigitalOut led2(LED2); //SUPERLOOP
+DigitalOut led3(LED3); //MQTT
+DigitalOut led4(LED4); //SERIAL,BLUE,PC
+
+
+//Serial pc(USBTX, USBRX); setup in easy connect
+MMA7660 MMA(p28, p27); //I2C Accelerometer
+LM75B sensor(p28,p27); // Temperature
+Serial blue(p13,p14);  //tx, rx
+Servo s1(p21);
+Servo s2(p22);
+
+
+int arrivedcount = 0; //number of message received back from mqtt broker
+int version = 88; //version number used for test messages to mqtt
+
+//topics to subscribe
+////////////////////////////////////////////////////////////////////////////////////
+//thingspeak channel as topic  
+////////////////////////////////////////////////////////////////////////////////////   
+//char* publish_topic = "channels/1322442/publish/fields/field1/PUWBQJL3X64GWL2P";
+//char* subscribe_topic = "channels/1322442/subscribe/fields/field1/H0P76513L8Z3N555";
+
+//mosquitto mbed sample broker topic
+///////////////////////////////////////////////////////////////////////////////////
+//char* publish_topic = "mbed-sample/";
+//char* subscribe_topic = "mbed-sample/";
+
+//bee_botte mqtt topic
+///////////////////////////////////////////////////////////////////////////////////
+
+/////this is where you change your bot id..so maybe change to 011, 011do1, o11do2 etc...011so1
+char id[] = "010"; //bot id
+
+#define DO1  "010do1"
+#define DO2  "010do2"
+#define DO3  "010do3"
+#define DO4  "010do4"
+
+#define AO1  "010ao1"
+#define AO2  "010ao2"
+#define AO3  "010ao3"
+#define AO4  "010ao4"
+
+#define SO1  "010so1"
+//////end of changes required ///////////////////////////////////////////////////////////////////
+
+//beebotte topics
+char* publish_topic = "jason/text";
+char* subscribe_topic = "jason/text";
+char mqtt_message_received[255];
+char blue_message_received[255];
+char pc_message_received[255];
+
+//outputs variables you can assign these to whatever you want leds,servos
+long int ao1=0;
+long int ao2=0;
+long int ao3=0;
+long int ao4=0;
+
+long int do1=0;
+long int do2=0;
+long int do3=0;
+long int do4=0;
+
+char so1[20] = "..";
+
+//input variables
+float ai1=0;
+float ai2=0;
+float ai3=0;
+float ai4=0;
+
+int di1=0;
+int di2=0;
+int di3=0;
+int di4=0;
+
+char si1[20] = "..";
+
+int client_yielding = false;  //set yeilding for message to false at startup
+
+//100ms timer
+Ticker _100ms_ticker;
+int timer_100ms_count = 0;
+
+//this funtion runs every 100ms and increments timer_100ms_count by 1
+///////////////////////////////////////////////////////////////////////
+void _100ms_timer(void)
+{
+timer_100ms_count = timer_100ms_count+1;
+
+//stop overflow 1000 secs
+if(timer_100ms_count> 10000)
+    {
+    timer_100ms_count = 0;
+    }
+}
+
+// Simon's Watchdog code from
+// http://mbed.org/forum/mbed/topic/508/
+class Watchdog {
+public:
+// Load timeout value in watchdog timer and enable
+    void kick(float s) {
+        LPC_WDT->WDCLKSEL = 0x1;                // Set CLK src to PCLK
+        uint32_t clk = SystemCoreClock / 16;    // WD has a fixed /4 prescaler, PCLK default is /4
+        LPC_WDT->WDTC = s * (float)clk;
+        LPC_WDT->WDMOD = 0x3;                   // Enabled and Reset
+        kick();
+    }
+// reset the watchdog timer
+// by writing this required bit pattern
+    void kick() {
+        LPC_WDT->WDFEED = 0xAA;
+        LPC_WDT->WDFEED = 0x55;
+    }
+};
+
+// Setup the watchdog timer
+Watchdog wdt;
+ 
+/////////////////////////////////////start//////////////////////////////////////////
+int main(int argc, char* argv[])
+{
+int rc =0;
+int superloop_count = 0;  
+static int watchdog_count;
+
+//reset
+reset = 0;
+wait(1);
+reset = 1;
+wait(3);
+
+// turn off all leds
+led1=0;
+led2=0;
+led3=0;
+led4=0;
+
+//set servo to halfway
+s1 = 0.5;
+s2 = 0.5;
+
+// On reset, indicate a watchdog reset or a pushbutton reset on LED 4 or 3
+if ((LPC_WDT->WDMOD >> 2) & 1)
+     {
+     led4 = 1; 
+     watchdog_count = watchdog_count + 1; //not working
+     }
+else 
+     led3 = 1;
+        
+wait(1);
+
+// setup a 15 second timeout on watchdog timer hardware
+// needs to be longer than worst case main loop exection time
+wdt.kick(15.0);  
+
+// set up a function to play every 100 msec
+_100ms_ticker.attach(&_100ms_timer, 0.1);
+
+//serial comms at 115200 bluetooth at 9600
+pc.baud(115200); 
+blue.baud(9600);
+
+logMessage1("HelloMQTT: version is:%u Watchdog Resets:%u \r\n",
+                             version,watchdog_count);
+blue.printf("HelloMQTT: version is:%u Watchdog Resets:%u \r\n",
+                             version,watchdog_count);
+
+///calibrate servos
+//s1.calibrate(0.001, 45.0); 
+//s2.calibrate(0.001, 45.0); 
+
+//1.0 test if wifi wired
+// 1.0 set up a wifi connection
+///////////////////////////////////////////////////////////////////////////////////
+NetworkInterface* network = easy_connect(up); //press joystick at boot up for debug
+MQTTNetwork mqttNetwork(network);
+MQTT::Client<MQTTNetwork, Countdown> client(mqttNetwork);   
+
+if(ip_addr1 != 0)
+    {
+    logMessage2("IP address %s\r\n", ip_addr1);
+   
+    //2.0 set up a MQTT connection
+    ////////////////////////////////////////////////////////////////////
+    //MQTTNetwork mqttNetwork(network);
+    //MQTT::Client<MQTTNetwork, Countdown> client(mqttNetwork);
+    
+    char* hostname = HOSTNAME; //"54.157.36.210";  //"103.224.182.253"; // 
+    int port = 1883;
+    
+    //logMessage1("Conecting to %s:%d\r\n", hostname, port);
+    rc = mqttNetwork.connect(hostname, port);   
+    if (rc != 0)
+        logMessage2("rc from TCP connect is %d\r\n", rc);
+
+    //3.0 connect MQTT client
+    ///////////////////////////////////////////////////////////////////
+    MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
+    data.MQTTVersion = 3;
+    data.clientID.cstring = CLIENTID; //Make sure your robot name here..JASON_BERRY_BOT 
+    data.username.cstring = USERNAME; // token from beebotte or anything for thingsspeak
+    data.password.cstring = PASSWORD; // this thinspeak mqtt api found in thingspeak profiles
+    
+    if ((rc = client.connect(data)) != 0)
+        {
+        //logMessage2("rc MQTT connect is %d\r\n", rc);
+        if (rc == 5)
+            logMessage2("Bad MQTT password or username\r\n");
+        }
+    else
+        {    
+        //subscribe and read last message posted to mqtt
+        ////////////////////////////////////////////////    
+        //logMessage1("start of subscribe..\r\n");
+    
+        if ((rc = client.subscribe(subscribe_topic, MQTT::QOS0, messageArrived)) != 0)
+            logMessage2("rc MQTT subsc is %d\r\n", rc);
+        }
+    }
+ else
+    {
+    logMessage1("wifi unsucessful debug!!!");
+    rc = 1;
+    }
+//end of wifi and subscibe 
+///////////////////////////////////////////////////////////////////////    
+
+// clear lcd of startup messages
+lcd.cls();
+
+
+//superloop
+///////////////////////////////////////////////////////////////////////////////////////////////  
+while(1)
+    {
+    char mqtt_message_send[200];
+    
+    //update your outputs
+    /////////////////////////////////////////////
+    led1=do1;
+    // =do2;
+    // =do3;
+    // =do4;
+    
+    s1 = float(ao1)/255;  //a01 goes from 0 255 convert to 0.0 to 1.0 fo s1
+    s2 = float(ao2)/255;  //a01 goes from 0 255 convert to 0.0 to 1.0 fo s1
+    // =a03;
+    // =ao4;
+    
+    // =so1;
+    
+    
+    //update your inputs
+    /////////////////////////////////////////////
+    
+    //analog inputs
+    ///////////////////////////////////////////////////////////////////////////////
+    // ai1: assigned to temperature sensor,  ai2: accel_x
+    if (!sensor.open()) 
+        {
+        pc.printf("Temperature not device not detected!\n\r");
+        }    
+    else
+        {
+        ai1 = sensor.temp();
+        }
+
+    // ai2: assigned to acceration_x
+    if (!MMA.testConnection())    
+        {
+        pc.printf("accelormeter !! spelling j !!!\n\r");  
+        }
+    else
+        {
+        ai2 = MMA.x();    
+        ai3 = MMA.y();  
+        }
+
+    //ai4 tba
+    ai4=0;
+
+    //digital inputs
+    /////////////////////////////////////////////////////////////////////////////////
+    // di1: assigned to joystick down(pin 12)..
+    // note cannot use joystick fire used for bluetoothrx
+    // ai1: assigned to temperature sensor,  ai2: accel_x
+    di1 = down; 
+
+    //di12,3,4 tba
+    di2=0;
+    di3=0;
+    di4=0;
+
+    //string inputs
+    /////////////////////////////////////////////////////////////////////////////////
+    //si1 is a string meesage max 20chars, where boards can send text meesages to mqtt
+    snprintf(si1,20,"jhello world..xxxx");
+    
+    //end of setting inout and ouput variables ////////////////////////////////////
+    
+        
+    /////////////////////////////////////////////
+    //create a message with your ai,di and si
+    build_bots_AI_DI_SI_message(mqtt_message_send);
+        
+     
+    //logMessage1(".start of publish1 yeild=%u\r\n",client_yielding);
+    //wait(1);
+    
+    //publish message every 500 msecs to mqtt with all  ai,di and si data
+    //also as part of publish read back any messages form mqtt subscribe
+    
+    if(timer_100ms_count >= 5 && (rc == 0))  
+        {
+        //"kick" to reset watchdog timer and avoid a reset
+        wdt.kick();
+        
+   
+        timer_100ms_count = 0;
+        
+        //create a meesage with your ai,di and si
+        //build_bots_AI_DI_SI_message(buf);
+        
+        //publish and write to mqtt broker..this will then make its way to BEEBOTTE or thinspeak channel
+        ////////////////////////////////////////////////////////////////////////////////////
+        // note can only publish every 15 seconds to thingspeak mqtt, as often as you like to beebottte
+        MQTT::Message message;
+
+        // QoS 0
+        //sprintf(buf, "Hello World!  QoS 0 message from app version %f\r\n", version);
+        //sprintf(buf, "%u",arrivedcount); //version); //MMA.x());//test number is version
+        
+        message.qos = MQTT::QOS0;
+        message.retained = false; //false;
+        message.dup = false;
+        message.payload = (void*)mqtt_message_send;
+        message.payloadlen = strlen(mqtt_message_send)+1;
+        
+        //logMessage2("mqtt message: %s  :: lenght:%d \r\n",  message.payload, message.payloadlen);
+        rc = client.publish(publish_topic, message);
+        if (rc != 0)
+            {
+            logMessage2("rc MQTT publish error is %d %d\r\n", rc, message.payloadlen);
+            client.yield(10);
+            }
+        else
+            client.yield(10);
+        //logMessage2(".end of publish..\r\n");
+        } 
+       
+
+    //check if any comms from bluetooth, if so send last mqqt message received
+    if (blue.readable()) 
+      {
+       blue.gets(blue_message_received,255);   
+       
+       //send back response
+       blue.printf("%s\r\n",mqtt_message_received);
+       blue.printf("%s\r\n",mqtt_message_send);
+       
+       //check bluetooth message for drive commands
+       control_bots_AO_DO_SO_message(blue_message_received);  
+       
+       strcpy(mqtt_message_send,blue_message_received);
+       
+       //publish and write to mqtt broker..this will then make its way to BEEBOTTE or thinspeak channel
+       ////////////////////////////////////////////////////////////////////////////////////
+       // note can only publish every 15 seconds to thingspeak mqtt, as often as you like to beebottte
+       MQTT::Message message;
+
+       // QoS 0
+       //sprintf(buf, "Hello World!  QoS 0 message from app version %f\r\n", version);
+       //sprintf(buf, "%u",arrivedcount); //version); //MMA.x());//test number is version
+        
+       message.qos = MQTT::QOS0;
+       message.retained = false; //false;
+       message.dup = false;
+       message.payload = (void*)mqtt_message_send;
+       message.payloadlen = strlen(mqtt_message_send)+1;
+        
+       //logMessage2("mqtt message: %s  :: lenght:%d \r\n",  message.payload, message.payloadlen);
+       rc = client.publish(publish_topic, message);
+          if (rc != 0)
+                {
+                logMessage2("rc MQTT publish error is %d %d\r\n", rc, message.payloadlen);
+                client.yield(10);
+                }
+            else
+                client.yield(10);
+            //logMessage2(".end of publish..\r\n");
+
+
+        led4 = !led4;
+    }// end of bluetooth read  
+
+    //check if any comms from pc, if so send last mqqt message received      
+    if (pc.readable()) 
+      {
+       pc.gets(pc_message_received,255);     
+             
+       //send back response
+       pc.printf("%s\r\n",mqtt_message_received);
+       pc.printf("%s\r\n",mqtt_message_send);
+       
+       //check pc message for drive commands
+       control_bots_AO_DO_SO_message(pc_message_received);  
+       
+       strcpy(mqtt_message_send,pc_message_received);
+       //drive_command_for_another_bot = 0;     
+        
+       //publish and write to mqtt broker..this will then make its way to BEEBOTTE or thinspeak channel
+       ////////////////////////////////////////////////////////////////////////////////////
+       // note can only publish every 15 seconds to thingspeak mqtt, as often as you like to beebottte
+       MQTT::Message message;
+
+       // QoS 0
+       //sprintf(buf, "Hello World!  QoS 0 message from app version %f\r\n", version);
+       //sprintf(buf, "%u",arrivedcount); //version); //MMA.x());//test number is version
+        
+       message.qos = MQTT::QOS0;
+       message.retained = false; //false;
+       message.dup = false;
+       message.payload = (void*)mqtt_message_send;
+       message.payloadlen = strlen(mqtt_message_send)+1;
+        
+       //logMessage2("mqtt message: %s  :: lenght:%d \r\n",  message.payload, message.payloadlen);
+       rc = client.publish(publish_topic, message);
+       if (rc != 0)
+            {
+            logMessage2("rc MQTT publish error is %d %d\r\n", rc, message.payloadlen);
+            client.yield(10);
+            }
+       else
+            client.yield(10);
+   
+      //toggle serial message received  
+      led4 = !led4;
+      }//end of pc read 
+     
+     //toggle heartbeat every 1 loops, 
+     if(superloop_count > 1)
+        {
+        superloop_count = 0;    
+        led2 = !led2;
+        }
+      else
+        superloop_count++;   
+        
+       //diplay variables locally
+       /////////////////////////////////////// 
+       lcd.locate(0,1);    
+       //lcd.printf(mqtt_message_received,"Payload %s", (char*)ser_bt_mqtt_rec_mes);
+       lcd.printf("ao1:%ld,:%ld,:%ld,:%ld do1:%ld,%ld,%ld,%ld",ao1,ao2,ao3,ao4,do1,do2,do3,do4);//printf long int slider
+       lcd.locate(0,11);
+       lcd.printf("so1: %20s",so1);
+       //lcd.printf("do1:%ld, do2:%ld, do3:%ld, do4:%ld ",do1,do2,do3,do4);//printf long int slider
+       lcd.locate(0,21);                   
+       lcd.printf("count:%u ip:%s",arrivedcount,ip_addr1);   
+         
+     }//end of while(1)
+    
+}//end of main, note: no super loop here runs once at reset
+
+void control_bots_AO_DO_SO_message(char *ser_bt_mqtt_rec_mes)
+///////////////////////////////////////////////////////////////////////////////
+//
+// control, message in this format {"data":"do1=0","ispublic": true}
+//
+//
+///////////////////////////////////////////////////////////////////////////////
+{
+char *ptr_str;
+
+    
+//010do1: mapped to led1 on mbed
+if((ptr_str = strstr((char*)ser_bt_mqtt_rec_mes, DO1)) != NULL ) // "010do1"
+     { 
+     do1 = strtol(ptr_str+8,NULL,10);
+     } 
+if((ptr_str = strstr((char*)ser_bt_mqtt_rec_mes, DO2)) != NULL ) 
+     { 
+     do2 = strtol(ptr_str+8,NULL,10); 
+     }
+if((ptr_str = strstr((char*)ser_bt_mqtt_rec_mes, DO3)) != NULL ) 
+     { 
+     do3 = strtol(ptr_str+8,NULL,10);   
+     } 
+if((ptr_str = strstr((char*)ser_bt_mqtt_rec_mes, DO4)) != NULL ) 
+     { 
+     do4 = strtol(ptr_str+8,NULL,10);   
+     }  
+
+
+if((ptr_str = strstr((char*)ser_bt_mqtt_rec_mes, AO1)) != NULL ) 
+    { 
+    ao1 = strtol(ptr_str+8,NULL,10); // convert string to a long integer      
+    }
+if((ptr_str = strstr((char*)ser_bt_mqtt_rec_mes, AO2)) != NULL ) 
+    {
+    ao2 = strtol(ptr_str+8,NULL,10); // convert string to a long integer
+    }
+if((ptr_str = strstr((char*)ser_bt_mqtt_rec_mes, AO3)) != NULL ) 
+    {
+    ao3 = strtol(ptr_str+8,NULL,10); // convert string to a long integer
+    }
+if((ptr_str = strstr((char*)ser_bt_mqtt_rec_mes, AO4)) != NULL ) 
+    {
+    ao4 = strtol(ptr_str+8,NULL,10); // convert string to a long integer
+    }
+
+if((ptr_str = strstr((char*)ser_bt_mqtt_rec_mes, SO1)) != NULL ) 
+    {
+    strncpy(so1,ptr_str+8,20); //copy 20 chars from messsage to so1
+    }
+
+}
+     
+void build_bots_AI_DI_SI_message(char *buf)
+///////////////////////////////////////////////////////////////////////////////
+//
+//
+//
+///////////////////////////////////////////////////////////////////////////////
+{
+//build message store in buf
+sprintf(buf,"{\"data\":\"bot_id:%.3s, %5u\",\"%.3sai1\":%.1f,\"%.3sai2\":%.2f,\"%.3sai3\":%.2f,\"%.3sai4\":%.2f,\"%.3sdi1\":%u,\"%.3sdi2\":%u,\"%.3sdi3\":%u,\"%.3sdi4\":%u,\"%.3ssi1\":%.20s,\"is public\":true}",
+                                id,arrivedcount,id,ai1,id,ai2,id,ai3,id,ai4,id,di1,id,di2,id,di3,id,di4,id,si1);  
+}        
+        
+        
+/*int boot_up(void)
+{
+//set up a wifi connection
+//////////////////////////
+    
+//connect to wifi network and display ip address..display in serial
+NetworkInterface* network = easy_connect(up); //press joystick at boot up for debug
+    
+if(ip_addr1 != 0)
+     logMessage2("IP address %s\r\n", ip_addr1);
+else
+     logMessage1("wifi unsucessful debug!!!");
+   
+//set up a MQTT connection
+//////////////////////////
+    
+MQTTNetwork mqttNetwork(network);
+
+MQTT::Client<MQTTNetwork, Countdown> client(mqttNetwork);
+    
+char* hostname = HOSTNAME; //"54.157.36.210";  //"103.224.182.253"; // 
+int port = 1883;
+    
+logMessage1("Conecting to %s:%d\r\n", hostname, port);
+
+int rc = mqttNetwork.connect(hostname, port);
+if (rc != 0)
+        logMessage2("rc from TCP connect is %d\r\n", rc);
+
+//connect to client
+///////////////////
+MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
+data.MQTTVersion = 3;
+data.clientID.cstring = CLIENTID;       //can be any name,client just dont connect from two different client 
+data.username.cstring = USERNAME;          //with identical name,client
+data.password.cstring = PASSWORD;  // this thinspeak mqtt api found in thingspeak profiles
+    
+if ((rc = client.connect(data)) != 0)
+    logMessage2("rc MQTT connect is %d\r\n", rc);
+    
+//subscribe and read last message posted to mqtt
+////////////////////////////////////////////////    
+//note data received with handled above in  
+    
+ 
+logMessage1("start of subscribe..\r\n");
+if ((rc = client.subscribe(subscribe_topic, MQTT::QOS0, messageArrived)) != 0)
+    logMessage2("rc MQTT subsc is %d\r\n", rc);
+    
+   
+    return 0;
+} 
+
+void mqtt_message_publish(void)
+{
+  //while (1)
+  //       {
+             
+        // if(client_yielding == false)
+          //  {
+         //   logMessage1(".start of yeilding1\r\n");    
+         //   client.yield(100);
+         //   client_yielding = true;
+        //    }
+               
+         logMessage1(".start of publish1 yeild=%u\r\n",client_yielding);
+         //wait(1);
+         if(up == 0)       
+            {
+            //publish and write to mqtt broker..this will then make its way to thinspeak channel
+            ////////////////////////////////////////////////////////////////////////////////////
+            // note can only publish every 15 seconds to thingspeak mqtt 
+    
+            MQTT::Message message;
+
+            // QoS 0
+            char buf[100];
+            //sprintf(buf, "Hello World!  QoS 0 message from app version %f\r\n", version);
+            sprintf(buf, "%u",arrivedcount); //version); //MMA.x());//test number is version
+            message.qos = MQTT::QOS0;
+            message.retained = false; //false;
+            message.dup = false;
+            message.payload = (void*)buf;
+            message.payloadlen = strlen(buf)+1;
+                       
+           
+            logMessage1(".start of publish2\r\n");
+           //wait(1);
+            
+            int rc = client.publish(publish_topic , message);
+            //rc = client.publish(publish_topic , message);
+            
+            //wait(4);
+             //while (arrivedcount < 1)
+            client.yield(10);
+                
+            logMessage2(".end of publish..\r\n");  
+            }
+        else
+            {
+             client.yield(10);
+            }
+ not tested on this set up other fields possible
+    // QoS 1
+    sprintf(buf, "Hello World!  QoS 1 message from app version %f\r\n", version);
+    message.qos = MQTT::QOS1;
+    message.payloadlen = strlen(buf)+1;
+    rc = client.publish(publish_topic , message);
+    while (arrivedcount < 2)
+        client.yield(100);
+
+    // QoS 2
+    sprintf(buf, "Hello World!  QoS 2 message from app version %f\r\n", version);
+    message.qos = MQTT::QOS2;
+    message.payloadlen = strlen(buf)+1;
+    rc = client.publish(topic, message);
+    while (arrivedcount < 3)
+        client.yield(100);
+*/
+    // }//end of test while
+            
+     //unsubscribe 
+    ////////////////////////////////////////
+    /*
+    if ((rc = client.unsubscribe(subscribe_topic)) != 0)
+        logMessage1("rc from unsubscribe was %d\r\n", rc);
+
+    //  and disconnect from client
+    if ((rc = client.disconnect()) != 0)
+        logMessage1("rc from disconnect was %d\r\n", rc);
+        
+    //disconnect from wifi network
+    //////////////////////////////
+    //this would use a lot of current (mA) up battery life,
+    //if connecting and disconnecting to wifi each time you sent received a message
+    
+    mqttNetwork.disconnect();
+    wait(1);
+    
+    logMessage1("Version %d: finish %d msgs\r\n", version, arrivedcount);
+    logMessage2("IP address %s\r\n", ip_addr1);
+    
+}
+*/
+
+//   [FINITE_STATE_MACHINE_STATE] [STATE_SCALE_mod] [STATE_FACE_X_mod]<sp><CR><LF>
+
+//message received handler
+void messageArrived(MQTT::MessageData& md)
+{
+    MQTT::Message &message = md.message;
+    //logMessage1("Message arrived: qos %d, retained %d, dup %d, packetid %d\r\n", message.qos, message.retained, message.dup, message.id);
+    //logMessage2("Payload %.*s  :count %u\r\n", message.payloadlen, (char*)message.payload,arrivedcount);
+    //blue.printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\r\n", message.qos, message.retained, message.dup, message.id);
+    //blue.printf("Payload %.*s  :count %u\r\n", message.payloadlen, (char*)message.payload,arrivedcount);
+    
+
+    
+     sprintf(mqtt_message_received,"Payload %.*s  :count %u\r\n", message.payloadlen, (char*)message.payload,arrivedcount);
+    //sprintf(str, "hello %s", "world");
+    //snprintf(str, 255, "hello %s",  "world")
+     
+    ++arrivedcount;
+    
+    control_bots_AO_DO_SO_message(mqtt_message_received);
+    
+    //if message received from mqtt then do not need to send on outputs
+    //drive_command_for_another_bot = 0;     
+    
+    
+    /*
+    //led1 =1; led1=0;
+    if( strstr((char*)message.payload, "led1=1") != NULL ) {
+            led1=1;
+           // lcd.cls();
+           // lcd.locate(0,3);
+          //  lcd.printf("led 1");
+        }
+    if( strstr((char*)message.payload, "led1=0") != NULL ) {
+            led1=0;
+           // lcd.cls();
+           // lcd.locate(0,3);
+           // lcd.printf("led 0");
+        }
+    */    
+    client_yielding = false; //can yeild for a message again
+    led3 = !led3;
+    
+}
+
 ///////////////////////////////////////////////////////////////////
 //IFFT TO THINSPEAK TO THINSPEAK MQTT BROKER TO MBED/ESP8266 CLIENT
 ///////////////////////////////////////////////////////////////////
@@ -47,10 +825,10 @@
 //        check thingspeak channel to see if 89 is plotted
 
 // Step3: FIND MQTT.THINGSPEAK.COM FIXED IP ADDRESS
-//        Find out fixed ip address of mqtt.thinspeak.com using windons command: tracert mqtt.thinspeak.com
-//        at time of testing it was 34.231.253.113...doesnt seem to change much :-) but check it.
+//        Find out fixed ip address of mqtt.thingspeak.com using windons command: tracert mqtt.thingspeak.com
+//        at time of testing it w as 34.231.253.113...doesnt seem to change much :-) but check it.
 
-// Step4: SETUP A DESK MQTT CLIENT TO TEST THINGSPEAK MQTT BROKER
+// Step3.1: SETUP A DESK MQTT CLIENT TO TEST THINGSPEAK MQTT BROKER
 //        Set up a desktop client to test thingspeak mqtt broker, I used MQTT.fx 1.71 from
 //        https://mqttfx.jensd.de/index.php/download.
 //
@@ -60,7 +838,7 @@
 //        In summary from mathswork,
 //        click in droplist and select new profile, 
 //        broker type: select mqtt broker, 
-//        broker address: put in ip address for mqtt.thingspeak.com that you found in step 2, 34.231.253.113 
+//        broker address: put in ip address for mqtt.thingspeak.com that you found in step 2,54.157.36.210  ..(old 34.231.253.113 
 //        broker port: is 1883, 
 //        client id: anything, 
 //        username: anything
@@ -74,6 +852,8 @@
 //        next put some number in field below.. then press publish go to thingspeak and check channel for plot update,
 //        put another number in field and publish, again check thinspeak channel..hopefully you see data
 //
+//        can also use Mosquitto test broker,test.mosquitto.org [5.196.95.208], topic mbed-sample/
+//     
 //        SUBSCRIBE
 //        Click on subscribe tab and enter channels/1322442/subscribe/fields/field1/H0P76513L8Z3N555, 
 //        click subscribe 
@@ -81,8 +861,10 @@
 //        Go to browser and enter some channel data as step 3, 
 //        https://api.thingspeak.com/update?api_key=PUWBQJL3X64GWL2P&field1=67
 //        you should see message in mqtt panel with data 67
-
-
+//
+//        Can also suscribe use mosquitto test broker topic mbed-sample
+//
+//
 // Step4: CONFIGURE THE CODE WITH WIFI SETTINGS, DEBUG, PINS ETC
 //        In the mbed_app.json file change as required, I wired esp8266 rx to mbed tx pin9, esp8266 tx to mbed rx pin 10        
 //        "esp8266-tx": {
@@ -101,11 +883,16 @@
 //
 //       If you want to see all the at commands for esp8266, then select debug true in mbed_app.json file
 //       pretty cool to see all the messages and good obviously for debug.
-
+//
+// step4.1: UPDATE LINE 86 in easy-connect.h with netwrok details as follows,
+//          in easy connect folder, then mcr20a-rf-driver folder...should see it    
+//          connect_success = esp.connect("eir89748912-2.4G","28eb111b");
+//          ----------------------------------------------------------------
+//
 // Step 5: COMPILE
 //         Compile the code should have no errors
 //         famous last words !!!!
-
+//
 // Step 6: WIRE UP ESP8266 TO MBED
 //         Wire up the ESP8266 to mbed see following for pinouts esp8266 h
 //         https://circuits4you.com/2016/12/14/esp8266-pin-diagram/
@@ -169,166 +956,13 @@
 
 //////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
-#include "mbed.h"
- // change this to 1 to output messages to LCD instead of serial
-#define USE_LCD 0
-
-#if USE_LCD
-#include "C12832.h"
-
-// the actual pins are defined in mbed_app.json and can be overridden per target
-C12832 lcd(LCD_MOSI, LCD_SCK, LCD_MISO, LCD_A0, LCD_NCS);
-
-#define logMessage lcd.cls();lcd.printf
-
-#else  //use serial printf
-
-#define logMessage pc.printf
-Serial pc(USBTX, USBRX);
-#endif
-
-#define MQTTCLIENT_QOS2 1
-
-#include "easy-connect.h"
-#include "MQTTNetwork.h"
-#include "MQTTmbed.h"
-#include "MQTTClient.h"
-
-int arrivedcount = 0; //number of message received back from mqtt broker
-
-//message received handler
-void messageArrived(MQTT::MessageData& md)
-{
-    MQTT::Message &message = md.message;
-    logMessage("Message arrived: qos %d, retained %d, dup %d, packetid %d\r\n", message.qos, message.retained, message.dup, message.id);
-    logMessage("Payload %.*s\r\n", message.payloadlen, (char*)message.payload);
-    ++arrivedcount;
-    
-}
-
-
-int main(int argc, char* argv[])
-{
-    int version = 111; //version number used for test messages to mqtt
-   
-    // mqtt publish and subscribe topics linked to mqtt.thinspeak.com, channel api 
-    // and channel read(suscribe) and write(publish) api...these can be used in any mqtt client conection..
-    // for pc testing could use mqtt.fx
-    // the following mathswork shows how to set up a desktop client to test thinspeak channel
-    // https://uk.mathworks.com/help/thingspeak/use-desktop-mqtt-client-to-publish-to-a-channel.html#d122e3819
-    
-    char* publish_topic = "channels/1322442/publish/fields/field1/PUWBQJL3X64GWL2P";
-    char* subscribe_topic = "channels/1322442/subscribe/fields/field1/H0P76513L8Z3N555";
-    
-    //serial comms at 115200 
-    pc.baud(115200); 
-    
-    logMessage("HelloMQTT: version is %u\r\n", version);
-
-    //set up a wifi connection
-    //////////////////////////
-    
-    //connect to wifi network and display ip address..display in serial
-    NetworkInterface* network = easy_connect(true);
-    if (!network) {
-        return -1;
-    }
-    
-    //set up a MQTT connection
-    //////////////////////////
-    
-    MQTTNetwork mqttNetwork(network);
-
-    MQTT::Client<MQTTNetwork, Countdown> client(mqttNetwork);
-    
-    // fixed ip for mqtt broker at mqtt.thingspeak.com found using 
-    // windows command: tracert mqtt.thinspeak.com ..note will change keep an eye on it
-    char* hostname =  "34.231.253.113"; 
-    int port = 1883;
-    
-    logMessage("Connecting to %s:%d\r\n", hostname, port);
-    int rc = mqttNetwork.connect(hostname, port);
-    if (rc != 0)
-        logMessage("rc from TCP connect is %d\r\n", rc);
+// mqtt publish and subscribe topics linked to mqtt.thinspeak.com, channel api 
+// and channel read(suscribe) and write(publish) api...these can be used in any mqtt client conection..
+// for pc testing could use mqtt.fx
+// the following mathswork shows how to set up a desktop client to test thinspeak channel
+// https://uk.mathworks.com/help/thingspeak/use-desktop-mqtt-client-to-publish-to-a-channel.html#d122e3819
 
-    //connect to client
-    MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
-    data.MQTTVersion = 3;
-    data.clientID.cstring = "mbed-sample";       //can be any name,client just dont connect from two different client 
-    data.username.cstring = "testuser";          //with identical name,client
-    data.password.cstring = "HXPY4BZ5Z0OPAH0W";  // this thinspeak mqtt api found in thingspeak profiles
-    
-    if ((rc = client.connect(data)) != 0)
-        logMessage("rc from MQTT connect is %d\r\n", rc);
-    
-    //subscribe and read last message posted to mqtt
-    ////////////////////////////////////////////////    
-    //note data received with handled above in  
-    if ((rc = client.subscribe(subscribe_topic, MQTT::QOS0, messageArrived)) != 0)
-        logMessage("rc from MQTT subscribe is %d\r\n", rc);
-        
-    
-    //debug test message can be turned off once happy with functioning    
-    logMessage("test point 1..end of subscribe..\r\n");
-    
-    //publish and write to mqtt broker..this will then make its way to thinspeak channel
-    ////////////////////////////////////////////////////////////////////////////////////
-    // note can only publish every 15 seconds to thingspeak mqtt 
-    
-    MQTT::Message message;
-
-    // QoS 0
-    char buf[100];
-    //sprintf(buf, "Hello World!  QoS 0 message from app version %f\r\n", version);
-    sprintf(buf, "%u",version);//test number is version
-    message.qos = MQTT::QOS0;
-    message.retained = false; //false;
-    message.dup = false;
-    message.payload = (void*)buf;
-    message.payloadlen = strlen(buf)+1;
-     
-    logMessage("test point 2..start of publish..\r\n");
-    rc = client.publish(publish_topic , message);
-    while (arrivedcount < 1)
-        client.yield(100);
-    logMessage("test point 3...end of publish..\r\n");  
-    
-/* not tested on this set up other fields possible
-    // QoS 1
-    sprintf(buf, "Hello World!  QoS 1 message from app version %f\r\n", version);
-    message.qos = MQTT::QOS1;
-    message.payloadlen = strlen(buf)+1;
-    rc = client.publish(publish_topic , message);
-    while (arrivedcount < 2)
-        client.yield(100);
-
-    // QoS 2
-    sprintf(buf, "Hello World!  QoS 2 message from app version %f\r\n", version);
-    message.qos = MQTT::QOS2;
-    message.payloadlen = strlen(buf)+1;
-    rc = client.publish(topic, message);
-    while (arrivedcount < 3)
-        client.yield(100);
-*/
-
-    //unsubscribe and disconnect from client
-    ////////////////////////////////////////
-    if ((rc = client.unsubscribe(subscribe_topic)) != 0)
-        logMessage("rc from unsubscribe was %d\r\n", rc);
-
-    if ((rc = client.disconnect()) != 0)
-        logMessage("rc from disconnect was %d\r\n", rc);
-        
-    //disconnect from wifi network
-    //////////////////////////////
-    //this would use a lot of current (mA) up battery life,
-    //if connecting and disconnecting to wifi each time you sent received a message
-    
-    mqttNetwork.disconnect();
-    wait(1);
-    
-    logMessage("Version %d: finish %d msgs\r\n", version, arrivedcount);
-
-    return 0;
-    
-}//end of main, note: no super loop here runs once at reset
+//roborealm send
+//{"data":"bot_id:110","010ao1":[MOUSE_X],"010so1":[JASONS_S01],"ispublic":true}<CR><LF>
+//roborealm receive
+//{"data":"bot_id:[MESSAGE_ID], [MESSAGE_COUNT]","[AI_1_BOT]ai1":[AI_1_VALUE],"[AI_2_BOT]ai2":[AI_2_VALUE],"[AI_3_BOT]ai3":[AI_3_VALUE],"[AI_4_BOT]ai4":[AI_4_VALUE],"[DI_1_BOT]di1":[DI_1_VALUE],"[DI_2_BOT]di2":[DI_2_VALUE],"[DI_3_BOT]di3":[DI_3_VALUE],"[DI_4_BOT]di4":[DI_4_VALUE],"[SI_1_BOT]si1":[SI_1_VALUE],
\ No newline at end of file