demo1

Dependencies:   SHT30-DIS-B WakeUp mbed

Fork of M1DK_Skywire_Demo by NimbeLink

Revision:
11:78a28ca6409f
Parent:
10:5974a7b2cf38
Child:
13:f827f384f0a1
--- a/main.cpp	Tue Mar 29 20:03:13 2016 +0000
+++ b/main.cpp	Wed Feb 01 18:36:43 2017 +0000
@@ -1,5 +1,5 @@
 /* main.cpp */
-/* v3.0
+/* v5.0
  * Copyright (C) 2016 nimbelink.com, MIT License
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
@@ -27,15 +27,12 @@
  * Rev A (Legacy): https://freeboard.io/board/jqlneI
  * Rev B (Current): https://freeboard.io/board/LhnbrX
  *
- * Please consult the wiki for more information
+ * Please consult the wiki for more information.
  */
 
 /*
  * INSTRUCTIONS FOR USING THIS CODE
- * 1. Under the "DEFINE THE SKYWIRE MODEM" section of the code, uncomment the modem that you
- * are using to enable to proper setup and features of your Skywire modem.
- *
- * NOTE: Make sure the other Skywire modems listed are commented out.
+ * 1. This code will automatically detect the modem you are using.
  *
  * 2. Change the "DeviceID" to a unique identifier for your Nucleo board. One recommendation
  * would be to use the MEID/IMEI of your Skywire Modem.
@@ -43,67 +40,62 @@
  * 3. If applicable, change the APN for your Skywire Modem.
  */
 
-#include "mbed.h"
-#include "LPS331.h"
-#include "LIS3DH.h"
-#include "LM75B.h"
-#include "hts221.h"
-#include <string>
+#include "mbed.h"           // mbed Library
+#include "pinmap.h"         // pinmap needed for hardware flow control
+
+#include "SHT30DISB.h"
 
-/*
- * DEFINE THE SKYWIRE MODEM
- * Uncomment only the modem that you are using.
- * Make sure only one modem is uncommented!
- */
-//#define NL_SW_1xRTT_V         // Verizon 2G Modem - CE910-DUAL
-//#define NL_SW_1xRTT_S         // Sprint 2G Modem - CE910-DUAL
-//#define NL_SW_1xRTT_A         // Aeris 2G Modem - CE910-DUAL
-//#define NL_SW_GPRS            // AT&T/T-Mobile 2G Modem
-//#define NL_SW_EVDO_V          // Verizon 3G Modem
-//#define NL_SW_EVDO_A          // Aeris 3G Modem
-//#define NL_SW_HSPAP           // AT&T/T-Mobile 3G Modem
-//#define NL_SW_HSPAPG          // AT&T/T-Mobile 3G Modem w/ GPS
-//#define NL_SW_HSPAPE          // GSM 3G Modem, EU
-//#define NL_SW_LTE_TSVG        // Verizon 4G LTE Modem
-//#define NL_SW_LTE_TNAG        // AT&T/T-Mobile 4G LTE Modem
-//#define NL_SW_LTE_TEUG        // GSM 4G LTE Modem, EU
-//#define NL_SW_LTE_GELS3       // VZW LTE Cat 1 Modem
+enum Skywire_Modem {
+    NL_SW_1xRTT_V,      // Verizon 2G Modem - CE910-DUAL
+    NL_SW_1xRTT_S,      // Sprint 2G Modem - CE910-DUAL
+    NL_SW_1xRTT_A,      // Aeris 2G Modem - CE910-DUAL
+    NL_SW_GPRS,         // AT&T/T-Mobile 2G Modem
+    NL_SW_EVDO_V,       // Verizon 3G Modem
+    NL_SW_EVDO_A,       // Aeris 3G Modem
+    NL_SW_HSPAP,        // AT&T/T-Mobile 3G Modem
+    NL_SW_HSPAPG,       // AT&T/T-Mobile 3G Modem w/ GPS
+    NL_SW_HSPAPE,       // GSM 3G Modem, EU
+    NL_SW_LTE_TSVG,     // Verizon 4G LTE Modem
+    NL_SW_LTE_TNAG,     // AT&T/T-Mobile 4G LTE Modem
+    NL_SW_LTE_TEUG,     // GSM 4G LTE Modem, EU
+    NL_SW_LTE_GELS3,    // VZW LTE CAT 1 Modem
+    NL_SW_LTE_S7588     // VZW LTE CAT 4 Modem
+};
 
-/* --CHANGE THIS FOR YOUR SETUP-- */
-#define DeviceID "yourDeviceIDhere"  //Freeboard DweetIO unique ID
+// Our modem
+Skywire_Modem MODEM;
 
-/* --CHANGE THIS FOR YORU SETUP (IF APPLICABLE)-- */
-#if defined NL_SW_HSPAP || defined NL_SW_HSPAPG || defined NL_SW_HSPAPE || defined NL_SW_LTE_TSVG || defined NL_SW_LTE_TNAG || defined NL_SW_LTE_TEUG || defined NL_SW_LTE_GELS3
-    std::string APN = "yourAPNhere";
-#endif
+// --CHANGE THIS FOR YOUR SETUP--
+#define DeviceID "M1DK-UNIT1"  //DweetIO unique ID
+
+// --CHANGE THIS FOR YOUR SETUP (IF APPLICABLE)--
+char const *APN = "NIMBLINK.GW12.VZWENTP";
+//char const *APN = "mw01.VZWSTATIC";
+//char const *APN = "VZWINTERNET";
 
 DigitalOut myled(LED1);                             // Main LED
-DigitalOut skywire_en(PA_6);                        // Skywire Enable
-DigitalOut skywire_rts(PA_7);                       // Skywire Send
-DigitalOut green_LED(PA_1);                         // Green LED
-DigitalOut red_LED(PA_4);                           // Red LED
-DigitalOut blue_LED(PB_0);                          // Blue LED
+DigitalOut skywire_en(A2);                        // Skywire Enable
+PinName skywire_rts(D5);                          // Skywire Send
+DigitalOut green_LED(D7);                         // Green LED
+DigitalOut red_LED(D10);                           // Red LED
 
-AnalogIn photo_trans(PA_0);                         // Photo Transistor
-AnalogIn pot(PC_0);                                 // Potentiometer
-DigitalIn button1(PB_3);                            // Button 1
-DigitalIn button2(PC_1);                            // Button 2
+AnalogIn photo_trans(A3);                         // Photo Transistor
+DigitalOut photo_trans_nEN(D11);                         // Photo Transistor
+DigitalIn button1(D13);                            // Button 1
 
 Serial skywire(PA_9,PA_10);                         // Serial comms to Skywire
 Serial debug_pc(USBTX, USBRX);                      // USB connection to PC
 
 I2C i2c(PB_9,PB_8);                                 // Setup I2C bus for sensors
 bool sw1;                                           // Boolean to check if button 1 is pressed
-bool sw2;                                           // Boolean to check if button 2 is pressed
+
 
-LPS331 pressure(i2c);                               // Pressure Sensor
-LM75B LM75_temp(PB_9,PB_8);                         // Temp Sensor
-// Accelerometer
-LIS3DH accel(i2c, LIS3DH_V_CHIP_ADDR, LIS3DH_DR_NR_LP_100HZ, LIS3DH_FS_2G);
-HTS221 humidity(PB_9, PB_8);                        // Humidity Sensor
+SHT30DISB SHT30(i2c);
 
 // char array for reading from Skywire
 char str[255];
+char csq[3]="99";
+int csq_val = 99;
 
 // Variables for GPS
 float latitude;
@@ -117,6 +109,9 @@
 char rx_buffer[buffer_size+1];
 char rx_line[buffer_size];
 
+// Provisioning Flag for CE910 and DE910 only
+int prov_flag = 0;
+
 // Interrupt for the Skywire
 void Skywire_Rx_interrupt()
 {
@@ -129,18 +124,51 @@
     return;
 }
 
+
+// Function to "pretty print" the delay time
+void wait_print(int time)
+{
+    for (int i = time; i >= 0; i--)
+    {
+        if (i == 1) {
+            debug_pc.printf("Delay Time Remaining: %d second  \r", i);
+        }
+        else {
+            debug_pc.printf("Delay Time Remaining: %d seconds \r", i);
+        }
+        green_LED = 0;
+        wait(0.5);
+        green_LED = 1;
+        wait(0.5);
+    }
+    debug_pc.printf("Delay finished!                  \r\n");
+    return;
+}
+
+
+
 // Function to blink LEDs for debugging
 // NOTE: Currently not used
 void blink_leds(int num) 
 {
     for (int i = 0; i < num; i++) {
-        blue_LED = 0;
-        wait(0.5);
-        blue_LED = 1;
-        wait(0.5);
+        green_LED = 0;
+        myled=0;
+        wait(0.25);
+        green_LED = 1;
+        myled=1;
+        wait(0.25);
     }
 }
-
+void blink_redled(int num) 
+{
+    for (int i = 0; i < num; i++) {
+        red_LED = 0;
+        wait(0.25);
+        red_LED = 1;
+        wait(0.25);
+    }
+}
 // Read line from the UART
 void read_line() 
 {
@@ -170,22 +198,232 @@
 }
 
 // Wait for specific response
-int WaitForResponse(char* response, int num) 
+int WaitForResponse(const char *response) 
 {
+    debug_pc.printf("Command sent. Waiting for: %s\r\n", response);
     do {
         read_line();
         debug_pc.printf("Waiting for: %s, Recieved: %s\r\n", response, rx_line);
-    } while (strncmp(rx_line, response, num));
+    } while (strncmp(rx_line, response, strlen(response)));
+    return 0;
+}
+
+// Get and parse the AT+GMM response
+// Depending on what it gets, parse and return
+int GetGMMResponse() 
+{
+    //int ret_val = 0;
+    do {
+        read_line();
+        debug_pc.printf("Waiting for: %s, Received: %s\r\n", "OK", rx_line);
+        if (!strncmp(rx_line, "GE910-QUAD-V3", 13)) {
+            MODEM = NL_SW_GPRS;
+            debug_pc.printf("Modem detected: NL-SW-GPRS\r\n");
+            //ret_val = 1;
+            return 1;
+        }
+        if (!strncmp(rx_line, "CE910-DUAL", 10)) {
+            MODEM = NL_SW_1xRTT_V;
+            debug_pc.printf("Modem detected: NL-SW-1xRTT\r\n");
+            //ret_val = 1;
+            return 1;
+        }
+        if (!strncmp(rx_line, "DE910-DUAL", 10)) {
+            MODEM = NL_SW_EVDO_V;
+            debug_pc.printf("Modem detected: NL-SW-EVDO\r\n");
+            //ret_val = 1;
+            return 1;
+        }
+        if (!strncmp(rx_line, "HE910-NAD", 9)) {
+            MODEM = NL_SW_HSPAP;
+            debug_pc.printf("Modem detected: NL-SW-HSPAP\r\n");
+            //ret_val = 1;
+            return 1;
+        }
+        if (!strncmp(rx_line, "HE910-D", 7)) {
+            MODEM = NL_SW_HSPAPG;
+            debug_pc.printf("Modem detected: NL-SW-HSPAPG\r\n");
+            //ret_val = 1;
+            return 1;
+        }
+        if (!strncmp(rx_line, "LE910-SVG", 9)) {
+            MODEM = NL_SW_LTE_TSVG;
+            debug_pc.printf("Modem detected: NL-SW-LTE-TSVG\r\n");
+            //ret_val = 1;
+            return 1;
+        }
+        if (!strncmp(rx_line, "LE910-NAG", 9)) {
+            MODEM = NL_SW_LTE_TNAG;
+            debug_pc.printf("Modem detected: NL-SW-LTE-TNAG\r\n");
+            //ret_val = 1;
+            return 1;
+        }
+        if (!strncmp(rx_line, "HL7588", 6)) {
+            MODEM = NL_SW_LTE_S7588;
+            debug_pc.printf("Modem detected: NL-SW-LTE-S7588\r\n");
+            //ret_val = 1;
+            return 1;
+        }
+    } while (strncmp(rx_line, "OK", 2));
     return 0;
 }
 
+// Get and parse the AT+GMM response
+// Depending on what it gets, parse and return
+int GetATIResponse()
+{   
+    int ret_val = 0;
+    do {
+        read_line();
+        debug_pc.printf("Waiting for: %s, Received: %s\r\n", "OK", rx_line);
+        if (!strncmp(rx_line, "ELS31-V", 7)) {
+            MODEM = NL_SW_LTE_GELS3;
+            debug_pc.printf("Modem detected: NL-SW-LTE-GELS3\r\n");
+            ret_val = 1;
+        }
+    } while (strncmp(rx_line, "OK", 2));
+    return ret_val;
+}
+
+// Get the Skywire Model
+int getSkywireModel()
+{
+    // Send ATI. Prints everything for CAT1, so we will check that first
+    debug_pc.printf("Sending ATI...\r\n");
+    skywire.printf("ATI\r");
+    if (GetATIResponse()) {
+        return 0;
+    }
+    // If you get here, then we have a non-CAT1 modem
+    debug_pc.printf("Sending AT+GMM...\r\n");
+    skywire.printf("AT+GMM\r");
+    if (GetGMMResponse()) {
+        return 0;
+    }
+    // Otherwise, we have an error (or no modem), so sit here and blink
+    else {
+        debug_pc.printf("Unable to detect modem: ERROR\r\n");
+        while(1) {
+            blink_leds(10);
+        }
+    }
+}
+
+
+
+int bootSkywire()
+{   
+    int ret_val = 0;
+ 
+    
+    //Enable Skywire
+    skywire.printf("AT\r");
+    read_line();
+    debug_pc.printf("Waiting for: %s, Received: %s\r\n", "OK", rx_line);
+    if (strncmp(rx_line, "OK", 2)) {
+        ret_val=1;
+        debug_pc.printf("retval Value: %i\r\n", ret_val);
+    }
+    else {
+        debug_pc.printf("Modem not detected; Attempt reboot.\r\n");
+        skywire_en=0;
+        debug_pc.printf("Toggling enable pin low\r\n");
+        wait_print(4);
+        skywire_en=1;
+        debug_pc.printf("Toggling enable pin high\r\n");
+        wait_print(10);
+        debug_pc.printf("retval Value2: %i\r\n", ret_val);
+        skywire.printf("AT\r");
+        read_line();
+        debug_pc.printf("Waiting for: %s, Received: %s\r\n", "OK", rx_line);
+        if (strncmp(rx_line, "OK", 2)) {
+            ret_val=1;
+            debug_pc.printf("retval Value: %i\r\n", ret_val);
+            }
+         
+         }
+
+    return ret_val;
+}
+
+
+int GetCSQResponse()
+{   
+    do {
+        skywire.printf("AT+CSQ\r");
+        WaitForResponse("OK");
+        csq[0]=rx_line[6];
+        csq[1]=rx_line[7];
+        csq_val=atoi(csq);  
+//        debug_pc.printf("Waiting for CSQ: %s\r\n", csq);
+//        debug_pc.printf("CSQ Value: %i\r\n", csq_val);
+        
+    } while (!strncmp(rx_line, "CSQ: 99,", 8));
+    return csq_val;
+}
+
+
+
+int Provision(void)
+{   
+    // 1x green means entered the provisioning process
+
+    debug_pc.printf("\r\n\r\n");
+
+    blink_leds(1);
+    debug_pc.printf("Provisioning request detected\r\n");
+    debug_pc.printf("Sending provisioning command...\r\n");
+    skywire.printf("ATD*22899;\r\n");
+    WaitForResponse("OK");
+
+    blink_leds(2);
+    debug_pc.printf("Command sent, OK received!\r\n");
+    debug_pc.printf("Waiting for additional responses...\r\n");
+    WaitForResponse("#OTASP: 0");
+    WaitForResponse("#OTASP: 1");
+    WaitForResponse("#OTASP: 2");
+
+    blink_leds(3);
+    debug_pc.printf("Waiting for NO CARRIER...\r\n");
+    WaitForResponse("NO CARRIER");
+
+    blink_leds(4);
+    debug_pc.printf("\r\nReceived NO CARRIER! Rebooting...\r\n");
+    skywire.printf("AT#REBOOT\r\n");
+    //WaitForResponse("OK", 2);
+
+    green_LED = 0;
+    red_LED = 0;
+
+    // wait 60 seconds for reboot, blink LEDs to indicate something is happening
+    wait_print(60);
+
+    return 0;
+
+}
+
 int main() 
 {
-    float axis[3];
-    float press;
+    
+    
+    // First, check if the button is being held, set flag
+    // and turn on green LED
+    if (button1 == 0) {
+        prov_flag = 1;
+        green_LED = 1;
+        red_LED = 0;
+    } else {
+        green_LED = 0;
+        red_LED = 0;
+    }
+
+    debug_pc.printf("Prov Flag: %d\r\n", prov_flag);
+
+    red_LED = 1;
+
     float temp;
     float humi;
-    float dummy_temp;
+    float photo;
 
     // Setup serial comms with Skywire and PC
     debug_pc.baud(115200);
@@ -193,130 +431,247 @@
     debug_pc.printf("SystemCoreClock = %d Hz\r\n", SystemCoreClock);
     skywire.attach(&Skywire_Rx_interrupt, Serial::RxIrq);
 
-    // Turn on blue LED
-    green_LED = 0;
-    red_LED = 0;
-    blue_LED = 1;
+    //skywire_rts=0;
+
+    // On the NL-SW-LTE-GELS3, flow control is required for consistent results
+    //pin_function(skywire_rts, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF7_USART1));
+    pin_function(skywire_rts, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF7_USART1));
 
-    skywire_rts=0;
+    UART_HandleTypeDef handle;
+    handle.Instance = (USART_TypeDef *)USART1_BASE;
+    __HAL_USART_DISABLE(&handle);
+
+    __HAL_UART_HWCONTROL_CTS_ENABLE(&handle);
+
+    __HAL_UART_HWCONTROL_RTS_ENABLE(&handle);
+    __HAL_USART_ENABLE(&handle);
+
     myled=0;
+    blink_leds(30);
     debug_pc.printf("Starting Demo...\r\n");
     debug_pc.printf("Waiting for Skywire to Boot...\r\n");
+        green_LED = !green_LED;
 
-    //Enable Skywire
-    skywire_en=0;
-    wait(2);
-    skywire_en=1;
-    wait(2);
-    skywire_en=0;
+//    while(!bootSkywire()){
+//        debug_pc.printf("Waiting for Skywire to Boot...\r\n");
+//        
+//        }
+//        
+//    skywire.printf("AT\r");
+//    WaitForResponse("OK");
 
     myled=1;
+    
+    blink_leds(1);
+    // Wait time is different for each modem, so wait up to one minute
+//    wait_print(10);
 
-    // Wait for modem to initialize
-    // Wait time is different for each modem
-    #if defined NL_SW_LTE_GELS3
-        wait(60);
-    #elif defined NL_SW_LTE_TSVG || defined NL_SW_LTE_TNAG || defined NL_SW_LTE_TEUG
-        wait(15);
-    #else
-        wait(5);
-    #endif
+    // Start temp reading///////////////////////////////////////////////////////////////
 
-    // Start temp reading
-    LM75_temp.open();
-
-    //Turn off echo
+    // Turn off echo
     // Helps with checking responses from Skywire
     debug_pc.printf("Turning off echo...\r\n");
-    skywire.printf("ATE0\r\n");
-    WaitForResponse("OK", 2);
+    skywire.printf("ATE0\r");
+    WaitForResponse("OK");
+        green_LED = !green_LED;
+
+    // Get the modem from the Skywire, and set MODEM
+    getSkywireModel();
+
+    // If you have a CDMA-based 2G or 3G Skywire, and you pressed the provision button,
+    // provision the modem
+    if ((MODEM == NL_SW_1xRTT_V || MODEM == NL_SW_EVDO_V) && prov_flag == 1) {
+        Provision();
+    }
 
+    // Debug stuff - get signal quality
+    
+    debug_pc.printf("Getting CSQ...\r\n");
+    GetCSQResponse();
+        green_LED = !green_LED;
+    while(csq_val==99 || csq_val==0)
+    {
+        
+        debug_pc.printf("CSQ Value: %i\r\n",csq_val);
+        debug_pc.printf("No network sginal detected. \r\n");
+        debug_pc.printf("Waiting for device to connect to the network. \r\n");
+        debug_pc.printf("Please check antenna connections if network is not found after 30 seconds. \r\n");
+        wait(1);            
+        //add elapsed time
+        debug_pc.printf("Checking network connectrion. \r\n");
+        GetCSQResponse();
+        red_LED = !red_LED;
+        }
+        
+    debug_pc.printf("Network detected. Checking authorization...\r\n");
+    
+    
+    if (MODEM == NL_SW_LTE_TSVG || MODEM == NL_SW_LTE_TNAG) {
+        green_LED = !green_LED;
+        debug_pc.printf("Sending AT+CGREG...\r\n");
+        skywire.printf("AT+CGREG?\r");
+    } else if (MODEM == NL_SW_LTE_GELS3) {
+        green_LED = !green_LED;
+        debug_pc.printf("Sending AT+CEREG...\r\n");
+        skywire.printf("AT+CEREG?\r");
+    } else {
+        green_LED = !green_LED;
+        debug_pc.printf("Sending AT+CREG...\r\n");
+        skywire.printf("AT+CREG?\r");
+    }
+    WaitForResponse("OK");
+    
     // Turn on DNS Response Caching
     // Used on the Telit-based Skywires
-    #if !defined NL_SW_LTE_GELS3
-        debug_pc.printf("Turning on DNS Cacheing to improve speed...");
-        skywire.printf("AT#CACHEDNS=1\r\n");
-        WaitForResponse("OK", 2);
-    #endif
+    if (MODEM != NL_SW_LTE_GELS3) {
+        green_LED = !green_LED;
+        debug_pc.printf("Turning on DNS Cacheing to improve speed...\r\n");
+        skywire.printf("AT#CACHEDNS=1\r");
+        WaitForResponse("OK");
+    }
 
     debug_pc.printf("Connecting to Network...\r\n");
     // get IP address
-    #if defined NL_SW_LTE_GELS3
-        // The last parameter in AT+SQNSCFG sets the timeout if transmit buffer is not full
-        // Time is in hundreds of ms: so, a value of 5 = 500ms
+    if (MODEM == NL_SW_LTE_GELS3) {
+        green_LED = !green_LED;
+        // set CFUN
+        debug_pc.printf("Sending AT+CFUN=1\r\n");
+        skywire.printf("AT+CFUN=1\r");
+        WaitForResponse("OK");
+
+        // Setup AT+CGDCONT
+        debug_pc.printf("Sending AT+CGDCONT...\r\n");
+        skywire.printf("AT+CGDCONT=3,\"IPV4V6\",\"%s\"\r", APN);
+        WaitForResponse("OK");
+
+        // Turn on AT+CGATT
+        debug_pc.printf("Sending AT+CGATT=1\r\n");
+        skywire.printf("AT+CGATT=1\r");
+        WaitForResponse("OK");
+
+        debug_pc.printf("AT^SISS=0, srvType...\r\n");
+        skywire.printf("AT^SISS=0,\"srvType\",\"Socket\"\r");
+        WaitForResponse("OK");
+        wait(1);
+        debug_pc.printf("AT^SISS=0, conId...\r\n");
+        skywire.printf("AT^SISS=0,\"conId\",3\r");
+        WaitForResponse("OK");
+        wait(1);
+        debug_pc.printf("AT^SISS=0,address...\r\n");
+        skywire.printf("AT^SISS=0,\"address\",\"socktcp://dweet.io:80\"\r");
+        //skywire.printf("AT^SISS=0,\"address\",\"socktcp://52.205.164.103:80\"\r");
+        WaitForResponse("OK");
+        wait(1);
+
+        // AT^SICA activation
+        debug_pc.printf("Activating context...\r\n");
+        skywire.printf("AT^SICA=1,3\r");
+        WaitForResponse("OK");
+        wait(1);
+
+        // AT^SICA? query
+        debug_pc.printf("AT^SICA?\r\n");
+        skywire.printf("AT^SICA?\r");
+        //WaitForResponse("OK");
+        //wait(1);
+
+    } else if (MODEM == NL_SW_LTE_TSVG) {
+        green_LED = !green_LED;
+  // The last parameter in AT#SCFG sets the timeout if transmit buffer is not full
+        // Time is in hundreds of ms: so, a value of 5 = 500 ms
         debug_pc.printf("Configuring context part 1...\r\n");
-        skywire.printf("AT+SQNSCFG=3,3,300,90,600,5\r\n");
-        WaitForResponse("OK", 2);
+        skywire.printf("AT#SCFG=3,3,300,90,600,5\r");
+        WaitForResponse("OK");
+        green_LED = !green_LED;
         wait(1);
         debug_pc.printf("Configuring context part 2...\r\n");
-        skywire.printf("AT+CGDCONT=3,\"IP\",\"vzwinternet\"\r\n");
-        WaitForResponse("OK", 2);
+        skywire.printf("AT+CGDCONT=3,\"IP\",\"%s\"\r", APN);
+        WaitForResponse("OK");//
+        green_LED = !green_LED;
+//        wait(1);
+//        debug_pc.printf("Configuring context...\r\n");
+//        skywire.printf("AT#OTAUIDM=0\r", APN);
+//        WaitForResponse("#DREL");
         wait(1);
-        debug_pc.printf("Activating context...\r\n");
-        skywire.printf("AT+CGACT=1,3\r\n");
-    #elif defined NL_SW_LTE_TSVG
+        debug_pc.printf("activating context ...\r\n");
+        skywire.printf("AT#SGACT=3,1\r");
+        WaitForResponse("#SGACT");
+        green_LED = !green_LED;
+    } else if (MODEM == NL_SW_LTE_TNAG || MODEM == NL_SW_LTE_TEUG || MODEM == NL_SW_HSPAP || MODEM == NL_SW_HSPAPG || MODEM == NL_SW_HSPAPE || MODEM == NL_SW_GPRS) {
+        debug_pc.printf("Configuring context part 1...\r\n");
+        skywire.printf("AT#SCFG=1,1,300,90,600,5\r");
+        WaitForResponse("OK");
+        green_LED = !green_LED;
+        wait(1);
+        debug_pc.printf("Configuring context part 2...\r\n");
+        skywire.printf("AT+CGDCONT=1,\"IP\",\"%s\"\r", APN);
+        WaitForResponse("OK");
+        green_LED = !green_LED;
+        wait(1);
+        skywire.printf("AT#SGACT=1,1\r");
+        WaitForResponse("#SGACT");
+        green_LED = !green_LED;
+    } else {
         // The last parameter in AT#SCFG sets the timeout if transmit buffer is not full
         // Time is in hundreds of ms: so, a value of 5 = 500 ms
-        skywire.printf("AT#SCFG=3,3,300,90,600,5\r\n");
-        WaitForResponse("OK", 2);
-        wait(1);
-        skywire.printf("AT+CGDCONT=3,\"IP\",\"%s\"\r\n", APN);
-        WaitForResponse("OK", 2);
-        wait(1);
-        skywire.printf("AT#SGACT=3,1\r\n");
-        WaitForResponse("#SGACT", 6);
-    #else
-        // The last parameter in AT#SCFG sets the timeout if transmit buffer is not full
-        // Time is in hundreds of ms: so, a value of 5 = 500 ms
-        skywire.printf("AT#SCFG=1,1,300,90,600,5\r\n");
-        WaitForResponse("OK", 2);
-        skywire.printf("AT#SGACT=1,1\r\n");
-        WaitForResponse("#SGACT", 6);
-    #endif
-    WaitForResponse("OK", 2);
+        skywire.printf("AT#SCFG=1,1,300,90,600,5\r");
+        green_LED = !green_LED;
+        WaitForResponse("OK");
+        green_LED = !green_LED;
+        skywire.printf("AT#SGACT=1,1\r");
+        WaitForResponse("#SGACT");
+        green_LED = !green_LED;
+    }
+    WaitForResponse("OK");
 
+        green_LED = !green_LED;
     // Get triangulation data
     // NOTE: This only works on the below modems!
-    #if defined NL_SW_1xRTT_V || defined NL_SW_1xRTT_S || defined NL_SW_1xRTT_A || defined NL_SW_GPRS || defined NL_SW_EVDO_V || defined NL_SW_EVDO_A || defined NL_SW_HSPAP || defined NL_SW_HSPAPG || defined NL_SW_HSPAPE
+    if (MODEM == NL_SW_1xRTT_V || MODEM == NL_SW_1xRTT_S || MODEM == NL_SW_1xRTT_A || MODEM == NL_SW_GPRS || MODEM == NL_SW_EVDO_V || MODEM == NL_SW_EVDO_A || MODEM == NL_SW_HSPAP || MODEM == NL_SW_HSPAPG || MODEM == NL_SW_HSPAPE) {
         //get location approximation from cell tower information
-        skywire.printf("AT#AGPSSND\r\n");
-        WaitForResponse("#AGPSRING:", 10);
+        skywire.printf("AT#AGPSSND\r");
+        WaitForResponse("#AGPSRING:");
         sscanf(rx_line, "%s %d,%f,%f,", str, &number, &latitude, &longitude);
         debug_pc.printf("Location: Latt:%f, Long:%f\r\n", latitude, longitude);
-    #endif
+    }
 
-    red_LED = 0;
-    blue_LED = 0;
-    green_LED = 1;
+    red_LED = 1;
+    green_LED = 0;
+
+    char post[256];
 
     while(1) {
         // Green on to indicate code position
         // Start of loop. Either entered loop for the first time or just sent to dweet.io
         red_LED = 0;
-        blue_LED = 0;
         green_LED = 1;
         // connect to dweet.io
-        #if defined NL_SW_LTE_GELS3
-            skywire.printf("AT+SQNSD=3,0,80,\"dweet.io\"\r\n");
-        #elif defined NL_SW_LTE_TSVG
-            skywire.printf("AT#SD=3,0,80,\"dweet.io\"\r\n");
-        #else
-            skywire.printf("AT#SD=1,0,80,\"dweet.io\"\r\n");
-        #endif
-        WaitForResponse("CONNECT", 7);
-
-
+        if (MODEM == NL_SW_LTE_GELS3) {
+            debug_pc.printf("Opening socket...\r\n");
+            skywire.printf("AT^SISO=0\r");
+            WaitForResponse("OK");
+        }
+        else if (MODEM == NL_SW_LTE_TSVG) {
+            skywire.printf("AT#SD=3,0,80,\"dweet.io\"\r");
+            WaitForResponse("CONNECT");
+        }
+        else {
+            skywire.printf("AT#SD=1,0,80,\"dweet.io\"\r");
+            WaitForResponse("CONNECT");
+        }
+        
         // Update the sensors
-        temp = (float)LM75_temp;
+      //  temp = (float)LM75_temp;
         //temp = (temp * 9)/5 + 32;                     // convert C to F
-        debug_pc.printf("Temp = %.3f\r\n", temp);
-        press=(float)pressure.value() / 4096;
-        debug_pc.printf("Pressure = %.3f\r\n", press);
-        humidity.ReadTempHumi(&dummy_temp, &humi);
-        debug_pc.printf("Humidity = %.3f\r\n", humi);
-        accel.read_data(axis);
-        debug_pc.printf("Accel = %.3f, %.3f, %.3f\r\n", axis[0], axis[1], axis[2]);
-
+     //   debug_pc.printf("Temp = %.3f\r\n", temp);
+      //  press=(float)pressure.value() / 4096;
+      //  debug_pc.printf("Pressure = %.3f\r\n", press);
+//        humidity.ReadTempHumi(&dummy_temp, &humi);
+//debug_pc.printf("Humidity = %.3f\r\n", humi);
+        temp=SHT30.cTemp();
+        humi=SHT30.humidity();
+        photo_trans_nEN=0;
+        photo=photo_trans*200;
         wait(1);
 
         // Check buttons for presses
@@ -324,33 +679,38 @@
             sw1 = 0;
         else
             sw1 = 1;
-        if (button2 == 0)
-            sw2 = 0;
-        else
-            sw2 = 1;
         
         // Green on to indicate code position:
         // Sensors updated, have not sent to dweet.io
         red_LED = 1;
         green_LED = 0;
-        blue_LED = 0;
 
-        debug_pc.printf("Sending information...\r\n");
-        // Report the sensor data to dweet.io
-        skywire.printf("POST /dweet/for/%s?temp=%.3f&p=%.3f&X=%.3f&Y=%.3f&Z=%.3f&La=%f&Lo=%f&sw1=%d&sw2=%d&pot=%.3f&photo=%.3f&humidity=%.3f HTTP/1.0\r\n\r\n", DeviceID, temp, press, axis[0], axis[1], axis[2], latitude, longitude, sw1, sw2, (pot * 3), (photo_trans * 100), humi);
-
-        // Blue on to indicate code position
+        if (MODEM == NL_SW_LTE_GELS3) {
+            sprintf(post, "POST /dweet/for/%s?temp=%.3f&sw1=%d&photo=%.3f&humidity=%.3f HTTP/1.0\r\n\r\n", DeviceID, temp, sw1, photo, humi);
+            debug_pc.printf("Writing...\r\n");
+            skywire.printf("AT^SISW=0,%d\r", strlen(post));
+            WaitForResponse("^SISW:");
+            debug_pc.printf("Sending information...\r\n");
+            skywire.printf("%s", post);
+            WaitForResponse("^SISR:");
+            debug_pc.printf("Reading...\r\n");
+            skywire.printf("AT^SISR=0,1000\r");
+            WaitForResponse("OK");
+            skywire.printf("AT^SISC=0\r");
+            WaitForResponse("OK");
+        }
+        else {
+            debug_pc.printf("Sending information...\r\n");
+            // Report the sensor data to dweet.io
+            skywire.printf("POST /dweet/for/%s?temp=%.3f&sw1=%d&photo=%.3f&humidity=%.3f HTTP/1.0\r\n\r\n", DeviceID, temp, sw1, photo, humi);
+            WaitForResponse("NO CARRIER");
+        }        
+        
+        // green on to indicate code position
         // Data sent to dweet
         red_LED = 0;
-        green_LED = 0;
-        blue_LED = 1;
-
-        // Wait for response from dweet.io
-        #if defined NL_SW_LTE_GELS3
-            WaitForResponse("OK", 2);
-            wait(1);
-        #else
-            WaitForResponse("NO CARRIER", 10);
-        #endif
+        green_LED = 1;
+            
+        
     }
-}
\ No newline at end of file
+}