hellomqttt to thingspeak mqtt and ifttt

Dependencies:   Servo MQTTPacket FP

Committer:
jasonberry
Date:
Thu Mar 11 13:56:02 2021 +0000
Revision:
24:d2e25cdf9084
Parent:
21:a68bd76740f9
Child:
25:ca1b1098c77f
hellomqtt to thingspeak to ifttt

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 24:d2e25cdf9084 31 ///////////////////////////////////////////////////////////////////
jasonberry 24:d2e25cdf9084 32 //IFFT TO THINSPEAK TO THINSPEAK MQTT BROKER TO MBED/ESP8266 CLIENT
jasonberry 24:d2e25cdf9084 33 ///////////////////////////////////////////////////////////////////
jasonberry 24:d2e25cdf9084 34
jasonberry 24:d2e25cdf9084 35 // Step1: THINGSPEAK CHANNEL
jasonberry 24:d2e25cdf9084 36 //..........................
jasonberry 24:d2e25cdf9084 37 //
jasonberry 24:d2e25cdf9084 38 // Set up a Thinspeak channel, record the channel number(1322442), channel read api(mine was H0P76513L8Z3N555),
jasonberry 24:d2e25cdf9084 39 // channel write api(mine was 1PUWBQJL3X64GWL2P),
jasonberry 24:d2e25cdf9084 40 // and look up my profile for thinspeakMQTT api(mine was HXPY4BZ5Z0OPAH0W) ...start channel off
jasonberry 24:d2e25cdf9084 41 //
jasonberry 24:d2e25cdf9084 42 // Step2: TEST THINGSPEAK CHANNEL VIA BROSWER
jasonberry 24:d2e25cdf9084 43 //...........................................
jasonberry 24:d2e25cdf9084 44 //
jasonberry 24:d2e25cdf9084 45 // Test thingspeak channel is working using browser by entering in test value 89 as follows
jasonberry 24:d2e25cdf9084 46 // https://api.thingspeak.com/update?api_key=PUWBQJL3X64GWL2P&field1=89
jasonberry 24:d2e25cdf9084 47 // check thingspeak channel to see if 89 is plotted
jasonberry 24:d2e25cdf9084 48
jasonberry 24:d2e25cdf9084 49 // Step3: FIND MQTT.THINGSPEAK.COM FIXED IP ADDRESS
jasonberry 24:d2e25cdf9084 50 // Find out fixed ip address of mqtt.thinspeak.com using windons command: tracert mqtt.thinspeak.com
jasonberry 24:d2e25cdf9084 51 // at time of testing it was 34.231.253.113...doesnt seem to change much :-) but check it.
jasonberry 24:d2e25cdf9084 52
jasonberry 24:d2e25cdf9084 53 // Step4: SETUP A DESK MQTT CLIENT TO TEST THINGSPEAK MQTT BROKER
jasonberry 24:d2e25cdf9084 54 // Set up a desktop client to test thingspeak mqtt broker, I used MQTT.fx 1.71 from
jasonberry 24:d2e25cdf9084 55 // https://mqttfx.jensd.de/index.php/download.
jasonberry 24:d2e25cdf9084 56 //
jasonberry 24:d2e25cdf9084 57 // This is a good tuturial to show how to set up client and send messages to thigspeak channel via desltop mqtt
jasonberry 24:d2e25cdf9084 58 // https://uk.mathworks.com/help/thingspeak/use-desktop-mqtt-client-to-publish-to-a-channel.html#d122e3819
jasonberry 24:d2e25cdf9084 59 //
jasonberry 24:d2e25cdf9084 60 // In summary from mathswork,
jasonberry 24:d2e25cdf9084 61 // click in droplist and select new profile,
jasonberry 24:d2e25cdf9084 62 // broker type: select mqtt broker,
jasonberry 24:d2e25cdf9084 63 // broker address: put in ip address for mqtt.thingspeak.com that you found in step 2, 34.231.253.113
jasonberry 24:d2e25cdf9084 64 // broker port: is 1883,
jasonberry 24:d2e25cdf9084 65 // client id: anything,
jasonberry 24:d2e25cdf9084 66 // username: anything
jasonberry 24:d2e25cdf9084 67 // password: is thingspeakMQTT.api HXPY4BZ5Z0OPAH0W
jasonberry 24:d2e25cdf9084 68 // Save profile as mbed
jasonberry 24:d2e25cdf9084 69 // Next ready to go, press connect hopefully you connect sucessfully
jasonberry 24:d2e25cdf9084 70 //
jasonberry 24:d2e25cdf9084 71 // PUBLISH:
jasonberry 24:d2e25cdf9084 72 // Ok now set up to publish to thinspeak channel via mqtt
jasonberry 24:d2e25cdf9084 73 // put channels/1322442/publish/fields/field1/PUWBQJL3X64GWL2P in publish entry
jasonberry 24:d2e25cdf9084 74 // next put some number in field below.. then press publish go to thingspeak and check channel for plot update,
jasonberry 24:d2e25cdf9084 75 // put another number in field and publish, again check thinspeak channel..hopefully you see data
jasonberry 24:d2e25cdf9084 76 //
jasonberry 24:d2e25cdf9084 77 // SUBSCRIBE
jasonberry 24:d2e25cdf9084 78 // Click on subscribe tab and enter channels/1322442/subscribe/fields/field1/H0P76513L8Z3N555,
jasonberry 24:d2e25cdf9084 79 // click subscribe
jasonberry 24:d2e25cdf9084 80 // Test subscribe by sending data to thinsgspeak channel via browser
jasonberry 24:d2e25cdf9084 81 // Go to browser and enter some channel data as step 3,
jasonberry 24:d2e25cdf9084 82 // https://api.thingspeak.com/update?api_key=PUWBQJL3X64GWL2P&field1=67
jasonberry 24:d2e25cdf9084 83 // you should see message in mqtt panel with data 67
jasonberry 24:d2e25cdf9084 84
jasonberry 24:d2e25cdf9084 85
jasonberry 24:d2e25cdf9084 86 // Step4: CONFIGURE THE CODE WITH WIFI SETTINGS, DEBUG, PINS ETC
jasonberry 24:d2e25cdf9084 87 // 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 88 // "esp8266-tx": {
jasonberry 24:d2e25cdf9084 89 // "help": "Pin used as TX (connects to ESP8266 RX)",
jasonberry 24:d2e25cdf9084 90 // "value": "p9"
jasonberry 24:d2e25cdf9084 91 // },
jasonberry 24:d2e25cdf9084 92 // "esp8266-rx": {
jasonberry 24:d2e25cdf9084 93 // "help": "Pin used as RX (connects to ESP8266 TX)",
jasonberry 24:d2e25cdf9084 94 // "value": "p10"
jasonberry 24:d2e25cdf9084 95 // },
jasonberry 24:d2e25cdf9084 96 // "esp8266-ssid": {
jasonberry 24:d2e25cdf9084 97 // "value": "1231321423dfdf16_2G"
jasonberry 24:d2e25cdf9084 98 // },
jasonberry 24:d2e25cdf9084 99 // "esp8266-password": {
jasonberry 24:d2e25cdf9084 100 // "value": "12345"
jasonberry 24:d2e25cdf9084 101 //
jasonberry 24:d2e25cdf9084 102 // If you want to see all the at commands for esp8266, then select debug true in mbed_app.json file
jasonberry 24:d2e25cdf9084 103 // pretty cool to see all the messages and good obviously for debug.
jasonberry 24:d2e25cdf9084 104
jasonberry 24:d2e25cdf9084 105 // Step 5: COMPILE
jasonberry 24:d2e25cdf9084 106 // Compile the code should have no errors
jasonberry 24:d2e25cdf9084 107 // famous last words !!!!
jasonberry 24:d2e25cdf9084 108
jasonberry 24:d2e25cdf9084 109 // Step 6: WIRE UP ESP8266 TO MBED
jasonberry 24:d2e25cdf9084 110 // Wire up the ESP8266 to mbed see following for pinouts esp8266 h
jasonberry 24:d2e25cdf9084 111 // https://circuits4you.com/2016/12/14/esp8266-pin-diagram/
jasonberry 24:d2e25cdf9084 112 // Use an external power supply for wifi
jasonberry 24:d2e25cdf9084 113 //
jasonberry 24:d2e25cdf9084 114 // ESP pin1(gnd) to MBED pin 1(gnd)
jasonberry 24:d2e25cdf9084 115 // ESP pin2(tx) to MBED pin 10(rx)
jasonberry 24:d2e25cdf9084 116 // ESP pin4(ch en) to MBED 3.3v , I used pin one xbee break out connector on baseboard..
jasonberry 24:d2e25cdf9084 117 // see see page 3 for pinout of xbee connector, pin closest to ethernet entry point ,
jasonberry 24:d2e25cdf9084 118 // https://os.mbed.com/media/uploads/chris/mbed-014.1_b.pdf
jasonberry 24:d2e25cdf9084 119 //
jasonberry 24:d2e25cdf9084 120 // 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 121 // ESP pin7(rx) to MBED pin 9(tx)
jasonberry 24:d2e25cdf9084 122 // ESP pin8(3.3v) to MBED pin 40(3.3v)
jasonberry 24:d2e25cdf9084 123
jasonberry 24:d2e25cdf9084 124 //Step 7: DOWNLOAD TO BOARD
jasonberry 24:d2e25cdf9084 125
jasonberry 24:d2e25cdf9084 126 //Step 8: PUTTY
jasonberry 24:d2e25cdf9084 127 // Open putty with mbed comm port and 115200 baud
jasonberry 24:d2e25cdf9084 128
jasonberry 24:d2e25cdf9084 129 //Step 9: RESET AND RUN CODE
jasonberry 24:d2e25cdf9084 130 // Watch putty serial to see if this is output (NO DEBUG)
jasonberry 24:d2e25cdf9084 131 // HelloMQTT: version is 111
jasonberry 24:d2e25cdf9084 132 // [EasyConnect] Using WiFi (ESP8266)
jasonberry 24:d2e25cdf9084 133 // [EasyConnect] Connecting to WiFi..
jasonberry 24:d2e25cdf9084 134 // [EasyConnect] Connected to Network successfully
jasonberry 24:d2e25cdf9084 135 // [EasyConnect] IP address 192.168.1.6
jasonberry 24:d2e25cdf9084 136 // Connecting to 34.231.253.113:1883
jasonberry 24:d2e25cdf9084 137 // test point 1..end of subscribe..
jasonberry 24:d2e25cdf9084 138 // test point 2..start of publish..
jasonberry 24:d2e25cdf9084 139 // Message arrived: qos 0, retained 0, dup 0, packetid 51817
jasonberry 24:d2e25cdf9084 140 // Payload 111
jasonberry 24:d2e25cdf9084 141 // Message arrived: qos 0, retained 0, dup 0, packetid 51817
jasonberry 24:d2e25cdf9084 142 // Payload 111
jasonberry 24:d2e25cdf9084 143 // test point 3...end of publish..
jasonberry 24:d2e25cdf9084 144 // Version 111: finish 2 msgs
jasonberry 24:d2e25cdf9084 145
jasonberry 24:d2e25cdf9084 146 //Step 10: TEST IF THINGSPEAK UPDATED WITH VALUE FROM MBED!!! (MBED WRITE)
jasonberry 24:d2e25cdf9084 147 // Check to see if 111 went to thingspeak channel
jasonberry 24:d2e25cdf9084 148
jasonberry 24:d2e25cdf9084 149 //Step 11: TEST IF VLAUE SENT TO THINGSPEAK FROM BROWSER FINDS ITS WAS TO (MBED READ)
jasonberry 24:d2e25cdf9084 150 // Send test value 44 via browser https://api.thingspeak.com/update?api_key=PUWBQJL3X64GWL2P&field1=44
jasonberry 24:d2e25cdf9084 151 // check 44 is in subscribe section of desktop mqtt.fx
jasonberry 24:d2e25cdf9084 152 //
jasonberry 24:d2e25cdf9084 153 // Then run code by reseting board,check putty output to see if 44 is picked up as a message in mbed.
jasonberry 24:d2e25cdf9084 154 //
jasonberry 24:d2e25cdf9084 155
jasonberry 24:d2e25cdf9084 156 //Step 12: SEND TEMPERATURE UP
jasonberry 24:d2e25cdf9084 157 //
jasonberry 24:d2e25cdf9084 158
jasonberry 24:d2e25cdf9084 159 //Step 13: TURN ON/OFF LED1 ON MBED
jasonberry 24:d2e25cdf9084 160 //
jasonberry 24:d2e25cdf9084 161
jasonberry 24:d2e25cdf9084 162 //Step14: Set IFTTT APPLET TO SEND VALUE TO MBED VIA THINGSPEAK MQTT
jasonberry 24:d2e25cdf9084 163 // use webhook see tuturial for controlling leds via google assistant via ardunio, used this to help
jasonberry 24:d2e25cdf9084 164 // see https://www.engineersgarage.com/electronic-projects/esp32-voice-operated-home-automation-with-thingspeak-mqtt-ifttt-and-google-assistant/
jasonberry 24:d2e25cdf9084 165
jasonberry 24:d2e25cdf9084 166 //Step15: HTTP-TO-MQTT-BRIDGE(not tested)
jasonberry 24:d2e25cdf9084 167 // Set up https to mqtt bridge which allow us to send ifttt to mbed without thinspeak.
jasonberry 24:d2e25cdf9084 168 // see this tutrial https://www.home-assistant.io/blog/2017/03/28/http-to-mqtt-bridge/
jasonberry 24:d2e25cdf9084 169
jasonberry 24:d2e25cdf9084 170 //////////////////////////////////////////////////////////////////////////////////////////////////////////////
jasonberry 24:d2e25cdf9084 171
jasonberry 24:d2e25cdf9084 172 #include "mbed.h"
Jan Jongboom 21:a68bd76740f9 173 // change this to 1 to output messages to LCD instead of serial
Jan Jongboom 21:a68bd76740f9 174 #define USE_LCD 0
icraggs 1:a1d5c7a6acbc 175
icraggs 18:07a79d8f01c3 176 #if USE_LCD
icraggs 17:0811bdbdd78a 177 #include "C12832.h"
icraggs 2:638c854c0695 178
Jan Jongboom 20:49c9daf2b0ff 179 // the actual pins are defined in mbed_app.json and can be overridden per target
Jan Jongboom 20:49c9daf2b0ff 180 C12832 lcd(LCD_MOSI, LCD_SCK, LCD_MISO, LCD_A0, LCD_NCS);
icraggs 17:0811bdbdd78a 181
Jan Jongboom 20:49c9daf2b0ff 182 #define logMessage lcd.cls();lcd.printf
Jan Jongboom 20:49c9daf2b0ff 183
jasonberry 24:d2e25cdf9084 184 #else //use serial printf
Jan Jongboom 20:49c9daf2b0ff 185
jasonberry 24:d2e25cdf9084 186 #define logMessage pc.printf
jasonberry 24:d2e25cdf9084 187 Serial pc(USBTX, USBRX);
icraggs 18:07a79d8f01c3 188 #endif
icraggs 18:07a79d8f01c3 189
icraggs 17:0811bdbdd78a 190 #define MQTTCLIENT_QOS2 1
icraggs 0:0cae29831d01 191
Jan Jongboom 20:49c9daf2b0ff 192 #include "easy-connect.h"
Jan Jongboom 20:49c9daf2b0ff 193 #include "MQTTNetwork.h"
Jan Jongboom 20:49c9daf2b0ff 194 #include "MQTTmbed.h"
icraggs 2:638c854c0695 195 #include "MQTTClient.h"
icraggs 2:638c854c0695 196
jasonberry 24:d2e25cdf9084 197 int arrivedcount = 0; //number of message received back from mqtt broker
icraggs 2:638c854c0695 198
jasonberry 24:d2e25cdf9084 199 //message received handler
icraggs 9:5beb8609e9f7 200 void messageArrived(MQTT::MessageData& md)
icraggs 2:638c854c0695 201 {
icraggs 9:5beb8609e9f7 202 MQTT::Message &message = md.message;
Jan Jongboom 20:49c9daf2b0ff 203 logMessage("Message arrived: qos %d, retained %d, dup %d, packetid %d\r\n", message.qos, message.retained, message.dup, message.id);
Jan Jongboom 20:49c9daf2b0ff 204 logMessage("Payload %.*s\r\n", message.payloadlen, (char*)message.payload);
icraggs 2:638c854c0695 205 ++arrivedcount;
jasonberry 24:d2e25cdf9084 206
icraggs 2:638c854c0695 207 }
icraggs 0:0cae29831d01 208
icraggs 2:638c854c0695 209
icraggs 2:638c854c0695 210 int main(int argc, char* argv[])
Jan Jongboom 20:49c9daf2b0ff 211 {
jasonberry 24:d2e25cdf9084 212 int version = 111; //version number used for test messages to mqtt
jasonberry 24:d2e25cdf9084 213
jasonberry 24:d2e25cdf9084 214 // mqtt publish and subscribe topics linked to mqtt.thinspeak.com, channel api
jasonberry 24:d2e25cdf9084 215 // and channel read(suscribe) and write(publish) api...these can be used in any mqtt client conection..
jasonberry 24:d2e25cdf9084 216 // for pc testing could use mqtt.fx
jasonberry 24:d2e25cdf9084 217 // the following mathswork shows how to set up a desktop client to test thinspeak channel
jasonberry 24:d2e25cdf9084 218 // https://uk.mathworks.com/help/thingspeak/use-desktop-mqtt-client-to-publish-to-a-channel.html#d122e3819
jasonberry 24:d2e25cdf9084 219
jasonberry 24:d2e25cdf9084 220 char* publish_topic = "channels/1322442/publish/fields/field1/PUWBQJL3X64GWL2P";
jasonberry 24:d2e25cdf9084 221 char* subscribe_topic = "channels/1322442/subscribe/fields/field1/H0P76513L8Z3N555";
jasonberry 24:d2e25cdf9084 222
jasonberry 24:d2e25cdf9084 223 //serial comms at 115200
jasonberry 24:d2e25cdf9084 224 pc.baud(115200);
jasonberry 24:d2e25cdf9084 225
jasonberry 24:d2e25cdf9084 226 logMessage("HelloMQTT: version is %u\r\n", version);
Jan Jongboom 20:49c9daf2b0ff 227
jasonberry 24:d2e25cdf9084 228 //set up a wifi connection
jasonberry 24:d2e25cdf9084 229 //////////////////////////
jasonberry 24:d2e25cdf9084 230
jasonberry 24:d2e25cdf9084 231 //connect to wifi network and display ip address..display in serial
Jan Jongboom 20:49c9daf2b0ff 232 NetworkInterface* network = easy_connect(true);
Jan Jongboom 20:49c9daf2b0ff 233 if (!network) {
Jan Jongboom 20:49c9daf2b0ff 234 return -1;
Jan Jongboom 20:49c9daf2b0ff 235 }
jasonberry 24:d2e25cdf9084 236
jasonberry 24:d2e25cdf9084 237 //set up a MQTT connection
jasonberry 24:d2e25cdf9084 238 //////////////////////////
jasonberry 24:d2e25cdf9084 239
Jan Jongboom 20:49c9daf2b0ff 240 MQTTNetwork mqttNetwork(network);
Jan Jongboom 20:49c9daf2b0ff 241
Jan Jongboom 21:a68bd76740f9 242 MQTT::Client<MQTTNetwork, Countdown> client(mqttNetwork);
jasonberry 24:d2e25cdf9084 243
jasonberry 24:d2e25cdf9084 244 // fixed ip for mqtt broker at mqtt.thingspeak.com found using
jasonberry 24:d2e25cdf9084 245 // windows command: tracert mqtt.thinspeak.com ..note will change keep an eye on it
jasonberry 24:d2e25cdf9084 246 char* hostname = "34.231.253.113";
icraggs 6:e4c690c45021 247 int port = 1883;
jasonberry 24:d2e25cdf9084 248
Jan Jongboom 20:49c9daf2b0ff 249 logMessage("Connecting to %s:%d\r\n", hostname, port);
Jan Jongboom 20:49c9daf2b0ff 250 int rc = mqttNetwork.connect(hostname, port);
icraggs 6:e4c690c45021 251 if (rc != 0)
Jan Jongboom 20:49c9daf2b0ff 252 logMessage("rc from TCP connect is %d\r\n", rc);
Jan Jongboom 20:49c9daf2b0ff 253
jasonberry 24:d2e25cdf9084 254 //connect to client
Jan Jongboom 20:49c9daf2b0ff 255 MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
icraggs 6:e4c690c45021 256 data.MQTTVersion = 3;
jasonberry 24:d2e25cdf9084 257 data.clientID.cstring = "mbed-sample"; //can be any name,client just dont connect from two different client
jasonberry 24:d2e25cdf9084 258 data.username.cstring = "testuser"; //with identical name,client
jasonberry 24:d2e25cdf9084 259 data.password.cstring = "HXPY4BZ5Z0OPAH0W"; // this thinspeak mqtt api found in thingspeak profiles
jasonberry 24:d2e25cdf9084 260
icraggs 16:28d062c5522b 261 if ((rc = client.connect(data)) != 0)
Jan Jongboom 20:49c9daf2b0ff 262 logMessage("rc from MQTT connect is %d\r\n", rc);
jasonberry 24:d2e25cdf9084 263
jasonberry 24:d2e25cdf9084 264 //subscribe and read last message posted to mqtt
jasonberry 24:d2e25cdf9084 265 ////////////////////////////////////////////////
jasonberry 24:d2e25cdf9084 266 //note data received with handled above in
jasonberry 24:d2e25cdf9084 267 if ((rc = client.subscribe(subscribe_topic, MQTT::QOS0, messageArrived)) != 0)
Jan Jongboom 20:49c9daf2b0ff 268 logMessage("rc from MQTT subscribe is %d\r\n", rc);
jasonberry 24:d2e25cdf9084 269
jasonberry 24:d2e25cdf9084 270
jasonberry 24:d2e25cdf9084 271 //debug test message can be turned off once happy with functioning
jasonberry 24:d2e25cdf9084 272 logMessage("test point 1..end of subscribe..\r\n");
jasonberry 24:d2e25cdf9084 273
jasonberry 24:d2e25cdf9084 274 //publish and write to mqtt broker..this will then make its way to thinspeak channel
jasonberry 24:d2e25cdf9084 275 ////////////////////////////////////////////////////////////////////////////////////
jasonberry 24:d2e25cdf9084 276 // note can only publish every 15 seconds to thingspeak mqtt
jasonberry 24:d2e25cdf9084 277
icraggs 2:638c854c0695 278 MQTT::Message message;
icraggs 0:0cae29831d01 279
icraggs 2:638c854c0695 280 // QoS 0
icraggs 2:638c854c0695 281 char buf[100];
jasonberry 24:d2e25cdf9084 282 //sprintf(buf, "Hello World! QoS 0 message from app version %f\r\n", version);
jasonberry 24:d2e25cdf9084 283 sprintf(buf, "%u",version);//test number is version
icraggs 2:638c854c0695 284 message.qos = MQTT::QOS0;
jasonberry 24:d2e25cdf9084 285 message.retained = false; //false;
icraggs 2:638c854c0695 286 message.dup = false;
icraggs 2:638c854c0695 287 message.payload = (void*)buf;
icraggs 2:638c854c0695 288 message.payloadlen = strlen(buf)+1;
jasonberry 24:d2e25cdf9084 289
jasonberry 24:d2e25cdf9084 290 logMessage("test point 2..start of publish..\r\n");
jasonberry 24:d2e25cdf9084 291 rc = client.publish(publish_topic , message);
icraggs 12:086a9314e8a5 292 while (arrivedcount < 1)
icraggs 2:638c854c0695 293 client.yield(100);
jasonberry 24:d2e25cdf9084 294 logMessage("test point 3...end of publish..\r\n");
jasonberry 24:d2e25cdf9084 295
jasonberry 24:d2e25cdf9084 296 /* not tested on this set up other fields possible
icraggs 2:638c854c0695 297 // QoS 1
Jan Jongboom 20:49c9daf2b0ff 298 sprintf(buf, "Hello World! QoS 1 message from app version %f\r\n", version);
icraggs 2:638c854c0695 299 message.qos = MQTT::QOS1;
icraggs 2:638c854c0695 300 message.payloadlen = strlen(buf)+1;
jasonberry 24:d2e25cdf9084 301 rc = client.publish(publish_topic , message);
icraggs 12:086a9314e8a5 302 while (arrivedcount < 2)
icraggs 2:638c854c0695 303 client.yield(100);
Jan Jongboom 20:49c9daf2b0ff 304
icraggs 2:638c854c0695 305 // QoS 2
Jan Jongboom 20:49c9daf2b0ff 306 sprintf(buf, "Hello World! QoS 2 message from app version %f\r\n", version);
icraggs 2:638c854c0695 307 message.qos = MQTT::QOS2;
icraggs 2:638c854c0695 308 message.payloadlen = strlen(buf)+1;
icraggs 16:28d062c5522b 309 rc = client.publish(topic, message);
icraggs 12:086a9314e8a5 310 while (arrivedcount < 3)
icraggs 2:638c854c0695 311 client.yield(100);
jasonberry 24:d2e25cdf9084 312 */
Jan Jongboom 20:49c9daf2b0ff 313
jasonberry 24:d2e25cdf9084 314 //unsubscribe and disconnect from client
jasonberry 24:d2e25cdf9084 315 ////////////////////////////////////////
jasonberry 24:d2e25cdf9084 316 if ((rc = client.unsubscribe(subscribe_topic)) != 0)
Jan Jongboom 20:49c9daf2b0ff 317 logMessage("rc from unsubscribe was %d\r\n", rc);
Jan Jongboom 20:49c9daf2b0ff 318
icraggs 8:a3e3113054a1 319 if ((rc = client.disconnect()) != 0)
Jan Jongboom 20:49c9daf2b0ff 320 logMessage("rc from disconnect was %d\r\n", rc);
jasonberry 24:d2e25cdf9084 321
jasonberry 24:d2e25cdf9084 322 //disconnect from wifi network
jasonberry 24:d2e25cdf9084 323 //////////////////////////////
jasonberry 24:d2e25cdf9084 324 //this would use a lot of current (mA) up battery life,
jasonberry 24:d2e25cdf9084 325 //if connecting and disconnecting to wifi each time you sent received a message
jasonberry 24:d2e25cdf9084 326
Jan Jongboom 20:49c9daf2b0ff 327 mqttNetwork.disconnect();
jasonberry 24:d2e25cdf9084 328 wait(1);
jasonberry 24:d2e25cdf9084 329
jasonberry 24:d2e25cdf9084 330 logMessage("Version %d: finish %d msgs\r\n", version, arrivedcount);
Jan Jongboom 20:49c9daf2b0ff 331
icraggs 0:0cae29831d01 332 return 0;
jasonberry 24:d2e25cdf9084 333
jasonberry 24:d2e25cdf9084 334 }//end of main, note: no super loop here runs once at reset