GSMA version

Dependencies:   FXOS8700CQ mbed

Fork of AvnetATT_shape_hackathon by Rick McConney

Revision:
36:f8d96ff1dd1b
Parent:
35:2e864bae3af0
Child:
37:ee01f752524a
--- a/main.cpp	Mon Sep 19 16:16:17 2016 +0000
+++ b/main.cpp	Wed Sep 21 15:42:01 2016 +0000
@@ -10,6 +10,7 @@
 #include "hardware.h"
 I2C i2c(PTC11, PTC10);    //SDA, SCL -- define the I2C pins being used
 I2C proximityi2c(PTE25, PTE24);
+
 // comment out the following line if color is not supported on the terminal
 #define USE_COLOR
 #ifdef USE_COLOR
@@ -36,6 +37,10 @@
 
 #define MDM_DBG_OFF                             0
 #define MDM_DBG_AT_CMDS                         (1 << 0)
+
+#define MUXADDRESS 0x70
+#define PROXIMITYADDRESS  0x39
+
 int mdm_dbgmask = MDM_DBG_OFF;
 
 Serial         pc(USBTX, USBRX);
@@ -62,8 +67,10 @@
 #define MDM_ERR_TIMEOUT                         -1
 
 #define MAX_AT_RSP_LEN                          255
-
-short proximity = 0;
+#define NUM_PROXIMIY_SENSORS 8
+short proximityData [NUM_PROXIMIY_SENSORS][3];
+short lastProximityData [NUM_PROXIMIY_SENSORS][3];
+bool proximityChange = false;
 
 void prox_write_reg(char address,char reg, char cmd)
 {
@@ -90,8 +97,13 @@
     proximityi2c.read(address<<1, rxbuffer, 1 );
     return (unsigned char)rxbuffer[0];
 }
+void proximity_sensor_off(int sensor)
+{
+    prox_write(MUXADDRESS,1<<sensor); 
+    prox_write_reg(PROXIMITYADDRESS,0x41,0x00);
+}
 
-void init_proximity_sensor(int sensor)
+void proximity_sensor_on(int sensor)
 {
     char C25ma = 0x00;
     char C50ma = 0x01;
@@ -122,89 +134,54 @@
     
     char Als50Ps50 = 0x0C;
      
-    char muxaddress = 0x70;
-    char proxaddress = 0x39;
-    prox_write(muxaddress,sensor); // sensor 2
-    prox_write_reg(proxaddress,0x41,Als0Ps400); // initiate ALS: and PS
-    prox_write_reg(proxaddress,0x42,GainAls64Ir64|C25ma); // set ALS_VIS=ALS_IR GAIN = 64 current 25ma
+
+    prox_write(MUXADDRESS,1<<sensor); 
+    prox_write_reg(PROXIMITYADDRESS,0x41,Als400Ps400); // initiate ALS: and PS
+    prox_write_reg(PROXIMITYADDRESS,0x42,GainAls64Ir64|C25ma); // set ALS_VIS=ALS_IR GAIN = 64 current 25ma
+    wait(0.5);
 }
        
-short read_proximity(int sensor)
+short* read_proximity(int sensor)
 {
-    
-    char muxaddress = 0x70;
-    char proxaddress = 0x39;
-    prox_write(muxaddress,sensor); // sensor 2
-    unsigned char prox_lsb = prox_read_reg(proxaddress,0x44);
-    unsigned char prox_msb = prox_read_reg(proxaddress,0x45);
-    unsigned char ALS_lsb = prox_read_reg(proxaddress,0x46);
-    unsigned char ALS_msb = prox_read_reg(proxaddress,0x47);
-    unsigned char IR_lsb = prox_read_reg(proxaddress,0x48);
-    unsigned char IR_msb = prox_read_reg(proxaddress,0x49);
+    short readings[3];
+   
+    prox_write(MUXADDRESS,1<<sensor);
+    //proximity_sensor_on(sensor);
+    unsigned char prox_lsb = prox_read_reg(PROXIMITYADDRESS,0x44);
+    unsigned char prox_msb = prox_read_reg(PROXIMITYADDRESS,0x45);
+    unsigned char ALS_lsb = prox_read_reg(PROXIMITYADDRESS,0x46);
+    unsigned char ALS_msb = prox_read_reg(PROXIMITYADDRESS,0x47);
+    unsigned char IR_lsb = prox_read_reg(PROXIMITYADDRESS,0x48);
+    unsigned char IR_msb = prox_read_reg(PROXIMITYADDRESS,0x49);
     
     short proximity = prox_msb*256+prox_lsb;
     short ALS = ALS_msb*256+ALS_lsb;
     short IR = IR_msb*256+IR_lsb;
-    pc.printf(GRN "Sensor %d\n\r",sensor);
-    pc.printf(GRN "Prox %d\n\r",proximity);
-    pc.printf(GRN "ALS %d\n\r",ALS);
-    pc.printf(GRN "IR %d\n\r",IR);
-    return proximity;
+    readings[0] = proximity;
+    readings[1] = ALS;
+    readings[2] = IR;
+    //proximity_sensor_off(sensor);
+    //pc.printf(GRN "Sensor %d = %d, %d, %d\n\r",sensor,proximity,ALS,IR);
+
+
+    return readings;
 }
     
     
-   /* 
-I2C_w_3 (sfh_address*2, 0x41, 0x08); // initiate ALS: 400ms rep rate, T_int=100ms
-I2C_w_3 (sfh_address*2, 0x42, 0x28); // set ALS_VIS=ALS_IR GAIN = 64
-I2C_w_2_r_1 (sfh_address*2, 0x46); // read lsb of ALS_VIS, register 0x46
-Content1 = Content;
-I2C_w_2_r_1 (sfh_address*2, 0x47); // read msb of ALS_VIS, register 0x47
-ALS_VIS = (Content * 256 + Content1); // combining LSB+MSB byte to decimal value
-I2C_w_2_r_1 (sfh_address*2, 0x48); // read lsb of ALS_IR, register 0x48
-Content1 = Content;
-I2C_w_2_r_1 (sfh_address*2, 0x49); // read msb of ALS_IR, register 0x49
-ALS_IR = (Content * 256 + Content1); // combining LSB+MSB byte to decimal value
-// Lux Calculation based on ALS Gain = 64 and ALS_Int_Time = 100 ms
-// Lux value in front of sensor, no cover glass
-IF ((ALS_IR / ALS_VIS) < 0.109)
- {LUX = (1.534 * ALS_VIS / 64 - 3.759 * ALS_IR / 64) * 1};
-ELSE IF ((ALS_IR / ALS_VIS) < 0.429)
- {LUX = (1.339 * ALS_VIS / 64 – 1.972 * ALS_IR / 64) * 1};
-ELSE IF ((ALS_IR/ALS_VIS) < (0.95 * 1.45))
- {LUX = (0.701 * ALS_VIS / 64 – 0.483 * ALS_IR / 64) * 1};
-ELSE IF ((ALS_IR/ALS_VIS) < (1.5 * 1.45))
- {LUX = (2 * 0.701 * ALS_VIS / 64 – 1.18 * 0.483 * ALS_IR / 64) * 1};
-ELSE IF ((ALS_IR/ALS_VIS) < (2.5 * 1.45))
-{LUX = (4 * 0.701 * ALS_VIS / 64 – 1.33 * 0.483 * ALS_IR / 64) * 1};
-Else {LUX = 8 * 0.701 * ALS_VIS / 64}; 
-*/
-short oldread_proximity( void )
+
+
+
+//********************************************************************************************************************************************
+//* Set the RGB LED's Color
+//* LED Color 0=Off to 7=White.  3 bits represent BGR (bit0=Red, bit1=Green, bit2=Blue) 
+//********************************************************************************************************************************************
+void SetLedColor(unsigned char ucColor)
 {
-    char muxaddress = (0x70<<1);
-    char proxaddress = (0x39<<1);
-    char reg = 0x40;
-    short muxvalue = 0;
-    short value = 0;
-    char sensor = 0x02;
-    
-    char txbuffer [1];
-    char rxbuffer [1];
-    rxbuffer[0] = 0;
-    txbuffer[0] = sensor;
-    proximityi2c.write(muxaddress, txbuffer, 1,false );
-    proximityi2c.read(muxaddress, rxbuffer, 1 );
-    muxvalue = (unsigned char)rxbuffer[0];
-    
-    rxbuffer[0] = 0;
-    txbuffer[0] = reg;
-    proximityi2c.write(proxaddress, txbuffer, 1,false );
-    proximityi2c.read(proxaddress, rxbuffer, 1 );
-    value = (unsigned char)rxbuffer[0];
-    pc.printf(GRN "Mux %d\n\r",muxvalue);
-    pc.printf(GRN "Proximity %d\n\r",value);
-    return value;
-    
-}
+    //Note that when an LED is on, you write a 0 to it:
+    led_red = !(ucColor & 0x1); //bit 0
+    led_green = !(ucColor & 0x2); //bit 1
+    led_blue = !(ucColor & 0x4); //bit 2
+} //SetLedColor() 
 
 ssize_t mdm_getline(char *buff, size_t size, int timeout_ms) {
     int cin = -1;
@@ -326,15 +303,53 @@
     Timer timer;
     timer.start();
     while (timer.read() < 60) {
+        SetLedColor(0x1); //red 
         const char * rsp_lst[] = { ok_str, error_str, NULL };
         int rc = mdm_sendAtCmd("AT", rsp_lst, 500);
         if (rc == 0)
             return true; //timer.read();
+        SetLedColor(0); //off
+        wait_ms(1000 - (timer.read_ms() % 1000));
+        pc.printf("\r%d",timer.read_ms()/1000);
+         
+    }
+    return false;       
+}
+
+bool oldwakeModem()
+{
+    Timer timer;
+    timer.start();
+    while (timer.read() < 60) {
+        const char * rsp_lst[] = { ok_str, error_str, NULL };
+        int rc = mdm_sendAtCmd("AT", rsp_lst, 500);
+        if (rc == 0)
+            return true; 
         wait_ms(1000 - (timer.read_ms() % 1000));
         pc.printf("\r%d",timer.read_ms()/1000);
     }
-    return false;       
+    return false;    
 }
+
+bool wakeModem()
+{
+    const char * rsp_lst[] = { ok_str, error_str, NULL };
+    int tries = 60;
+    pc.printf("wake ");
+    while (tries > 0) {
+        tries--; 
+        pc.printf(".");     
+        int rc = mdm_sendAtCmd("AT", rsp_lst, 500);
+        if (rc == 0)
+        {
+            pc.printf("\r\n");
+            return true;
+        } 
+        wait(1.0);
+    }
+    return false;    
+}
+
 int oldmdm_init(void) {
     // Hard reset the modem (doesn't go through
     // the signal level translator)
@@ -435,15 +450,23 @@
     pc.printf("D %s",rsp);
     return MDM_OK;
 }
-
+void system_reset()
+{
+    printf(RED "\n\rSystem resetting..." DEF "\n");
+    NVIC_SystemReset();
+}
 void reinitialize_mdm(void)
 {
+    system_reset();
+    /*
     // Initialize the modem
     printf(GRN "Modem RE-initializing..." DEF "\r\n");
     if (!mdm_init()) {
-        printf(RED "\n\rModem RE-initialization failed!" DEF "\n");
+        
+        system_reset();
     }
     printf("\r\n");
+    */
 }
 // These are built on the fly
 string MyServerIpAddress;
@@ -482,7 +505,19 @@
     {
         case PROXIMITY:
         {
-            sprintf(modem_string, "GET %s%s?serial=%s&temp=%s&humidity=%s&proximity=%d %s%s\r\n\r\n", FLOW_BASE_URL, FLOW_INPUT_NAME, FLOW_DEVICE_NAME, SENSOR_DATA.Temperature, SENSOR_DATA.Humidity,proximity, FLOW_URL_TYPE, MY_SERVER_URL);
+            char dataStr[NUM_PROXIMIY_SENSORS*32];
+            int i=0;
+            int index = 0;
+            for (i=0; i<NUM_PROXIMIY_SENSORS; i++)
+            {
+                if(i<NUM_PROXIMIY_SENSORS-1)
+                    index += snprintf(&dataStr[index], 128, "{\"s\":%d,\"p\":%d,\"l\":%d,\"r\":%d},", i,proximityData[i][0],proximityData[i][1],proximityData[i][2]);
+                else
+                    index += snprintf(&dataStr[index], 128, "{\"s\":%d,\"p\":%d,\"l\":%d,\"r\":%d}", i,proximityData[i][0],proximityData[i][1],proximityData[i][2]);
+            } 
+            sprintf(modem_string, "GET %s%s?serial=%s&data=[%s] %s%s\r\n\r\n", 
+            FLOW_BASE_URL, FLOW_INPUT_NAME, FLOW_DEVICE_NAME, dataStr, FLOW_URL_TYPE, MY_SERVER_URL);
+            
             break;
         }
         case TEMP_HUMIDITY_ONLY:
@@ -526,17 +561,7 @@
     }            
 } //OneMsFunction()
 
-//********************************************************************************************************************************************
-//* Set the RGB LED's Color
-//* LED Color 0=Off to 7=White.  3 bits represent BGR (bit0=Red, bit1=Green, bit2=Blue) 
-//********************************************************************************************************************************************
-void SetLedColor(unsigned char ucColor)
-{
-    //Note that when an LED is on, you write a 0 to it:
-    led_red = !(ucColor & 0x1); //bit 0
-    led_green = !(ucColor & 0x2); //bit 1
-    led_blue = !(ucColor & 0x4); //bit 2
-} //SetLedColor()
+
 
 //********************************************************************************************************************************************
 //* Process JSON response messages
@@ -637,12 +662,13 @@
 } //parse_JSON
 
 int main() {
+
     int i;
+
     HTS221 hts221;
     pc.baud(115200);
     proximityi2c.frequency(400000);
-    for(int i = 0;i<8;i++)
-        init_proximity_sensor(i);
+
     
     void hts221_init(void);
 
@@ -661,7 +687,34 @@
     printf("Temp  is: %0.2f F \n\r",CTOF(hts221.readTemperature()));
     printf("Humid is: %02d %%\n\r",hts221.readHumidity());
     
+        
+    //test
+  /*  
+    int count = 0;
+    while(count < 4)
+    {
+        if(count == 0)
+            proximity_sensor_on(i);
+        for(int i = 0;i<NUM_PROXIMIY_SENSORS;i++)
+        {
+                
+                short* readings = read_proximity(i);
+                proximityData[i][0] = readings[0];  
+                proximityData[i][1] = readings[1];
+                proximityData[i][2] = readings[2];
+        }
+        proximity_sensor_off(i);
+        wait(2);
+        count++;
+    }
+*/
 
+    for(int i = 0;i<NUM_PROXIMIY_SENSORS;i++)
+    {               
+        proximity_sensor_on(i); 
+        lastProximityData[i][0] = 1000;                       
+    }
+            
     sensors_init();
     read_sensors();
 
@@ -674,6 +727,8 @@
         }
     } while (!i);
     
+  
+         
     //Software init
     software_init_mdm();
  
@@ -681,53 +736,88 @@
     resolve_mdm();
 
     //Create a 1ms timer tick function:
-    OneMsTicker.attach(OneMsFunction, 0.001f) ;
+    //OneMsTicker.attach(OneMsFunction, 0.001f) ;
 
-    iTimer1Interval_ms = SENSOR_UPDATE_INTERVAL_MS;
+   // iTimer1Interval_ms = SENSOR_UPDATE_INTERVAL_MS;
 
     // Open the socket (connect to the server)
     sockopen_mdm();
 
     // Set LED BLUE for partial init
     SetLedColor(0x4);
-
+   
     // Send and receive data perpetually
     while(1) {
         static unsigned ledOnce = 0;
-        if  (bTimerExpiredFlag)
+        if  (true || bTimerExpiredFlag)
         {
+            
             bTimerExpiredFlag = false;
             sprintf(SENSOR_DATA.Temperature, "%0.2f", CTOF(hts221.readTemperature()));
             sprintf(SENSOR_DATA.Humidity, "%02d", hts221.readHumidity());
             read_sensors(); //read available external sensors from a PMOD and the on-board motion sensor
-            for(int i = 0;i<8;i++)
-                proximity = read_proximity(i);
-            char modem_string[512];
-            GenerateModemString(&modem_string[0]);
-            printf(BLU "Sending to modem : %s" DEF "\n", modem_string); 
-            sockwrite_mdm(modem_string);
-            sockread_mdm(&MySocketData, 1024, 20);
             
-            // If any non-zero response from server, make it GREEN one-time
-            //  then the actual FLOW responses will set the color.
-            if ((!ledOnce) && (MySocketData.length() > 0))
-            {
-                ledOnce = 1;
-                SetLedColor(0x2);
+            SetLedColor(0x2); //green       
+            for(int i = 0;i<NUM_PROXIMIY_SENSORS;i++)
+            {     
+                        
+                short* readings = read_proximity(i);
+                proximityData[i][0] = readings[0];  
+                proximityData[i][1] = readings[1];
+                proximityData[i][2] = readings[2];  
+                if(abs(proximityData[i][0] - lastProximityData[i][0]) > 5)
+                {
+                    proximityChange = true;
+                }  
+                lastProximityData[i][0] =  proximityData[i][0]; 
+                  
+                     
             }
+           
+                
+            SetLedColor(0); //off 
             
-            printf(BLU "Read back : %s" DEF "\n", &MySocketData[0]);
-            char myJsonResponse[512];
-            if (extract_JSON(&MySocketData[0], &myJsonResponse[0]))
+            if(proximityChange)
             {
-                printf(GRN "JSON : %s" DEF "\n", &myJsonResponse[0]);
-                parse_JSON(&myJsonResponse[0]);
+                SetLedColor(0x04); //blue
+                proximityChange = false;
+                char modem_string[512];
+                GenerateModemString(&modem_string[0]);
+                printf(BLU "Sending to modem : %s" DEF "\r\n", modem_string);
+                wakeModem(); 
+                sockwrite_mdm(modem_string);
+                sockread_mdm(&MySocketData, 1024, 20);
+            
+                // If any non-zero response from server, make it GREEN one-time
+                //  then the actual FLOW responses will set the color.
+                if (MySocketData.length() > 0)
+                {                   
+                    SetLedColor(0x2); // green
+                    printf(BLU "Read back : %s" DEF "\r\n", &MySocketData[0]);
+                    char myJsonResponse[512];
+                    if (extract_JSON(&MySocketData[0], &myJsonResponse[0]))
+                    {
+                        printf(GRN "JSON : %s" DEF "\r\n", &myJsonResponse[0]);
+                        //parse_JSON(&myJsonResponse[0]);
+                    }
+                    else
+                    {
+                        printf(RED "JSON : %s" DEF "\r\n", &myJsonResponse[0]); //most likely an incomplete JSON string
+                        //parse_JSON(&myJsonResponse[0]); //This is risky, as the string may be corrupted
+                    }
+                    SetLedColor(0); // off             
+                }
+                else
+                {
+                   SetLedColor(0x1); //red
+                   system_reset();
+                   
+                } 
+            
+                
             }
-            else
-            {
-                printf(RED "JSON : %s" DEF "\n", &myJsonResponse[0]); //most likely an incomplete JSON string
-                parse_JSON(&myJsonResponse[0]); //This is risky, as the string may be corrupted
-            }
+            wait(0.2);
         } //bTimerExpiredFlag
+        
     } //forever loop
 }