Pubnub demo for AT&T IoT Starter Kit. Functionally similar to the Flow demo.
Dependencies: FXOS8700CQ MODSERIAL mbed
Pubnub demo for AT&T IoT Starter Kit
This demo is functionally similar to the Flow demo, so you can find general information here: https://developer.mbed.org/users/JMF/code/Avnet_ATT_Cellular_IOT/.
The only difference is that we use Pubnub to publish the measurements and subscribe to receiving the instructions to set the LED.
Settings
Pubnub related settings are:
Pubnub settings in `config_me.h`
PUBNUB_SUBSCRIBE_KEY PUBNUB_PUBLISH_KEY PUBNUB_CHANNEL
All are documented in their respective comments.
Pubnub context class
Similar to Pubnub SDKs, we provide a Pubnub context class. It is defined in pubnub.h
header file and implemented in pubnub.cpp
.
It provides only the fundamental "publish" and "subscribe" methods. They are documented in the header file.
This class is reusable in other code (it is not specific to this demo), it has a very narrow interface to the AT&T IoT cellular modem code. For example of use, you can look at the main()
(in main.c
).
Sample of published data
Published message w/measurement data
{"serial":"vstarterkit001","temp":89.61,"humidity":35,"accelX":0.97,"accelY":0.013,"accelZ":-0.038}
Don't worry, nobody got burnt, the temperature is in degrees Fahrenheit. :)
Publish a message (from, say, the Pubnub console http://pubnub.com/console) of the form {"LED":<name-of-the-color>}
on the channel that this demo listens to (default is hello_world
) to turn the LED to that color on the Starter Kit:
Turn LED to red
{"LED":"Red"}
Turn LED to green
{"LED":"Green"}
Turn LED to blue
{"LED":"Blue"}
main.cpp@81:a5df87708b9a, 2016-09-02 (annotated)
- Committer:
- sveljko
- Date:
- Fri Sep 02 17:44:55 2016 +0000
- Revision:
- 81:a5df87708b9a
- Parent:
- 77:c65eae5b9958
First version that works, forked from official AT&T IoT starter kit repository.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
fkellermavnet | 68:6e311c747045 | 1 | /* =================================================================== |
fkellermavnet | 68:6e311c747045 | 2 | Copyright © 2016, AVNET Inc. |
fkellermavnet | 68:6e311c747045 | 3 | |
fkellermavnet | 68:6e311c747045 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
fkellermavnet | 68:6e311c747045 | 5 | you may not use this file except in compliance with the License. |
fkellermavnet | 68:6e311c747045 | 6 | You may obtain a copy of the License at |
fkellermavnet | 68:6e311c747045 | 7 | |
fkellermavnet | 68:6e311c747045 | 8 | http://www.apache.org/licenses/LICENSE-2.0 |
fkellermavnet | 68:6e311c747045 | 9 | |
fkellermavnet | 68:6e311c747045 | 10 | Unless required by applicable law or agreed to in writing, |
fkellermavnet | 68:6e311c747045 | 11 | software distributed under the License is distributed on an |
fkellermavnet | 68:6e311c747045 | 12 | "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, |
fkellermavnet | 68:6e311c747045 | 13 | either express or implied. See the License for the specific |
fkellermavnet | 68:6e311c747045 | 14 | language governing permissions and limitations under the License. |
fkellermavnet | 68:6e311c747045 | 15 | |
fkellermavnet | 68:6e311c747045 | 16 | ======================================================================== */ |
fkellermavnet | 68:6e311c747045 | 17 | |
stefanrousseau | 55:3abf9e3f42e6 | 18 | #include "mbed.h" |
JMF | 0:9d5134074d84 | 19 | #include <cctype> |
JMF | 0:9d5134074d84 | 20 | #include <string> |
JMF | 2:0e2ef866af95 | 21 | #include "config_me.h" |
stefanrousseau | 4:f83bedd9cab4 | 22 | #include "sensors.h" |
stefanrousseau | 61:f6b93129f954 | 23 | #include "cell_modem.h" |
stefanrousseau | 11:e6602513730f | 24 | #include "hardware.h" |
stefanrousseau | 11:e6602513730f | 25 | |
sveljko | 81:a5df87708b9a | 26 | #include "pubnub.h" |
sveljko | 81:a5df87708b9a | 27 | |
stefanrousseau | 61:f6b93129f954 | 28 | I2C i2c(PTC11, PTC10); //SDA, SCL -- define the I2C pins being used |
stefanrousseau | 56:cb42ff383dab | 29 | MODSERIAL pc(USBTX, USBRX, 256, 256); // tx, rx with default tx, rx buffer sizes |
stefanrousseau | 63:90d7c69993cd | 30 | MODSERIAL mdm(PTD3, PTD2, 4096, 4096); |
stefanrousseau | 16:17c5916f2d12 | 31 | DigitalOut led_green(LED_GREEN); |
stefanrousseau | 16:17c5916f2d12 | 32 | DigitalOut led_red(LED_RED); |
stefanrousseau | 16:17c5916f2d12 | 33 | DigitalOut led_blue(LED_BLUE); |
JMF | 0:9d5134074d84 | 34 | |
JMF | 0:9d5134074d84 | 35 | |
stefanrousseau | 3:26b3cc155f39 | 36 | //******************************************************************************************************************************************** |
stefanrousseau | 12:7c94ec5069dc | 37 | //* Create string with sensor readings that can be sent to flow as an HTTP get |
stefanrousseau | 3:26b3cc155f39 | 38 | //******************************************************************************************************************************************** |
stefanrousseau | 12:7c94ec5069dc | 39 | K64F_Sensors_t SENSOR_DATA = |
stefanrousseau | 3:26b3cc155f39 | 40 | { |
stefanrousseau | 12:7c94ec5069dc | 41 | .Temperature = "0", |
stefanrousseau | 12:7c94ec5069dc | 42 | .Humidity = "0", |
stefanrousseau | 12:7c94ec5069dc | 43 | .AccelX = "0", |
stefanrousseau | 12:7c94ec5069dc | 44 | .AccelY = "0", |
stefanrousseau | 12:7c94ec5069dc | 45 | .AccelZ = "0", |
stefanrousseau | 12:7c94ec5069dc | 46 | .MagnetometerX = "0", |
stefanrousseau | 12:7c94ec5069dc | 47 | .MagnetometerY = "0", |
stefanrousseau | 12:7c94ec5069dc | 48 | .MagnetometerZ = "0", |
stefanrousseau | 12:7c94ec5069dc | 49 | .AmbientLightVis = "0", |
stefanrousseau | 12:7c94ec5069dc | 50 | .AmbientLightIr = "0", |
stefanrousseau | 12:7c94ec5069dc | 51 | .UVindex = "0", |
stefanrousseau | 12:7c94ec5069dc | 52 | .Proximity = "0", |
stefanrousseau | 12:7c94ec5069dc | 53 | .Temperature_Si7020 = "0", |
stefanrousseau | 55:3abf9e3f42e6 | 54 | .Humidity_Si7020 = "0", |
stefanrousseau | 55:3abf9e3f42e6 | 55 | .Virtual_Sensor1 = "0", |
stefanrousseau | 55:3abf9e3f42e6 | 56 | .Virtual_Sensor2 = "0", |
stefanrousseau | 55:3abf9e3f42e6 | 57 | .Virtual_Sensor3 = "0", |
stefanrousseau | 55:3abf9e3f42e6 | 58 | .Virtual_Sensor4 = "0", |
stefanrousseau | 55:3abf9e3f42e6 | 59 | .Virtual_Sensor5 = "0", |
stefanrousseau | 55:3abf9e3f42e6 | 60 | .Virtual_Sensor6 = "0", |
stefanrousseau | 55:3abf9e3f42e6 | 61 | .Virtual_Sensor7 = "0", |
stefanrousseau | 71:45a5e426df81 | 62 | .Virtual_Sensor8 = "0", |
stefanrousseau | 72:b500e1507b5f | 63 | .GPS_Satellites = "0", |
stefanrousseau | 71:45a5e426df81 | 64 | .GPS_Latitude = "0", |
stefanrousseau | 71:45a5e426df81 | 65 | .GPS_Longitude = "0", |
stefanrousseau | 71:45a5e426df81 | 66 | .GPS_Altitude = "0", |
stefanrousseau | 71:45a5e426df81 | 67 | .GPS_Speed = "0", |
stefanrousseau | 71:45a5e426df81 | 68 | .GPS_Course = "0" |
stefanrousseau | 3:26b3cc155f39 | 69 | }; |
stefanrousseau | 12:7c94ec5069dc | 70 | |
fkellermavnet | 77:c65eae5b9958 | 71 | void display_app_firmware_version(void) |
fkellermavnet | 77:c65eae5b9958 | 72 | { |
fkellermavnet | 77:c65eae5b9958 | 73 | PUTS("\r\n\r\nApp Firmware: Release 1.0 - built: "__DATE__" "__TIME__"\r\n\r\n"); |
fkellermavnet | 77:c65eae5b9958 | 74 | } |
fkellermavnet | 77:c65eae5b9958 | 75 | |
sveljko | 81:a5df87708b9a | 76 | |
sveljko | 81:a5df87708b9a | 77 | static void GeneratePubnubJSON(char *s, unsigned n) |
stefanrousseau | 3:26b3cc155f39 | 78 | { |
sveljko | 81:a5df87708b9a | 79 | switch (iSensorsToReport) { |
sveljko | 81:a5df87708b9a | 80 | case TEMP_HUMIDITY_ONLY: |
sveljko | 81:a5df87708b9a | 81 | snprintf(s, n, "{\"serial\":\"%s\",\"temp\":%s,\"humidity\":%s}", THE_DEVICE_NAME, SENSOR_DATA.Temperature, SENSOR_DATA.Humidity); |
sveljko | 81:a5df87708b9a | 82 | break; |
sveljko | 81:a5df87708b9a | 83 | case TEMP_HUMIDITY_ACCELEROMETER: |
sveljko | 81:a5df87708b9a | 84 | snprintf(s, n, "{\"serial\":\"%s\",\"temp\":%s,\"humidity\":%s,\"accelX\":%s,\"accelY\":%s,\"accelZ\":%s}", THE_DEVICE_NAME, SENSOR_DATA.Temperature, SENSOR_DATA.Humidity, SENSOR_DATA.AccelX, SENSOR_DATA.AccelY, SENSOR_DATA.AccelZ); |
sveljko | 81:a5df87708b9a | 85 | break; |
sveljko | 81:a5df87708b9a | 86 | case TEMP_HUMIDITY_ACCELEROMETER_PMODSENSORS: |
sveljko | 81:a5df87708b9a | 87 | snprintf(s, n, "{\"serial\":\"%s\",\"temp\":%s,\"humidity\":%s,\"accelX\":%s,\"accelY\":%s,\"accelZ\":%s,\"proximity\":%s,\"light_uv\":%s,\"light_vis\":%s,\"light_ir\":%s}", THE_DEVICE_NAME, SENSOR_DATA.Temperature, SENSOR_DATA.Humidity, SENSOR_DATA.AccelX,SENSOR_DATA.AccelY,SENSOR_DATA.AccelZ, SENSOR_DATA.Proximity, SENSOR_DATA.UVindex, SENSOR_DATA.AmbientLightVis, SENSOR_DATA.AmbientLightIr); |
sveljko | 81:a5df87708b9a | 88 | break; |
sveljko | 81:a5df87708b9a | 89 | case TEMP_HUMIDITY_ACCELEROMETER_PMODSENSORS_VIRTUALSENSORS: |
sveljko | 81:a5df87708b9a | 90 | snprintf(s, n, "{\"serial\":\"%s\",\"temp\":%s,\"humidity\":%s,\"accelX\":%s,\"accelY\":%s,\"accelZ\":%s,\"proximity\":%s,\"light_uv\":%s,\"light_vis\":%s,\"light_ir\":%s,\"virt_sens1\":%s,\"virt_sens2\":%s,\"virt_sens3\":%s,\"virt_sens4\":%s,\"virt_sens5\":%s,\"virt_sens6\":%s,\"virt_sens7\":%s,\"virt_sens8\":%s}", |
sveljko | 81:a5df87708b9a | 91 | THE_DEVICE_NAME, |
sveljko | 81:a5df87708b9a | 92 | SENSOR_DATA.Temperature, SENSOR_DATA.Humidity, |
sveljko | 81:a5df87708b9a | 93 | SENSOR_DATA.AccelX,SENSOR_DATA.AccelY,SENSOR_DATA.AccelZ, |
sveljko | 81:a5df87708b9a | 94 | SENSOR_DATA.Proximity, SENSOR_DATA.UVindex, SENSOR_DATA.AmbientLightVis, SENSOR_DATA.AmbientLightIr, |
sveljko | 81:a5df87708b9a | 95 | SENSOR_DATA.Virtual_Sensor1, SENSOR_DATA.Virtual_Sensor2, |
sveljko | 81:a5df87708b9a | 96 | SENSOR_DATA.Virtual_Sensor3, SENSOR_DATA.Virtual_Sensor4, |
sveljko | 81:a5df87708b9a | 97 | SENSOR_DATA.Virtual_Sensor5, SENSOR_DATA.Virtual_Sensor6, |
sveljko | 81:a5df87708b9a | 98 | SENSOR_DATA.Virtual_Sensor7, SENSOR_DATA.Virtual_Sensor8); |
sveljko | 81:a5df87708b9a | 99 | break; |
sveljko | 81:a5df87708b9a | 100 | default: |
sveljko | 81:a5df87708b9a | 101 | snprintf(s, n, "\"Invalid sensors selected: %d\"", iSensorsToReport); |
sveljko | 81:a5df87708b9a | 102 | break; |
sveljko | 81:a5df87708b9a | 103 | } |
sveljko | 81:a5df87708b9a | 104 | } |
sveljko | 81:a5df87708b9a | 105 | |
sveljko | 81:a5df87708b9a | 106 | |
sveljko | 81:a5df87708b9a | 107 | static void print_pubnub_result(pubnub_ctx::result r) |
sveljko | 81:a5df87708b9a | 108 | { |
sveljko | 81:a5df87708b9a | 109 | switch (r) { |
sveljko | 81:a5df87708b9a | 110 | case pubnub_ctx::format_error: |
sveljko | 81:a5df87708b9a | 111 | PRINTF(RED "Pubnub response format error" DEF "\r\n"); |
sveljko | 81:a5df87708b9a | 112 | break; |
sveljko | 81:a5df87708b9a | 113 | case pubnub_ctx::response_too_short: |
sveljko | 81:a5df87708b9a | 114 | PRINTF(RED "Pubnub response too short" DEF "\r\n"); |
sveljko | 81:a5df87708b9a | 115 | break; |
sveljko | 81:a5df87708b9a | 116 | case pubnub_ctx::missing_open_bracket: |
sveljko | 81:a5df87708b9a | 117 | PRINTF(RED "Pubnub response missing open bracket `[`" DEF "\r\n"); |
sveljko | 81:a5df87708b9a | 118 | break; |
sveljko | 81:a5df87708b9a | 119 | case pubnub_ctx::missing_close_bracket: |
sveljko | 81:a5df87708b9a | 120 | PRINTF(RED "Pubnub response missing close bracket `]`" DEF "\r\n"); |
sveljko | 81:a5df87708b9a | 121 | break; |
sveljko | 81:a5df87708b9a | 122 | case pubnub_ctx::missing_time_token: |
sveljko | 81:a5df87708b9a | 123 | PRINTF(RED "Pubnub subscribe response missing time token" DEF "\r\n"); |
sveljko | 81:a5df87708b9a | 124 | break; |
sveljko | 81:a5df87708b9a | 125 | case pubnub_ctx::bad_time_token: |
sveljko | 81:a5df87708b9a | 126 | PRINTF(RED "Pubnub subscribe response bad time token" DEF "\r\n"); |
sveljko | 81:a5df87708b9a | 127 | break; |
sveljko | 81:a5df87708b9a | 128 | case pubnub_ctx::publish_failed: |
sveljko | 81:a5df87708b9a | 129 | PRINTF(RED "Pubnub publish failed" DEF "\r\n"); |
sveljko | 81:a5df87708b9a | 130 | break; |
sveljko | 81:a5df87708b9a | 131 | case pubnub_ctx::ok: |
sveljko | 81:a5df87708b9a | 132 | PRINTF(GRN "Pubnub transaction success" DEF "\r\n"); |
sveljko | 81:a5df87708b9a | 133 | break; |
sveljko | 81:a5df87708b9a | 134 | default: |
sveljko | 81:a5df87708b9a | 135 | PRINTF(RED "Unknown Pubnub erorr %d" DEF "\r\n", static_cast<int>(r)); |
sveljko | 81:a5df87708b9a | 136 | break; |
sveljko | 81:a5df87708b9a | 137 | } |
sveljko | 81:a5df87708b9a | 138 | } |
stefanrousseau | 3:26b3cc155f39 | 139 | |
stefanrousseau | 3:26b3cc155f39 | 140 | |
stefanrousseau | 3:26b3cc155f39 | 141 | //Periodic timer |
stefanrousseau | 3:26b3cc155f39 | 142 | Ticker OneMsTicker; |
stefanrousseau | 3:26b3cc155f39 | 143 | volatile bool bTimerExpiredFlag = false; |
stefanrousseau | 3:26b3cc155f39 | 144 | int OneMsTicks = 0; |
stefanrousseau | 3:26b3cc155f39 | 145 | int iTimer1Interval_ms = 1000; |
stefanrousseau | 3:26b3cc155f39 | 146 | //******************************************************************************************************************************************** |
stefanrousseau | 3:26b3cc155f39 | 147 | //* Periodic 1ms timer tick |
stefanrousseau | 3:26b3cc155f39 | 148 | //******************************************************************************************************************************************** |
stefanrousseau | 3:26b3cc155f39 | 149 | void OneMsFunction() |
stefanrousseau | 3:26b3cc155f39 | 150 | { |
stefanrousseau | 3:26b3cc155f39 | 151 | OneMsTicks++; |
stefanrousseau | 3:26b3cc155f39 | 152 | if ((OneMsTicks % iTimer1Interval_ms) == 0) |
stefanrousseau | 3:26b3cc155f39 | 153 | { |
stefanrousseau | 3:26b3cc155f39 | 154 | bTimerExpiredFlag = true; |
stefanrousseau | 3:26b3cc155f39 | 155 | } |
stefanrousseau | 3:26b3cc155f39 | 156 | } //OneMsFunction() |
stefanrousseau | 3:26b3cc155f39 | 157 | |
stefanrousseau | 16:17c5916f2d12 | 158 | //******************************************************************************************************************************************** |
stefanrousseau | 16:17c5916f2d12 | 159 | //* Set the RGB LED's Color |
stefanrousseau | 16:17c5916f2d12 | 160 | //* LED Color 0=Off to 7=White. 3 bits represent BGR (bit0=Red, bit1=Green, bit2=Blue) |
stefanrousseau | 16:17c5916f2d12 | 161 | //******************************************************************************************************************************************** |
stefanrousseau | 16:17c5916f2d12 | 162 | void SetLedColor(unsigned char ucColor) |
stefanrousseau | 16:17c5916f2d12 | 163 | { |
stefanrousseau | 16:17c5916f2d12 | 164 | //Note that when an LED is on, you write a 0 to it: |
stefanrousseau | 16:17c5916f2d12 | 165 | led_red = !(ucColor & 0x1); //bit 0 |
stefanrousseau | 16:17c5916f2d12 | 166 | led_green = !(ucColor & 0x2); //bit 1 |
stefanrousseau | 16:17c5916f2d12 | 167 | led_blue = !(ucColor & 0x4); //bit 2 |
stefanrousseau | 16:17c5916f2d12 | 168 | } //SetLedColor() |
stefanrousseau | 16:17c5916f2d12 | 169 | |
stefanrousseau | 16:17c5916f2d12 | 170 | //******************************************************************************************************************************************** |
stefanrousseau | 61:f6b93129f954 | 171 | //* Process the JSON response. In this example we are only extracting a LED color. |
stefanrousseau | 16:17c5916f2d12 | 172 | //******************************************************************************************************************************************** |
sveljko | 81:a5df87708b9a | 173 | bool parse_JSON(char const* json_string) |
stefanrousseau | 16:17c5916f2d12 | 174 | { |
sveljko | 81:a5df87708b9a | 175 | char const* beginquote; |
stefanrousseau | 16:17c5916f2d12 | 176 | char token[] = "\"LED\":\""; |
stefanrousseau | 16:17c5916f2d12 | 177 | beginquote = strstr(json_string, token ); |
stefanrousseau | 16:17c5916f2d12 | 178 | if ((beginquote != 0)) |
stefanrousseau | 16:17c5916f2d12 | 179 | { |
stefanrousseau | 16:17c5916f2d12 | 180 | char cLedColor = beginquote[strlen(token)]; |
stefanrousseau | 64:09004cd610df | 181 | PRINTF(GRN "LED Found : %c" DEF "\r\n", cLedColor); |
stefanrousseau | 16:17c5916f2d12 | 182 | switch(cLedColor) |
stefanrousseau | 16:17c5916f2d12 | 183 | { |
stefanrousseau | 16:17c5916f2d12 | 184 | case 'O': |
stefanrousseau | 16:17c5916f2d12 | 185 | { //Off |
stefanrousseau | 16:17c5916f2d12 | 186 | SetLedColor(0); |
stefanrousseau | 16:17c5916f2d12 | 187 | break; |
stefanrousseau | 16:17c5916f2d12 | 188 | } |
stefanrousseau | 16:17c5916f2d12 | 189 | case 'R': |
stefanrousseau | 16:17c5916f2d12 | 190 | { //Red |
stefanrousseau | 16:17c5916f2d12 | 191 | SetLedColor(1); |
stefanrousseau | 16:17c5916f2d12 | 192 | break; |
stefanrousseau | 16:17c5916f2d12 | 193 | } |
stefanrousseau | 16:17c5916f2d12 | 194 | case 'G': |
stefanrousseau | 16:17c5916f2d12 | 195 | { //Green |
stefanrousseau | 16:17c5916f2d12 | 196 | SetLedColor(2); |
stefanrousseau | 16:17c5916f2d12 | 197 | break; |
stefanrousseau | 16:17c5916f2d12 | 198 | } |
stefanrousseau | 16:17c5916f2d12 | 199 | case 'Y': |
stefanrousseau | 16:17c5916f2d12 | 200 | { //Yellow |
stefanrousseau | 16:17c5916f2d12 | 201 | SetLedColor(3); |
stefanrousseau | 16:17c5916f2d12 | 202 | break; |
stefanrousseau | 16:17c5916f2d12 | 203 | } |
stefanrousseau | 16:17c5916f2d12 | 204 | case 'B': |
stefanrousseau | 16:17c5916f2d12 | 205 | { //Blue |
stefanrousseau | 16:17c5916f2d12 | 206 | SetLedColor(4); |
stefanrousseau | 16:17c5916f2d12 | 207 | break; |
stefanrousseau | 16:17c5916f2d12 | 208 | } |
stefanrousseau | 16:17c5916f2d12 | 209 | case 'M': |
stefanrousseau | 16:17c5916f2d12 | 210 | { //Magenta |
stefanrousseau | 16:17c5916f2d12 | 211 | SetLedColor(5); |
stefanrousseau | 16:17c5916f2d12 | 212 | break; |
stefanrousseau | 16:17c5916f2d12 | 213 | } |
stefanrousseau | 16:17c5916f2d12 | 214 | case 'T': |
stefanrousseau | 16:17c5916f2d12 | 215 | { //Turquoise |
stefanrousseau | 16:17c5916f2d12 | 216 | SetLedColor(6); |
stefanrousseau | 16:17c5916f2d12 | 217 | break; |
stefanrousseau | 16:17c5916f2d12 | 218 | } |
stefanrousseau | 16:17c5916f2d12 | 219 | case 'W': |
stefanrousseau | 16:17c5916f2d12 | 220 | { //White |
stefanrousseau | 16:17c5916f2d12 | 221 | SetLedColor(7); |
stefanrousseau | 16:17c5916f2d12 | 222 | break; |
stefanrousseau | 16:17c5916f2d12 | 223 | } |
stefanrousseau | 16:17c5916f2d12 | 224 | default: |
stefanrousseau | 16:17c5916f2d12 | 225 | { |
stefanrousseau | 16:17c5916f2d12 | 226 | break; |
stefanrousseau | 16:17c5916f2d12 | 227 | } |
stefanrousseau | 16:17c5916f2d12 | 228 | } //switch(cLedColor) |
stefanrousseau | 16:17c5916f2d12 | 229 | return true; |
stefanrousseau | 16:17c5916f2d12 | 230 | } |
stefanrousseau | 16:17c5916f2d12 | 231 | else |
stefanrousseau | 16:17c5916f2d12 | 232 | { |
stefanrousseau | 16:17c5916f2d12 | 233 | return false; |
stefanrousseau | 16:17c5916f2d12 | 234 | } |
stefanrousseau | 16:17c5916f2d12 | 235 | } //parse_JSON |
stefanrousseau | 16:17c5916f2d12 | 236 | |
sveljko | 81:a5df87708b9a | 237 | |
sveljko | 81:a5df87708b9a | 238 | int main() |
sveljko | 81:a5df87708b9a | 239 | { |
stefanrousseau | 61:f6b93129f954 | 240 | static unsigned ledOnce = 0; |
stefanrousseau | 72:b500e1507b5f | 241 | //delay so that the debug terminal can open after power-on reset: |
fkellermavnet | 77:c65eae5b9958 | 242 | wait (5.0); |
stefanrousseau | 61:f6b93129f954 | 243 | pc.baud(115200); |
fkellermavnet | 77:c65eae5b9958 | 244 | |
fkellermavnet | 77:c65eae5b9958 | 245 | display_app_firmware_version(); |
fkellermavnet | 77:c65eae5b9958 | 246 | |
stefanrousseau | 64:09004cd610df | 247 | PRINTF(GRN "Hello World from the Cellular IoT Kit!\r\n\r\n"); |
JMF | 0:9d5134074d84 | 248 | |
stefanrousseau | 61:f6b93129f954 | 249 | //Initialize the I2C sensors that are present |
stefanrousseau | 11:e6602513730f | 250 | sensors_init(); |
stefanrousseau | 12:7c94ec5069dc | 251 | read_sensors(); |
stefanrousseau | 11:e6602513730f | 252 | |
stefanrousseau | 61:f6b93129f954 | 253 | // Set LED to RED until init finishes |
stefanrousseau | 61:f6b93129f954 | 254 | SetLedColor(0x1); //Red |
JMF | 0:9d5134074d84 | 255 | // Initialize the modem |
stefanrousseau | 64:09004cd610df | 256 | PRINTF("\r\n"); |
stefanrousseau | 61:f6b93129f954 | 257 | cell_modem_init(); |
fkellermavnet | 77:c65eae5b9958 | 258 | display_wnc_firmware_rev(); |
fkellermavnet | 77:c65eae5b9958 | 259 | |
stefanrousseau | 61:f6b93129f954 | 260 | // Set LED BLUE for partial init |
stefanrousseau | 61:f6b93129f954 | 261 | SetLedColor(0x4); //Blue |
JMF | 0:9d5134074d84 | 262 | |
stefanrousseau | 3:26b3cc155f39 | 263 | //Create a 1ms timer tick function: |
stefanrousseau | 61:f6b93129f954 | 264 | iTimer1Interval_ms = SENSOR_UPDATE_INTERVAL_MS; |
stefanrousseau | 3:26b3cc155f39 | 265 | OneMsTicker.attach(OneMsFunction, 0.001f) ; |
fkellermavnet | 26:8d6e7e7cdcae | 266 | |
sveljko | 81:a5df87708b9a | 267 | // Create the Pubnub context and message vector |
sveljko | 81:a5df87708b9a | 268 | pubnub_ctx pb(PUBNUB_PUBLISH_KEY, PUBNUB_SUBSCRIBE_KEY); |
sveljko | 81:a5df87708b9a | 269 | std::vector<std::string> messages; |
sveljko | 81:a5df87708b9a | 270 | |
sveljko | 81:a5df87708b9a | 271 | // If you wish, you can set the UUID to use - which will make that UUID |
sveljko | 81:a5df87708b9a | 272 | // visible for Presence related Pubnub features |
sveljko | 81:a5df87708b9a | 273 | // pb.set_uuid(THE_DEVICE_NAME); |
sveljko | 81:a5df87708b9a | 274 | |
JMF | 2:0e2ef866af95 | 275 | // Send and receive data perpetually |
JMF | 2:0e2ef866af95 | 276 | while(1) { |
sveljko | 81:a5df87708b9a | 277 | #ifdef USE_VIRTUAL_SENSORS |
stefanrousseau | 55:3abf9e3f42e6 | 278 | ProcessUsbInterface(); |
sveljko | 81:a5df87708b9a | 279 | #endif |
stefanrousseau | 3:26b3cc155f39 | 280 | if (bTimerExpiredFlag) |
stefanrousseau | 3:26b3cc155f39 | 281 | { |
stefanrousseau | 3:26b3cc155f39 | 282 | bTimerExpiredFlag = false; |
stefanrousseau | 4:f83bedd9cab4 | 283 | read_sensors(); //read available external sensors from a PMOD and the on-board motion sensor |
sveljko | 81:a5df87708b9a | 284 | |
sveljko | 81:a5df87708b9a | 285 | char json_string[512]; |
sveljko | 81:a5df87708b9a | 286 | GeneratePubnubJSON(json_string, sizeof json_string); |
sveljko | 81:a5df87708b9a | 287 | print_pubnub_result(pb.publish(PUBNUB_CHANNEL, json_string)); |
sveljko | 81:a5df87708b9a | 288 | |
sveljko | 81:a5df87708b9a | 289 | messages.clear(); |
sveljko | 81:a5df87708b9a | 290 | print_pubnub_result(pb.subscribe(PUBNUB_CHANNEL, messages)); |
sveljko | 81:a5df87708b9a | 291 | if (!ledOnce && !messages.empty()) { |
sveljko | 81:a5df87708b9a | 292 | ledOnce = 1; |
sveljko | 81:a5df87708b9a | 293 | SetLedColor(0x2); |
sveljko | 81:a5df87708b9a | 294 | } |
sveljko | 81:a5df87708b9a | 295 | for (std::vector<std::string>::iterator it = messages.begin(); it != messages.end(); ++it) { |
sveljko | 81:a5df87708b9a | 296 | char const *s = it->c_str(); |
sveljko | 81:a5df87708b9a | 297 | PRINTF(BLU "Pubnub message: %s" DEF "\n", s); |
sveljko | 81:a5df87708b9a | 298 | parse_JSON(s); |
stefanrousseau | 16:17c5916f2d12 | 299 | } |
stefanrousseau | 3:26b3cc155f39 | 300 | } //bTimerExpiredFlag |
stefanrousseau | 3:26b3cc155f39 | 301 | } //forever loop |
JMF | 0:9d5134074d84 | 302 | } |