Proximity strip reader

Dependencies:   mbed sfh7779

Fork of StarterKit by Rick McConney

Committer:
elmkom
Date:
Fri Oct 07 17:01:22 2016 +0000
Revision:
42:8500f0cb2ea5
Parent:
41:85a736a9b900
Child:
44:60008ebffdd4
code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JMF 0:9d5134074d84 1 #include "mbed.h"
JMF 0:9d5134074d84 2 #include <cctype>
JMF 0:9d5134074d84 3 #include <string>
JMF 0:9d5134074d84 4 #include "SerialBuffered.h"
JMF 0:9d5134074d84 5 #include "HTS221.h"
JMF 2:0e2ef866af95 6 #include "config_me.h"
stefanrousseau 4:f83bedd9cab4 7 #include "sensors.h"
elmkom 38:532a0d929756 8 #include "Proximity.h"
elmkom 42:8500f0cb2ea5 9 #include "Wnc.h"
JMF 0:9d5134074d84 10
stefanrousseau 11:e6602513730f 11 #include "hardware.h"
elmkom 42:8500f0cb2ea5 12
elmkom 42:8500f0cb2ea5 13 extern SerialBuffered mdm;
elmkom 42:8500f0cb2ea5 14 Wnc wnc;
elmkom 42:8500f0cb2ea5 15
stefanrousseau 11:e6602513730f 16 I2C i2c(PTC11, PTC10); //SDA, SCL -- define the I2C pins being used
elmkom 41:85a736a9b900 17
elmkom 42:8500f0cb2ea5 18 #define PROXIMITYON 1
elmkom 41:85a736a9b900 19
elmkom 41:85a736a9b900 20 #if PROXIMITYON == 1
elmkom 38:532a0d929756 21 Proximity proximityStrip;
elmkom 41:85a736a9b900 22 #endif
elmkom 36:f8d96ff1dd1b 23
JMF 0:9d5134074d84 24 // comment out the following line if color is not supported on the terminal
JMF 0:9d5134074d84 25 #define USE_COLOR
JMF 0:9d5134074d84 26 #ifdef USE_COLOR
JMF 0:9d5134074d84 27 #define BLK "\033[30m"
JMF 0:9d5134074d84 28 #define RED "\033[31m"
JMF 0:9d5134074d84 29 #define GRN "\033[32m"
JMF 0:9d5134074d84 30 #define YEL "\033[33m"
JMF 0:9d5134074d84 31 #define BLU "\033[34m"
JMF 0:9d5134074d84 32 #define MAG "\033[35m"
JMF 0:9d5134074d84 33 #define CYN "\033[36m"
JMF 0:9d5134074d84 34 #define WHT "\033[37m"
JMF 0:9d5134074d84 35 #define DEF "\033[39m"
JMF 0:9d5134074d84 36 #else
JMF 0:9d5134074d84 37 #define BLK
JMF 0:9d5134074d84 38 #define RED
JMF 0:9d5134074d84 39 #define GRN
JMF 0:9d5134074d84 40 #define YEL
JMF 0:9d5134074d84 41 #define BLU
JMF 0:9d5134074d84 42 #define MAG
JMF 0:9d5134074d84 43 #define CYN
JMF 0:9d5134074d84 44 #define WHT
JMF 0:9d5134074d84 45 #define DEF
JMF 0:9d5134074d84 46 #endif
JMF 0:9d5134074d84 47
JMF 0:9d5134074d84 48 #define MDM_DBG_OFF 0
JMF 0:9d5134074d84 49 #define MDM_DBG_AT_CMDS (1 << 0)
elmkom 36:f8d96ff1dd1b 50
elmkom 36:f8d96ff1dd1b 51 #define MUXADDRESS 0x70
elmkom 36:f8d96ff1dd1b 52 #define PROXIMITYADDRESS 0x39
elmkom 36:f8d96ff1dd1b 53
elmkom 41:85a736a9b900 54
elmkom 41:85a736a9b900 55
elmkom 42:8500f0cb2ea5 56
JMF 0:9d5134074d84 57 int mdm_dbgmask = MDM_DBG_OFF;
JMF 0:9d5134074d84 58
JMF 0:9d5134074d84 59 Serial pc(USBTX, USBRX);
elmkom 42:8500f0cb2ea5 60
stefanrousseau 16:17c5916f2d12 61 DigitalOut led_green(LED_GREEN);
stefanrousseau 16:17c5916f2d12 62 DigitalOut led_red(LED_RED);
stefanrousseau 16:17c5916f2d12 63 DigitalOut led_blue(LED_BLUE);
JMF 0:9d5134074d84 64
JMF 0:9d5134074d84 65
JMF 0:9d5134074d84 66
elmkom 41:85a736a9b900 67
elmkom 41:85a736a9b900 68 DigitalIn slot1(PTB3,PullUp);
elmkom 41:85a736a9b900 69 DigitalIn slot2(PTB10,PullUp);
elmkom 41:85a736a9b900 70 DigitalIn slot3(PTB11,PullUp);
elmkom 42:8500f0cb2ea5 71
elmkom 41:85a736a9b900 72 int lastSlot1;
elmkom 41:85a736a9b900 73 int lastSlot2;
elmkom 41:85a736a9b900 74 int lastSlot3;
elmkom 42:8500f0cb2ea5 75 char *iccid;
JMF 0:9d5134074d84 76 #define TOUPPER(a) (a) //toupper(a)
JMF 0:9d5134074d84 77
JMF 0:9d5134074d84 78
JMF 0:9d5134074d84 79 #define MDM_OK 0
JMF 0:9d5134074d84 80 #define MDM_ERR_TIMEOUT -1
JMF 0:9d5134074d84 81
JMF 0:9d5134074d84 82 #define MAX_AT_RSP_LEN 255
elmkom 38:532a0d929756 83
elmkom 36:f8d96ff1dd1b 84 bool proximityChange = false;
elmkom 42:8500f0cb2ea5 85
elmkom 41:85a736a9b900 86 bool toggleLed = false;
elmkom 41:85a736a9b900 87 int seqNum;
elmkom 36:f8d96ff1dd1b 88
elmkom 38:532a0d929756 89
elmkom 36:f8d96ff1dd1b 90 //********************************************************************************************************************************************
elmkom 36:f8d96ff1dd1b 91 //* Set the RGB LED's Color
elmkom 36:f8d96ff1dd1b 92 //* LED Color 0=Off to 7=White. 3 bits represent BGR (bit0=Red, bit1=Green, bit2=Blue)
elmkom 36:f8d96ff1dd1b 93 //********************************************************************************************************************************************
elmkom 36:f8d96ff1dd1b 94 void SetLedColor(unsigned char ucColor)
elmkom 35:2e864bae3af0 95 {
elmkom 42:8500f0cb2ea5 96 if(wnc.isPowerSaveOn())
elmkom 38:532a0d929756 97 {
elmkom 38:532a0d929756 98 led_red = !0;
elmkom 38:532a0d929756 99 led_green = !0;
elmkom 42:8500f0cb2ea5 100 led_blue = !(ucColor & 0x4);
elmkom 38:532a0d929756 101 }
elmkom 38:532a0d929756 102 else
elmkom 38:532a0d929756 103 {
elmkom 38:532a0d929756 104 //Note that when an LED is on, you write a 0 to it:
elmkom 38:532a0d929756 105 led_red = !(ucColor & 0x1); //bit 0
elmkom 38:532a0d929756 106 led_green = !(ucColor & 0x2); //bit 1
elmkom 38:532a0d929756 107 led_blue = !(ucColor & 0x4); //bit 2
elmkom 38:532a0d929756 108 }
elmkom 36:f8d96ff1dd1b 109 } //SetLedColor()
elmkom 35:2e864bae3af0 110
JMF 0:9d5134074d84 111
elmkom 36:f8d96ff1dd1b 112 void system_reset()
elmkom 36:f8d96ff1dd1b 113 {
elmkom 36:f8d96ff1dd1b 114 printf(RED "\n\rSystem resetting..." DEF "\n");
elmkom 36:f8d96ff1dd1b 115 NVIC_SystemReset();
elmkom 36:f8d96ff1dd1b 116 }
elmkom 42:8500f0cb2ea5 117
JMF 2:0e2ef866af95 118 // These are built on the fly
JMF 2:0e2ef866af95 119 string MyServerIpAddress;
JMF 2:0e2ef866af95 120 string MySocketData;
JMF 2:0e2ef866af95 121
JMF 2:0e2ef866af95 122 // These are to be built on the fly
JMF 2:0e2ef866af95 123 string my_temp;
JMF 2:0e2ef866af95 124 string my_humidity;
JMF 2:0e2ef866af95 125
JMF 0:9d5134074d84 126 #define CTOF(x) ((x)*1.8+32)
JMF 0:9d5134074d84 127
stefanrousseau 3:26b3cc155f39 128 //********************************************************************************************************************************************
stefanrousseau 12:7c94ec5069dc 129 //* Create string with sensor readings that can be sent to flow as an HTTP get
stefanrousseau 3:26b3cc155f39 130 //********************************************************************************************************************************************
stefanrousseau 12:7c94ec5069dc 131 K64F_Sensors_t SENSOR_DATA =
stefanrousseau 3:26b3cc155f39 132 {
stefanrousseau 12:7c94ec5069dc 133 .Temperature = "0",
stefanrousseau 12:7c94ec5069dc 134 .Humidity = "0",
stefanrousseau 12:7c94ec5069dc 135 .AccelX = "0",
stefanrousseau 12:7c94ec5069dc 136 .AccelY = "0",
stefanrousseau 12:7c94ec5069dc 137 .AccelZ = "0",
stefanrousseau 12:7c94ec5069dc 138 .MagnetometerX = "0",
stefanrousseau 12:7c94ec5069dc 139 .MagnetometerY = "0",
stefanrousseau 12:7c94ec5069dc 140 .MagnetometerZ = "0",
stefanrousseau 12:7c94ec5069dc 141 .AmbientLightVis = "0",
stefanrousseau 12:7c94ec5069dc 142 .AmbientLightIr = "0",
stefanrousseau 12:7c94ec5069dc 143 .UVindex = "0",
stefanrousseau 12:7c94ec5069dc 144 .Proximity = "0",
stefanrousseau 12:7c94ec5069dc 145 .Temperature_Si7020 = "0",
stefanrousseau 12:7c94ec5069dc 146 .Humidity_Si7020 = "0"
stefanrousseau 3:26b3cc155f39 147 };
stefanrousseau 12:7c94ec5069dc 148
elmkom 41:85a736a9b900 149 void GenerateModemString(char * modem_string,int sensor)
stefanrousseau 3:26b3cc155f39 150 {
elmkom 41:85a736a9b900 151 switch(sensor)
stefanrousseau 12:7c94ec5069dc 152 {
elmkom 41:85a736a9b900 153 #if PROXIMITYON == 1
elmkom 38:532a0d929756 154 case PROXIMITY_ONLY:
elmkom 35:2e864bae3af0 155 {
elmkom 41:85a736a9b900 156
elmkom 38:532a0d929756 157 char* data = proximityStrip.getDataStr();
elmkom 41:85a736a9b900 158 seqNum++;
elmkom 42:8500f0cb2ea5 159 sprintf(modem_string, "GET %s%s?serial=%s&seq=%d&data=%s %s%s\r\n\r\n", FLOW_BASE_URL, "/shelf", iccid, seqNum, data, FLOW_URL_TYPE, MY_SERVER_URL);
elmkom 41:85a736a9b900 160 break;
elmkom 41:85a736a9b900 161 }
elmkom 41:85a736a9b900 162 #endif
elmkom 41:85a736a9b900 163 case SWITCH_ONLY:
elmkom 41:85a736a9b900 164 {
elmkom 41:85a736a9b900 165 char data[32];
elmkom 41:85a736a9b900 166 sprintf(data,"[{\"p\":%d},{\"p\":%d},{\"p\":%d}]",lastSlot1*26,lastSlot2*26,lastSlot3*26);
elmkom 41:85a736a9b900 167 seqNum++;
elmkom 42:8500f0cb2ea5 168 sprintf(modem_string, "GET %s%s?serial=%s&seq=%d&data=%s %s%s\r\n\r\n", FLOW_BASE_URL, "/car", iccid, seqNum, data, FLOW_URL_TYPE, MY_SERVER_URL);
elmkom 35:2e864bae3af0 169 break;
elmkom 35:2e864bae3af0 170 }
stefanrousseau 12:7c94ec5069dc 171 case TEMP_HUMIDITY_ONLY:
stefanrousseau 12:7c94ec5069dc 172 {
stefanrousseau 12:7c94ec5069dc 173 sprintf(modem_string, "GET %s%s?serial=%s&temp=%s&humidity=%s %s%s\r\n\r\n", FLOW_BASE_URL, FLOW_INPUT_NAME, FLOW_DEVICE_NAME, SENSOR_DATA.Temperature, SENSOR_DATA.Humidity, FLOW_URL_TYPE, MY_SERVER_URL);
stefanrousseau 12:7c94ec5069dc 174 break;
stefanrousseau 12:7c94ec5069dc 175 }
stefanrousseau 12:7c94ec5069dc 176 case TEMP_HUMIDITY_ACCELEROMETER:
stefanrousseau 12:7c94ec5069dc 177 {
stefanrousseau 12:7c94ec5069dc 178 sprintf(modem_string, "GET %s%s?serial=%s&temp=%s&humidity=%s&accelX=%s&accelY=%s&accelZ=%s %s%s\r\n\r\n", FLOW_BASE_URL, FLOW_INPUT_NAME, FLOW_DEVICE_NAME, SENSOR_DATA.Temperature, SENSOR_DATA.Humidity, SENSOR_DATA.AccelX,SENSOR_DATA.AccelY,SENSOR_DATA.AccelZ, FLOW_URL_TYPE, MY_SERVER_URL);
stefanrousseau 12:7c94ec5069dc 179 break;
stefanrousseau 12:7c94ec5069dc 180 }
stefanrousseau 12:7c94ec5069dc 181 case TEMP_HUMIDITY_ACCELEROMETER_PMODSENSORS:
stefanrousseau 12:7c94ec5069dc 182 {
stefanrousseau 12:7c94ec5069dc 183 sprintf(modem_string, "GET %s%s?serial=%s&temp=%s&humidity=%s&accelX=%s&accelY=%s&accelZ=%s&proximity=%s&light_uv=%s&light_vis=%s&light_ir=%s %s%s\r\n\r\n", FLOW_BASE_URL, FLOW_INPUT_NAME, FLOW_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, FLOW_URL_TYPE, MY_SERVER_URL);
stefanrousseau 12:7c94ec5069dc 184 break;
stefanrousseau 12:7c94ec5069dc 185 }
stefanrousseau 12:7c94ec5069dc 186 default:
stefanrousseau 12:7c94ec5069dc 187 {
stefanrousseau 12:7c94ec5069dc 188 sprintf(modem_string, "Invalid sensor selected\r\n\r\n");
stefanrousseau 12:7c94ec5069dc 189 break;
stefanrousseau 12:7c94ec5069dc 190 }
stefanrousseau 16:17c5916f2d12 191 } //switch(iSensorsToReport)
stefanrousseau 3:26b3cc155f39 192 } //GenerateModemString
stefanrousseau 3:26b3cc155f39 193
stefanrousseau 3:26b3cc155f39 194
stefanrousseau 16:17c5916f2d12 195 //********************************************************************************************************************************************
stefanrousseau 16:17c5916f2d12 196 //* Process JSON response messages
stefanrousseau 16:17c5916f2d12 197 //********************************************************************************************************************************************
elmkom 40:45e4636d20cc 198 bool extract_reply(char* search_field, char* found_string)
stefanrousseau 16:17c5916f2d12 199 {
stefanrousseau 16:17c5916f2d12 200 char* beginquote;
stefanrousseau 16:17c5916f2d12 201 char* endquote;
elmkom 40:45e4636d20cc 202 beginquote = strstr(search_field, "\r\n\r\n"); //start of data
elmkom 40:45e4636d20cc 203 endquote = strchr(search_field, '\0');
stefanrousseau 16:17c5916f2d12 204 if (beginquote != 0)
stefanrousseau 16:17c5916f2d12 205 {
stefanrousseau 16:17c5916f2d12 206 uint16_t ifoundlen;
stefanrousseau 16:17c5916f2d12 207 if (endquote != 0)
stefanrousseau 16:17c5916f2d12 208 {
stefanrousseau 16:17c5916f2d12 209 ifoundlen = (uint16_t) (endquote - beginquote) + 1;
stefanrousseau 16:17c5916f2d12 210 strncpy(found_string, beginquote, ifoundlen );
stefanrousseau 16:17c5916f2d12 211 found_string[ifoundlen] = 0; //null terminate
stefanrousseau 16:17c5916f2d12 212 return true;
stefanrousseau 16:17c5916f2d12 213 }
stefanrousseau 16:17c5916f2d12 214 }
elmkom 40:45e4636d20cc 215
elmkom 40:45e4636d20cc 216 return false;
elmkom 40:45e4636d20cc 217
elmkom 40:45e4636d20cc 218 } //extract_reply
stefanrousseau 16:17c5916f2d12 219
elmkom 40:45e4636d20cc 220
elmkom 40:45e4636d20cc 221 void parse_reply(char* reply)
elmkom 40:45e4636d20cc 222 {
elmkom 40:45e4636d20cc 223 char *tokens[5];
elmkom 40:45e4636d20cc 224 int index = 0;
elmkom 40:45e4636d20cc 225 tokens[index++] = strtok(reply," ");
elmkom 40:45e4636d20cc 226 while( index < 5 )
elmkom 40:45e4636d20cc 227 {
elmkom 40:45e4636d20cc 228 char* token = strtok(NULL, " ");
elmkom 40:45e4636d20cc 229 if(token == NULL) break;
elmkom 40:45e4636d20cc 230 tokens[index++] = token;
elmkom 40:45e4636d20cc 231 }
elmkom 40:45e4636d20cc 232 if(strcmp("PSM",tokens[1]) == 0)
elmkom 40:45e4636d20cc 233 {
elmkom 40:45e4636d20cc 234
elmkom 40:45e4636d20cc 235 int t3412 = atoi(tokens[3]);
elmkom 40:45e4636d20cc 236 int t3324 = atoi(tokens[4]);
elmkom 40:45e4636d20cc 237 pc.printf("t3412 %d t3324 %d\r\n",t3412,t3324);
elmkom 42:8500f0cb2ea5 238 // setTauTimer(t3412);
elmkom 42:8500f0cb2ea5 239 //setActivityTimer(t3324);
elmkom 40:45e4636d20cc 240
elmkom 40:45e4636d20cc 241 if(strcmp("true",tokens[2]) == 0)
elmkom 40:45e4636d20cc 242 {
elmkom 40:45e4636d20cc 243 pc.printf("PSM ON\r\n");
elmkom 42:8500f0cb2ea5 244 wnc.setPowerSave(true,t3412,t3324);
elmkom 40:45e4636d20cc 245 }
elmkom 40:45e4636d20cc 246 else
elmkom 40:45e4636d20cc 247 {
elmkom 40:45e4636d20cc 248 pc.printf("PSM OFF\r\n");
elmkom 42:8500f0cb2ea5 249 wnc.setPowerSave(false,t3412,t3324);
elmkom 40:45e4636d20cc 250 }
elmkom 40:45e4636d20cc 251 }
elmkom 40:45e4636d20cc 252 }
stefanrousseau 16:17c5916f2d12 253
elmkom 41:85a736a9b900 254 bool checkSlots()
elmkom 41:85a736a9b900 255 {
elmkom 41:85a736a9b900 256 bool changed = false;
elmkom 41:85a736a9b900 257 int s1 = !slot1;
elmkom 41:85a736a9b900 258 int s2 = !slot2;
elmkom 41:85a736a9b900 259 int s3 = !slot3;
elmkom 41:85a736a9b900 260 if(lastSlot1 != s1 || lastSlot2 != s2 ||lastSlot3 != s3)
elmkom 41:85a736a9b900 261 {
elmkom 41:85a736a9b900 262 pc.printf("slot 1 %d\r\n",s1);
elmkom 41:85a736a9b900 263 pc.printf("slot 2 %d\r\n",s2);
elmkom 41:85a736a9b900 264 pc.printf("slot 3 %d\r\n",s3);
elmkom 41:85a736a9b900 265 changed = true;
elmkom 41:85a736a9b900 266 }
elmkom 41:85a736a9b900 267 lastSlot1 = s1;
elmkom 41:85a736a9b900 268 lastSlot2 = s2;
elmkom 41:85a736a9b900 269 lastSlot3 = s3;
elmkom 41:85a736a9b900 270 return changed;
elmkom 41:85a736a9b900 271 }
elmkom 41:85a736a9b900 272
JMF 0:9d5134074d84 273 int main() {
elmkom 36:f8d96ff1dd1b 274
JMF 2:0e2ef866af95 275 int i;
elmkom 41:85a736a9b900 276
JMF 0:9d5134074d84 277 HTS221 hts221;
JMF 0:9d5134074d84 278 pc.baud(115200);
elmkom 42:8500f0cb2ea5 279
JMF 0:9d5134074d84 280 void hts221_init(void);
JMF 0:9d5134074d84 281
fkellermavnet 20:27a4f27254d0 282 // Set LED to RED until init finishes
fkellermavnet 20:27a4f27254d0 283 SetLedColor(0x1);
fkellermavnet 20:27a4f27254d0 284
JMF 1:af7a42f7d465 285 pc.printf(BLU "Hello World from AT&T Shape!\r\n\n\r");
JMF 0:9d5134074d84 286 pc.printf(GRN "Initialize the HTS221\n\r");
JMF 0:9d5134074d84 287
JMF 0:9d5134074d84 288 i = hts221.begin();
JMF 0:9d5134074d84 289 if( i )
JMF 0:9d5134074d84 290 pc.printf(BLU "HTS221 Detected! (0x%02X)\n\r",i);
JMF 0:9d5134074d84 291 else
JMF 0:9d5134074d84 292 pc.printf(RED "HTS221 NOT DETECTED!!\n\r");
JMF 0:9d5134074d84 293
JMF 0:9d5134074d84 294 printf("Temp is: %0.2f F \n\r",CTOF(hts221.readTemperature()));
JMF 0:9d5134074d84 295 printf("Humid is: %02d %%\n\r",hts221.readHumidity());
elmkom 35:2e864bae3af0 296
stefanrousseau 11:e6602513730f 297
JMF 0:9d5134074d84 298 // Initialize the modem
JMF 0:9d5134074d84 299 printf(GRN "Modem initializing... will take up to 60 seconds" DEF "\r\n");
fkellermavnet 14:0c353e212296 300 do {
elmkom 42:8500f0cb2ea5 301 i=wnc.init();
fkellermavnet 14:0c353e212296 302 if (!i) {
fkellermavnet 14:0c353e212296 303 pc.printf(RED "Modem initialization failed!" DEF "\n");
fkellermavnet 14:0c353e212296 304 }
fkellermavnet 14:0c353e212296 305 } while (!i);
JMF 0:9d5134074d84 306
elmkom 42:8500f0cb2ea5 307 // wnc.setIn();
elmkom 42:8500f0cb2ea5 308 wnc.send("AT+CPSMS=0",WNC_WAIT_TIME_MS);
elmkom 42:8500f0cb2ea5 309
elmkom 42:8500f0cb2ea5 310 wnc.toggleWake();
elmkom 42:8500f0cb2ea5 311
elmkom 41:85a736a9b900 312
elmkom 42:8500f0cb2ea5 313 iccid = wnc.getIccid();
elmkom 42:8500f0cb2ea5 314 printf(GRN "ICCID = %s" DEF "\r\n",iccid);
elmkom 42:8500f0cb2ea5 315
JMF 2:0e2ef866af95 316 //Software init
elmkom 42:8500f0cb2ea5 317 wnc.startInternet();
elmkom 42:8500f0cb2ea5 318 //wnc.ping("108.244.165.22");
stefanrousseau 3:26b3cc155f39 319
fkellermavnet 20:27a4f27254d0 320 // Set LED BLUE for partial init
fkellermavnet 20:27a4f27254d0 321 SetLedColor(0x4);
elmkom 38:532a0d929756 322
elmkom 42:8500f0cb2ea5 323 wnc.setPowerSave(false,12*60*60,20);
elmkom 41:85a736a9b900 324
elmkom 41:85a736a9b900 325 #if PROXIMITYON == 1
elmkom 41:85a736a9b900 326 proximityStrip.init();
elmkom 38:532a0d929756 327 proximityStrip.on();
elmkom 41:85a736a9b900 328 #endif
elmkom 38:532a0d929756 329
elmkom 38:532a0d929756 330 int count = 0;
JMF 2:0e2ef866af95 331 // Send and receive data perpetually
JMF 2:0e2ef866af95 332 while(1) {
elmkom 38:532a0d929756 333
elmkom 42:8500f0cb2ea5 334 wnc.checkPassthrough();
elmkom 38:532a0d929756 335 //sprintf(SENSOR_DATA.Temperature, "%0.2f", CTOF(hts221.readTemperature()));
elmkom 38:532a0d929756 336 //sprintf(SENSOR_DATA.Humidity, "%02d", hts221.readHumidity());
elmkom 38:532a0d929756 337 // read_sensors(); //read available external sensors from a PMOD and the on-board motion sensor
elmkom 41:85a736a9b900 338 toggleLed = !toggleLed;
elmkom 41:85a736a9b900 339 if(toggleLed)
elmkom 41:85a736a9b900 340 SetLedColor(0x2); //green
elmkom 41:85a736a9b900 341 else
elmkom 41:85a736a9b900 342 SetLedColor(0); //off
elmkom 41:85a736a9b900 343
elmkom 41:85a736a9b900 344 #if PROXIMITYON == 1
elmkom 38:532a0d929756 345 proximityStrip.scan();
elmkom 41:85a736a9b900 346 #else
elmkom 41:85a736a9b900 347 bool slotsChanged = checkSlots();
elmkom 41:85a736a9b900 348 #endif
elmkom 38:532a0d929756 349
elmkom 41:85a736a9b900 350
elmkom 41:85a736a9b900 351 #if PROXIMITYON == 1
elmkom 41:85a736a9b900 352 if(count >= 5*60 || proximityStrip.changed(50))
elmkom 41:85a736a9b900 353 #else
elmkom 41:85a736a9b900 354 if(count >= 5*60 || slotsChanged)
elmkom 41:85a736a9b900 355 #endif
elmkom 38:532a0d929756 356 {
elmkom 42:8500f0cb2ea5 357 if(wnc.isPowerSaveOn())
elmkom 42:8500f0cb2ea5 358 {
elmkom 42:8500f0cb2ea5 359 wnc.wakeFromPowerSave();
elmkom 42:8500f0cb2ea5 360 //wnc.ping("108.244.165.22");
elmkom 42:8500f0cb2ea5 361 }
elmkom 38:532a0d929756 362 count = 0;
elmkom 38:532a0d929756 363 SetLedColor(0x04); //blue
elmkom 36:f8d96ff1dd1b 364
elmkom 38:532a0d929756 365 char modem_string[512];
elmkom 41:85a736a9b900 366
elmkom 41:85a736a9b900 367 #if PROXIMITYON == 1
elmkom 41:85a736a9b900 368 GenerateModemString(&modem_string[0],PROXIMITY_ONLY);
elmkom 41:85a736a9b900 369 #else
elmkom 41:85a736a9b900 370 GenerateModemString(&modem_string[0],SWITCH_ONLY);
elmkom 41:85a736a9b900 371 #endif
elmkom 41:85a736a9b900 372
elmkom 38:532a0d929756 373 printf(BLU "Sending to modem : %s" DEF "\r\n", modem_string);
elmkom 42:8500f0cb2ea5 374
elmkom 42:8500f0cb2ea5 375 if(wnc.connect("108.244.165.22",5005))
elmkom 42:8500f0cb2ea5 376 {
elmkom 42:8500f0cb2ea5 377 char * reply = wnc.writeSocket(modem_string);
elmkom 42:8500f0cb2ea5 378
elmkom 42:8500f0cb2ea5 379 char* mydata;
elmkom 42:8500f0cb2ea5 380 int tries = 4;
elmkom 42:8500f0cb2ea5 381 while(tries > 0) // wait for reply
elmkom 42:8500f0cb2ea5 382 {
elmkom 42:8500f0cb2ea5 383 tries--;
elmkom 42:8500f0cb2ea5 384 mydata = wnc.readSocket();
elmkom 42:8500f0cb2ea5 385 if (strlen(mydata) > 0)
elmkom 42:8500f0cb2ea5 386 break;
elmkom 42:8500f0cb2ea5 387
elmkom 42:8500f0cb2ea5 388 wait(0.2);
elmkom 42:8500f0cb2ea5 389 }
elmkom 42:8500f0cb2ea5 390
elmkom 42:8500f0cb2ea5 391 if (strlen(mydata) > 0)
elmkom 42:8500f0cb2ea5 392 {
elmkom 37:ee01f752524a 393
elmkom 42:8500f0cb2ea5 394 SetLedColor(0x2); // green
elmkom 42:8500f0cb2ea5 395 //only copy on sucessful send
elmkom 38:532a0d929756 396
elmkom 42:8500f0cb2ea5 397 printf(BLU "Read back : %s" DEF "\r\n", mydata);
elmkom 42:8500f0cb2ea5 398 char datareply[512];
elmkom 42:8500f0cb2ea5 399 if (extract_reply(mydata, datareply))
elmkom 42:8500f0cb2ea5 400 {
elmkom 42:8500f0cb2ea5 401 printf(GRN "JSON : %s" DEF "\r\n", datareply);
elmkom 42:8500f0cb2ea5 402 parse_reply(datareply);
elmkom 42:8500f0cb2ea5 403 }
elmkom 42:8500f0cb2ea5 404 SetLedColor(0); // off
elmkom 42:8500f0cb2ea5 405 }
elmkom 42:8500f0cb2ea5 406 else // no reply reset
elmkom 38:532a0d929756 407 {
elmkom 42:8500f0cb2ea5 408 SetLedColor(0x1); //red
elmkom 42:8500f0cb2ea5 409 system_reset();
elmkom 42:8500f0cb2ea5 410 }
elmkom 42:8500f0cb2ea5 411 wnc.disconnect();
elmkom 38:532a0d929756 412 }
elmkom 42:8500f0cb2ea5 413 else // failed to connect reset
elmkom 38:532a0d929756 414 {
elmkom 38:532a0d929756 415 SetLedColor(0x1); //red
elmkom 42:8500f0cb2ea5 416 system_reset();
elmkom 42:8500f0cb2ea5 417 }
elmkom 42:8500f0cb2ea5 418 if(wnc.isPowerSaveOn())
elmkom 42:8500f0cb2ea5 419 wnc.resumePowerSave();
elmkom 38:532a0d929756 420 }
elmkom 38:532a0d929756 421 count++;
elmkom 38:532a0d929756 422 wait(0.2);
stefanrousseau 3:26b3cc155f39 423 } //forever loop
JMF 0:9d5134074d84 424 }