hellomqttt to thingspeak mqtt and ifttt

Dependencies:   Servo MQTTPacket FP

Committer:
jasonberry
Date:
Wed May 05 14:48:01 2021 +0000
Revision:
25:ca1b1098c77f
Parent:
24:d2e25cdf9084
TEST

Who changed what in which revision?

UserRevisionLine numberNew contents of line
icraggs 1:a1d5c7a6acbc 1 /*******************************************************************************
icraggs 17:0811bdbdd78a 2 * Copyright (c) 2014, 2015 IBM Corp.
icraggs 1:a1d5c7a6acbc 3 *
icraggs 1:a1d5c7a6acbc 4 * All rights reserved. This program and the accompanying materials
icraggs 1:a1d5c7a6acbc 5 * are made available under the terms of the Eclipse Public License v1.0
icraggs 1:a1d5c7a6acbc 6 * and Eclipse Distribution License v1.0 which accompany this distribution.
icraggs 1:a1d5c7a6acbc 7 *
icraggs 1:a1d5c7a6acbc 8 * The Eclipse Public License is available at
icraggs 1:a1d5c7a6acbc 9 * http://www.eclipse.org/legal/epl-v10.html
icraggs 1:a1d5c7a6acbc 10 * and the Eclipse Distribution License is available at
icraggs 1:a1d5c7a6acbc 11 * http://www.eclipse.org/org/documents/edl-v10.php.
icraggs 1:a1d5c7a6acbc 12 *
icraggs 1:a1d5c7a6acbc 13 * Contributors:
icraggs 1:a1d5c7a6acbc 14 * Ian Craggs - initial API and implementation and/or initial documentation
icraggs 17:0811bdbdd78a 15 * Ian Craggs - make sure QoS2 processing works, and add device headers
icraggs 1:a1d5c7a6acbc 16 *******************************************************************************/
Jan Jongboom 20:49c9daf2b0ff 17
icraggs 2:638c854c0695 18 /**
jasonberry 24:d2e25cdf9084 19 original ported form here https://os.mbed.com/teams/mqtt/wiki/Using-MQTT
jasonberry 24:d2e25cdf9084 20
icraggs 2:638c854c0695 21 This is a sample program to illustrate the use of the MQTT Client library
icraggs 2:638c854c0695 22 on the mbed platform. The Client class requires two classes which mediate
icraggs 2:638c854c0695 23 access to system interfaces for networking and timing. As long as these two
icraggs 2:638c854c0695 24 classes provide the required public programming interfaces, it does not matter
icraggs 2:638c854c0695 25 what facilities they use underneath. In this program, they use the mbed
icraggs 2:638c854c0695 26 system libraries.
icraggs 2:638c854c0695 27 */
Jan Jongboom 20:49c9daf2b0ff 28
jasonberry 24:d2e25cdf9084 29 //STARTED WITH IMPORT FROM HERE https://os.mbed.com/teams/mqtt/code/HelloMQTT/
jasonberry 24:d2e25cdf9084 30
jasonberry 25:ca1b1098c77f 31 #include "mbed.h"
jasonberry 25:ca1b1098c77f 32 #include "string"
jasonberry 25:ca1b1098c77f 33
jasonberry 25:ca1b1098c77f 34 #define USE_LCD 1// change this to 1 to output messages to LCD instead of serial
jasonberry 25:ca1b1098c77f 35
jasonberry 25:ca1b1098c77f 36 #if USE_LCD
jasonberry 25:ca1b1098c77f 37 #include "C12832.h"
jasonberry 25:ca1b1098c77f 38 C12832 lcd(p5, p7, p6, p8, p11);
jasonberry 25:ca1b1098c77f 39 #define logMessage1 wait(2);lcd.cls();lcd.locate(0,3);lcd.printf
jasonberry 25:ca1b1098c77f 40 #define logMessage2 wait(2);lcd.cls();lcd.locate(0,15);lcd.printf
jasonberry 25:ca1b1098c77f 41 #else //use serial printf
jasonberry 25:ca1b1098c77f 42 #include "C12832.h"
jasonberry 25:ca1b1098c77f 43 C12832 lcd(p5, p7, p6, p8, p11);
jasonberry 25:ca1b1098c77f 44 #define logMessage1 pc.printf
jasonberry 25:ca1b1098c77f 45 #define logMessage2 pc.printf
jasonberry 25:ca1b1098c77f 46 #endif
jasonberry 25:ca1b1098c77f 47
jasonberry 25:ca1b1098c77f 48 #define MQTTCLIENT_QOS2 1
jasonberry 25:ca1b1098c77f 49
jasonberry 25:ca1b1098c77f 50 #define CLIENTID "JASON_BERRY_BOT"
jasonberry 25:ca1b1098c77f 51 //#define USERNAME "testuser"
jasonberry 25:ca1b1098c77f 52 //#define PASSWORD "TUBNO9VEWTIJZ63R" //thingspeak
jasonberry 25:ca1b1098c77f 53
jasonberry 25:ca1b1098c77f 54 #define USERNAME "token_2F8Fe1gxYIbVpZbr" //beebotte mqtt
jasonberry 25:ca1b1098c77f 55 #define PASSWORD "token_2F8Fe1gxYIbVpZbr" //beebotte mqtt
jasonberry 25:ca1b1098c77f 56
jasonberry 25:ca1b1098c77f 57 //#define HOSTNAME "54.157.36.210" //thingspeak mqtt broker 15 sec limit on messages
jasonberry 25:ca1b1098c77f 58 //#define HOSTNAME "5.196.95.208" //mosquitto test broker no limit on messages
jasonberry 25:ca1b1098c77f 59 #define HOSTNAME "54.221.205.80" //beebotte broker, which can be triggered from iffft webhooks
jasonberry 25:ca1b1098c77f 60
jasonberry 25:ca1b1098c77f 61 ///STUDENT CHANGE YOUR NETWORK WIFI PASSWORD ETC HERE
jasonberry 25:ca1b1098c77f 62 #define NETWORK_ID "HUAWEI P30 Pro"
jasonberry 25:ca1b1098c77f 63 #define NETWORK_PASSWORD "4f54ed7ab852"
jasonberry 25:ca1b1098c77f 64
jasonberry 25:ca1b1098c77f 65 #include "easy-connect.h"
jasonberry 25:ca1b1098c77f 66 #include "MQTTNetwork.h"
jasonberry 25:ca1b1098c77f 67 #include "MQTTmbed.h"
jasonberry 25:ca1b1098c77f 68 #include "MQTTClient.h"
jasonberry 25:ca1b1098c77f 69 #include "MMA7660.h"
jasonberry 25:ca1b1098c77f 70 #include "LM75B.h"
jasonberry 25:ca1b1098c77f 71 #include "Servo.h"
jasonberry 25:ca1b1098c77f 72
jasonberry 25:ca1b1098c77f 73 //funtions prototypes
jasonberry 25:ca1b1098c77f 74 int boot_up(void);
jasonberry 25:ca1b1098c77f 75 void messageArrived(MQTT::MessageData& md);
jasonberry 25:ca1b1098c77f 76 void build_bots_AI_DI_SI_message(char *buf);
jasonberry 25:ca1b1098c77f 77 void control_bots_AO_DO_SO_message(char *ser_bt_mqtt_rec_mes);
jasonberry 25:ca1b1098c77f 78
jasonberry 25:ca1b1098c77f 79 //hardware mapping
jasonberry 25:ca1b1098c77f 80 DigitalIn up(p16);
jasonberry 25:ca1b1098c77f 81 DigitalIn down(p12);
jasonberry 25:ca1b1098c77f 82 DigitalOut reset(p30);
jasonberry 25:ca1b1098c77f 83 DigitalOut led1(LED1); //TEST DO
jasonberry 25:ca1b1098c77f 84 DigitalOut led2(LED2); //SUPERLOOP
jasonberry 25:ca1b1098c77f 85 DigitalOut led3(LED3); //MQTT
jasonberry 25:ca1b1098c77f 86 DigitalOut led4(LED4); //SERIAL,BLUE,PC
jasonberry 25:ca1b1098c77f 87
jasonberry 25:ca1b1098c77f 88
jasonberry 25:ca1b1098c77f 89 //Serial pc(USBTX, USBRX); setup in easy connect
jasonberry 25:ca1b1098c77f 90 MMA7660 MMA(p28, p27); //I2C Accelerometer
jasonberry 25:ca1b1098c77f 91 LM75B sensor(p28,p27); // Temperature
jasonberry 25:ca1b1098c77f 92 Serial blue(p13,p14); //tx, rx
jasonberry 25:ca1b1098c77f 93 Servo s1(p21);
jasonberry 25:ca1b1098c77f 94 Servo s2(p22);
jasonberry 25:ca1b1098c77f 95
jasonberry 25:ca1b1098c77f 96
jasonberry 25:ca1b1098c77f 97 int arrivedcount = 0; //number of message received back from mqtt broker
jasonberry 25:ca1b1098c77f 98 int version = 88; //version number used for test messages to mqtt
jasonberry 25:ca1b1098c77f 99
jasonberry 25:ca1b1098c77f 100 //topics to subscribe
jasonberry 25:ca1b1098c77f 101 ////////////////////////////////////////////////////////////////////////////////////
jasonberry 25:ca1b1098c77f 102 //thingspeak channel as topic
jasonberry 25:ca1b1098c77f 103 ////////////////////////////////////////////////////////////////////////////////////
jasonberry 25:ca1b1098c77f 104 //char* publish_topic = "channels/1322442/publish/fields/field1/PUWBQJL3X64GWL2P";
jasonberry 25:ca1b1098c77f 105 //char* subscribe_topic = "channels/1322442/subscribe/fields/field1/H0P76513L8Z3N555";
jasonberry 25:ca1b1098c77f 106
jasonberry 25:ca1b1098c77f 107 //mosquitto mbed sample broker topic
jasonberry 25:ca1b1098c77f 108 ///////////////////////////////////////////////////////////////////////////////////
jasonberry 25:ca1b1098c77f 109 //char* publish_topic = "mbed-sample/";
jasonberry 25:ca1b1098c77f 110 //char* subscribe_topic = "mbed-sample/";
jasonberry 25:ca1b1098c77f 111
jasonberry 25:ca1b1098c77f 112 //bee_botte mqtt topic
jasonberry 25:ca1b1098c77f 113 ///////////////////////////////////////////////////////////////////////////////////
jasonberry 25:ca1b1098c77f 114
jasonberry 25:ca1b1098c77f 115 /////this is where you change your bot id..so maybe change to 011, 011do1, o11do2 etc...011so1
jasonberry 25:ca1b1098c77f 116 char id[] = "010"; //bot id
jasonberry 25:ca1b1098c77f 117
jasonberry 25:ca1b1098c77f 118 #define DO1 "010do1"
jasonberry 25:ca1b1098c77f 119 #define DO2 "010do2"
jasonberry 25:ca1b1098c77f 120 #define DO3 "010do3"
jasonberry 25:ca1b1098c77f 121 #define DO4 "010do4"
jasonberry 25:ca1b1098c77f 122
jasonberry 25:ca1b1098c77f 123 #define AO1 "010ao1"
jasonberry 25:ca1b1098c77f 124 #define AO2 "010ao2"
jasonberry 25:ca1b1098c77f 125 #define AO3 "010ao3"
jasonberry 25:ca1b1098c77f 126 #define AO4 "010ao4"
jasonberry 25:ca1b1098c77f 127
jasonberry 25:ca1b1098c77f 128 #define SO1 "010so1"
jasonberry 25:ca1b1098c77f 129 //////end of changes required ///////////////////////////////////////////////////////////////////
jasonberry 25:ca1b1098c77f 130
jasonberry 25:ca1b1098c77f 131 //beebotte topics
jasonberry 25:ca1b1098c77f 132 char* publish_topic = "jason/text";
jasonberry 25:ca1b1098c77f 133 char* subscribe_topic = "jason/text";
jasonberry 25:ca1b1098c77f 134 char mqtt_message_received[255];
jasonberry 25:ca1b1098c77f 135 char blue_message_received[255];
jasonberry 25:ca1b1098c77f 136 char pc_message_received[255];
jasonberry 25:ca1b1098c77f 137
jasonberry 25:ca1b1098c77f 138 //outputs variables you can assign these to whatever you want leds,servos
jasonberry 25:ca1b1098c77f 139 long int ao1=0;
jasonberry 25:ca1b1098c77f 140 long int ao2=0;
jasonberry 25:ca1b1098c77f 141 long int ao3=0;
jasonberry 25:ca1b1098c77f 142 long int ao4=0;
jasonberry 25:ca1b1098c77f 143
jasonberry 25:ca1b1098c77f 144 long int do1=0;
jasonberry 25:ca1b1098c77f 145 long int do2=0;
jasonberry 25:ca1b1098c77f 146 long int do3=0;
jasonberry 25:ca1b1098c77f 147 long int do4=0;
jasonberry 25:ca1b1098c77f 148
jasonberry 25:ca1b1098c77f 149 char so1[20] = "..";
jasonberry 25:ca1b1098c77f 150
jasonberry 25:ca1b1098c77f 151 //input variables
jasonberry 25:ca1b1098c77f 152 float ai1=0;
jasonberry 25:ca1b1098c77f 153 float ai2=0;
jasonberry 25:ca1b1098c77f 154 float ai3=0;
jasonberry 25:ca1b1098c77f 155 float ai4=0;
jasonberry 25:ca1b1098c77f 156
jasonberry 25:ca1b1098c77f 157 int di1=0;
jasonberry 25:ca1b1098c77f 158 int di2=0;
jasonberry 25:ca1b1098c77f 159 int di3=0;
jasonberry 25:ca1b1098c77f 160 int di4=0;
jasonberry 25:ca1b1098c77f 161
jasonberry 25:ca1b1098c77f 162 char si1[20] = "..";
jasonberry 25:ca1b1098c77f 163
jasonberry 25:ca1b1098c77f 164 int client_yielding = false; //set yeilding for message to false at startup
jasonberry 25:ca1b1098c77f 165
jasonberry 25:ca1b1098c77f 166 //100ms timer
jasonberry 25:ca1b1098c77f 167 Ticker _100ms_ticker;
jasonberry 25:ca1b1098c77f 168 int timer_100ms_count = 0;
jasonberry 25:ca1b1098c77f 169
jasonberry 25:ca1b1098c77f 170 //this funtion runs every 100ms and increments timer_100ms_count by 1
jasonberry 25:ca1b1098c77f 171 ///////////////////////////////////////////////////////////////////////
jasonberry 25:ca1b1098c77f 172 void _100ms_timer(void)
jasonberry 25:ca1b1098c77f 173 {
jasonberry 25:ca1b1098c77f 174 timer_100ms_count = timer_100ms_count+1;
jasonberry 25:ca1b1098c77f 175
jasonberry 25:ca1b1098c77f 176 //stop overflow 1000 secs
jasonberry 25:ca1b1098c77f 177 if(timer_100ms_count> 10000)
jasonberry 25:ca1b1098c77f 178 {
jasonberry 25:ca1b1098c77f 179 timer_100ms_count = 0;
jasonberry 25:ca1b1098c77f 180 }
jasonberry 25:ca1b1098c77f 181 }
jasonberry 25:ca1b1098c77f 182
jasonberry 25:ca1b1098c77f 183 // Simon's Watchdog code from
jasonberry 25:ca1b1098c77f 184 // http://mbed.org/forum/mbed/topic/508/
jasonberry 25:ca1b1098c77f 185 class Watchdog {
jasonberry 25:ca1b1098c77f 186 public:
jasonberry 25:ca1b1098c77f 187 // Load timeout value in watchdog timer and enable
jasonberry 25:ca1b1098c77f 188 void kick(float s) {
jasonberry 25:ca1b1098c77f 189 LPC_WDT->WDCLKSEL = 0x1; // Set CLK src to PCLK
jasonberry 25:ca1b1098c77f 190 uint32_t clk = SystemCoreClock / 16; // WD has a fixed /4 prescaler, PCLK default is /4
jasonberry 25:ca1b1098c77f 191 LPC_WDT->WDTC = s * (float)clk;
jasonberry 25:ca1b1098c77f 192 LPC_WDT->WDMOD = 0x3; // Enabled and Reset
jasonberry 25:ca1b1098c77f 193 kick();
jasonberry 25:ca1b1098c77f 194 }
jasonberry 25:ca1b1098c77f 195 // reset the watchdog timer
jasonberry 25:ca1b1098c77f 196 // by writing this required bit pattern
jasonberry 25:ca1b1098c77f 197 void kick() {
jasonberry 25:ca1b1098c77f 198 LPC_WDT->WDFEED = 0xAA;
jasonberry 25:ca1b1098c77f 199 LPC_WDT->WDFEED = 0x55;
jasonberry 25:ca1b1098c77f 200 }
jasonberry 25:ca1b1098c77f 201 };
jasonberry 25:ca1b1098c77f 202
jasonberry 25:ca1b1098c77f 203 // Setup the watchdog timer
jasonberry 25:ca1b1098c77f 204 Watchdog wdt;
jasonberry 25:ca1b1098c77f 205
jasonberry 25:ca1b1098c77f 206 /////////////////////////////////////start//////////////////////////////////////////
jasonberry 25:ca1b1098c77f 207 int main(int argc, char* argv[])
jasonberry 25:ca1b1098c77f 208 {
jasonberry 25:ca1b1098c77f 209 int rc =0;
jasonberry 25:ca1b1098c77f 210 int superloop_count = 0;
jasonberry 25:ca1b1098c77f 211 static int watchdog_count;
jasonberry 25:ca1b1098c77f 212
jasonberry 25:ca1b1098c77f 213 //reset
jasonberry 25:ca1b1098c77f 214 reset = 0;
jasonberry 25:ca1b1098c77f 215 wait(1);
jasonberry 25:ca1b1098c77f 216 reset = 1;
jasonberry 25:ca1b1098c77f 217 wait(3);
jasonberry 25:ca1b1098c77f 218
jasonberry 25:ca1b1098c77f 219 // turn off all leds
jasonberry 25:ca1b1098c77f 220 led1=0;
jasonberry 25:ca1b1098c77f 221 led2=0;
jasonberry 25:ca1b1098c77f 222 led3=0;
jasonberry 25:ca1b1098c77f 223 led4=0;
jasonberry 25:ca1b1098c77f 224
jasonberry 25:ca1b1098c77f 225 //set servo to halfway
jasonberry 25:ca1b1098c77f 226 s1 = 0.5;
jasonberry 25:ca1b1098c77f 227 s2 = 0.5;
jasonberry 25:ca1b1098c77f 228
jasonberry 25:ca1b1098c77f 229 // On reset, indicate a watchdog reset or a pushbutton reset on LED 4 or 3
jasonberry 25:ca1b1098c77f 230 if ((LPC_WDT->WDMOD >> 2) & 1)
jasonberry 25:ca1b1098c77f 231 {
jasonberry 25:ca1b1098c77f 232 led4 = 1;
jasonberry 25:ca1b1098c77f 233 watchdog_count = watchdog_count + 1; //not working
jasonberry 25:ca1b1098c77f 234 }
jasonberry 25:ca1b1098c77f 235 else
jasonberry 25:ca1b1098c77f 236 led3 = 1;
jasonberry 25:ca1b1098c77f 237
jasonberry 25:ca1b1098c77f 238 wait(1);
jasonberry 25:ca1b1098c77f 239
jasonberry 25:ca1b1098c77f 240 // setup a 15 second timeout on watchdog timer hardware
jasonberry 25:ca1b1098c77f 241 // needs to be longer than worst case main loop exection time
jasonberry 25:ca1b1098c77f 242 wdt.kick(15.0);
jasonberry 25:ca1b1098c77f 243
jasonberry 25:ca1b1098c77f 244 // set up a function to play every 100 msec
jasonberry 25:ca1b1098c77f 245 _100ms_ticker.attach(&_100ms_timer, 0.1);
jasonberry 25:ca1b1098c77f 246
jasonberry 25:ca1b1098c77f 247 //serial comms at 115200 bluetooth at 9600
jasonberry 25:ca1b1098c77f 248 pc.baud(115200);
jasonberry 25:ca1b1098c77f 249 blue.baud(9600);
jasonberry 25:ca1b1098c77f 250
jasonberry 25:ca1b1098c77f 251 logMessage1("HelloMQTT: version is:%u Watchdog Resets:%u \r\n",
jasonberry 25:ca1b1098c77f 252 version,watchdog_count);
jasonberry 25:ca1b1098c77f 253 blue.printf("HelloMQTT: version is:%u Watchdog Resets:%u \r\n",
jasonberry 25:ca1b1098c77f 254 version,watchdog_count);
jasonberry 25:ca1b1098c77f 255
jasonberry 25:ca1b1098c77f 256 ///calibrate servos
jasonberry 25:ca1b1098c77f 257 //s1.calibrate(0.001, 45.0);
jasonberry 25:ca1b1098c77f 258 //s2.calibrate(0.001, 45.0);
jasonberry 25:ca1b1098c77f 259
jasonberry 25:ca1b1098c77f 260 //1.0 test if wifi wired
jasonberry 25:ca1b1098c77f 261 // 1.0 set up a wifi connection
jasonberry 25:ca1b1098c77f 262 ///////////////////////////////////////////////////////////////////////////////////
jasonberry 25:ca1b1098c77f 263 NetworkInterface* network = easy_connect(up); //press joystick at boot up for debug
jasonberry 25:ca1b1098c77f 264 MQTTNetwork mqttNetwork(network);
jasonberry 25:ca1b1098c77f 265 MQTT::Client<MQTTNetwork, Countdown> client(mqttNetwork);
jasonberry 25:ca1b1098c77f 266
jasonberry 25:ca1b1098c77f 267 if(ip_addr1 != 0)
jasonberry 25:ca1b1098c77f 268 {
jasonberry 25:ca1b1098c77f 269 logMessage2("IP address %s\r\n", ip_addr1);
jasonberry 25:ca1b1098c77f 270
jasonberry 25:ca1b1098c77f 271 //2.0 set up a MQTT connection
jasonberry 25:ca1b1098c77f 272 ////////////////////////////////////////////////////////////////////
jasonberry 25:ca1b1098c77f 273 //MQTTNetwork mqttNetwork(network);
jasonberry 25:ca1b1098c77f 274 //MQTT::Client<MQTTNetwork, Countdown> client(mqttNetwork);
jasonberry 25:ca1b1098c77f 275
jasonberry 25:ca1b1098c77f 276 char* hostname = HOSTNAME; //"54.157.36.210"; //"103.224.182.253"; //
jasonberry 25:ca1b1098c77f 277 int port = 1883;
jasonberry 25:ca1b1098c77f 278
jasonberry 25:ca1b1098c77f 279 //logMessage1("Conecting to %s:%d\r\n", hostname, port);
jasonberry 25:ca1b1098c77f 280 rc = mqttNetwork.connect(hostname, port);
jasonberry 25:ca1b1098c77f 281 if (rc != 0)
jasonberry 25:ca1b1098c77f 282 logMessage2("rc from TCP connect is %d\r\n", rc);
jasonberry 25:ca1b1098c77f 283
jasonberry 25:ca1b1098c77f 284 //3.0 connect MQTT client
jasonberry 25:ca1b1098c77f 285 ///////////////////////////////////////////////////////////////////
jasonberry 25:ca1b1098c77f 286 MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
jasonberry 25:ca1b1098c77f 287 data.MQTTVersion = 3;
jasonberry 25:ca1b1098c77f 288 data.clientID.cstring = CLIENTID; //Make sure your robot name here..JASON_BERRY_BOT
jasonberry 25:ca1b1098c77f 289 data.username.cstring = USERNAME; // token from beebotte or anything for thingsspeak
jasonberry 25:ca1b1098c77f 290 data.password.cstring = PASSWORD; // this thinspeak mqtt api found in thingspeak profiles
jasonberry 25:ca1b1098c77f 291
jasonberry 25:ca1b1098c77f 292 if ((rc = client.connect(data)) != 0)
jasonberry 25:ca1b1098c77f 293 {
jasonberry 25:ca1b1098c77f 294 //logMessage2("rc MQTT connect is %d\r\n", rc);
jasonberry 25:ca1b1098c77f 295 if (rc == 5)
jasonberry 25:ca1b1098c77f 296 logMessage2("Bad MQTT password or username\r\n");
jasonberry 25:ca1b1098c77f 297 }
jasonberry 25:ca1b1098c77f 298 else
jasonberry 25:ca1b1098c77f 299 {
jasonberry 25:ca1b1098c77f 300 //subscribe and read last message posted to mqtt
jasonberry 25:ca1b1098c77f 301 ////////////////////////////////////////////////
jasonberry 25:ca1b1098c77f 302 //logMessage1("start of subscribe..\r\n");
jasonberry 25:ca1b1098c77f 303
jasonberry 25:ca1b1098c77f 304 if ((rc = client.subscribe(subscribe_topic, MQTT::QOS0, messageArrived)) != 0)
jasonberry 25:ca1b1098c77f 305 logMessage2("rc MQTT subsc is %d\r\n", rc);
jasonberry 25:ca1b1098c77f 306 }
jasonberry 25:ca1b1098c77f 307 }
jasonberry 25:ca1b1098c77f 308 else
jasonberry 25:ca1b1098c77f 309 {
jasonberry 25:ca1b1098c77f 310 logMessage1("wifi unsucessful debug!!!");
jasonberry 25:ca1b1098c77f 311 rc = 1;
jasonberry 25:ca1b1098c77f 312 }
jasonberry 25:ca1b1098c77f 313 //end of wifi and subscibe
jasonberry 25:ca1b1098c77f 314 ///////////////////////////////////////////////////////////////////////
jasonberry 25:ca1b1098c77f 315
jasonberry 25:ca1b1098c77f 316 // clear lcd of startup messages
jasonberry 25:ca1b1098c77f 317 lcd.cls();
jasonberry 25:ca1b1098c77f 318
jasonberry 25:ca1b1098c77f 319
jasonberry 25:ca1b1098c77f 320 //superloop
jasonberry 25:ca1b1098c77f 321 ///////////////////////////////////////////////////////////////////////////////////////////////
jasonberry 25:ca1b1098c77f 322 while(1)
jasonberry 25:ca1b1098c77f 323 {
jasonberry 25:ca1b1098c77f 324 char mqtt_message_send[200];
jasonberry 25:ca1b1098c77f 325
jasonberry 25:ca1b1098c77f 326 //update your outputs
jasonberry 25:ca1b1098c77f 327 /////////////////////////////////////////////
jasonberry 25:ca1b1098c77f 328 led1=do1;
jasonberry 25:ca1b1098c77f 329 // =do2;
jasonberry 25:ca1b1098c77f 330 // =do3;
jasonberry 25:ca1b1098c77f 331 // =do4;
jasonberry 25:ca1b1098c77f 332
jasonberry 25:ca1b1098c77f 333 s1 = float(ao1)/255; //a01 goes from 0 255 convert to 0.0 to 1.0 fo s1
jasonberry 25:ca1b1098c77f 334 s2 = float(ao2)/255; //a01 goes from 0 255 convert to 0.0 to 1.0 fo s1
jasonberry 25:ca1b1098c77f 335 // =a03;
jasonberry 25:ca1b1098c77f 336 // =ao4;
jasonberry 25:ca1b1098c77f 337
jasonberry 25:ca1b1098c77f 338 // =so1;
jasonberry 25:ca1b1098c77f 339
jasonberry 25:ca1b1098c77f 340
jasonberry 25:ca1b1098c77f 341 //update your inputs
jasonberry 25:ca1b1098c77f 342 /////////////////////////////////////////////
jasonberry 25:ca1b1098c77f 343
jasonberry 25:ca1b1098c77f 344 //analog inputs
jasonberry 25:ca1b1098c77f 345 ///////////////////////////////////////////////////////////////////////////////
jasonberry 25:ca1b1098c77f 346 // ai1: assigned to temperature sensor, ai2: accel_x
jasonberry 25:ca1b1098c77f 347 if (!sensor.open())
jasonberry 25:ca1b1098c77f 348 {
jasonberry 25:ca1b1098c77f 349 pc.printf("Temperature not device not detected!\n\r");
jasonberry 25:ca1b1098c77f 350 }
jasonberry 25:ca1b1098c77f 351 else
jasonberry 25:ca1b1098c77f 352 {
jasonberry 25:ca1b1098c77f 353 ai1 = sensor.temp();
jasonberry 25:ca1b1098c77f 354 }
jasonberry 25:ca1b1098c77f 355
jasonberry 25:ca1b1098c77f 356 // ai2: assigned to acceration_x
jasonberry 25:ca1b1098c77f 357 if (!MMA.testConnection())
jasonberry 25:ca1b1098c77f 358 {
jasonberry 25:ca1b1098c77f 359 pc.printf("accelormeter !! spelling j !!!\n\r");
jasonberry 25:ca1b1098c77f 360 }
jasonberry 25:ca1b1098c77f 361 else
jasonberry 25:ca1b1098c77f 362 {
jasonberry 25:ca1b1098c77f 363 ai2 = MMA.x();
jasonberry 25:ca1b1098c77f 364 ai3 = MMA.y();
jasonberry 25:ca1b1098c77f 365 }
jasonberry 25:ca1b1098c77f 366
jasonberry 25:ca1b1098c77f 367 //ai4 tba
jasonberry 25:ca1b1098c77f 368 ai4=0;
jasonberry 25:ca1b1098c77f 369
jasonberry 25:ca1b1098c77f 370 //digital inputs
jasonberry 25:ca1b1098c77f 371 /////////////////////////////////////////////////////////////////////////////////
jasonberry 25:ca1b1098c77f 372 // di1: assigned to joystick down(pin 12)..
jasonberry 25:ca1b1098c77f 373 // note cannot use joystick fire used for bluetoothrx
jasonberry 25:ca1b1098c77f 374 // ai1: assigned to temperature sensor, ai2: accel_x
jasonberry 25:ca1b1098c77f 375 di1 = down;
jasonberry 25:ca1b1098c77f 376
jasonberry 25:ca1b1098c77f 377 //di12,3,4 tba
jasonberry 25:ca1b1098c77f 378 di2=0;
jasonberry 25:ca1b1098c77f 379 di3=0;
jasonberry 25:ca1b1098c77f 380 di4=0;
jasonberry 25:ca1b1098c77f 381
jasonberry 25:ca1b1098c77f 382 //string inputs
jasonberry 25:ca1b1098c77f 383 /////////////////////////////////////////////////////////////////////////////////
jasonberry 25:ca1b1098c77f 384 //si1 is a string meesage max 20chars, where boards can send text meesages to mqtt
jasonberry 25:ca1b1098c77f 385 snprintf(si1,20,"jhello world..xxxx");
jasonberry 25:ca1b1098c77f 386
jasonberry 25:ca1b1098c77f 387 //end of setting inout and ouput variables ////////////////////////////////////
jasonberry 25:ca1b1098c77f 388
jasonberry 25:ca1b1098c77f 389
jasonberry 25:ca1b1098c77f 390 /////////////////////////////////////////////
jasonberry 25:ca1b1098c77f 391 //create a message with your ai,di and si
jasonberry 25:ca1b1098c77f 392 build_bots_AI_DI_SI_message(mqtt_message_send);
jasonberry 25:ca1b1098c77f 393
jasonberry 25:ca1b1098c77f 394
jasonberry 25:ca1b1098c77f 395 //logMessage1(".start of publish1 yeild=%u\r\n",client_yielding);
jasonberry 25:ca1b1098c77f 396 //wait(1);
jasonberry 25:ca1b1098c77f 397
jasonberry 25:ca1b1098c77f 398 //publish message every 500 msecs to mqtt with all ai,di and si data
jasonberry 25:ca1b1098c77f 399 //also as part of publish read back any messages form mqtt subscribe
jasonberry 25:ca1b1098c77f 400
jasonberry 25:ca1b1098c77f 401 if(timer_100ms_count >= 5 && (rc == 0))
jasonberry 25:ca1b1098c77f 402 {
jasonberry 25:ca1b1098c77f 403 //"kick" to reset watchdog timer and avoid a reset
jasonberry 25:ca1b1098c77f 404 wdt.kick();
jasonberry 25:ca1b1098c77f 405
jasonberry 25:ca1b1098c77f 406
jasonberry 25:ca1b1098c77f 407 timer_100ms_count = 0;
jasonberry 25:ca1b1098c77f 408
jasonberry 25:ca1b1098c77f 409 //create a meesage with your ai,di and si
jasonberry 25:ca1b1098c77f 410 //build_bots_AI_DI_SI_message(buf);
jasonberry 25:ca1b1098c77f 411
jasonberry 25:ca1b1098c77f 412 //publish and write to mqtt broker..this will then make its way to BEEBOTTE or thinspeak channel
jasonberry 25:ca1b1098c77f 413 ////////////////////////////////////////////////////////////////////////////////////
jasonberry 25:ca1b1098c77f 414 // note can only publish every 15 seconds to thingspeak mqtt, as often as you like to beebottte
jasonberry 25:ca1b1098c77f 415 MQTT::Message message;
jasonberry 25:ca1b1098c77f 416
jasonberry 25:ca1b1098c77f 417 // QoS 0
jasonberry 25:ca1b1098c77f 418 //sprintf(buf, "Hello World! QoS 0 message from app version %f\r\n", version);
jasonberry 25:ca1b1098c77f 419 //sprintf(buf, "%u",arrivedcount); //version); //MMA.x());//test number is version
jasonberry 25:ca1b1098c77f 420
jasonberry 25:ca1b1098c77f 421 message.qos = MQTT::QOS0;
jasonberry 25:ca1b1098c77f 422 message.retained = false; //false;
jasonberry 25:ca1b1098c77f 423 message.dup = false;
jasonberry 25:ca1b1098c77f 424 message.payload = (void*)mqtt_message_send;
jasonberry 25:ca1b1098c77f 425 message.payloadlen = strlen(mqtt_message_send)+1;
jasonberry 25:ca1b1098c77f 426
jasonberry 25:ca1b1098c77f 427 //logMessage2("mqtt message: %s :: lenght:%d \r\n", message.payload, message.payloadlen);
jasonberry 25:ca1b1098c77f 428 rc = client.publish(publish_topic, message);
jasonberry 25:ca1b1098c77f 429 if (rc != 0)
jasonberry 25:ca1b1098c77f 430 {
jasonberry 25:ca1b1098c77f 431 logMessage2("rc MQTT publish error is %d %d\r\n", rc, message.payloadlen);
jasonberry 25:ca1b1098c77f 432 client.yield(10);
jasonberry 25:ca1b1098c77f 433 }
jasonberry 25:ca1b1098c77f 434 else
jasonberry 25:ca1b1098c77f 435 client.yield(10);
jasonberry 25:ca1b1098c77f 436 //logMessage2(".end of publish..\r\n");
jasonberry 25:ca1b1098c77f 437 }
jasonberry 25:ca1b1098c77f 438
jasonberry 25:ca1b1098c77f 439
jasonberry 25:ca1b1098c77f 440 //check if any comms from bluetooth, if so send last mqqt message received
jasonberry 25:ca1b1098c77f 441 if (blue.readable())
jasonberry 25:ca1b1098c77f 442 {
jasonberry 25:ca1b1098c77f 443 blue.gets(blue_message_received,255);
jasonberry 25:ca1b1098c77f 444
jasonberry 25:ca1b1098c77f 445 //send back response
jasonberry 25:ca1b1098c77f 446 blue.printf("%s\r\n",mqtt_message_received);
jasonberry 25:ca1b1098c77f 447 blue.printf("%s\r\n",mqtt_message_send);
jasonberry 25:ca1b1098c77f 448
jasonberry 25:ca1b1098c77f 449 //check bluetooth message for drive commands
jasonberry 25:ca1b1098c77f 450 control_bots_AO_DO_SO_message(blue_message_received);
jasonberry 25:ca1b1098c77f 451
jasonberry 25:ca1b1098c77f 452 strcpy(mqtt_message_send,blue_message_received);
jasonberry 25:ca1b1098c77f 453
jasonberry 25:ca1b1098c77f 454 //publish and write to mqtt broker..this will then make its way to BEEBOTTE or thinspeak channel
jasonberry 25:ca1b1098c77f 455 ////////////////////////////////////////////////////////////////////////////////////
jasonberry 25:ca1b1098c77f 456 // note can only publish every 15 seconds to thingspeak mqtt, as often as you like to beebottte
jasonberry 25:ca1b1098c77f 457 MQTT::Message message;
jasonberry 25:ca1b1098c77f 458
jasonberry 25:ca1b1098c77f 459 // QoS 0
jasonberry 25:ca1b1098c77f 460 //sprintf(buf, "Hello World! QoS 0 message from app version %f\r\n", version);
jasonberry 25:ca1b1098c77f 461 //sprintf(buf, "%u",arrivedcount); //version); //MMA.x());//test number is version
jasonberry 25:ca1b1098c77f 462
jasonberry 25:ca1b1098c77f 463 message.qos = MQTT::QOS0;
jasonberry 25:ca1b1098c77f 464 message.retained = false; //false;
jasonberry 25:ca1b1098c77f 465 message.dup = false;
jasonberry 25:ca1b1098c77f 466 message.payload = (void*)mqtt_message_send;
jasonberry 25:ca1b1098c77f 467 message.payloadlen = strlen(mqtt_message_send)+1;
jasonberry 25:ca1b1098c77f 468
jasonberry 25:ca1b1098c77f 469 //logMessage2("mqtt message: %s :: lenght:%d \r\n", message.payload, message.payloadlen);
jasonberry 25:ca1b1098c77f 470 rc = client.publish(publish_topic, message);
jasonberry 25:ca1b1098c77f 471 if (rc != 0)
jasonberry 25:ca1b1098c77f 472 {
jasonberry 25:ca1b1098c77f 473 logMessage2("rc MQTT publish error is %d %d\r\n", rc, message.payloadlen);
jasonberry 25:ca1b1098c77f 474 client.yield(10);
jasonberry 25:ca1b1098c77f 475 }
jasonberry 25:ca1b1098c77f 476 else
jasonberry 25:ca1b1098c77f 477 client.yield(10);
jasonberry 25:ca1b1098c77f 478 //logMessage2(".end of publish..\r\n");
jasonberry 25:ca1b1098c77f 479
jasonberry 25:ca1b1098c77f 480
jasonberry 25:ca1b1098c77f 481 led4 = !led4;
jasonberry 25:ca1b1098c77f 482 }// end of bluetooth read
jasonberry 25:ca1b1098c77f 483
jasonberry 25:ca1b1098c77f 484 //check if any comms from pc, if so send last mqqt message received
jasonberry 25:ca1b1098c77f 485 if (pc.readable())
jasonberry 25:ca1b1098c77f 486 {
jasonberry 25:ca1b1098c77f 487 pc.gets(pc_message_received,255);
jasonberry 25:ca1b1098c77f 488
jasonberry 25:ca1b1098c77f 489 //send back response
jasonberry 25:ca1b1098c77f 490 pc.printf("%s\r\n",mqtt_message_received);
jasonberry 25:ca1b1098c77f 491 pc.printf("%s\r\n",mqtt_message_send);
jasonberry 25:ca1b1098c77f 492
jasonberry 25:ca1b1098c77f 493 //check pc message for drive commands
jasonberry 25:ca1b1098c77f 494 control_bots_AO_DO_SO_message(pc_message_received);
jasonberry 25:ca1b1098c77f 495
jasonberry 25:ca1b1098c77f 496 strcpy(mqtt_message_send,pc_message_received);
jasonberry 25:ca1b1098c77f 497 //drive_command_for_another_bot = 0;
jasonberry 25:ca1b1098c77f 498
jasonberry 25:ca1b1098c77f 499 //publish and write to mqtt broker..this will then make its way to BEEBOTTE or thinspeak channel
jasonberry 25:ca1b1098c77f 500 ////////////////////////////////////////////////////////////////////////////////////
jasonberry 25:ca1b1098c77f 501 // note can only publish every 15 seconds to thingspeak mqtt, as often as you like to beebottte
jasonberry 25:ca1b1098c77f 502 MQTT::Message message;
jasonberry 25:ca1b1098c77f 503
jasonberry 25:ca1b1098c77f 504 // QoS 0
jasonberry 25:ca1b1098c77f 505 //sprintf(buf, "Hello World! QoS 0 message from app version %f\r\n", version);
jasonberry 25:ca1b1098c77f 506 //sprintf(buf, "%u",arrivedcount); //version); //MMA.x());//test number is version
jasonberry 25:ca1b1098c77f 507
jasonberry 25:ca1b1098c77f 508 message.qos = MQTT::QOS0;
jasonberry 25:ca1b1098c77f 509 message.retained = false; //false;
jasonberry 25:ca1b1098c77f 510 message.dup = false;
jasonberry 25:ca1b1098c77f 511 message.payload = (void*)mqtt_message_send;
jasonberry 25:ca1b1098c77f 512 message.payloadlen = strlen(mqtt_message_send)+1;
jasonberry 25:ca1b1098c77f 513
jasonberry 25:ca1b1098c77f 514 //logMessage2("mqtt message: %s :: lenght:%d \r\n", message.payload, message.payloadlen);
jasonberry 25:ca1b1098c77f 515 rc = client.publish(publish_topic, message);
jasonberry 25:ca1b1098c77f 516 if (rc != 0)
jasonberry 25:ca1b1098c77f 517 {
jasonberry 25:ca1b1098c77f 518 logMessage2("rc MQTT publish error is %d %d\r\n", rc, message.payloadlen);
jasonberry 25:ca1b1098c77f 519 client.yield(10);
jasonberry 25:ca1b1098c77f 520 }
jasonberry 25:ca1b1098c77f 521 else
jasonberry 25:ca1b1098c77f 522 client.yield(10);
jasonberry 25:ca1b1098c77f 523
jasonberry 25:ca1b1098c77f 524 //toggle serial message received
jasonberry 25:ca1b1098c77f 525 led4 = !led4;
jasonberry 25:ca1b1098c77f 526 }//end of pc read
jasonberry 25:ca1b1098c77f 527
jasonberry 25:ca1b1098c77f 528 //toggle heartbeat every 1 loops,
jasonberry 25:ca1b1098c77f 529 if(superloop_count > 1)
jasonberry 25:ca1b1098c77f 530 {
jasonberry 25:ca1b1098c77f 531 superloop_count = 0;
jasonberry 25:ca1b1098c77f 532 led2 = !led2;
jasonberry 25:ca1b1098c77f 533 }
jasonberry 25:ca1b1098c77f 534 else
jasonberry 25:ca1b1098c77f 535 superloop_count++;
jasonberry 25:ca1b1098c77f 536
jasonberry 25:ca1b1098c77f 537 //diplay variables locally
jasonberry 25:ca1b1098c77f 538 ///////////////////////////////////////
jasonberry 25:ca1b1098c77f 539 lcd.locate(0,1);
jasonberry 25:ca1b1098c77f 540 //lcd.printf(mqtt_message_received,"Payload %s", (char*)ser_bt_mqtt_rec_mes);
jasonberry 25:ca1b1098c77f 541 lcd.printf("ao1:%ld,:%ld,:%ld,:%ld do1:%ld,%ld,%ld,%ld",ao1,ao2,ao3,ao4,do1,do2,do3,do4);//printf long int slider
jasonberry 25:ca1b1098c77f 542 lcd.locate(0,11);
jasonberry 25:ca1b1098c77f 543 lcd.printf("so1: %20s",so1);
jasonberry 25:ca1b1098c77f 544 //lcd.printf("do1:%ld, do2:%ld, do3:%ld, do4:%ld ",do1,do2,do3,do4);//printf long int slider
jasonberry 25:ca1b1098c77f 545 lcd.locate(0,21);
jasonberry 25:ca1b1098c77f 546 lcd.printf("count:%u ip:%s",arrivedcount,ip_addr1);
jasonberry 25:ca1b1098c77f 547
jasonberry 25:ca1b1098c77f 548 }//end of while(1)
jasonberry 25:ca1b1098c77f 549
jasonberry 25:ca1b1098c77f 550 }//end of main, note: no super loop here runs once at reset
jasonberry 25:ca1b1098c77f 551
jasonberry 25:ca1b1098c77f 552 void control_bots_AO_DO_SO_message(char *ser_bt_mqtt_rec_mes)
jasonberry 25:ca1b1098c77f 553 ///////////////////////////////////////////////////////////////////////////////
jasonberry 25:ca1b1098c77f 554 //
jasonberry 25:ca1b1098c77f 555 // control, message in this format {"data":"do1=0","ispublic": true}
jasonberry 25:ca1b1098c77f 556 //
jasonberry 25:ca1b1098c77f 557 //
jasonberry 25:ca1b1098c77f 558 ///////////////////////////////////////////////////////////////////////////////
jasonberry 25:ca1b1098c77f 559 {
jasonberry 25:ca1b1098c77f 560 char *ptr_str;
jasonberry 25:ca1b1098c77f 561
jasonberry 25:ca1b1098c77f 562
jasonberry 25:ca1b1098c77f 563 //010do1: mapped to led1 on mbed
jasonberry 25:ca1b1098c77f 564 if((ptr_str = strstr((char*)ser_bt_mqtt_rec_mes, DO1)) != NULL ) // "010do1"
jasonberry 25:ca1b1098c77f 565 {
jasonberry 25:ca1b1098c77f 566 do1 = strtol(ptr_str+8,NULL,10);
jasonberry 25:ca1b1098c77f 567 }
jasonberry 25:ca1b1098c77f 568 if((ptr_str = strstr((char*)ser_bt_mqtt_rec_mes, DO2)) != NULL )
jasonberry 25:ca1b1098c77f 569 {
jasonberry 25:ca1b1098c77f 570 do2 = strtol(ptr_str+8,NULL,10);
jasonberry 25:ca1b1098c77f 571 }
jasonberry 25:ca1b1098c77f 572 if((ptr_str = strstr((char*)ser_bt_mqtt_rec_mes, DO3)) != NULL )
jasonberry 25:ca1b1098c77f 573 {
jasonberry 25:ca1b1098c77f 574 do3 = strtol(ptr_str+8,NULL,10);
jasonberry 25:ca1b1098c77f 575 }
jasonberry 25:ca1b1098c77f 576 if((ptr_str = strstr((char*)ser_bt_mqtt_rec_mes, DO4)) != NULL )
jasonberry 25:ca1b1098c77f 577 {
jasonberry 25:ca1b1098c77f 578 do4 = strtol(ptr_str+8,NULL,10);
jasonberry 25:ca1b1098c77f 579 }
jasonberry 25:ca1b1098c77f 580
jasonberry 25:ca1b1098c77f 581
jasonberry 25:ca1b1098c77f 582 if((ptr_str = strstr((char*)ser_bt_mqtt_rec_mes, AO1)) != NULL )
jasonberry 25:ca1b1098c77f 583 {
jasonberry 25:ca1b1098c77f 584 ao1 = strtol(ptr_str+8,NULL,10); // convert string to a long integer
jasonberry 25:ca1b1098c77f 585 }
jasonberry 25:ca1b1098c77f 586 if((ptr_str = strstr((char*)ser_bt_mqtt_rec_mes, AO2)) != NULL )
jasonberry 25:ca1b1098c77f 587 {
jasonberry 25:ca1b1098c77f 588 ao2 = strtol(ptr_str+8,NULL,10); // convert string to a long integer
jasonberry 25:ca1b1098c77f 589 }
jasonberry 25:ca1b1098c77f 590 if((ptr_str = strstr((char*)ser_bt_mqtt_rec_mes, AO3)) != NULL )
jasonberry 25:ca1b1098c77f 591 {
jasonberry 25:ca1b1098c77f 592 ao3 = strtol(ptr_str+8,NULL,10); // convert string to a long integer
jasonberry 25:ca1b1098c77f 593 }
jasonberry 25:ca1b1098c77f 594 if((ptr_str = strstr((char*)ser_bt_mqtt_rec_mes, AO4)) != NULL )
jasonberry 25:ca1b1098c77f 595 {
jasonberry 25:ca1b1098c77f 596 ao4 = strtol(ptr_str+8,NULL,10); // convert string to a long integer
jasonberry 25:ca1b1098c77f 597 }
jasonberry 25:ca1b1098c77f 598
jasonberry 25:ca1b1098c77f 599 if((ptr_str = strstr((char*)ser_bt_mqtt_rec_mes, SO1)) != NULL )
jasonberry 25:ca1b1098c77f 600 {
jasonberry 25:ca1b1098c77f 601 strncpy(so1,ptr_str+8,20); //copy 20 chars from messsage to so1
jasonberry 25:ca1b1098c77f 602 }
jasonberry 25:ca1b1098c77f 603
jasonberry 25:ca1b1098c77f 604 }
jasonberry 25:ca1b1098c77f 605
jasonberry 25:ca1b1098c77f 606 void build_bots_AI_DI_SI_message(char *buf)
jasonberry 25:ca1b1098c77f 607 ///////////////////////////////////////////////////////////////////////////////
jasonberry 25:ca1b1098c77f 608 //
jasonberry 25:ca1b1098c77f 609 //
jasonberry 25:ca1b1098c77f 610 //
jasonberry 25:ca1b1098c77f 611 ///////////////////////////////////////////////////////////////////////////////
jasonberry 25:ca1b1098c77f 612 {
jasonberry 25:ca1b1098c77f 613 //build message store in buf
jasonberry 25:ca1b1098c77f 614 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}",
jasonberry 25:ca1b1098c77f 615 id,arrivedcount,id,ai1,id,ai2,id,ai3,id,ai4,id,di1,id,di2,id,di3,id,di4,id,si1);
jasonberry 25:ca1b1098c77f 616 }
jasonberry 25:ca1b1098c77f 617
jasonberry 25:ca1b1098c77f 618
jasonberry 25:ca1b1098c77f 619 /*int boot_up(void)
jasonberry 25:ca1b1098c77f 620 {
jasonberry 25:ca1b1098c77f 621 //set up a wifi connection
jasonberry 25:ca1b1098c77f 622 //////////////////////////
jasonberry 25:ca1b1098c77f 623
jasonberry 25:ca1b1098c77f 624 //connect to wifi network and display ip address..display in serial
jasonberry 25:ca1b1098c77f 625 NetworkInterface* network = easy_connect(up); //press joystick at boot up for debug
jasonberry 25:ca1b1098c77f 626
jasonberry 25:ca1b1098c77f 627 if(ip_addr1 != 0)
jasonberry 25:ca1b1098c77f 628 logMessage2("IP address %s\r\n", ip_addr1);
jasonberry 25:ca1b1098c77f 629 else
jasonberry 25:ca1b1098c77f 630 logMessage1("wifi unsucessful debug!!!");
jasonberry 25:ca1b1098c77f 631
jasonberry 25:ca1b1098c77f 632 //set up a MQTT connection
jasonberry 25:ca1b1098c77f 633 //////////////////////////
jasonberry 25:ca1b1098c77f 634
jasonberry 25:ca1b1098c77f 635 MQTTNetwork mqttNetwork(network);
jasonberry 25:ca1b1098c77f 636
jasonberry 25:ca1b1098c77f 637 MQTT::Client<MQTTNetwork, Countdown> client(mqttNetwork);
jasonberry 25:ca1b1098c77f 638
jasonberry 25:ca1b1098c77f 639 char* hostname = HOSTNAME; //"54.157.36.210"; //"103.224.182.253"; //
jasonberry 25:ca1b1098c77f 640 int port = 1883;
jasonberry 25:ca1b1098c77f 641
jasonberry 25:ca1b1098c77f 642 logMessage1("Conecting to %s:%d\r\n", hostname, port);
jasonberry 25:ca1b1098c77f 643
jasonberry 25:ca1b1098c77f 644 int rc = mqttNetwork.connect(hostname, port);
jasonberry 25:ca1b1098c77f 645 if (rc != 0)
jasonberry 25:ca1b1098c77f 646 logMessage2("rc from TCP connect is %d\r\n", rc);
jasonberry 25:ca1b1098c77f 647
jasonberry 25:ca1b1098c77f 648 //connect to client
jasonberry 25:ca1b1098c77f 649 ///////////////////
jasonberry 25:ca1b1098c77f 650 MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
jasonberry 25:ca1b1098c77f 651 data.MQTTVersion = 3;
jasonberry 25:ca1b1098c77f 652 data.clientID.cstring = CLIENTID; //can be any name,client just dont connect from two different client
jasonberry 25:ca1b1098c77f 653 data.username.cstring = USERNAME; //with identical name,client
jasonberry 25:ca1b1098c77f 654 data.password.cstring = PASSWORD; // this thinspeak mqtt api found in thingspeak profiles
jasonberry 25:ca1b1098c77f 655
jasonberry 25:ca1b1098c77f 656 if ((rc = client.connect(data)) != 0)
jasonberry 25:ca1b1098c77f 657 logMessage2("rc MQTT connect is %d\r\n", rc);
jasonberry 25:ca1b1098c77f 658
jasonberry 25:ca1b1098c77f 659 //subscribe and read last message posted to mqtt
jasonberry 25:ca1b1098c77f 660 ////////////////////////////////////////////////
jasonberry 25:ca1b1098c77f 661 //note data received with handled above in
jasonberry 25:ca1b1098c77f 662
jasonberry 25:ca1b1098c77f 663
jasonberry 25:ca1b1098c77f 664 logMessage1("start of subscribe..\r\n");
jasonberry 25:ca1b1098c77f 665 if ((rc = client.subscribe(subscribe_topic, MQTT::QOS0, messageArrived)) != 0)
jasonberry 25:ca1b1098c77f 666 logMessage2("rc MQTT subsc is %d\r\n", rc);
jasonberry 25:ca1b1098c77f 667
jasonberry 25:ca1b1098c77f 668
jasonberry 25:ca1b1098c77f 669 return 0;
jasonberry 25:ca1b1098c77f 670 }
jasonberry 25:ca1b1098c77f 671
jasonberry 25:ca1b1098c77f 672 void mqtt_message_publish(void)
jasonberry 25:ca1b1098c77f 673 {
jasonberry 25:ca1b1098c77f 674 //while (1)
jasonberry 25:ca1b1098c77f 675 // {
jasonberry 25:ca1b1098c77f 676
jasonberry 25:ca1b1098c77f 677 // if(client_yielding == false)
jasonberry 25:ca1b1098c77f 678 // {
jasonberry 25:ca1b1098c77f 679 // logMessage1(".start of yeilding1\r\n");
jasonberry 25:ca1b1098c77f 680 // client.yield(100);
jasonberry 25:ca1b1098c77f 681 // client_yielding = true;
jasonberry 25:ca1b1098c77f 682 // }
jasonberry 25:ca1b1098c77f 683
jasonberry 25:ca1b1098c77f 684 logMessage1(".start of publish1 yeild=%u\r\n",client_yielding);
jasonberry 25:ca1b1098c77f 685 //wait(1);
jasonberry 25:ca1b1098c77f 686 if(up == 0)
jasonberry 25:ca1b1098c77f 687 {
jasonberry 25:ca1b1098c77f 688 //publish and write to mqtt broker..this will then make its way to thinspeak channel
jasonberry 25:ca1b1098c77f 689 ////////////////////////////////////////////////////////////////////////////////////
jasonberry 25:ca1b1098c77f 690 // note can only publish every 15 seconds to thingspeak mqtt
jasonberry 25:ca1b1098c77f 691
jasonberry 25:ca1b1098c77f 692 MQTT::Message message;
jasonberry 25:ca1b1098c77f 693
jasonberry 25:ca1b1098c77f 694 // QoS 0
jasonberry 25:ca1b1098c77f 695 char buf[100];
jasonberry 25:ca1b1098c77f 696 //sprintf(buf, "Hello World! QoS 0 message from app version %f\r\n", version);
jasonberry 25:ca1b1098c77f 697 sprintf(buf, "%u",arrivedcount); //version); //MMA.x());//test number is version
jasonberry 25:ca1b1098c77f 698 message.qos = MQTT::QOS0;
jasonberry 25:ca1b1098c77f 699 message.retained = false; //false;
jasonberry 25:ca1b1098c77f 700 message.dup = false;
jasonberry 25:ca1b1098c77f 701 message.payload = (void*)buf;
jasonberry 25:ca1b1098c77f 702 message.payloadlen = strlen(buf)+1;
jasonberry 25:ca1b1098c77f 703
jasonberry 25:ca1b1098c77f 704
jasonberry 25:ca1b1098c77f 705 logMessage1(".start of publish2\r\n");
jasonberry 25:ca1b1098c77f 706 //wait(1);
jasonberry 25:ca1b1098c77f 707
jasonberry 25:ca1b1098c77f 708 int rc = client.publish(publish_topic , message);
jasonberry 25:ca1b1098c77f 709 //rc = client.publish(publish_topic , message);
jasonberry 25:ca1b1098c77f 710
jasonberry 25:ca1b1098c77f 711 //wait(4);
jasonberry 25:ca1b1098c77f 712 //while (arrivedcount < 1)
jasonberry 25:ca1b1098c77f 713 client.yield(10);
jasonberry 25:ca1b1098c77f 714
jasonberry 25:ca1b1098c77f 715 logMessage2(".end of publish..\r\n");
jasonberry 25:ca1b1098c77f 716 }
jasonberry 25:ca1b1098c77f 717 else
jasonberry 25:ca1b1098c77f 718 {
jasonberry 25:ca1b1098c77f 719 client.yield(10);
jasonberry 25:ca1b1098c77f 720 }
jasonberry 25:ca1b1098c77f 721 not tested on this set up other fields possible
jasonberry 25:ca1b1098c77f 722 // QoS 1
jasonberry 25:ca1b1098c77f 723 sprintf(buf, "Hello World! QoS 1 message from app version %f\r\n", version);
jasonberry 25:ca1b1098c77f 724 message.qos = MQTT::QOS1;
jasonberry 25:ca1b1098c77f 725 message.payloadlen = strlen(buf)+1;
jasonberry 25:ca1b1098c77f 726 rc = client.publish(publish_topic , message);
jasonberry 25:ca1b1098c77f 727 while (arrivedcount < 2)
jasonberry 25:ca1b1098c77f 728 client.yield(100);
jasonberry 25:ca1b1098c77f 729
jasonberry 25:ca1b1098c77f 730 // QoS 2
jasonberry 25:ca1b1098c77f 731 sprintf(buf, "Hello World! QoS 2 message from app version %f\r\n", version);
jasonberry 25:ca1b1098c77f 732 message.qos = MQTT::QOS2;
jasonberry 25:ca1b1098c77f 733 message.payloadlen = strlen(buf)+1;
jasonberry 25:ca1b1098c77f 734 rc = client.publish(topic, message);
jasonberry 25:ca1b1098c77f 735 while (arrivedcount < 3)
jasonberry 25:ca1b1098c77f 736 client.yield(100);
jasonberry 25:ca1b1098c77f 737 */
jasonberry 25:ca1b1098c77f 738 // }//end of test while
jasonberry 25:ca1b1098c77f 739
jasonberry 25:ca1b1098c77f 740 //unsubscribe
jasonberry 25:ca1b1098c77f 741 ////////////////////////////////////////
jasonberry 25:ca1b1098c77f 742 /*
jasonberry 25:ca1b1098c77f 743 if ((rc = client.unsubscribe(subscribe_topic)) != 0)
jasonberry 25:ca1b1098c77f 744 logMessage1("rc from unsubscribe was %d\r\n", rc);
jasonberry 25:ca1b1098c77f 745
jasonberry 25:ca1b1098c77f 746 // and disconnect from client
jasonberry 25:ca1b1098c77f 747 if ((rc = client.disconnect()) != 0)
jasonberry 25:ca1b1098c77f 748 logMessage1("rc from disconnect was %d\r\n", rc);
jasonberry 25:ca1b1098c77f 749
jasonberry 25:ca1b1098c77f 750 //disconnect from wifi network
jasonberry 25:ca1b1098c77f 751 //////////////////////////////
jasonberry 25:ca1b1098c77f 752 //this would use a lot of current (mA) up battery life,
jasonberry 25:ca1b1098c77f 753 //if connecting and disconnecting to wifi each time you sent received a message
jasonberry 25:ca1b1098c77f 754
jasonberry 25:ca1b1098c77f 755 mqttNetwork.disconnect();
jasonberry 25:ca1b1098c77f 756 wait(1);
jasonberry 25:ca1b1098c77f 757
jasonberry 25:ca1b1098c77f 758 logMessage1("Version %d: finish %d msgs\r\n", version, arrivedcount);
jasonberry 25:ca1b1098c77f 759 logMessage2("IP address %s\r\n", ip_addr1);
jasonberry 25:ca1b1098c77f 760
jasonberry 25:ca1b1098c77f 761 }
jasonberry 25:ca1b1098c77f 762 */
jasonberry 25:ca1b1098c77f 763
jasonberry 25:ca1b1098c77f 764 // [FINITE_STATE_MACHINE_STATE] [STATE_SCALE_mod] [STATE_FACE_X_mod]<sp><CR><LF>
jasonberry 25:ca1b1098c77f 765
jasonberry 25:ca1b1098c77f 766 //message received handler
jasonberry 25:ca1b1098c77f 767 void messageArrived(MQTT::MessageData& md)
jasonberry 25:ca1b1098c77f 768 {
jasonberry 25:ca1b1098c77f 769 MQTT::Message &message = md.message;
jasonberry 25:ca1b1098c77f 770 //logMessage1("Message arrived: qos %d, retained %d, dup %d, packetid %d\r\n", message.qos, message.retained, message.dup, message.id);
jasonberry 25:ca1b1098c77f 771 //logMessage2("Payload %.*s :count %u\r\n", message.payloadlen, (char*)message.payload,arrivedcount);
jasonberry 25:ca1b1098c77f 772 //blue.printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\r\n", message.qos, message.retained, message.dup, message.id);
jasonberry 25:ca1b1098c77f 773 //blue.printf("Payload %.*s :count %u\r\n", message.payloadlen, (char*)message.payload,arrivedcount);
jasonberry 25:ca1b1098c77f 774
jasonberry 25:ca1b1098c77f 775
jasonberry 25:ca1b1098c77f 776
jasonberry 25:ca1b1098c77f 777 sprintf(mqtt_message_received,"Payload %.*s :count %u\r\n", message.payloadlen, (char*)message.payload,arrivedcount);
jasonberry 25:ca1b1098c77f 778 //sprintf(str, "hello %s", "world");
jasonberry 25:ca1b1098c77f 779 //snprintf(str, 255, "hello %s", "world")
jasonberry 25:ca1b1098c77f 780
jasonberry 25:ca1b1098c77f 781 ++arrivedcount;
jasonberry 25:ca1b1098c77f 782
jasonberry 25:ca1b1098c77f 783 control_bots_AO_DO_SO_message(mqtt_message_received);
jasonberry 25:ca1b1098c77f 784
jasonberry 25:ca1b1098c77f 785 //if message received from mqtt then do not need to send on outputs
jasonberry 25:ca1b1098c77f 786 //drive_command_for_another_bot = 0;
jasonberry 25:ca1b1098c77f 787
jasonberry 25:ca1b1098c77f 788
jasonberry 25:ca1b1098c77f 789 /*
jasonberry 25:ca1b1098c77f 790 //led1 =1; led1=0;
jasonberry 25:ca1b1098c77f 791 if( strstr((char*)message.payload, "led1=1") != NULL ) {
jasonberry 25:ca1b1098c77f 792 led1=1;
jasonberry 25:ca1b1098c77f 793 // lcd.cls();
jasonberry 25:ca1b1098c77f 794 // lcd.locate(0,3);
jasonberry 25:ca1b1098c77f 795 // lcd.printf("led 1");
jasonberry 25:ca1b1098c77f 796 }
jasonberry 25:ca1b1098c77f 797 if( strstr((char*)message.payload, "led1=0") != NULL ) {
jasonberry 25:ca1b1098c77f 798 led1=0;
jasonberry 25:ca1b1098c77f 799 // lcd.cls();
jasonberry 25:ca1b1098c77f 800 // lcd.locate(0,3);
jasonberry 25:ca1b1098c77f 801 // lcd.printf("led 0");
jasonberry 25:ca1b1098c77f 802 }
jasonberry 25:ca1b1098c77f 803 */
jasonberry 25:ca1b1098c77f 804 client_yielding = false; //can yeild for a message again
jasonberry 25:ca1b1098c77f 805 led3 = !led3;
jasonberry 25:ca1b1098c77f 806
jasonberry 25:ca1b1098c77f 807 }
jasonberry 25:ca1b1098c77f 808
jasonberry 24:d2e25cdf9084 809 ///////////////////////////////////////////////////////////////////
jasonberry 24:d2e25cdf9084 810 //IFFT TO THINSPEAK TO THINSPEAK MQTT BROKER TO MBED/ESP8266 CLIENT
jasonberry 24:d2e25cdf9084 811 ///////////////////////////////////////////////////////////////////
jasonberry 24:d2e25cdf9084 812
jasonberry 24:d2e25cdf9084 813 // Step1: THINGSPEAK CHANNEL
jasonberry 24:d2e25cdf9084 814 //..........................
jasonberry 24:d2e25cdf9084 815 //
jasonberry 24:d2e25cdf9084 816 // Set up a Thinspeak channel, record the channel number(1322442), channel read api(mine was H0P76513L8Z3N555),
jasonberry 24:d2e25cdf9084 817 // channel write api(mine was 1PUWBQJL3X64GWL2P),
jasonberry 24:d2e25cdf9084 818 // and look up my profile for thinspeakMQTT api(mine was HXPY4BZ5Z0OPAH0W) ...start channel off
jasonberry 24:d2e25cdf9084 819 //
jasonberry 24:d2e25cdf9084 820 // Step2: TEST THINGSPEAK CHANNEL VIA BROSWER
jasonberry 24:d2e25cdf9084 821 //...........................................
jasonberry 24:d2e25cdf9084 822 //
jasonberry 24:d2e25cdf9084 823 // Test thingspeak channel is working using browser by entering in test value 89 as follows
jasonberry 24:d2e25cdf9084 824 // https://api.thingspeak.com/update?api_key=PUWBQJL3X64GWL2P&field1=89
jasonberry 24:d2e25cdf9084 825 // check thingspeak channel to see if 89 is plotted
jasonberry 24:d2e25cdf9084 826
jasonberry 24:d2e25cdf9084 827 // Step3: FIND MQTT.THINGSPEAK.COM FIXED IP ADDRESS
jasonberry 25:ca1b1098c77f 828 // Find out fixed ip address of mqtt.thingspeak.com using windons command: tracert mqtt.thingspeak.com
jasonberry 25:ca1b1098c77f 829 // at time of testing it w as 34.231.253.113...doesnt seem to change much :-) but check it.
jasonberry 24:d2e25cdf9084 830
jasonberry 25:ca1b1098c77f 831 // Step3.1: SETUP A DESK MQTT CLIENT TO TEST THINGSPEAK MQTT BROKER
jasonberry 24:d2e25cdf9084 832 // Set up a desktop client to test thingspeak mqtt broker, I used MQTT.fx 1.71 from
jasonberry 24:d2e25cdf9084 833 // https://mqttfx.jensd.de/index.php/download.
jasonberry 24:d2e25cdf9084 834 //
jasonberry 24:d2e25cdf9084 835 // This is a good tuturial to show how to set up client and send messages to thigspeak channel via desltop mqtt
jasonberry 24:d2e25cdf9084 836 // https://uk.mathworks.com/help/thingspeak/use-desktop-mqtt-client-to-publish-to-a-channel.html#d122e3819
jasonberry 24:d2e25cdf9084 837 //
jasonberry 24:d2e25cdf9084 838 // In summary from mathswork,
jasonberry 24:d2e25cdf9084 839 // click in droplist and select new profile,
jasonberry 24:d2e25cdf9084 840 // broker type: select mqtt broker,
jasonberry 25:ca1b1098c77f 841 // 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
jasonberry 24:d2e25cdf9084 842 // broker port: is 1883,
jasonberry 24:d2e25cdf9084 843 // client id: anything,
jasonberry 24:d2e25cdf9084 844 // username: anything
jasonberry 24:d2e25cdf9084 845 // password: is thingspeakMQTT.api HXPY4BZ5Z0OPAH0W
jasonberry 24:d2e25cdf9084 846 // Save profile as mbed
jasonberry 24:d2e25cdf9084 847 // Next ready to go, press connect hopefully you connect sucessfully
jasonberry 24:d2e25cdf9084 848 //
jasonberry 24:d2e25cdf9084 849 // PUBLISH:
jasonberry 24:d2e25cdf9084 850 // Ok now set up to publish to thinspeak channel via mqtt
jasonberry 24:d2e25cdf9084 851 // put channels/1322442/publish/fields/field1/PUWBQJL3X64GWL2P in publish entry
jasonberry 24:d2e25cdf9084 852 // next put some number in field below.. then press publish go to thingspeak and check channel for plot update,
jasonberry 24:d2e25cdf9084 853 // put another number in field and publish, again check thinspeak channel..hopefully you see data
jasonberry 24:d2e25cdf9084 854 //
jasonberry 25:ca1b1098c77f 855 // can also use Mosquitto test broker,test.mosquitto.org [5.196.95.208], topic mbed-sample/
jasonberry 25:ca1b1098c77f 856 //
jasonberry 24:d2e25cdf9084 857 // SUBSCRIBE
jasonberry 24:d2e25cdf9084 858 // Click on subscribe tab and enter channels/1322442/subscribe/fields/field1/H0P76513L8Z3N555,
jasonberry 24:d2e25cdf9084 859 // click subscribe
jasonberry 24:d2e25cdf9084 860 // Test subscribe by sending data to thinsgspeak channel via browser
jasonberry 24:d2e25cdf9084 861 // Go to browser and enter some channel data as step 3,
jasonberry 24:d2e25cdf9084 862 // https://api.thingspeak.com/update?api_key=PUWBQJL3X64GWL2P&field1=67
jasonberry 24:d2e25cdf9084 863 // you should see message in mqtt panel with data 67
jasonberry 25:ca1b1098c77f 864 //
jasonberry 25:ca1b1098c77f 865 // Can also suscribe use mosquitto test broker topic mbed-sample
jasonberry 25:ca1b1098c77f 866 //
jasonberry 25:ca1b1098c77f 867 //
jasonberry 24:d2e25cdf9084 868 // Step4: CONFIGURE THE CODE WITH WIFI SETTINGS, DEBUG, PINS ETC
jasonberry 24:d2e25cdf9084 869 // In the mbed_app.json file change as required, I wired esp8266 rx to mbed tx pin9, esp8266 tx to mbed rx pin 10
jasonberry 24:d2e25cdf9084 870 // "esp8266-tx": {
jasonberry 24:d2e25cdf9084 871 // "help": "Pin used as TX (connects to ESP8266 RX)",
jasonberry 24:d2e25cdf9084 872 // "value": "p9"
jasonberry 24:d2e25cdf9084 873 // },
jasonberry 24:d2e25cdf9084 874 // "esp8266-rx": {
jasonberry 24:d2e25cdf9084 875 // "help": "Pin used as RX (connects to ESP8266 TX)",
jasonberry 24:d2e25cdf9084 876 // "value": "p10"
jasonberry 24:d2e25cdf9084 877 // },
jasonberry 24:d2e25cdf9084 878 // "esp8266-ssid": {
jasonberry 24:d2e25cdf9084 879 // "value": "1231321423dfdf16_2G"
jasonberry 24:d2e25cdf9084 880 // },
jasonberry 24:d2e25cdf9084 881 // "esp8266-password": {
jasonberry 24:d2e25cdf9084 882 // "value": "12345"
jasonberry 24:d2e25cdf9084 883 //
jasonberry 24:d2e25cdf9084 884 // If you want to see all the at commands for esp8266, then select debug true in mbed_app.json file
jasonberry 24:d2e25cdf9084 885 // pretty cool to see all the messages and good obviously for debug.
jasonberry 25:ca1b1098c77f 886 //
jasonberry 25:ca1b1098c77f 887 // step4.1: UPDATE LINE 86 in easy-connect.h with netwrok details as follows,
jasonberry 25:ca1b1098c77f 888 // in easy connect folder, then mcr20a-rf-driver folder...should see it
jasonberry 25:ca1b1098c77f 889 // connect_success = esp.connect("eir89748912-2.4G","28eb111b");
jasonberry 25:ca1b1098c77f 890 // ----------------------------------------------------------------
jasonberry 25:ca1b1098c77f 891 //
jasonberry 24:d2e25cdf9084 892 // Step 5: COMPILE
jasonberry 24:d2e25cdf9084 893 // Compile the code should have no errors
jasonberry 24:d2e25cdf9084 894 // famous last words !!!!
jasonberry 25:ca1b1098c77f 895 //
jasonberry 24:d2e25cdf9084 896 // Step 6: WIRE UP ESP8266 TO MBED
jasonberry 24:d2e25cdf9084 897 // Wire up the ESP8266 to mbed see following for pinouts esp8266 h
jasonberry 24:d2e25cdf9084 898 // https://circuits4you.com/2016/12/14/esp8266-pin-diagram/
jasonberry 24:d2e25cdf9084 899 // Use an external power supply for wifi
jasonberry 24:d2e25cdf9084 900 //
jasonberry 24:d2e25cdf9084 901 // ESP pin1(gnd) to MBED pin 1(gnd)
jasonberry 24:d2e25cdf9084 902 // ESP pin2(tx) to MBED pin 10(rx)
jasonberry 24:d2e25cdf9084 903 // ESP pin4(ch en) to MBED 3.3v , I used pin one xbee break out connector on baseboard..
jasonberry 24:d2e25cdf9084 904 // see see page 3 for pinout of xbee connector, pin closest to ethernet entry point ,
jasonberry 24:d2e25cdf9084 905 // https://os.mbed.com/media/uploads/chris/mbed-014.1_b.pdf
jasonberry 24:d2e25cdf9084 906 //
jasonberry 24:d2e25cdf9084 907 // ESP pin6(reset) to MBED pin 30 .. not used in this example..but might be used in future so just in case
jasonberry 24:d2e25cdf9084 908 // ESP pin7(rx) to MBED pin 9(tx)
jasonberry 24:d2e25cdf9084 909 // ESP pin8(3.3v) to MBED pin 40(3.3v)
jasonberry 24:d2e25cdf9084 910
jasonberry 24:d2e25cdf9084 911 //Step 7: DOWNLOAD TO BOARD
jasonberry 24:d2e25cdf9084 912
jasonberry 24:d2e25cdf9084 913 //Step 8: PUTTY
jasonberry 24:d2e25cdf9084 914 // Open putty with mbed comm port and 115200 baud
jasonberry 24:d2e25cdf9084 915
jasonberry 24:d2e25cdf9084 916 //Step 9: RESET AND RUN CODE
jasonberry 24:d2e25cdf9084 917 // Watch putty serial to see if this is output (NO DEBUG)
jasonberry 24:d2e25cdf9084 918 // HelloMQTT: version is 111
jasonberry 24:d2e25cdf9084 919 // [EasyConnect] Using WiFi (ESP8266)
jasonberry 24:d2e25cdf9084 920 // [EasyConnect] Connecting to WiFi..
jasonberry 24:d2e25cdf9084 921 // [EasyConnect] Connected to Network successfully
jasonberry 24:d2e25cdf9084 922 // [EasyConnect] IP address 192.168.1.6
jasonberry 24:d2e25cdf9084 923 // Connecting to 34.231.253.113:1883
jasonberry 24:d2e25cdf9084 924 // test point 1..end of subscribe..
jasonberry 24:d2e25cdf9084 925 // test point 2..start of publish..
jasonberry 24:d2e25cdf9084 926 // Message arrived: qos 0, retained 0, dup 0, packetid 51817
jasonberry 24:d2e25cdf9084 927 // Payload 111
jasonberry 24:d2e25cdf9084 928 // Message arrived: qos 0, retained 0, dup 0, packetid 51817
jasonberry 24:d2e25cdf9084 929 // Payload 111
jasonberry 24:d2e25cdf9084 930 // test point 3...end of publish..
jasonberry 24:d2e25cdf9084 931 // Version 111: finish 2 msgs
jasonberry 24:d2e25cdf9084 932
jasonberry 24:d2e25cdf9084 933 //Step 10: TEST IF THINGSPEAK UPDATED WITH VALUE FROM MBED!!! (MBED WRITE)
jasonberry 24:d2e25cdf9084 934 // Check to see if 111 went to thingspeak channel
jasonberry 24:d2e25cdf9084 935
jasonberry 24:d2e25cdf9084 936 //Step 11: TEST IF VLAUE SENT TO THINGSPEAK FROM BROWSER FINDS ITS WAS TO (MBED READ)
jasonberry 24:d2e25cdf9084 937 // Send test value 44 via browser https://api.thingspeak.com/update?api_key=PUWBQJL3X64GWL2P&field1=44
jasonberry 24:d2e25cdf9084 938 // check 44 is in subscribe section of desktop mqtt.fx
jasonberry 24:d2e25cdf9084 939 //
jasonberry 24:d2e25cdf9084 940 // Then run code by reseting board,check putty output to see if 44 is picked up as a message in mbed.
jasonberry 24:d2e25cdf9084 941 //
jasonberry 24:d2e25cdf9084 942
jasonberry 24:d2e25cdf9084 943 //Step 12: SEND TEMPERATURE UP
jasonberry 24:d2e25cdf9084 944 //
jasonberry 24:d2e25cdf9084 945
jasonberry 24:d2e25cdf9084 946 //Step 13: TURN ON/OFF LED1 ON MBED
jasonberry 24:d2e25cdf9084 947 //
jasonberry 24:d2e25cdf9084 948
jasonberry 24:d2e25cdf9084 949 //Step14: Set IFTTT APPLET TO SEND VALUE TO MBED VIA THINGSPEAK MQTT
jasonberry 24:d2e25cdf9084 950 // use webhook see tuturial for controlling leds via google assistant via ardunio, used this to help
jasonberry 24:d2e25cdf9084 951 // see https://www.engineersgarage.com/electronic-projects/esp32-voice-operated-home-automation-with-thingspeak-mqtt-ifttt-and-google-assistant/
jasonberry 24:d2e25cdf9084 952
jasonberry 24:d2e25cdf9084 953 //Step15: HTTP-TO-MQTT-BRIDGE(not tested)
jasonberry 24:d2e25cdf9084 954 // Set up https to mqtt bridge which allow us to send ifttt to mbed without thinspeak.
jasonberry 24:d2e25cdf9084 955 // see this tutrial https://www.home-assistant.io/blog/2017/03/28/http-to-mqtt-bridge/
jasonberry 24:d2e25cdf9084 956
jasonberry 24:d2e25cdf9084 957 //////////////////////////////////////////////////////////////////////////////////////////////////////////////
jasonberry 24:d2e25cdf9084 958
jasonberry 25:ca1b1098c77f 959 // mqtt publish and subscribe topics linked to mqtt.thinspeak.com, channel api
jasonberry 25:ca1b1098c77f 960 // and channel read(suscribe) and write(publish) api...these can be used in any mqtt client conection..
jasonberry 25:ca1b1098c77f 961 // for pc testing could use mqtt.fx
jasonberry 25:ca1b1098c77f 962 // the following mathswork shows how to set up a desktop client to test thinspeak channel
jasonberry 25:ca1b1098c77f 963 // https://uk.mathworks.com/help/thingspeak/use-desktop-mqtt-client-to-publish-to-a-channel.html#d122e3819
Jan Jongboom 20:49c9daf2b0ff 964
jasonberry 25:ca1b1098c77f 965 //roborealm send
jasonberry 25:ca1b1098c77f 966 //{"data":"bot_id:110","010ao1":[MOUSE_X],"010so1":[JASONS_S01],"ispublic":true}<CR><LF>
jasonberry 25:ca1b1098c77f 967 //roborealm receive
jasonberry 25:ca1b1098c77f 968 //{"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],