Proximity strip reader

Dependencies:   mbed sfh7779

Fork of StarterKit by Rick McConney

Committer:
elmkom
Date:
Wed Oct 26 18:40:40 2016 +0000
Revision:
44:60008ebffdd4
Parent:
42:8500f0cb2ea5
Child:
45:fe90f1fcb4e0
Gsma version

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 44:60008ebffdd4 12 #include "att160826.h"
elmkom 42:8500f0cb2ea5 13
elmkom 42:8500f0cb2ea5 14 extern SerialBuffered mdm;
elmkom 42:8500f0cb2ea5 15 Wnc wnc;
elmkom 42:8500f0cb2ea5 16
stefanrousseau 11:e6602513730f 17 I2C i2c(PTC11, PTC10); //SDA, SCL -- define the I2C pins being used
elmkom 44:60008ebffdd4 18 I2C *myi2c;
elmkom 41:85a736a9b900 19
elmkom 42:8500f0cb2ea5 20 #define PROXIMITYON 1
elmkom 41:85a736a9b900 21
elmkom 41:85a736a9b900 22 #if PROXIMITYON == 1
elmkom 44:60008ebffdd4 23 att160826_data_t strip_data[2];
elmkom 44:60008ebffdd4 24 att160826_data_t last_data[2];
elmkom 44:60008ebffdd4 25 ATT160826 *strips[2];
elmkom 44:60008ebffdd4 26 #elif PROXIMITYON == 2
elmkom 44:60008ebffdd4 27 Proximity proximityStrip[2];
elmkom 41:85a736a9b900 28 #endif
elmkom 36:f8d96ff1dd1b 29
JMF 0:9d5134074d84 30 // comment out the following line if color is not supported on the terminal
JMF 0:9d5134074d84 31 #define USE_COLOR
JMF 0:9d5134074d84 32 #ifdef USE_COLOR
JMF 0:9d5134074d84 33 #define BLK "\033[30m"
JMF 0:9d5134074d84 34 #define RED "\033[31m"
JMF 0:9d5134074d84 35 #define GRN "\033[32m"
JMF 0:9d5134074d84 36 #define YEL "\033[33m"
JMF 0:9d5134074d84 37 #define BLU "\033[34m"
JMF 0:9d5134074d84 38 #define MAG "\033[35m"
JMF 0:9d5134074d84 39 #define CYN "\033[36m"
JMF 0:9d5134074d84 40 #define WHT "\033[37m"
JMF 0:9d5134074d84 41 #define DEF "\033[39m"
JMF 0:9d5134074d84 42 #else
JMF 0:9d5134074d84 43 #define BLK
JMF 0:9d5134074d84 44 #define RED
JMF 0:9d5134074d84 45 #define GRN
JMF 0:9d5134074d84 46 #define YEL
JMF 0:9d5134074d84 47 #define BLU
JMF 0:9d5134074d84 48 #define MAG
JMF 0:9d5134074d84 49 #define CYN
JMF 0:9d5134074d84 50 #define WHT
JMF 0:9d5134074d84 51 #define DEF
JMF 0:9d5134074d84 52 #endif
JMF 0:9d5134074d84 53
JMF 0:9d5134074d84 54 #define MDM_DBG_OFF 0
JMF 0:9d5134074d84 55 #define MDM_DBG_AT_CMDS (1 << 0)
elmkom 36:f8d96ff1dd1b 56
elmkom 44:60008ebffdd4 57
elmkom 44:60008ebffdd4 58
elmkom 36:f8d96ff1dd1b 59
elmkom 41:85a736a9b900 60
elmkom 41:85a736a9b900 61
elmkom 42:8500f0cb2ea5 62
JMF 0:9d5134074d84 63 int mdm_dbgmask = MDM_DBG_OFF;
JMF 0:9d5134074d84 64
JMF 0:9d5134074d84 65 Serial pc(USBTX, USBRX);
elmkom 42:8500f0cb2ea5 66
stefanrousseau 16:17c5916f2d12 67 DigitalOut led_green(LED_GREEN);
stefanrousseau 16:17c5916f2d12 68 DigitalOut led_red(LED_RED);
stefanrousseau 16:17c5916f2d12 69 DigitalOut led_blue(LED_BLUE);
JMF 0:9d5134074d84 70
JMF 0:9d5134074d84 71
JMF 0:9d5134074d84 72
elmkom 41:85a736a9b900 73
elmkom 44:60008ebffdd4 74
elmkom 41:85a736a9b900 75 DigitalIn slot1(PTB3,PullUp);
elmkom 41:85a736a9b900 76 DigitalIn slot2(PTB10,PullUp);
elmkom 41:85a736a9b900 77 DigitalIn slot3(PTB11,PullUp);
elmkom 42:8500f0cb2ea5 78
elmkom 41:85a736a9b900 79 int lastSlot1;
elmkom 41:85a736a9b900 80 int lastSlot2;
elmkom 41:85a736a9b900 81 int lastSlot3;
elmkom 42:8500f0cb2ea5 82 char *iccid;
JMF 0:9d5134074d84 83 #define TOUPPER(a) (a) //toupper(a)
JMF 0:9d5134074d84 84
JMF 0:9d5134074d84 85
JMF 0:9d5134074d84 86 #define MDM_OK 0
JMF 0:9d5134074d84 87 #define MDM_ERR_TIMEOUT -1
JMF 0:9d5134074d84 88
JMF 0:9d5134074d84 89 #define MAX_AT_RSP_LEN 255
elmkom 38:532a0d929756 90
elmkom 41:85a736a9b900 91 bool toggleLed = false;
elmkom 41:85a736a9b900 92 int seqNum;
elmkom 36:f8d96ff1dd1b 93
elmkom 38:532a0d929756 94
elmkom 36:f8d96ff1dd1b 95 //********************************************************************************************************************************************
elmkom 36:f8d96ff1dd1b 96 //* Set the RGB LED's Color
elmkom 36:f8d96ff1dd1b 97 //* LED Color 0=Off to 7=White. 3 bits represent BGR (bit0=Red, bit1=Green, bit2=Blue)
elmkom 36:f8d96ff1dd1b 98 //********************************************************************************************************************************************
elmkom 36:f8d96ff1dd1b 99 void SetLedColor(unsigned char ucColor)
elmkom 35:2e864bae3af0 100 {
elmkom 42:8500f0cb2ea5 101 if(wnc.isPowerSaveOn())
elmkom 38:532a0d929756 102 {
elmkom 38:532a0d929756 103 led_red = !0;
elmkom 38:532a0d929756 104 led_green = !0;
elmkom 42:8500f0cb2ea5 105 led_blue = !(ucColor & 0x4);
elmkom 38:532a0d929756 106 }
elmkom 38:532a0d929756 107 else
elmkom 38:532a0d929756 108 {
elmkom 38:532a0d929756 109 //Note that when an LED is on, you write a 0 to it:
elmkom 38:532a0d929756 110 led_red = !(ucColor & 0x1); //bit 0
elmkom 38:532a0d929756 111 led_green = !(ucColor & 0x2); //bit 1
elmkom 38:532a0d929756 112 led_blue = !(ucColor & 0x4); //bit 2
elmkom 38:532a0d929756 113 }
elmkom 36:f8d96ff1dd1b 114 } //SetLedColor()
elmkom 35:2e864bae3af0 115
JMF 0:9d5134074d84 116
elmkom 36:f8d96ff1dd1b 117 void system_reset()
elmkom 36:f8d96ff1dd1b 118 {
elmkom 36:f8d96ff1dd1b 119 printf(RED "\n\rSystem resetting..." DEF "\n");
elmkom 36:f8d96ff1dd1b 120 NVIC_SystemReset();
elmkom 36:f8d96ff1dd1b 121 }
elmkom 42:8500f0cb2ea5 122
JMF 2:0e2ef866af95 123 // These are built on the fly
JMF 2:0e2ef866af95 124 string MyServerIpAddress;
JMF 2:0e2ef866af95 125 string MySocketData;
JMF 2:0e2ef866af95 126
JMF 2:0e2ef866af95 127 // These are to be built on the fly
JMF 2:0e2ef866af95 128 string my_temp;
JMF 2:0e2ef866af95 129 string my_humidity;
JMF 2:0e2ef866af95 130
JMF 0:9d5134074d84 131 #define CTOF(x) ((x)*1.8+32)
JMF 0:9d5134074d84 132
stefanrousseau 3:26b3cc155f39 133 //********************************************************************************************************************************************
stefanrousseau 12:7c94ec5069dc 134 //* Create string with sensor readings that can be sent to flow as an HTTP get
stefanrousseau 3:26b3cc155f39 135 //********************************************************************************************************************************************
stefanrousseau 12:7c94ec5069dc 136 K64F_Sensors_t SENSOR_DATA =
stefanrousseau 3:26b3cc155f39 137 {
stefanrousseau 12:7c94ec5069dc 138 .Temperature = "0",
stefanrousseau 12:7c94ec5069dc 139 .Humidity = "0",
stefanrousseau 12:7c94ec5069dc 140 .AccelX = "0",
stefanrousseau 12:7c94ec5069dc 141 .AccelY = "0",
stefanrousseau 12:7c94ec5069dc 142 .AccelZ = "0",
stefanrousseau 12:7c94ec5069dc 143 .MagnetometerX = "0",
stefanrousseau 12:7c94ec5069dc 144 .MagnetometerY = "0",
stefanrousseau 12:7c94ec5069dc 145 .MagnetometerZ = "0",
stefanrousseau 12:7c94ec5069dc 146 .AmbientLightVis = "0",
stefanrousseau 12:7c94ec5069dc 147 .AmbientLightIr = "0",
stefanrousseau 12:7c94ec5069dc 148 .UVindex = "0",
stefanrousseau 12:7c94ec5069dc 149 .Proximity = "0",
stefanrousseau 12:7c94ec5069dc 150 .Temperature_Si7020 = "0",
stefanrousseau 12:7c94ec5069dc 151 .Humidity_Si7020 = "0"
stefanrousseau 3:26b3cc155f39 152 };
stefanrousseau 12:7c94ec5069dc 153
elmkom 44:60008ebffdd4 154
elmkom 44:60008ebffdd4 155
elmkom 44:60008ebffdd4 156 void GenerateModemString(char * modem_string,int sensor,int strip)
stefanrousseau 3:26b3cc155f39 157 {
elmkom 41:85a736a9b900 158 switch(sensor)
stefanrousseau 12:7c94ec5069dc 159 {
elmkom 41:85a736a9b900 160 #if PROXIMITYON == 1
elmkom 44:60008ebffdd4 161
elmkom 44:60008ebffdd4 162 case PROXIMITY_ONLY:
elmkom 44:60008ebffdd4 163 {
elmkom 44:60008ebffdd4 164 if(strip == -1)
elmkom 44:60008ebffdd4 165 {
elmkom 44:60008ebffdd4 166 seqNum++;
elmkom 44:60008ebffdd4 167 sprintf(modem_string, "GET %s%s?ver=cat1&strip=%d&serial=%s&seq=%d&psm=%d&T=%d&t=%d %s%s\r\n\r\n", FLOW_BASE_URL, "/shelf", strip, iccid, seqNum, wnc.isPowerSaveOn(),wnc.gett3412Timer(),wnc.gett3324Timer(),FLOW_URL_TYPE, MY_SERVER_URL);
elmkom 44:60008ebffdd4 168 break;
elmkom 44:60008ebffdd4 169 }
elmkom 44:60008ebffdd4 170 int stripIndex = 0;
elmkom 44:60008ebffdd4 171 if(strip == 3)
elmkom 44:60008ebffdd4 172 stripIndex = 1;
elmkom 44:60008ebffdd4 173 char data[256];
elmkom 44:60008ebffdd4 174 int i=0;
elmkom 44:60008ebffdd4 175 int index = 0;
elmkom 44:60008ebffdd4 176 index += sprintf(&data[index],"[");
elmkom 44:60008ebffdd4 177 for (i=1; i<SENSORS_PER_STRIP; i++)
elmkom 44:60008ebffdd4 178 {
elmkom 44:60008ebffdd4 179 short p = last_data[stripIndex].sensor[i].sample.prox;
elmkom 44:60008ebffdd4 180 short l = last_data[stripIndex].sensor[i].sample.als_vis;
elmkom 44:60008ebffdd4 181 short r = last_data[stripIndex].sensor[i].sample.als_ir;
elmkom 44:60008ebffdd4 182 if(i<SENSORS_PER_STRIP-1)
elmkom 44:60008ebffdd4 183 index += snprintf(&data[index], 128, "{\"s\":%d,\"p\":%d,\"l\":%d,\"r\":%d},", i,p,l,r);
elmkom 44:60008ebffdd4 184 else
elmkom 44:60008ebffdd4 185 index += snprintf(&data[index], 128, "{\"s\":%d,\"p\":%d,\"l\":%d,\"r\":%d}]", i,p,l,r);
elmkom 44:60008ebffdd4 186
elmkom 44:60008ebffdd4 187 }
elmkom 44:60008ebffdd4 188
elmkom 44:60008ebffdd4 189
elmkom 44:60008ebffdd4 190 seqNum++;
elmkom 44:60008ebffdd4 191 sprintf(modem_string, "GET %s%s?ver=cat1&strip=%d&serial=%s&seq=%d&psm=%d&T=%d&t=%d&data=%s %s%s\r\n\r\n", FLOW_BASE_URL, "/shelf", strip, iccid, seqNum, wnc.isPowerSaveOn(),wnc.gett3412Timer(),wnc.gett3324Timer(), data, FLOW_URL_TYPE, MY_SERVER_URL);
elmkom 44:60008ebffdd4 192 break;
elmkom 44:60008ebffdd4 193 }
elmkom 44:60008ebffdd4 194 #elif PROXIMITYON == 2
elmkom 38:532a0d929756 195 case PROXIMITY_ONLY:
elmkom 35:2e864bae3af0 196 {
elmkom 41:85a736a9b900 197
elmkom 44:60008ebffdd4 198 char* data0 = proximityStrip[0].getDataStr();
elmkom 44:60008ebffdd4 199 char* data1 = proximityStrip[1].getDataStr();
elmkom 41:85a736a9b900 200 seqNum++;
elmkom 44:60008ebffdd4 201 sprintf(modem_string, "GET %s%s?serial=%s&seq=%d&data=%s&data1=%s %s%s\r\n\r\n", FLOW_BASE_URL, "/shelf", iccid, seqNum, data0,data1, FLOW_URL_TYPE, MY_SERVER_URL);
elmkom 41:85a736a9b900 202 break;
elmkom 41:85a736a9b900 203 }
elmkom 41:85a736a9b900 204 #endif
elmkom 41:85a736a9b900 205 case SWITCH_ONLY:
elmkom 41:85a736a9b900 206 {
elmkom 41:85a736a9b900 207 char data[32];
elmkom 41:85a736a9b900 208 sprintf(data,"[{\"p\":%d},{\"p\":%d},{\"p\":%d}]",lastSlot1*26,lastSlot2*26,lastSlot3*26);
elmkom 41:85a736a9b900 209 seqNum++;
elmkom 42:8500f0cb2ea5 210 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 211 break;
elmkom 35:2e864bae3af0 212 }
stefanrousseau 12:7c94ec5069dc 213 case TEMP_HUMIDITY_ONLY:
stefanrousseau 12:7c94ec5069dc 214 {
stefanrousseau 12:7c94ec5069dc 215 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 216 break;
stefanrousseau 12:7c94ec5069dc 217 }
stefanrousseau 12:7c94ec5069dc 218 case TEMP_HUMIDITY_ACCELEROMETER:
stefanrousseau 12:7c94ec5069dc 219 {
stefanrousseau 12:7c94ec5069dc 220 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 221 break;
stefanrousseau 12:7c94ec5069dc 222 }
stefanrousseau 12:7c94ec5069dc 223 case TEMP_HUMIDITY_ACCELEROMETER_PMODSENSORS:
stefanrousseau 12:7c94ec5069dc 224 {
stefanrousseau 12:7c94ec5069dc 225 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 226 break;
stefanrousseau 12:7c94ec5069dc 227 }
stefanrousseau 12:7c94ec5069dc 228 default:
stefanrousseau 12:7c94ec5069dc 229 {
stefanrousseau 12:7c94ec5069dc 230 sprintf(modem_string, "Invalid sensor selected\r\n\r\n");
stefanrousseau 12:7c94ec5069dc 231 break;
stefanrousseau 12:7c94ec5069dc 232 }
stefanrousseau 16:17c5916f2d12 233 } //switch(iSensorsToReport)
stefanrousseau 3:26b3cc155f39 234 } //GenerateModemString
stefanrousseau 3:26b3cc155f39 235
stefanrousseau 3:26b3cc155f39 236
stefanrousseau 16:17c5916f2d12 237 //********************************************************************************************************************************************
stefanrousseau 16:17c5916f2d12 238 //* Process JSON response messages
stefanrousseau 16:17c5916f2d12 239 //********************************************************************************************************************************************
elmkom 40:45e4636d20cc 240 bool extract_reply(char* search_field, char* found_string)
stefanrousseau 16:17c5916f2d12 241 {
stefanrousseau 16:17c5916f2d12 242 char* beginquote;
stefanrousseau 16:17c5916f2d12 243 char* endquote;
elmkom 40:45e4636d20cc 244 beginquote = strstr(search_field, "\r\n\r\n"); //start of data
elmkom 40:45e4636d20cc 245 endquote = strchr(search_field, '\0');
stefanrousseau 16:17c5916f2d12 246 if (beginquote != 0)
stefanrousseau 16:17c5916f2d12 247 {
stefanrousseau 16:17c5916f2d12 248 uint16_t ifoundlen;
stefanrousseau 16:17c5916f2d12 249 if (endquote != 0)
stefanrousseau 16:17c5916f2d12 250 {
stefanrousseau 16:17c5916f2d12 251 ifoundlen = (uint16_t) (endquote - beginquote) + 1;
stefanrousseau 16:17c5916f2d12 252 strncpy(found_string, beginquote, ifoundlen );
stefanrousseau 16:17c5916f2d12 253 found_string[ifoundlen] = 0; //null terminate
stefanrousseau 16:17c5916f2d12 254 return true;
stefanrousseau 16:17c5916f2d12 255 }
stefanrousseau 16:17c5916f2d12 256 }
elmkom 40:45e4636d20cc 257
elmkom 40:45e4636d20cc 258 return false;
elmkom 40:45e4636d20cc 259
elmkom 40:45e4636d20cc 260 } //extract_reply
stefanrousseau 16:17c5916f2d12 261
elmkom 40:45e4636d20cc 262
elmkom 40:45e4636d20cc 263 void parse_reply(char* reply)
elmkom 40:45e4636d20cc 264 {
elmkom 40:45e4636d20cc 265 char *tokens[5];
elmkom 40:45e4636d20cc 266 int index = 0;
elmkom 40:45e4636d20cc 267 tokens[index++] = strtok(reply," ");
elmkom 40:45e4636d20cc 268 while( index < 5 )
elmkom 40:45e4636d20cc 269 {
elmkom 40:45e4636d20cc 270 char* token = strtok(NULL, " ");
elmkom 40:45e4636d20cc 271 if(token == NULL) break;
elmkom 40:45e4636d20cc 272 tokens[index++] = token;
elmkom 40:45e4636d20cc 273 }
elmkom 40:45e4636d20cc 274 if(strcmp("PSM",tokens[1]) == 0)
elmkom 40:45e4636d20cc 275 {
elmkom 40:45e4636d20cc 276
elmkom 40:45e4636d20cc 277 int t3412 = atoi(tokens[3]);
elmkom 40:45e4636d20cc 278 int t3324 = atoi(tokens[4]);
elmkom 40:45e4636d20cc 279 pc.printf("t3412 %d t3324 %d\r\n",t3412,t3324);
elmkom 42:8500f0cb2ea5 280 // setTauTimer(t3412);
elmkom 42:8500f0cb2ea5 281 //setActivityTimer(t3324);
elmkom 40:45e4636d20cc 282
elmkom 40:45e4636d20cc 283 if(strcmp("true",tokens[2]) == 0)
elmkom 40:45e4636d20cc 284 {
elmkom 40:45e4636d20cc 285 pc.printf("PSM ON\r\n");
elmkom 42:8500f0cb2ea5 286 wnc.setPowerSave(true,t3412,t3324);
elmkom 40:45e4636d20cc 287 }
elmkom 40:45e4636d20cc 288 else
elmkom 40:45e4636d20cc 289 {
elmkom 40:45e4636d20cc 290 pc.printf("PSM OFF\r\n");
elmkom 42:8500f0cb2ea5 291 wnc.setPowerSave(false,t3412,t3324);
elmkom 40:45e4636d20cc 292 }
elmkom 40:45e4636d20cc 293 }
elmkom 40:45e4636d20cc 294 }
stefanrousseau 16:17c5916f2d12 295
elmkom 41:85a736a9b900 296 bool checkSlots()
elmkom 41:85a736a9b900 297 {
elmkom 41:85a736a9b900 298 bool changed = false;
elmkom 41:85a736a9b900 299 int s1 = !slot1;
elmkom 41:85a736a9b900 300 int s2 = !slot2;
elmkom 41:85a736a9b900 301 int s3 = !slot3;
elmkom 41:85a736a9b900 302 if(lastSlot1 != s1 || lastSlot2 != s2 ||lastSlot3 != s3)
elmkom 41:85a736a9b900 303 {
elmkom 41:85a736a9b900 304 pc.printf("slot 1 %d\r\n",s1);
elmkom 41:85a736a9b900 305 pc.printf("slot 2 %d\r\n",s2);
elmkom 41:85a736a9b900 306 pc.printf("slot 3 %d\r\n",s3);
elmkom 41:85a736a9b900 307 changed = true;
elmkom 41:85a736a9b900 308 }
elmkom 41:85a736a9b900 309 lastSlot1 = s1;
elmkom 41:85a736a9b900 310 lastSlot2 = s2;
elmkom 41:85a736a9b900 311 lastSlot3 = s3;
elmkom 41:85a736a9b900 312 return changed;
elmkom 41:85a736a9b900 313 }
elmkom 44:60008ebffdd4 314 #if PROXIMITYON == 1
elmkom 44:60008ebffdd4 315 bool scanStrip(int id)
elmkom 44:60008ebffdd4 316 {
elmkom 44:60008ebffdd4 317 bool stripChanged = false;
elmkom 44:60008ebffdd4 318 ATT160826& strip = *strips[id];
elmkom 44:60008ebffdd4 319 att160826_data_t *data = &strip_data[id];
elmkom 44:60008ebffdd4 320 bool ok = strip.scan(data);
elmkom 44:60008ebffdd4 321
elmkom 44:60008ebffdd4 322 if(ok)
elmkom 44:60008ebffdd4 323 {
elmkom 44:60008ebffdd4 324 // printf("%d\r\n",id);
elmkom 44:60008ebffdd4 325 for (int i = 1; i < SENSORS_PER_STRIP;i++) { // ignore first sensor it is blocked
elmkom 44:60008ebffdd4 326
elmkom 44:60008ebffdd4 327 /*
elmkom 44:60008ebffdd4 328 if (strip_data[id].sensor[i].present) {
elmkom 44:60008ebffdd4 329 printf(" %3d", strip_data[id].sensor[i].sample.prox);
elmkom 44:60008ebffdd4 330 } else {
elmkom 44:60008ebffdd4 331 printf(" %3s", "---");
elmkom 44:60008ebffdd4 332 }
elmkom 44:60008ebffdd4 333 */
elmkom 44:60008ebffdd4 334 if (strip_data[id].sensor[i].present) {
elmkom 44:60008ebffdd4 335
elmkom 44:60008ebffdd4 336 if (abs(last_data[id].sensor[i].sample.prox - strip_data[id].sensor[i].sample.prox) > 50)
elmkom 44:60008ebffdd4 337 {
elmkom 44:60008ebffdd4 338 stripChanged = true;
elmkom 44:60008ebffdd4 339 last_data[id].sensor[i].sample.prox = strip_data[id].sensor[i].sample.prox;
elmkom 44:60008ebffdd4 340 last_data[id].sensor[i].sample.als_vis = strip_data[id].sensor[i].sample.als_vis;
elmkom 44:60008ebffdd4 341 last_data[id].sensor[i].sample.als_ir = strip_data[id].sensor[i].sample.als_ir;
elmkom 44:60008ebffdd4 342 }
elmkom 44:60008ebffdd4 343
elmkom 44:60008ebffdd4 344 }
elmkom 44:60008ebffdd4 345 }
elmkom 44:60008ebffdd4 346 // printf("\r\n");
elmkom 44:60008ebffdd4 347 }
elmkom 44:60008ebffdd4 348 else
elmkom 44:60008ebffdd4 349 printf("scan of strip %d failed",id);
elmkom 44:60008ebffdd4 350 return stripChanged;
elmkom 44:60008ebffdd4 351 }
elmkom 44:60008ebffdd4 352 #endif
elmkom 41:85a736a9b900 353
JMF 0:9d5134074d84 354 int main() {
elmkom 36:f8d96ff1dd1b 355
JMF 2:0e2ef866af95 356 int i;
elmkom 41:85a736a9b900 357
JMF 0:9d5134074d84 358 HTS221 hts221;
JMF 0:9d5134074d84 359 pc.baud(115200);
elmkom 42:8500f0cb2ea5 360
JMF 0:9d5134074d84 361 void hts221_init(void);
JMF 0:9d5134074d84 362
fkellermavnet 20:27a4f27254d0 363 // Set LED to RED until init finishes
fkellermavnet 20:27a4f27254d0 364 SetLedColor(0x1);
fkellermavnet 20:27a4f27254d0 365
JMF 1:af7a42f7d465 366 pc.printf(BLU "Hello World from AT&T Shape!\r\n\n\r");
JMF 0:9d5134074d84 367 pc.printf(GRN "Initialize the HTS221\n\r");
JMF 0:9d5134074d84 368
JMF 0:9d5134074d84 369 i = hts221.begin();
JMF 0:9d5134074d84 370 if( i )
JMF 0:9d5134074d84 371 pc.printf(BLU "HTS221 Detected! (0x%02X)\n\r",i);
JMF 0:9d5134074d84 372 else
JMF 0:9d5134074d84 373 pc.printf(RED "HTS221 NOT DETECTED!!\n\r");
JMF 0:9d5134074d84 374
JMF 0:9d5134074d84 375 printf("Temp is: %0.2f F \n\r",CTOF(hts221.readTemperature()));
JMF 0:9d5134074d84 376 printf("Humid is: %02d %%\n\r",hts221.readHumidity());
elmkom 35:2e864bae3af0 377
stefanrousseau 11:e6602513730f 378
JMF 0:9d5134074d84 379 // Initialize the modem
JMF 0:9d5134074d84 380 printf(GRN "Modem initializing... will take up to 60 seconds" DEF "\r\n");
fkellermavnet 14:0c353e212296 381 do {
elmkom 42:8500f0cb2ea5 382 i=wnc.init();
fkellermavnet 14:0c353e212296 383 if (!i) {
fkellermavnet 14:0c353e212296 384 pc.printf(RED "Modem initialization failed!" DEF "\n");
fkellermavnet 14:0c353e212296 385 }
fkellermavnet 14:0c353e212296 386 } while (!i);
JMF 0:9d5134074d84 387
elmkom 42:8500f0cb2ea5 388 // wnc.setIn();
elmkom 44:60008ebffdd4 389 // wnc.send("AT+CPSMS=0",WNC_WAIT_TIME_MS);
elmkom 42:8500f0cb2ea5 390
elmkom 44:60008ebffdd4 391 // wnc.toggleWake();
elmkom 42:8500f0cb2ea5 392
elmkom 41:85a736a9b900 393
elmkom 42:8500f0cb2ea5 394 iccid = wnc.getIccid();
elmkom 42:8500f0cb2ea5 395 printf(GRN "ICCID = %s" DEF "\r\n",iccid);
elmkom 42:8500f0cb2ea5 396
JMF 2:0e2ef866af95 397 //Software init
elmkom 42:8500f0cb2ea5 398 wnc.startInternet();
elmkom 42:8500f0cb2ea5 399 //wnc.ping("108.244.165.22");
stefanrousseau 3:26b3cc155f39 400
fkellermavnet 20:27a4f27254d0 401 // Set LED BLUE for partial init
fkellermavnet 20:27a4f27254d0 402 SetLedColor(0x4);
elmkom 44:60008ebffdd4 403
elmkom 44:60008ebffdd4 404 wnc.setPowerSave(true,wnc.gett3412Timer(),wnc.gett3324Timer());
elmkom 41:85a736a9b900 405
elmkom 44:60008ebffdd4 406 #if PROXIMITYON == 1
elmkom 44:60008ebffdd4 407 bool stripChanged[2];
elmkom 44:60008ebffdd4 408 myi2c = new I2C(PTE25, PTE24);
elmkom 44:60008ebffdd4 409 myi2c->frequency(100000);
elmkom 44:60008ebffdd4 410 ATT160826 strip0 (myi2c, 0);
elmkom 44:60008ebffdd4 411 ATT160826 strip1 (myi2c, 3);
elmkom 44:60008ebffdd4 412 strips[0] = &strip0;
elmkom 44:60008ebffdd4 413 strips[1] = &strip1;
elmkom 44:60008ebffdd4 414
elmkom 44:60008ebffdd4 415 #elif PROXIMITYON == 2
elmkom 44:60008ebffdd4 416
elmkom 44:60008ebffdd4 417 myi2c = new I2C(PTE25, PTE24);
elmkom 44:60008ebffdd4 418 myi2c->frequency(100000);
elmkom 44:60008ebffdd4 419 Proximity strip0;
elmkom 44:60008ebffdd4 420 Proximity strip1;
elmkom 44:60008ebffdd4 421 proximityStrip[0] = strip0;
elmkom 44:60008ebffdd4 422 proximityStrip[1] = strip1;
elmkom 44:60008ebffdd4 423 proximityStrip[0].init(myi2c, 0x70);
elmkom 44:60008ebffdd4 424 proximityStrip[1].init(myi2c, 0x73);
elmkom 44:60008ebffdd4 425 proximityStrip[0].on();
elmkom 44:60008ebffdd4 426 proximityStrip[1].on();
elmkom 44:60008ebffdd4 427
elmkom 41:85a736a9b900 428 #endif
elmkom 38:532a0d929756 429
elmkom 38:532a0d929756 430 int count = 0;
JMF 2:0e2ef866af95 431 // Send and receive data perpetually
JMF 2:0e2ef866af95 432 while(1) {
elmkom 38:532a0d929756 433
elmkom 42:8500f0cb2ea5 434 wnc.checkPassthrough();
elmkom 38:532a0d929756 435 //sprintf(SENSOR_DATA.Temperature, "%0.2f", CTOF(hts221.readTemperature()));
elmkom 38:532a0d929756 436 //sprintf(SENSOR_DATA.Humidity, "%02d", hts221.readHumidity());
elmkom 38:532a0d929756 437 // read_sensors(); //read available external sensors from a PMOD and the on-board motion sensor
elmkom 41:85a736a9b900 438 toggleLed = !toggleLed;
elmkom 41:85a736a9b900 439 if(toggleLed)
elmkom 41:85a736a9b900 440 SetLedColor(0x2); //green
elmkom 41:85a736a9b900 441 else
elmkom 41:85a736a9b900 442 SetLedColor(0); //off
elmkom 41:85a736a9b900 443
elmkom 41:85a736a9b900 444 #if PROXIMITYON == 1
elmkom 44:60008ebffdd4 445
elmkom 44:60008ebffdd4 446 if(stripChanged[0] == false)
elmkom 44:60008ebffdd4 447 stripChanged[0] = scanStrip(0);
elmkom 44:60008ebffdd4 448 if(stripChanged[1] == false)
elmkom 44:60008ebffdd4 449 stripChanged[1] = scanStrip(1);
elmkom 44:60008ebffdd4 450
elmkom 44:60008ebffdd4 451 #elif PROXIMITYON == 2
elmkom 44:60008ebffdd4 452
elmkom 44:60008ebffdd4 453 printf("scan %d\r\n",count);
elmkom 44:60008ebffdd4 454 bool stripChanged = false;
elmkom 44:60008ebffdd4 455 proximityStrip[0].scan();
elmkom 44:60008ebffdd4 456 printf("scan0 done %d\r\n",count);
elmkom 44:60008ebffdd4 457 proximityStrip[1].scan();
elmkom 44:60008ebffdd4 458 printf("scan1 done %d\r\n",count);
elmkom 44:60008ebffdd4 459 stripChanged |= proximityStrip[0].changed(50);
elmkom 44:60008ebffdd4 460 printf("scan0 change %d\r\n",stripChanged);
elmkom 44:60008ebffdd4 461 stripChanged |= proximityStrip[1].changed(50);
elmkom 44:60008ebffdd4 462 printf("scan1 change %d\r\n",stripChanged);
elmkom 44:60008ebffdd4 463
elmkom 41:85a736a9b900 464 #else
elmkom 41:85a736a9b900 465 bool slotsChanged = checkSlots();
elmkom 41:85a736a9b900 466 #endif
elmkom 44:60008ebffdd4 467
elmkom 44:60008ebffdd4 468 char modem_string[512];
elmkom 41:85a736a9b900 469
elmkom 41:85a736a9b900 470 #if PROXIMITYON == 1
elmkom 44:60008ebffdd4 471 if(count >= 5*120)
elmkom 44:60008ebffdd4 472 {
elmkom 44:60008ebffdd4 473 stripChanged[0] = true;
elmkom 44:60008ebffdd4 474 stripChanged[1] = true;
elmkom 44:60008ebffdd4 475 }
elmkom 44:60008ebffdd4 476
elmkom 44:60008ebffdd4 477 if(stripChanged[0] || stripChanged[1])
elmkom 44:60008ebffdd4 478
elmkom 44:60008ebffdd4 479
elmkom 44:60008ebffdd4 480 #elif PROXIMITYON == 2
elmkom 44:60008ebffdd4 481 if(count >= 5*60 || stripChanged )
elmkom 41:85a736a9b900 482 #else
elmkom 41:85a736a9b900 483 if(count >= 5*60 || slotsChanged)
elmkom 41:85a736a9b900 484 #endif
elmkom 38:532a0d929756 485 {
elmkom 42:8500f0cb2ea5 486 if(wnc.isPowerSaveOn())
elmkom 42:8500f0cb2ea5 487 {
elmkom 42:8500f0cb2ea5 488 wnc.wakeFromPowerSave();
elmkom 42:8500f0cb2ea5 489 //wnc.ping("108.244.165.22");
elmkom 42:8500f0cb2ea5 490 }
elmkom 38:532a0d929756 491 count = 0;
elmkom 38:532a0d929756 492 SetLedColor(0x04); //blue
elmkom 36:f8d96ff1dd1b 493
elmkom 44:60008ebffdd4 494
elmkom 41:85a736a9b900 495
elmkom 41:85a736a9b900 496 #if PROXIMITYON == 1
elmkom 44:60008ebffdd4 497 if(stripChanged[0])
elmkom 44:60008ebffdd4 498 {
elmkom 44:60008ebffdd4 499 GenerateModemString(&modem_string[0],PROXIMITY_ONLY,0);
elmkom 44:60008ebffdd4 500 stripChanged[0] = false;
elmkom 44:60008ebffdd4 501 }
elmkom 44:60008ebffdd4 502 else if (stripChanged[1])
elmkom 44:60008ebffdd4 503 {
elmkom 44:60008ebffdd4 504 GenerateModemString(&modem_string[0],PROXIMITY_ONLY,3);
elmkom 44:60008ebffdd4 505 stripChanged[1] = false;
elmkom 44:60008ebffdd4 506 }
elmkom 44:60008ebffdd4 507 else
elmkom 44:60008ebffdd4 508 GenerateModemString(&modem_string[0],PROXIMITY_ONLY,-1);
elmkom 44:60008ebffdd4 509 #elif PROXIMITYON == 2
elmkom 44:60008ebffdd4 510 GenerateModemString(&modem_string[0],PROXIMITY_ONLY,0);
elmkom 41:85a736a9b900 511 #else
elmkom 44:60008ebffdd4 512 GenerateModemString(&modem_string[0],SWITCH_ONLY,0);
elmkom 41:85a736a9b900 513 #endif
elmkom 41:85a736a9b900 514
elmkom 38:532a0d929756 515 printf(BLU "Sending to modem : %s" DEF "\r\n", modem_string);
elmkom 42:8500f0cb2ea5 516
elmkom 44:60008ebffdd4 517 //if(wnc.connect("108.244.165.22",5005)) // node-red
elmkom 44:60008ebffdd4 518 if(wnc.connect("52.21.224.249",80)) // shelf
elmkom 44:60008ebffdd4 519 //if(wnc.connect("52.33.231.251",80)) // car
elmkom 42:8500f0cb2ea5 520 {
elmkom 42:8500f0cb2ea5 521 char * reply = wnc.writeSocket(modem_string);
elmkom 42:8500f0cb2ea5 522
elmkom 42:8500f0cb2ea5 523 char* mydata;
elmkom 42:8500f0cb2ea5 524 int tries = 4;
elmkom 42:8500f0cb2ea5 525 while(tries > 0) // wait for reply
elmkom 42:8500f0cb2ea5 526 {
elmkom 42:8500f0cb2ea5 527 tries--;
elmkom 42:8500f0cb2ea5 528 mydata = wnc.readSocket();
elmkom 42:8500f0cb2ea5 529 if (strlen(mydata) > 0)
elmkom 42:8500f0cb2ea5 530 break;
elmkom 42:8500f0cb2ea5 531
elmkom 44:60008ebffdd4 532 wait(0.5);
elmkom 42:8500f0cb2ea5 533 }
elmkom 42:8500f0cb2ea5 534
elmkom 42:8500f0cb2ea5 535 if (strlen(mydata) > 0)
elmkom 42:8500f0cb2ea5 536 {
elmkom 37:ee01f752524a 537
elmkom 42:8500f0cb2ea5 538 SetLedColor(0x2); // green
elmkom 42:8500f0cb2ea5 539 //only copy on sucessful send
elmkom 38:532a0d929756 540
elmkom 42:8500f0cb2ea5 541 printf(BLU "Read back : %s" DEF "\r\n", mydata);
elmkom 42:8500f0cb2ea5 542 char datareply[512];
elmkom 42:8500f0cb2ea5 543 if (extract_reply(mydata, datareply))
elmkom 42:8500f0cb2ea5 544 {
elmkom 42:8500f0cb2ea5 545 printf(GRN "JSON : %s" DEF "\r\n", datareply);
elmkom 42:8500f0cb2ea5 546 parse_reply(datareply);
elmkom 42:8500f0cb2ea5 547 }
elmkom 42:8500f0cb2ea5 548 SetLedColor(0); // off
elmkom 42:8500f0cb2ea5 549 }
elmkom 42:8500f0cb2ea5 550 else // no reply reset
elmkom 38:532a0d929756 551 {
elmkom 42:8500f0cb2ea5 552 SetLedColor(0x1); //red
elmkom 42:8500f0cb2ea5 553 system_reset();
elmkom 42:8500f0cb2ea5 554 }
elmkom 42:8500f0cb2ea5 555 wnc.disconnect();
elmkom 38:532a0d929756 556 }
elmkom 42:8500f0cb2ea5 557 else // failed to connect reset
elmkom 38:532a0d929756 558 {
elmkom 38:532a0d929756 559 SetLedColor(0x1); //red
elmkom 42:8500f0cb2ea5 560 system_reset();
elmkom 42:8500f0cb2ea5 561 }
elmkom 42:8500f0cb2ea5 562 if(wnc.isPowerSaveOn())
elmkom 42:8500f0cb2ea5 563 wnc.resumePowerSave();
elmkom 38:532a0d929756 564 }
elmkom 38:532a0d929756 565 count++;
elmkom 38:532a0d929756 566 wait(0.2);
stefanrousseau 3:26b3cc155f39 567 } //forever loop
JMF 0:9d5134074d84 568 }