M1DK Skywire Demo

Dependencies:   SHT30-DIS-B mbed

Fork of M1DK_Skywire_Demo by Greg Nash

Revision:
14:6a94410202bb
Parent:
13:f827f384f0a1
--- a/main.cpp	Tue Oct 24 20:43:32 2017 +0000
+++ b/main.cpp	Fri Jun 08 20:31:12 2018 +0000
@@ -1,5 +1,6 @@
 /* main.cpp */
-/* Copyright (C) 2017 nimbelink.com, MIT License
+/* V2 */
+/* Copyright (C) 2018 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,
@@ -34,7 +35,7 @@
 #include "SHT30DISB.h"
 
 // --CHANGE THIS FOR YOUR SETUP--
-#define DeviceID "<your Thingname here>"  //DweetIO unique ID
+#define DeviceID "thingname"  //DweetIO unique ID
 
 // --CHANGE THIS FOR YOUR SETUP (IF APPLICABLE)--
 const char *APN = "NIMBLINK.GW12.VZWENTP";
@@ -162,27 +163,60 @@
     debug_pc.printf("Interrupt attached...\r\n");
 
     // Reset the Skywire to get AT commands, and recover from any previous state
-    debug_pc.printf("Resetting Skywire...\r\n");
+    debug_pc.printf("Resetting Skywire. This gets the Skywire into a known state.\r\n");
+    wait_ms(1000);
     nRESET = 1;
     wait_ms(100);
     nRESET = 0;
     wait(3);
-    debug_pc.printf("Resetting baud...\r\n");
+    debug_pc.printf("\r\n");
+    debug_pc.printf("Resetting baud. This reduced the baudrate on your Skywire for reliability\r\n");
     skywire.printf("\rAT+IPR=115200\r");
     wait_ms(100);
     skywire.baud(115200);
     wait_ms(100);
+    debug_pc.printf("\r\n");
     debug_pc.printf("Baud reset!\r\n");
     myled=0;
+    debug_pc.printf("\r\n");
     debug_pc.printf("Starting Demo...\r\n");
-    debug_pc.printf("Waiting for Skywire to Boot...\r\n");
     green_LED = !green_LED;
     myled=1;
     wait_ms(100);
+    
+    debug_pc.printf("\r\n");
+    debug_pc.printf("Generally, you send a command and wait for a response.\r\n");
+    debug_pc.printf("Depending on the response, you may need to take action.\r\n");
+    debug_pc.printf("You'll need to wait for the response to the previous command\r\n");
+    debug_pc.printf("to finish before continuing on to the next command.\r\n");
+    debug_pc.printf("Sending AT to clear out previous commands.\r\n");
+    debug_pc.printf("\r\n");
+    wait_ms(1000);
+    skywire.printf("\r\r");
+    skywire.printf("AT\r");
+    WaitForResponse("OK");
+    debug_pc.printf("\r\n");
+    debug_pc.printf("Enabling echo to make it easier to see commands\r\n");
+    skywire.printf("ATE1\r");
+    WaitForResponse("OK");
+    debug_pc.printf("\r\n");
+    debug_pc.printf("Setting up Skywire network settings\r\n");
+    skywire.printf("AT+SQNAUTOINTERNET=1\r");
+    WaitForResponse("OK");
+    skywire.printf("AT^AUTOATT=1\r");
+    WaitForResponse("OK");
+    skywire.printf("AT+SQNAUTOCONNECT=1\r");
+    WaitForResponse("OK");
 
-    debug_pc.printf("Sending AT\r\n");
-    skywire.printf("\rAT\r");
+    debug_pc.printf("\r\n");
+    debug_pc.printf("Setting up socket...\r\n");
+    skywire.printf("AT+SQNSCFG=3,3,300,90,600,5\r");
     WaitForResponse("OK");
+    debug_pc.printf("\r\n");
+    debug_pc.printf("Resetting Skywire to get into known state\r\n");
+    skywire.printf("AT^RESET\r");
+    WaitForResponse("OK");
+    WaitForResponse("+SYSSTART");
     WaitForResponse("+CEREG: 1");
 
     // Check CSQ    
@@ -201,36 +235,42 @@
         debug_pc.printf("Checking network connectrion. \r\n");
         GetCSQResponse();
         red_LED = !red_LED;
-        }
+    }
         
+    debug_pc.printf("\r\n");
     debug_pc.printf("Network detected. Checking authorization...\r\n");
     skywire.printf("AT+CEREG?\r");
     WaitForResponse("OK");
 
-    //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;
 
+    debug_pc.printf("\r\n");
+    debug_pc.printf("Everything is setup, and you're registered on the network.\r\n");
+    debug_pc.printf("Entering the socket dial loop.\r\n");
+    
     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;
         
+        debug_pc.printf("Opening the socket\r\n");
         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
+            } else {
+                
                 break;
+            }
         }
-
+        debug_pc.printf("\r\n");
+        debug_pc.printf("Socket open!\r\n");
+        debug_pc.printf("\r\n");
+        debug_pc.printf("Updating sensors\r\n");
         //get temp and humi
         temp=SHT30.cTemp();
         humi=SHT30.humidity();
@@ -248,21 +288,25 @@
         // Sensors updated, have not sent to dweet.io
         red_LED = 1;
         green_LED = 0;
-
+        debug_pc.printf("\r\n");
+        debug_pc.printf("Sensors updated!\r\n");
+        debug_pc.printf("\r\n");
+        debug_pc.printf("Sending information\r\n");
         if (retries != 5) {
+            retries = 0;
             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");
         }
-//        }        
+        
+        debug_pc.printf("\r\n");
+        debug_pc.printf("Information sent!\r\n");
+ 
+        debug_pc.printf("\r\n");
         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;
     }