demo1

Dependencies:   SHT30-DIS-B WakeUp mbed

Fork of M1DK_Skywire_Demo by NimbeLink

Revision:
13:f827f384f0a1
Parent:
11:78a28ca6409f
Child:
14:44eb72d9995e
--- a/main.cpp	Wed Feb 01 18:37:54 2017 +0000
+++ b/main.cpp	Tue Oct 24 20:43:32 2017 +0000
@@ -1,6 +1,5 @@
 /* main.cpp */
-/* v5.0
- * Copyright (C) 2016 nimbelink.com, MIT License
+/* Copyright (C) 2017 nimbelink.com, MIT License
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
  * and associated documentation files (the "Software"), to deal in the Software without restriction,
@@ -20,24 +19,13 @@
 
 /*
  * DESCRIPTION
- * This code updated sensor data on the Nimbelink ST Development Kit (NL-AB-ST-NCL) and sends
- * the information to www.dweet.io using the Thingname "DeviceID". That information can be
- * viewed using a Freeboard at www.freeboard.io (account required). Depending on your
- * ST Development Kit version, clone the following freeboard:
- * Rev A (Legacy): https://freeboard.io/board/jqlneI
- * Rev B (Current): https://freeboard.io/board/LhnbrX
- *
- * Please consult the wiki for more information.
+ * This code sends sensor data to Dweet.io on the NL-M1DK.
  */
 
 /*
  * INSTRUCTIONS FOR USING THIS CODE
- * 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
+ * 1. Change the "DeviceID" to a unique identifier for your Nucleo board. One recommendation
  * would be to use the MEID/IMEI of your Skywire Modem.
- *
- * 3. If applicable, change the APN for your Skywire Modem.
  */
 
 #include "mbed.h"           // mbed Library
@@ -45,63 +33,41 @@
 
 #include "SHT30DISB.h"
 
-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
-};
-
-// Our modem
-Skywire_Modem MODEM;
-
 // --CHANGE THIS FOR YOUR SETUP--
-#define DeviceID "M1DK-UNIT1"  //DweetIO unique ID
+#define DeviceID "<your Thingname here>"  //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";
+const char *APN = "NIMBLINK.GW12.VZWENTP";
 
 DigitalOut myled(LED1);                             // Main 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
+DigitalOut skywire_rts(PB_5);                       // Skywire Send
+DigitalOut green_LED(D7);                           // Green LED
+DigitalOut red_LED(D10);                            // Red LED
 
-AnalogIn photo_trans(A3);                         // Photo Transistor
-DigitalOut photo_trans_nEN(D11);                         // Photo Transistor
-DigitalIn button1(D13);                            // Button 1
+DigitalOut nRESET(PB_4);                            // Skywire Reset line
+
+AnalogIn photo_trans(A3);                           // Photo Transistor
+DigitalOut photo_trans_nEN(D11);                    // Photo Transistor Enable
+DigitalIn button1(PA_5);                            // 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
 
+// SHT30 Sensor Setup
+SHT30DISB SHT30(i2c);
 
-SHT30DISB SHT30(i2c);
+// variable to switch state
+bool sw1;
 
 // char array for reading from Skywire
 char str[255];
+
+// Variables for checking CSQ
 char csq[3]="99";
 int csq_val = 99;
 
-// Variables for GPS
-float latitude;
-float longitude;
-int number;
-
 // Variables for UART comms
 volatile int rx_in=0;
 volatile int rx_out=0;
@@ -109,9 +75,6 @@
 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()
 {
@@ -124,51 +87,6 @@
     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++) {
-        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() 
 {
@@ -203,150 +121,17 @@
     debug_pc.printf("Command sent. Waiting for: %s\r\n", response);
     do {
         read_line();
+        // If we get ERROR, return with 1
+        if (strncmp(rx_line, "ERROR", strlen("ERROR")) == 0) {
+            debug_pc.printf("ERROR\r\n");
+            return 1;
+        }
         debug_pc.printf("Waiting for: %s, Recieved: %s\r\n", response, rx_line);
     } 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;
-}
-
-
+// Send AT+CSQ until we get a good signal
 int GetCSQResponse()
 {   
     do {
@@ -355,70 +140,12 @@
         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() 
 {
-    
-    
-    // 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;
@@ -427,68 +154,41 @@
 
     // Setup serial comms with Skywire and PC
     debug_pc.baud(115200);
-    skywire.baud(115200);
+    skywire.baud(921600);
+    skywire_rts = 0;
     debug_pc.printf("SystemCoreClock = %d Hz\r\n", SystemCoreClock);
+    debug_pc.printf("Attaching interrupt...\r\n");
     skywire.attach(&Skywire_Rx_interrupt, Serial::RxIrq);
-
-    //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));
+    debug_pc.printf("Interrupt attached...\r\n");
 
-    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);
-
+    // Reset the Skywire to get AT commands, and recover from any previous state
+    debug_pc.printf("Resetting Skywire...\r\n");
+    nRESET = 1;
+    wait_ms(100);
+    nRESET = 0;
+    wait(3);
+    debug_pc.printf("Resetting baud...\r\n");
+    skywire.printf("\rAT+IPR=115200\r");
+    wait_ms(100);
+    skywire.baud(115200);
+    wait_ms(100);
+    debug_pc.printf("Baud reset!\r\n");
     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;
-
-//    while(!bootSkywire()){
-//        debug_pc.printf("Waiting for Skywire to Boot...\r\n");
-//        
-//        }
-//        
-//    skywire.printf("AT\r");
-//    WaitForResponse("OK");
-
+    green_LED = !green_LED;
     myled=1;
-    
-    blink_leds(1);
-    // Wait time is different for each modem, so wait up to one minute
-//    wait_print(10);
-
-    // Start temp reading///////////////////////////////////////////////////////////////
+    wait_ms(100);
 
-    // Turn off echo
-    // Helps with checking responses from Skywire
-    debug_pc.printf("Turning off echo...\r\n");
-    skywire.printf("ATE0\r");
+    debug_pc.printf("Sending AT\r\n");
+    skywire.printf("\rAT\r");
     WaitForResponse("OK");
-        green_LED = !green_LED;
+    WaitForResponse("+CEREG: 1");
 
-    // 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
-    
+    // Check CSQ    
     debug_pc.printf("Getting CSQ...\r\n");
     GetCSQResponse();
-        green_LED = !green_LED;
+    green_LED = !green_LED;
     while(csq_val==99 || csq_val==0)
     {
         
@@ -504,170 +204,34 @@
         }
         
     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 (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 (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#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\",\"%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#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=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;
-    }
+    skywire.printf("AT+CEREG?\r");
     WaitForResponse("OK");
 
-        green_LED = !green_LED;
-    // Get triangulation data
-    // NOTE: This only works on the below modems!
-    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");
-        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);
-    }
+    //debug_pc.printf("Connecting to Network...\r\n");
+    debug_pc.printf("Setting up socket...\r\n");
+    skywire.printf("AT+SQNSCFG=3,3,300,90,600,50\r");
+    WaitForResponse("OK");
 
     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;
         green_LED = 1;
-        // connect to dweet.io
-        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");
+        
+        int retries = 0;
+        while (retries < 5) {
+            skywire.printf("AT+SQNSD=3,0,80,\"dweet.io\"\r");
+            if (WaitForResponse("CONNECT") == 1) {
+                retries += 1;
+                wait(1);
+            } else
+                break;
         }
-        else {
-            skywire.printf("AT#SD=1,0,80,\"dweet.io\"\r");
-            WaitForResponse("CONNECT");
-        }
-        
-        // Update the sensors
-      //  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);
+
+        //get temp and humi
         temp=SHT30.cTemp();
         humi=SHT30.humidity();
         photo_trans_nEN=0;
@@ -676,41 +240,30 @@
 
         // Check buttons for presses
         if (button1 == 0)
-            sw1 = 0;
+            sw1 = 1;
         else
-            sw1 = 1;
+            sw1 = 0;
         
         // Green on to indicate code position:
         // Sensors updated, have not sent to dweet.io
         red_LED = 1;
         green_LED = 0;
 
-        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 {
+        if (retries != 5) {
             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
+        }
+//        }        
+        debug_pc.printf("Closing socket...\r\n");
+        skywire.printf("AT+SQNSH=3\r");
+        WaitForResponse("OK");
+        skywire.printf("AT+CGATT=0\r");
+        WaitForResponse("OK");
+        skywire.printf("AT+CGATT=1\r");
+        WaitForResponse("+CEREG: 1");
         red_LED = 0;
         green_LED = 1;
-            
-        
     }
 }