This is the initial version of the IIoT quickstart program implemented on the AT&T IoT kit.
Dependencies: WNCInterface mbed-rtos mbed
IBM Watson IoT Platform Quickstart program using the AT&T/Avnet IoT Starter Kit
Using the AT&T Cellular IoT Starter Kit from Avnet the this program publishes temperature and/or humidity to the IBM Watson IoT Platform Quickstart site. The user can switch between temperature or humidity. The user can select which data series to display by selecting the event at the bottom of the display.
NOTE: This doc is specific to using the AT&T Cellular IoT Starter Kit which contains a FRDM-K64F from NXP. Ensure that the mbed online compiler has the platform set to FRDM-K64F.
1. Launch mbed online compiler in your browser
2. In a seperate browser Tab, goto the Avnet BluemixQS site and select the Import into Compiler button in the upper right portion of the window.
3. With the example program imported into you work-space, you have all the components needed. Simply execute the Compile button.
Expected execution outcome
Once the program is compiled and downloaded to the IoT Kit, perform the following steps:
1. Using a terminal program such as Hyperterm or Putty, connect to the Kit (select comm parameters of 115200-N81)
2. Press the `reset` button, then you should see the program start running! When it runs, the output will look similar to:
Sample Ouput
HTS221 Detected (0xBC) Temp is: 89.66 F Humid is: 08 % _____ * * *____ *____ Bluemix Quick Start example * *===* *==* using the AT&T IoT Starter Kit *___*===*___** AVNET *======* *====* This demonstration program operates the same as the original MicroZed IIoT Starter Kit except it only reads from the HTS221 temp sensor (no 31855 currently present and no generated data). Local network info... IP address is 10.61.23.226 MAC address is 11:02:72:14:95:91 Gateway address is 10.61.23.225 Your <uniqueID> is: IoT-11027214-2016 To run the demo, go to 'https://quickstart.internetofthings.ibmcloud.com/#/' and enter 'IoT-11027214-2016' as your device ID. The temperature data will then be displayed as it is received. You can switch between temperature and humidity by depressing SW2. --------------------------------------------------------------- (0) Attempting TCP connect to quickstart.messaging.internetofthings.ibmcloud.com:1883: Success! (0) Attempting MQTT connect to quickstart.messaging.internetofthings.ibmcloud.com:1883: Success! Publishing MQTT message '{"d" : {"temp" : " 90.2" }}' (27)
3. Once the program is running, go to https://quickstart.internetofthings.ibmcloud.com/# and enter your device ID (this is IoT-11027214-2016 in the above sample output) and select the GO button. As data is received it will automatically graphed and displayed below the graph. If you want to switch and display humidity rather than temperature (temperature is the default), depress SW2 on the FRDM-K64F board and select humid in the data under the graph.
License
This library is released under the Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License and may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
main.cpp@2:d6c16dd96091, 2016-11-17 (annotated)
- Committer:
- root@developer-sjc-cyan-compiler.local.mbed.org
- Date:
- Thu Nov 17 18:28:43 2016 +0000
- Revision:
- 2:d6c16dd96091
- Parent:
- 1:d8d4c57daaad
Added tag att_cellular_K64_wnc_14A2A_20161117 for changeset d8d4c57daaad
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JMF | 0:6a929f0d0e58 | 1 | //#define MQTT_DEBUG |
JMF | 0:6a929f0d0e58 | 2 | |
JMF | 1:d8d4c57daaad | 3 | //org=9k09br |
JMF | 1:d8d4c57daaad | 4 | //type=cc |
JMF | 1:d8d4c57daaad | 5 | //id=Mz-1107199-7010 |
JMF | 1:d8d4c57daaad | 6 | //auth-method=token |
JMF | 1:d8d4c57daaad | 7 | //auth-token=SecurityToekn99 |
JMF | 1:d8d4c57daaad | 8 | // |
JMF | 1:d8d4c57daaad | 9 | |
JMF | 0:6a929f0d0e58 | 10 | #include "mbed.h" |
JMF | 0:6a929f0d0e58 | 11 | #include "MQTTClient.h" |
JMF | 0:6a929f0d0e58 | 12 | #include "MQTTFormat.h" |
JMF | 0:6a929f0d0e58 | 13 | |
JMF | 0:6a929f0d0e58 | 14 | //#include "MQTTEthernet.h" |
JMF | 0:6a929f0d0e58 | 15 | #include "MQTTWNCInterface.h" |
JMF | 0:6a929f0d0e58 | 16 | #include "rtos.h" |
JMF | 0:6a929f0d0e58 | 17 | #include "k64f.h" |
JMF | 0:6a929f0d0e58 | 18 | #include "HTS221.h" |
JMF | 0:6a929f0d0e58 | 19 | |
JMF | 0:6a929f0d0e58 | 20 | I2C i2c(PTC11, PTC10); //SDA, SCL -- define the I2C pins being used |
JMF | 0:6a929f0d0e58 | 21 | MODSERIAL pc(USBTX,USBRX,256,256); |
JMF | 0:6a929f0d0e58 | 22 | |
JMF | 0:6a929f0d0e58 | 23 | #include "hardware.h" |
JMF | 0:6a929f0d0e58 | 24 | |
JMF | 0:6a929f0d0e58 | 25 | |
JMF | 0:6a929f0d0e58 | 26 | // connect options for MQTT broker |
JMF | 0:6a929f0d0e58 | 27 | #define CLIENT "quickstart" |
JMF | 0:6a929f0d0e58 | 28 | #define URL CLIENT ".messaging.internetofthings.ibmcloud.com" |
JMF | 0:6a929f0d0e58 | 29 | #define CONFIGTYPE "d:" CLIENT ":MicroZed:%s" |
JMF | 0:6a929f0d0e58 | 30 | |
JMF | 0:6a929f0d0e58 | 31 | #define PORT 1883 // MQTT broker port number |
JMF | 0:6a929f0d0e58 | 32 | #define CLIENTID "96430312d8f7" // use MAC address without colons |
JMF | 0:6a929f0d0e58 | 33 | #define USERNAME "" // not required for demo app |
JMF | 0:6a929f0d0e58 | 34 | #define PASSWORD "" // not required for demo app |
JMF | 0:6a929f0d0e58 | 35 | #define PUBLISH_TOPIC "iot-2/evt/status/fmt/json" // MQTT topic |
JMF | 0:6a929f0d0e58 | 36 | #define SUBSCRIBTOPIC "iot-2/cmd/+/fmt/+" |
JMF | 0:6a929f0d0e58 | 37 | |
JMF | 0:6a929f0d0e58 | 38 | #define HTS221_STR " Temp is: %0.2f F \n\r Humid is: %02d %%\n\r" |
JMF | 0:6a929f0d0e58 | 39 | |
JMF | 0:6a929f0d0e58 | 40 | Queue<uint32_t, 6> messageQ; |
JMF | 0:6a929f0d0e58 | 41 | |
JMF | 0:6a929f0d0e58 | 42 | struct rcvd_errs{ |
JMF | 0:6a929f0d0e58 | 43 | int err; |
JMF | 0:6a929f0d0e58 | 44 | char *er; |
JMF | 0:6a929f0d0e58 | 45 | }; |
JMF | 0:6a929f0d0e58 | 46 | |
JMF | 0:6a929f0d0e58 | 47 | rcvd_errs response[] = { |
JMF | 0:6a929f0d0e58 | 48 | 200, "200 OK - Request has succeeded.", |
JMF | 0:6a929f0d0e58 | 49 | 201, "201 Created - Request has been fulfilled and a new resource created.", |
JMF | 0:6a929f0d0e58 | 50 | 202, "202 Accepted - The request has been accepted for processing, but the processing will be completed asynchronously", |
JMF | 0:6a929f0d0e58 | 51 | 204, "204 No Content - The server has fulfilled the request but does not need to return an entity-body.", |
JMF | 0:6a929f0d0e58 | 52 | 400, "400 Bad Request - Bad request (e.g. sending an array when the API is expecting a hash.)", |
JMF | 0:6a929f0d0e58 | 53 | 401, "401 Unauthorized - No valid API key provided.", |
JMF | 0:6a929f0d0e58 | 54 | 403, "403 Forbidden - You tried to access a disabled device, or your API key is not allowed to access that resource, etc.", |
JMF | 0:6a929f0d0e58 | 55 | 404, "404 Not Found - The requested item could not be found.", |
JMF | 0:6a929f0d0e58 | 56 | 405, "405 Method Not Allowed - The HTTP method specified is not allowed.", |
JMF | 0:6a929f0d0e58 | 57 | 415, "415 Unsupported Media Type - The requested media type is currently not supported.", |
JMF | 0:6a929f0d0e58 | 58 | 422, "422 Unprocessable Entity - Can result from sending invalid fields.", |
JMF | 0:6a929f0d0e58 | 59 | 429, "429 Too Many Requests - The user has sent too many requests in a given period of time.", |
JMF | 0:6a929f0d0e58 | 60 | 500, "500 Server errors - Something went wrong in the M2X server", |
JMF | 0:6a929f0d0e58 | 61 | 502, "502 Server errors - Something went wrong in the M2X server", |
JMF | 0:6a929f0d0e58 | 62 | 503, "503 Server errors - Something went wrong in the M2X server", |
JMF | 0:6a929f0d0e58 | 63 | 504, "504 Server errors - Something went wrong in the M2X server", |
JMF | 0:6a929f0d0e58 | 64 | }; |
JMF | 0:6a929f0d0e58 | 65 | #define RCMAX sizeof(response)/sizeof(rcvd_errs) |
JMF | 0:6a929f0d0e58 | 66 | |
JMF | 0:6a929f0d0e58 | 67 | char * response_str(int rc) { |
JMF | 0:6a929f0d0e58 | 68 | static char *unkown = "Unknown error code..."; |
JMF | 0:6a929f0d0e58 | 69 | int i=0; |
JMF | 0:6a929f0d0e58 | 70 | while( response[i].err != rc && i < RCMAX) |
JMF | 0:6a929f0d0e58 | 71 | i++; |
JMF | 0:6a929f0d0e58 | 72 | return (i<RCMAX? response[i].er : unkown); |
JMF | 0:6a929f0d0e58 | 73 | } |
JMF | 0:6a929f0d0e58 | 74 | |
JMF | 0:6a929f0d0e58 | 75 | // LED color control function |
JMF | 0:6a929f0d0e58 | 76 | void controlLED(color_t led_color) { |
JMF | 0:6a929f0d0e58 | 77 | switch(led_color) { |
JMF | 0:6a929f0d0e58 | 78 | case red : |
JMF | 0:6a929f0d0e58 | 79 | greenLED = blueLED = 1; |
JMF | 0:6a929f0d0e58 | 80 | redLED = 0.7; |
JMF | 0:6a929f0d0e58 | 81 | break; |
JMF | 0:6a929f0d0e58 | 82 | case green : |
JMF | 0:6a929f0d0e58 | 83 | redLED = blueLED = 1; |
JMF | 0:6a929f0d0e58 | 84 | greenLED = 0.7; |
JMF | 0:6a929f0d0e58 | 85 | break; |
JMF | 0:6a929f0d0e58 | 86 | case blue : |
JMF | 0:6a929f0d0e58 | 87 | redLED = greenLED = 1; |
JMF | 0:6a929f0d0e58 | 88 | blueLED = 0.7; |
JMF | 0:6a929f0d0e58 | 89 | break; |
JMF | 0:6a929f0d0e58 | 90 | case off : |
JMF | 0:6a929f0d0e58 | 91 | redLED = greenLED = blueLED = 1; |
JMF | 0:6a929f0d0e58 | 92 | break; |
JMF | 0:6a929f0d0e58 | 93 | } |
JMF | 0:6a929f0d0e58 | 94 | } |
JMF | 0:6a929f0d0e58 | 95 | |
JMF | 0:6a929f0d0e58 | 96 | // Switch 2 interrupt handler |
JMF | 0:6a929f0d0e58 | 97 | void sw2_ISR(void) { |
JMF | 0:6a929f0d0e58 | 98 | messageQ.put((uint32_t*)22); |
JMF | 0:6a929f0d0e58 | 99 | } |
JMF | 0:6a929f0d0e58 | 100 | |
JMF | 0:6a929f0d0e58 | 101 | // Switch3 interrupt handler |
JMF | 0:6a929f0d0e58 | 102 | void sw3_ISR(void) { |
JMF | 0:6a929f0d0e58 | 103 | messageQ.put((uint32_t*)33); |
JMF | 0:6a929f0d0e58 | 104 | } |
JMF | 0:6a929f0d0e58 | 105 | |
JMF | 0:6a929f0d0e58 | 106 | // MQTT message arrived callback function |
JMF | 0:6a929f0d0e58 | 107 | void messageArrived(MQTT::MessageData& md) { |
JMF | 0:6a929f0d0e58 | 108 | MQTT::Message &message = md.message; |
JMF | 0:6a929f0d0e58 | 109 | printf("Receiving MQTT message: %.*s\r\n", message.payloadlen, (char*)message.payload); |
JMF | 0:6a929f0d0e58 | 110 | |
JMF | 0:6a929f0d0e58 | 111 | if (message.payloadlen == 3) { |
JMF | 0:6a929f0d0e58 | 112 | if (strncmp((char*)message.payload, "red", 3) == 0) |
JMF | 0:6a929f0d0e58 | 113 | controlLED(red); |
JMF | 0:6a929f0d0e58 | 114 | |
JMF | 0:6a929f0d0e58 | 115 | else if(strncmp((char*)message.payload, "grn", 3) == 0) |
JMF | 0:6a929f0d0e58 | 116 | controlLED(green); |
JMF | 0:6a929f0d0e58 | 117 | |
JMF | 0:6a929f0d0e58 | 118 | else if(strncmp((char*)message.payload, "blu", 3) == 0) |
JMF | 0:6a929f0d0e58 | 119 | controlLED(blue); |
JMF | 0:6a929f0d0e58 | 120 | |
JMF | 0:6a929f0d0e58 | 121 | else if(strncmp((char*)message.payload, "off", 3) == 0) |
JMF | 0:6a929f0d0e58 | 122 | controlLED(off); |
JMF | 0:6a929f0d0e58 | 123 | } |
JMF | 0:6a929f0d0e58 | 124 | } |
JMF | 0:6a929f0d0e58 | 125 | |
JMF | 0:6a929f0d0e58 | 126 | int main() { |
JMF | 0:6a929f0d0e58 | 127 | int rc, qStart=0, txSel=0, good = 0; |
JMF | 0:6a929f0d0e58 | 128 | Timer tmr; |
JMF | 0:6a929f0d0e58 | 129 | char* topic = PUBLISH_TOPIC; |
JMF | 1:d8d4c57daaad | 130 | char clientID[100], buf[100]; |
JMF | 1:d8d4c57daaad | 131 | string st, uniqueID; |
JMF | 0:6a929f0d0e58 | 132 | |
JMF | 0:6a929f0d0e58 | 133 | HTS221 hts221; |
JMF | 0:6a929f0d0e58 | 134 | |
JMF | 0:6a929f0d0e58 | 135 | pc.baud(115200); |
JMF | 0:6a929f0d0e58 | 136 | rc = hts221.init(); |
JMF | 0:6a929f0d0e58 | 137 | if ( rc ) { |
JMF | 0:6a929f0d0e58 | 138 | pc.printf(BLU "HTS221 Detected (0x%02X)\n\r",rc); |
JMF | 0:6a929f0d0e58 | 139 | pc.printf(HTS221_STR, CTOF(hts221.readTemperature()), hts221.readHumidity()/10); |
JMF | 0:6a929f0d0e58 | 140 | } |
JMF | 0:6a929f0d0e58 | 141 | else { |
JMF | 0:6a929f0d0e58 | 142 | pc.printf(RED "HTS221 NOT DETECTED!\n\r"); |
JMF | 0:6a929f0d0e58 | 143 | } |
JMF | 0:6a929f0d0e58 | 144 | |
JMF | 0:6a929f0d0e58 | 145 | |
JMF | 0:6a929f0d0e58 | 146 | // turn off LED |
JMF | 0:6a929f0d0e58 | 147 | controlLED(blue); |
JMF | 0:6a929f0d0e58 | 148 | |
JMF | 0:6a929f0d0e58 | 149 | // set SW2 and SW3 to generate interrupt on falling edge |
JMF | 0:6a929f0d0e58 | 150 | switch2.fall(&sw2_ISR); |
JMF | 0:6a929f0d0e58 | 151 | switch3.fall(&sw3_ISR); |
JMF | 0:6a929f0d0e58 | 152 | |
JMF | 0:6a929f0d0e58 | 153 | // initialize ethernet interface |
JMF | 0:6a929f0d0e58 | 154 | MQTTwnc ipstack = MQTTwnc(); |
JMF | 0:6a929f0d0e58 | 155 | |
JMF | 0:6a929f0d0e58 | 156 | // get and display client network info |
JMF | 0:6a929f0d0e58 | 157 | WNCInterface& eth = ipstack.getEth(); |
JMF | 0:6a929f0d0e58 | 158 | |
JMF | 0:6a929f0d0e58 | 159 | // construct the MQTT client |
JMF | 0:6a929f0d0e58 | 160 | MQTT::Client<MQTTwnc, Countdown> client = MQTT::Client<MQTTwnc, Countdown>(ipstack); |
JMF | 0:6a929f0d0e58 | 161 | |
JMF | 0:6a929f0d0e58 | 162 | char* hostname = URL; |
JMF | 0:6a929f0d0e58 | 163 | int port = PORT; |
JMF | 1:d8d4c57daaad | 164 | st = eth.getMACAddress(); |
JMF | 1:d8d4c57daaad | 165 | uniqueID="IoT-"; |
JMF | 1:d8d4c57daaad | 166 | uniqueID += st[0]; |
JMF | 1:d8d4c57daaad | 167 | uniqueID += st[1]; |
JMF | 1:d8d4c57daaad | 168 | uniqueID += st[3]; |
JMF | 1:d8d4c57daaad | 169 | uniqueID += st[4]; |
JMF | 1:d8d4c57daaad | 170 | uniqueID += st[6]; |
JMF | 1:d8d4c57daaad | 171 | uniqueID += st[7]; |
JMF | 1:d8d4c57daaad | 172 | uniqueID += st[9]; |
JMF | 1:d8d4c57daaad | 173 | uniqueID += st[10]; |
JMF | 1:d8d4c57daaad | 174 | uniqueID += "-2016"; |
JMF | 1:d8d4c57daaad | 175 | |
JMF | 1:d8d4c57daaad | 176 | sprintf(clientID, CONFIGTYPE, uniqueID.c_str()); |
JMF | 0:6a929f0d0e58 | 177 | |
JMF | 0:6a929f0d0e58 | 178 | printf(" _____\r\n"); |
JMF | 0:6a929f0d0e58 | 179 | printf(" * *\r\n"); |
JMF | 1:d8d4c57daaad | 180 | printf(" *____ *____ Bluemix Quick Start example\r\n"); |
JMF | 1:d8d4c57daaad | 181 | printf(" * *===* *==* using the AT&T IoT Starter Kit\r\n"); |
JMF | 0:6a929f0d0e58 | 182 | printf(" *___*===*___** AVNET\r\n"); |
JMF | 0:6a929f0d0e58 | 183 | printf(" *======*\r\n"); |
JMF | 0:6a929f0d0e58 | 184 | printf(" *====*\r\n"); |
JMF | 0:6a929f0d0e58 | 185 | printf("\r\n"); |
JMF | 0:6a929f0d0e58 | 186 | printf("This demonstration program operates the same as the original \r\n"); |
JMF | 0:6a929f0d0e58 | 187 | printf("MicroZed IIoT Starter Kit except it only reads from the HTS221 \r\n"); |
JMF | 0:6a929f0d0e58 | 188 | printf("temp sensor (no 31855 currently present and no generated data).\r\n"); |
JMF | 0:6a929f0d0e58 | 189 | printf("\r\n"); |
JMF | 0:6a929f0d0e58 | 190 | printf("Local network info...\r\n"); |
JMF | 0:6a929f0d0e58 | 191 | printf("IP address is %s\r\n", eth.getIPAddress()); |
JMF | 0:6a929f0d0e58 | 192 | printf("MAC address is %s\r\n", eth.getMACAddress()); |
JMF | 0:6a929f0d0e58 | 193 | printf("Gateway address is %s\r\n", eth.getGateway()); |
JMF | 1:d8d4c57daaad | 194 | printf("Your <uniqueID> is: %s\r\n", uniqueID.c_str()); |
JMF | 1:d8d4c57daaad | 195 | printf("\r\nTo run the demo, go to 'https://quickstart.internetofthings.ibmcloud.com/#/'\r\n"); |
JMF | 1:d8d4c57daaad | 196 | printf("and enter '%s' as your device ID. The temprature data will then be displayed\r\n",uniqueID.c_str()); |
JMF | 1:d8d4c57daaad | 197 | printf("as it is received. You can switch between temprature and humidity by depressing SW2.\r\n"); |
JMF | 0:6a929f0d0e58 | 198 | printf("---------------------------------------------------------------\r\n"); |
JMF | 0:6a929f0d0e58 | 199 | |
JMF | 0:6a929f0d0e58 | 200 | MQTTPacket_connectData data = MQTTPacket_connectData_initializer; |
JMF | 0:6a929f0d0e58 | 201 | |
JMF | 0:6a929f0d0e58 | 202 | int tries; |
JMF | 0:6a929f0d0e58 | 203 | |
JMF | 0:6a929f0d0e58 | 204 | while( !good ) { |
JMF | 0:6a929f0d0e58 | 205 | tries=0; |
JMF | 0:6a929f0d0e58 | 206 | // connect to TCP socket and check return code |
JMF | 0:6a929f0d0e58 | 207 | tmr.start(); |
JMF | 0:6a929f0d0e58 | 208 | rc = 1; |
JMF | 0:6a929f0d0e58 | 209 | while( rc && tries < 3) { |
JMF | 0:6a929f0d0e58 | 210 | printf("\r\n\r\n(%d) Attempting TCP connect to %s:%d: ", tries++, hostname, port); |
JMF | 0:6a929f0d0e58 | 211 | rc = ipstack.connect(hostname, port); |
JMF | 0:6a929f0d0e58 | 212 | if( rc ) { |
JMF | 0:6a929f0d0e58 | 213 | printf("Failed (%d)!\r\n",rc); |
JMF | 0:6a929f0d0e58 | 214 | while( tmr.read_ms() < 5000 ) ; |
JMF | 0:6a929f0d0e58 | 215 | tmr.reset(); |
JMF | 0:6a929f0d0e58 | 216 | } |
JMF | 0:6a929f0d0e58 | 217 | else { |
JMF | 0:6a929f0d0e58 | 218 | printf("Success!\r\n"); |
JMF | 0:6a929f0d0e58 | 219 | rc = 0; |
JMF | 0:6a929f0d0e58 | 220 | } |
JMF | 0:6a929f0d0e58 | 221 | } |
JMF | 0:6a929f0d0e58 | 222 | if( tries < 3 ) |
JMF | 0:6a929f0d0e58 | 223 | tries = 0; |
JMF | 0:6a929f0d0e58 | 224 | else |
JMF | 0:6a929f0d0e58 | 225 | continue; |
JMF | 0:6a929f0d0e58 | 226 | |
JMF | 0:6a929f0d0e58 | 227 | data.willFlag = 0; |
JMF | 0:6a929f0d0e58 | 228 | data.MQTTVersion = 3; |
JMF | 0:6a929f0d0e58 | 229 | |
JMF | 0:6a929f0d0e58 | 230 | data.clientID.cstring = clientID; |
JMF | 0:6a929f0d0e58 | 231 | // data.username.cstring = USERNAME; |
JMF | 0:6a929f0d0e58 | 232 | // data.password.cstring = PASSWORD; |
JMF | 0:6a929f0d0e58 | 233 | data.keepAliveInterval = 10; |
JMF | 0:6a929f0d0e58 | 234 | data.cleansession = 1; |
JMF | 0:6a929f0d0e58 | 235 | |
JMF | 0:6a929f0d0e58 | 236 | rc = 1; |
JMF | 0:6a929f0d0e58 | 237 | tmr.reset(); |
JMF | 0:6a929f0d0e58 | 238 | while( !client.isConnected() && rc && tries < 3) { |
JMF | 0:6a929f0d0e58 | 239 | printf("(%d) Attempting MQTT connect to %s:%d: ", tries++, hostname, port); |
JMF | 0:6a929f0d0e58 | 240 | rc = client.connect(data); |
JMF | 0:6a929f0d0e58 | 241 | if( rc ) { |
JMF | 0:6a929f0d0e58 | 242 | printf("Failed (%d)!\r\n",rc); |
JMF | 0:6a929f0d0e58 | 243 | while( tmr.read_ms() < 5000 ); |
JMF | 0:6a929f0d0e58 | 244 | tmr.reset(); |
JMF | 0:6a929f0d0e58 | 245 | } |
JMF | 0:6a929f0d0e58 | 246 | else |
JMF | 0:6a929f0d0e58 | 247 | printf("Success!\r\n"); |
JMF | 0:6a929f0d0e58 | 248 | } |
JMF | 0:6a929f0d0e58 | 249 | |
JMF | 0:6a929f0d0e58 | 250 | if( tries < 3 ) |
JMF | 0:6a929f0d0e58 | 251 | tries = 0; |
JMF | 0:6a929f0d0e58 | 252 | else |
JMF | 0:6a929f0d0e58 | 253 | continue; |
JMF | 0:6a929f0d0e58 | 254 | |
JMF | 0:6a929f0d0e58 | 255 | #if 0 |
JMF | 0:6a929f0d0e58 | 256 | //Only need to subscribe if we are not running quickstart |
JMF | 0:6a929f0d0e58 | 257 | // subscribe to MQTT topic |
JMF | 0:6a929f0d0e58 | 258 | tmr.reset(); |
JMF | 0:6a929f0d0e58 | 259 | rc = 1; |
JMF | 0:6a929f0d0e58 | 260 | while( rc && client.isConnected() && tries < 3) { |
JMF | 0:6a929f0d0e58 | 261 | printf("(%d) Attempting to subscribing to MQTT topic %s: ", tries, SUBSCRIBTOPIC); |
JMF | 0:6a929f0d0e58 | 262 | rc = client.subscribe(SUBSCRIBTOPIC, MQTT::QOS0, messageArrived); |
JMF | 0:6a929f0d0e58 | 263 | if( rc ) { |
JMF | 0:6a929f0d0e58 | 264 | printf("Failed (%d)!\r\n", rc); |
JMF | 0:6a929f0d0e58 | 265 | while( tmr.read_ms() < 5000 ); |
JMF | 0:6a929f0d0e58 | 266 | tries++; |
JMF | 0:6a929f0d0e58 | 267 | tmr.reset(); |
JMF | 0:6a929f0d0e58 | 268 | } |
JMF | 0:6a929f0d0e58 | 269 | else { |
JMF | 0:6a929f0d0e58 | 270 | good=1; |
JMF | 0:6a929f0d0e58 | 271 | printf("Subscribe successful!\r\n"); |
JMF | 0:6a929f0d0e58 | 272 | } |
JMF | 0:6a929f0d0e58 | 273 | } |
JMF | 0:6a929f0d0e58 | 274 | #else |
JMF | 0:6a929f0d0e58 | 275 | good = 1; |
JMF | 0:6a929f0d0e58 | 276 | #endif |
JMF | 0:6a929f0d0e58 | 277 | while (!good); |
JMF | 0:6a929f0d0e58 | 278 | } |
JMF | 0:6a929f0d0e58 | 279 | |
JMF | 0:6a929f0d0e58 | 280 | MQTT::Message message; |
JMF | 0:6a929f0d0e58 | 281 | message.qos = MQTT::QOS0; |
JMF | 0:6a929f0d0e58 | 282 | message.retained = false; |
JMF | 0:6a929f0d0e58 | 283 | message.dup = false; |
JMF | 0:6a929f0d0e58 | 284 | message.payload = (void*)buf; |
JMF | 0:6a929f0d0e58 | 285 | |
JMF | 0:6a929f0d0e58 | 286 | while(true) { |
JMF | 0:6a929f0d0e58 | 287 | osEvent switchEvent = messageQ.get(100); |
JMF | 0:6a929f0d0e58 | 288 | |
JMF | 0:6a929f0d0e58 | 289 | if( switchEvent.value.v == 22 ) //switch between sending humidity & temp |
JMF | 0:6a929f0d0e58 | 290 | txSel = !txSel; |
JMF | 0:6a929f0d0e58 | 291 | |
JMF | 0:6a929f0d0e58 | 292 | if( switchEvent.value.v == 33) //user wants to run in Quickstart of Demo mode |
JMF | 0:6a929f0d0e58 | 293 | qStart = !qStart; |
JMF | 0:6a929f0d0e58 | 294 | |
JMF | 0:6a929f0d0e58 | 295 | memset(buf,0x00,sizeof(buf)); |
JMF | 0:6a929f0d0e58 | 296 | if( txSel ) { |
JMF | 0:6a929f0d0e58 | 297 | rc = hts221.readHumidity(); |
JMF | 0:6a929f0d0e58 | 298 | sprintf(buf, "{\"d\" : {\"humd\" : \"%2d.%d\" }}", rc/10, rc-((rc/10)*10)); |
JMF | 0:6a929f0d0e58 | 299 | printf("Publishing MQTT message '%s' ", (char*)message.payload); |
JMF | 0:6a929f0d0e58 | 300 | } |
JMF | 0:6a929f0d0e58 | 301 | else { |
JMF | 0:6a929f0d0e58 | 302 | sprintf(buf, "{\"d\" : {\"temp\" : \"%5.1f\" }}", CTOF(hts221.readTemperature())); |
JMF | 0:6a929f0d0e58 | 303 | printf("Publishing MQTT message '%s' ", (char*)message.payload); |
JMF | 0:6a929f0d0e58 | 304 | } |
JMF | 0:6a929f0d0e58 | 305 | message.payloadlen = strlen(buf); |
JMF | 0:6a929f0d0e58 | 306 | printf("(%d)\r\n",message.payloadlen); |
JMF | 0:6a929f0d0e58 | 307 | rc = client.publish(topic, message); |
JMF | 0:6a929f0d0e58 | 308 | if( rc ) { |
JMF | 0:6a929f0d0e58 | 309 | printf("Publish request failed! (%d)\r\n",rc); |
JMF | 0:6a929f0d0e58 | 310 | FATAL_WNC_ERROR(resume); |
JMF | 0:6a929f0d0e58 | 311 | } |
JMF | 0:6a929f0d0e58 | 312 | |
JMF | 0:6a929f0d0e58 | 313 | client.yield(6000); |
JMF | 0:6a929f0d0e58 | 314 | } |
JMF | 0:6a929f0d0e58 | 315 | } |