Allows SMS to be sent to query Alexa. Also allows HTTP requests to be sent to Alexa via an ESP8266-hosted web server.

Dependencies:   emic2 mbed

Revision:
0:1271d15b4d4b
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/UPDATED_BACKUP_main.txt	Tue May 02 03:13:27 2017 +0000
@@ -0,0 +1,1234 @@
+// Author: Alexander Mussa, Jonathan Osei-Owusu
+// Purpose: Integrate ESP8266 WiFi Chip with Adafruit FONA Cellular Module to send text commands to Amazon Echo Dot
+// 4180 Final Project
+
+/*************************************************** 
+  This is an example for our Adafruit FONA Cellular Module + ESP8266 WiFi Chip
+
+  Designed specifically to work with the Adafruit FONA 
+  ----> http://www.adafruit.com/products/1946
+  ----> http://www.adafruit.com/products/1963
+  ----> http://www.adafruit.com/products/2468
+  ----> http://www.adafruit.com/products/2542
+
+  These cellular modules use TTL Serial to communicate, 2 pins are 
+  required to interface
+  Adafruit invests time and resources providing this open source code, 
+  please support Adafruit and open-source hardware by purchasing 
+  products from Adafruit!
+
+  Written by Limor Fried/Ladyada for Adafruit Industries.  
+  BSD license, all text above must be included in any redistribution
+ ****************************************************/
+ 
+ /*
+  *  Modified by Jesse Baker & George Tzintzarov 03/30/2016 for use in mbed LPC1768
+  */
+
+/* 
+THIS CODE IS STILL IN PROGRESS!
+
+Open up the serial console on the Arduino at 4800 baud to interact with FONA
+
+Note that if you need to set a GPRS APN, username, and password scroll down to
+the commented section below just before the main "while (true)" loop.
+*/
+
+#include <string.h> //// BY JONATHAN: For strcpy()
+#include <ctype.h>
+//#include "SoftSerial.h" I dont think we need this
+#include "Adafruit_FONA.h"
+#include "emic2.h"
+
+#define FONA_RST p12
+#define FONA_TX p13
+#define FONA_RX p14
+#define FONA_RI p11
+
+// this is a large buffer for replies
+char replybuffer[255];
+char smsText[255], smsPhoneNo[11]; ///// By Jonathan: Text to send to Text -> Speech chip & phone # to send to 
+
+int myCount; // by Jonathan
+
+
+Serial pcSerial(USBTX, USBRX);
+Adafruit_FONA fona(FONA_TX, FONA_RX, FONA_RST, FONA_RI);
+//uLCD_4DGL uLCD(p28,p27,p30);
+
+// Turn on a LED when somebody call the FONA
+DigitalOut led1(LED1); 
+emic2 myTTS(p9, p10);
+
+///// ---------------- Below are global vars and functions for ESP2886 chip -------------//////////
+
+Serial esp(p28, p27); // tx, rx
+DigitalOut reset(p26);
+DigitalOut led4(LED4);
+Timer t;
+ 
+int  count,ended,timeout;
+char buf[2024];
+char snd[1024];
+
+char myText[1024]; //////////////// by Jonathan (string to post to web server)
+ 
+char ssid[32] = "Brocano";     // enter WiFi router ssid inside the quotes
+char pwd [32] = "habitacion"; // enter WiFi router password inside the quotes
+ 
+void SendCMD(),getreply(),ESPconfig(),ESPsetbaudrate();
+ void dev_recv()
+{
+    led1 = !led1;
+    while(esp.readable()) {
+        pcSerial.putc(esp.getc());
+    }
+}
+ 
+ //////// JONATHAN: CODE TO EDIIIIT ////////////////
+void pc_recv() //////// CODE TO EDIT TO INPUT TEXT FROM TERMINAL -> WEBSITE //////
+{
+    led4 = !led4;
+    char currChar;
+    myText[0] = '\0'; // clear text from char array
+    int i = 0;
+    while(pcSerial.readable()) {
+        currChar = pcSerial.getc();
+        pcSerial.putc(currChar); // Added by Jonathan. local echo
+        myText[i++] = currChar;
+    }
+}
+ 
+ 
+
+
+
+
+//// ---------------------End ESP8226 Global region ------------------------------////
+
+
+
+class FonaEventListener : public Adafruit_FONA::EventListener {
+    virtual void onRing() {
+        led1 = 1;
+    }
+    
+    virtual void onNoCarrier() {
+        led1 = 0; 
+    }
+};
+FonaEventListener fonaEventListener;
+
+// Functions defined after main()
+uint8_t readline(char *buff, uint8_t maxbuff, uint16_t timeout = 0);
+void printMenu(void);
+void flushSerial();
+char readBlocking();
+uint16_t readnumber();
+long map(long x, long in_min, long in_max, long out_min, long out_max);
+
+int main() {
+    myTTS.volume(5);
+    myTTS.voice(0);
+    pcSerial.baud(9600);
+    wait(1);
+    pcSerial.printf("\r\n");
+    
+    //// ------------------- Setting up ESP8266 WiFi Below --------------- ///
+    
+    reset=0; //hardware reset for 8266
+    pcSerial.printf("\f\n\r-------------ESP8266 Hardware Reset-------------\n\r");
+    wait(0.5);
+    reset=1;
+    timeout=2;
+    getreply();
+ 
+    esp.baud(9600);   // change this to the new ESP8266 baudrate if it is changed at any time.
+ 
+    //ESPsetbaudrate();   //******************  include this routine to set a different ESP8266 baudrate  ******************
+ 
+    ESPconfig();        //******************  include Config to set the ESP8266 configuration  ***********************
+ 
+ 
+ 
+    pcSerial.attach(&pc_recv, Serial::RxIrq);
+    esp.attach(&dev_recv, Serial::RxIrq);
+    
+      
+    
+    
+    //// ---------------------Setting up FONA GSM Chip Below ------------------- ////
+    
+    
+    pcSerial.printf("FONA basic test\r\n");
+    pcSerial.printf("Initializing....(May take 3 seconds)\r\n");
+    
+    // See if the FONA is responding
+    if (! fona.begin(9600)) {
+        pcSerial.printf("Couldn't find FONA\r\n");
+        while (1);
+    }
+    fona.setEventListener(&fonaEventListener);
+    pcSerial.printf("FONA is OK\r\n");
+    
+    // Print SIM card IMEI number.
+    char imei[15] = {0}; // MUST use a 16 character buffer for IMEI!
+    uint8_t imeiLen = fona.getIMEI(imei);
+    if (imeiLen > 0) {
+        pcSerial.printf("SIM card IMEI: %s\r\n", imei);
+    }
+    
+    // Optionally configure a GPRS APN, username, and password.
+    // You might need to do this to access your network's GPRS/data
+    // network.  Contact your provider for the exact APN, username,
+    // and password values.  Username and password are optional and
+    // can be removed, but APN is required.
+    //fona.setGPRSNetworkSettings("your APN", "your username", "your password");
+    //fona.setGPRSNetworkSettings("web.pt.lu", "", "");
+    
+    // Optionally configure HTTP gets to follow redirects over SSL.
+    // Default is not to follow SSL redirects, however if you uncomment
+    // the following line then redirects over SSL will be followed.
+    //fona.setHTTPSRedirect(true);
+
+    printMenu();
+    
+    
+    while (true) {
+        
+        pcSerial.printf("FONA> ");
+        // if nothing is available on the pcSearial port but there is something on the fona serial, then print it (flush it) to the pc Serial
+        
+        /*
+        while (! pcSerial.readable() ) {
+            if (fona.readable()) {
+                pcSerial.putc(fona.getc()); ///////  RESPONSIBLE FOR DETECTING RECV'D TXT///////
+            }
+        } */
+        
+        //////////////////// By Jonathan: Edited version of while() above
+        while (! pcSerial.readable()) {
+            if (fona.readable()) {
+                
+                //// Printf that incoming SMS detected
+                pcSerial.putc(fona.getc()); 
+
+                    
+                // ----------------------------------------------------------------//
+                
+                // read an SMS + send -> TTS chip
+                flushSerial();
+                pcSerial.printf("\r\nReading SMS #1\r\n");
+                
+                // Retrieve SMS sender address/phone number.
+                if (! fona.getSMSSender(1, replybuffer, 250)) {
+                    pcSerial.printf("Failed!\r\n");
+                    break;
+                }
+                pcSerial.printf("FROM: %s\r\n", replybuffer);
+                for (int i = 0; i < 10; i++) { /////// BY JONATHAN to store phone # to respond to
+                    smsPhoneNo[i] = replybuffer[i + 2]; // chop off the "+1" from phone # returned by replybuffer
+                }
+                smsPhoneNo[10] = '\0'; // null terminate.... NECESSARY??
+                pcSerial.printf("FROM: %s\r\n", smsPhoneNo);
+                
+                // Retrieve SMS value.
+                uint16_t smslen;
+                if (! fona.readSMS(1, replybuffer, 250, &smslen)) { // pass in buffer and max len!
+                    pcSerial.printf("Failed!\r\n");
+                    break;
+                }
+                pcSerial.printf("***** SMS #1 (%d) bytes *****\r\n", smslen); 
+                pcSerial.printf("%s\r\n", replybuffer);
+                pcSerial.printf("*****\r\n");
+                
+                
+                
+                //char message[255]; // CHANGED BY JONATHAN. 141 -> 255
+                char myString[255];
+                
+                for (int i = 0; i < 255; i++) {
+                    myString[i] = replybuffer[i + 5];   // chop of the "Alexa string"    
+                }
+         
+                strcpy(smsText, "Uh lex uh "); //Pronunciation Correction.
+                strcat(smsText, myString);
+                myTTS.speakf("S%s\r", smsText);  // Send text message to the text to speech module.
+                
+              
+                 // ----------------------------------------------------------------//
+                 
+                 // send an SMS back!
+                char message[255];
+                 
+                strcpy(message, "");
+            
+                flushSerial();
+                
+                pcSerial.printf("Replying to %s\r\n", smsPhoneNo);
+                
+                
+                if(strstr(replybuffer, "how old are you")) { // if reply buffer contains that string
+                    strcpy(message, "I'm 2 in human years, 14 in dog years, and 25 in cat years. I think AI years are marked in nanoseconds, so that makes me, like, a scrillion!");
+                }
+                else if(strstr(replybuffer, "what is the world population")) {
+                    strcpy(message, "The population of the world is about 7 Billion 400 Million.");
+                }
+                else if(strstr(replybuffer, "integral of x squared")) {
+                    strcpy(message, "The integral of x squared is one third x cubed plus a constant.");
+                }
+                else if(strstr(replybuffer, "discrete co sine transform")) {
+                    strcpy(message, "The discrete cosine transform expresses a finite sequence of data points in terms of a sum of cosine function oscillating at different frequencies.");
+                } 
+                else {
+                    strcpy(message, "Sorry, command not recognized.");   
+                }
+                
+                
+                
+               // message[255] = '\0';
+                
+                pcSerial.printf("%s\r\n", message);
+                if (!fona.sendSMS(smsPhoneNo, message))
+                    pcSerial.printf("Failed\r\n");
+                 else 
+                    pcSerial.printf("Sent!\r\n");
+                    
+                 // ----------------------------------------------------------------//
+                
+                // delete an SMS after response sent back
+                flushSerial();
+                pcSerial.printf("\r\nDeleting SMS #1\r\n");
+                if (fona.deleteSMS(1)) // delete SMS #1
+                    pcSerial.printf("OK!\r\n");
+                else 
+                    pcSerial.printf("Couldn't delete\r\n");
+                
+            } // end if
+        } // end while
+        
+        
+        
+    // Above while() by Jonathan //     
+        
+        
+        
+        // get the input command from the terminal
+        char command = pcSerial.getc();
+        pcSerial.printf("%c\r\n", command); //loops back to COM port
+        
+        
+        switch (command) {
+            case '?': {
+                printMenu();
+                break;
+            }
+            
+            case 'a': {
+                // read the ADC
+                uint16_t adc;
+                if (! fona.getADCVoltage(&adc)) {
+                    pcSerial.printf("Failed to read ADC\r\n");
+                } else {
+                    pcSerial.printf("ADC = %d mV\r\n", adc);
+                }
+                break;
+            }
+            
+            case 'b': {
+                // read the battery voltage and percentage
+                uint16_t vbat;
+                if (! fona.getBattVoltage(&vbat)) {
+                    pcSerial.printf("Failed to read Batt\r\n");
+                } else {
+                    pcSerial.printf("VBat = %d mV\r\n", vbat);
+                }
+                
+                if (! fona.getBattPercent(&vbat)) {
+                    pcSerial.printf("Failed to read Batt\r\n");
+                } else {
+                    pcSerial.printf("VPct = %d%%\r\n", vbat);
+                }
+                
+                break;
+            }
+            
+            case 'U': {
+                // Unlock the SIM with a PIN code
+                char PIN[5];
+                flushSerial();
+                pcSerial.printf("Enter 4-digit PIN\r\n");
+                readline(PIN, 3);
+                pcSerial.printf("%s\r\n", PIN);
+                pcSerial.printf("Unlocking SIM card: ");
+                if (! fona.unlockSIM(PIN)) {
+                    pcSerial.printf("Failed\r\n");
+                } else {
+                    pcSerial.printf("OK!\r\n");
+                }        
+                break;
+            }
+            
+            case 'C': {
+                // read the CCID
+                fona.getSIMCCID(replybuffer);  // make sure replybuffer is at least 21 bytes!
+                pcSerial.printf("SIM CCID = %s\r\n", replybuffer);
+                break;
+            }
+            
+            case 'i': {
+                // read the RSSI
+                uint8_t n = fona.getRSSI();
+                int8_t r = 0;
+                
+                pcSerial.printf("RSSI = %d: ", n);
+                if (n == 0) r = -115;
+                if (n == 1) r = -111;
+                if (n == 31) r = -52;
+                if ((n >= 2) && (n <= 30)) {
+                    r = map(n, 2, 30, -110, -54);
+                }
+                pcSerial.printf("%d dBm\r\n", r);
+                
+                break;
+            }
+            
+            case 'n': {
+                // read the network/cellular status
+                uint8_t n = fona.getNetworkStatus();
+                pcSerial.printf("Network status %d: ", n);
+                if (n == 0) pcSerial.printf("Not registered\r\n");
+                if (n == 1) pcSerial.printf("Registered (home)\r\n");
+                if (n == 2) pcSerial.printf("Not registered (searching)\r\n");
+                if (n == 3) pcSerial.printf("Denied\r\n");
+                if (n == 4) pcSerial.printf("Unknown\r\n");
+                if (n == 5) pcSerial.printf("Registered roaming\r\n");
+                break;
+            }
+            
+            /*** Audio ***/
+            case 'v': {
+                // set volume
+                flushSerial();
+                pcSerial.printf("Set Vol %%");
+                uint8_t vol = readnumber();
+                pcSerial.printf("\r\n");
+                if (! fona.setVolume(vol)) {
+                    pcSerial.printf("Failed\r\n");
+                } else {
+                    pcSerial.printf("OK!\r\n");
+                }
+                break;
+            }
+            
+            case 'V': {
+                uint8_t v = fona.getVolume();
+                pcSerial.printf("%d%%\r\n", v);
+                
+                break; 
+            }
+            
+            case 'H': {
+                // Set Headphone output
+                if (! fona.setAudio(FONA_HEADSETAUDIO)) {
+                    pcSerial.printf("Failed\r\n");
+                } else {
+                    pcSerial.printf("OK!\r\n");
+                }
+                fona.setMicVolume(FONA_HEADSETAUDIO, 15);
+                break;
+            }
+            case 'e': {
+                // Set External output
+                if (! fona.setAudio(FONA_EXTAUDIO)) {
+                    pcSerial.printf("Failed\r\n");
+                } else {
+                    pcSerial.printf("OK!\r\n");
+                }
+                
+                fona.setMicVolume(FONA_EXTAUDIO, 10);
+                break;
+            }
+            
+            case 'T': {
+                // play tone
+                flushSerial();
+                pcSerial.printf("Play tone #");
+                uint8_t kittone = readnumber();
+                pcSerial.printf("\r\n");
+                // play for 1 second (1000 ms)
+                if (! fona.playToolkitTone(kittone, 1000)) {
+                    pcSerial.printf("Failed\r\n");
+                } else {
+                    pcSerial.printf("OK!\r\n");
+                }
+                break;
+            }
+            
+            /*** FM Radio ***/
+            
+            case 'f': {
+                // get freq
+                flushSerial();
+                pcSerial.printf("FM Freq (eg 1011 == 101.1 MHz): ");
+                uint16_t station = readnumber();
+                pcSerial.printf("\r\n");
+                // FM radio ON using headset
+                if (fona.FMradio(true, FONA_HEADSETAUDIO)) {
+                    pcSerial.printf("Opened\r\n");
+                }
+                if (! fona.tuneFMradio(station)) {
+                    pcSerial.printf("Failed\r\n");
+                } else {
+                    pcSerial.printf("Tuned\r\n");
+                }
+                break;
+            }
+            case 'F': {
+                // FM radio off
+                if (! fona.FMradio(false)) {
+                    pcSerial.printf("Failed\r\n");
+                } else {
+                    pcSerial.printf("OK!\r\n");
+                }
+                break;
+            }
+            case 'm': {
+                // Set FM volume.
+                flushSerial();
+                pcSerial.printf("Set FM Vol [0-6]:");
+                uint8_t vol = readnumber();
+                pcSerial.printf("\r\n");
+                if (!fona.setFMVolume(vol)) {
+                    pcSerial.printf("Failed\r\n");
+                } else {
+                    pcSerial.printf("OK!\r\n");
+                }   
+                break;
+            }
+            case 'M': {
+                // Get FM volume.
+                int8_t fmvol = fona.getFMVolume();
+                if (fmvol < 0) {
+                    pcSerial.printf("Failed\r\n");
+                } else {
+                    pcSerial.printf("FM volume: %d\r\n", fmvol);
+                }
+                break;
+            }
+            case 'q': {
+                // Get FM station signal level (in decibels).
+                flushSerial();
+                pcSerial.printf("FM Freq (eg 1011 == 101.1 MHz): ");
+                uint16_t station = readnumber();
+                pcSerial.printf("\r\n");
+                int8_t level = fona.getFMSignalLevel(station);
+                if (level < 0) {
+                    pcSerial.printf("Failed! Make sure FM radio is on (tuned to station).\r\n");
+                } else {
+                    pcSerial.printf("Signal level (dB): %d\r\n", level);
+                }
+                break;
+            }
+            
+            /*** PWM ***/
+            
+            case 'P': {
+                // PWM Buzzer output @ 2KHz max
+                flushSerial();
+                pcSerial.printf("PWM Freq, 0 = Off, (1-2000): ");
+                uint16_t freq = readnumber();
+                pcSerial.printf("\r\n");
+                if (! fona.setPWM(freq)) {
+                    pcSerial.printf("Failed\r\n");
+                } else {
+                    pcSerial.printf("OK!\r\n");
+                }
+                break;
+            }
+            
+            /*** Call ***/
+            case 'c': {      
+                // call a phone!
+                char number[10];
+                flushSerial();
+                pcSerial.printf("Call #");
+                readline(number, 9);
+                pcSerial.printf("\r\n");
+                pcSerial.printf("Calling %s\r\n", number);
+                if (!fona.callPhone(number)) {
+                    pcSerial.printf("Failed\r\n");
+                } else {
+                    pcSerial.printf("Sent!\r\n");
+                }
+                
+                break;
+            }
+            case 'h': {
+                // hang up! 
+                if (! fona.hangUp()) {
+                    pcSerial.printf("Failed\r\n");
+                } else {
+                    pcSerial.printf("OK!\r\n");
+                }
+                break;     
+            }
+            
+            case 'p': {
+                // pick up! 
+                if (! fona.pickUp()) {
+                    pcSerial.printf("Failed\r\n");
+                } else {
+                    pcSerial.printf("OK!\r\n");
+                }
+                break;     
+            }
+            
+            /*** SMS ***/
+            
+            case 'N': {
+                // read the number of SMS's!
+                int8_t smsnum = fona.getNumSMS();
+                if (smsnum < 0) {
+                    pcSerial.printf("Could not read # SMS\r\n");
+                } else {
+                    pcSerial.printf("%d SMS's on SIM card!\r\n", smsnum); 
+                }
+                break;
+            }
+            case 'r': {
+                // read an SMS
+                flushSerial();
+                pcSerial.printf("Read #");
+                uint8_t smsn = readnumber();
+                pcSerial.printf("\r\nReading SMS #%d\r\n", smsn);
+                
+                // Retrieve SMS sender address/phone number.
+                if (! fona.getSMSSender(smsn, replybuffer, 250)) {
+                    pcSerial.printf("Failed!\r\n");
+                    break;
+                }
+                pcSerial.printf("FROM: %s\r\n", replybuffer);
+                
+                // Retrieve SMS value.
+                uint16_t smslen;
+                if (! fona.readSMS(smsn, replybuffer, 250, &smslen)) { // pass in buffer and max len!
+                    pcSerial.printf("Failed!\r\n");
+                    break;
+                }
+                pcSerial.printf("***** SMS #%d (%d) bytes *****\r\n", smsn, smslen); 
+                pcSerial.printf("%s\r\n", replybuffer);
+                pcSerial.printf("*****\r\n");
+                
+                break;
+            }
+            case 'R': {
+                // read all SMS
+                int8_t smsnum = fona.getNumSMS();
+                uint16_t smslen;
+                for (int8_t smsn=1; smsn<=smsnum; smsn++) {
+                    pcSerial.printf("\r\nReading SMS #%d\r\n", smsn);
+                    if (!fona.readSMS(smsn, replybuffer, 250, &smslen)) {  // pass in buffer and max len!
+                        pcSerial.printf("Failed!\r\n");
+                        break;
+                    }
+                    // if the length is zero, its a special case where the index number is higher
+                    // so increase the max we'll look at!
+                    if (smslen == 0) {
+                        pcSerial.printf("[empty slot]\r\n");
+                        smsnum++;
+                        continue;
+                    }
+                    
+                    pcSerial.printf("***** SMS #%d (%d) bytes *****\r\n", smsn, smslen);
+                    pcSerial.printf("%s\r\n", replybuffer);
+                    pcSerial.printf("*****\r\n");
+                }
+                break;
+            }
+            
+            case 'd': {
+                // delete an SMS
+                flushSerial();
+                pcSerial.printf("Delete #");
+                uint8_t smsn = readnumber();
+                
+                pcSerial.printf("\r\nDeleting SMS #%d\r\n", smsn);
+                if (fona.deleteSMS(smsn)) {
+                    pcSerial.printf("OK!\r\n");
+                } else {
+                    pcSerial.printf("Couldn't delete\r\n");
+                }
+                break;
+            }
+            
+            case 's': {
+                // send an SMS!
+                char sendto[10], message[141],currChar; // 'currChar' by Jonathan 
+                myCount = 0; // reset
+                
+                flushSerial();
+                pcSerial.printf("Send to #");
+                //currChar = pcSerial.getc();
+                readline(sendto, 9);
+                pcSerial.printf("%s\r\n", sendto);
+                pcSerial.printf("Type out one-line message (140 char): ");
+                
+                myCount = 0;
+                while (1) // whole while loop added by jonathan... WORKS PERFECTLY for sending texts to phone
+                {
+                    
+                    pcSerial.putc(currChar = pcSerial.getc()); // local echo
+                    if (currChar == '\r') 
+                    {
+                        pcSerial.printf("breaking\r\n");
+                        message[myCount] = '\0'; // null terminate... NECESSARY??
+                        break; 
+                    } 
+                    message[myCount] = currChar; // fill in char array
+                    myCount++;  // global var update 
+                    //pcSerial.printf("\n%d\n", myCount);               
+                } 
+                
+                //readline(message, myCount); // edited by JONATHAN (140 -> 'myCount'). THEN COMPLETELY COMMENTED OUT, b/c I write directly to message[] above
+                //pcSerial.printf("\n%d\n", myCount); // added by jonathan
+                
+                
+                pcSerial.printf("%s\r\n", message);
+                if (!fona.sendSMS(sendto, message)) {
+                    pcSerial.printf("Failed\r\n");
+                } else {
+                    pcSerial.printf("Sent!\r\n");
+                }
+                
+                break;
+            }
+            
+            /*** Time ***/
+            
+            case 'y': {
+                // enable network time sync
+                if (!fona.enableNetworkTimeSync(true))
+                    pcSerial.printf("Failed to enable\r\n");
+                break;
+            }
+            
+            case 'Y': {
+                // enable NTP time sync
+                if (!fona.enableNTPTimeSync(true, "pool.ntp.org"))
+                    pcSerial.printf("Failed to enable\r\n");
+                break;
+            }
+            
+            case 't': {
+                // read the time
+                char buffer[23];
+                
+                fona.getTime(buffer, 23);  // make sure replybuffer is at least 23 bytes!
+                pcSerial.printf("Time = %s\r\n", buffer);
+                break;
+            }
+            
+            /*********************************** GPS (SIM808 only) */
+            
+            case 'o': {
+                // turn GPS off
+                if (!fona.enableGPS(false))  
+                    pcSerial.printf("Failed to turn off\r\n");
+                break;
+            }
+            case 'O': {
+                // turn GPS on
+                if (!fona.enableGPS(true))  
+                    pcSerial.printf("Failed to turn on\r\n");
+                break;
+            }
+            case 'x': {
+                int8_t stat;
+                // check GPS fix
+                stat = fona.GPSstatus();
+                if (stat < 0)  
+                    pcSerial.printf("Failed to query\r\n");
+                if (stat == 0) pcSerial.printf("GPS off\r\n");
+                if (stat == 1) pcSerial.printf("No fix\r\n");
+                if (stat == 2) pcSerial.printf("2D fix\r\n");
+                if (stat == 3) pcSerial.printf("3D fix\r\n");
+                break;
+            }
+            
+            case 'L': {
+                // check for GPS location
+                char gpsdata[80];
+                fona.getGPS(0, gpsdata, 80);
+                pcSerial.printf("Reply in format: mode,longitude,latitude,altitude,utctime(yyyymmddHHMMSS),ttff,satellites,speed,course\r\n");
+                pcSerial.printf("%s\r\n", gpsdata);
+                
+                break;
+            }
+            
+            case 'E': {
+                flushSerial();
+                pcSerial.printf("GPS NMEA output sentences (0 = off, 34 = RMC+GGA, 255 = all)\r\n");
+                uint8_t nmeaout = readnumber();
+                
+                // turn on NMEA output
+                fona.enableGPSNMEA(nmeaout);
+                
+                break;
+            }
+            
+            /*********************************** GPRS */
+            
+            case 'g': {
+                // turn GPRS off
+                if (!fona.enableGPRS(false))  
+                    pcSerial.printf("Failed to turn off\r\n");
+                break;
+            }
+            case 'G': {
+                // turn GPRS on
+                if (!fona.enableGPRS(true))  
+                    pcSerial.printf("Failed to turn on\r\n");
+                break;
+            }
+            case 'l': {
+                // check for GSMLOC (requires GPRS)
+                uint16_t returncode;
+                
+                if (!fona.getGSMLoc(&returncode, replybuffer, 250))
+                    pcSerial.printf("Failed!\r\n");
+                if (returncode == 0) {
+                    pcSerial.printf("%s\r\n", replybuffer);
+                } else {
+                    pcSerial.printf("Fail code #%d\r\n", returncode);
+                }
+                
+                break;
+            }
+            case 'w': {
+                // read website URL
+                uint16_t statuscode;
+                int16_t length;
+                char url[80];
+                
+                flushSerial();
+                pcSerial.printf("NOTE: in beta! Use small webpages to read!\r\n");
+                pcSerial.printf("URL to read (e.g. www.adafruit.com/testwifi/index.html):\r\n");
+                pcSerial.printf("http://"); readline(url, 79);
+                pcSerial.printf("%s\r\n", url);
+                
+                pcSerial.printf("****\r\n");
+                if (!fona.HTTP_GET_start(url, &statuscode, (uint16_t *)&length)) {
+                    pcSerial.printf("Failed!\r\n");
+                    break;
+                }
+                while (length > 0) {
+                    while (fona.readable()) {
+                        char c = fona.getc();
+                        pcSerial.putc(c);
+                        length--;
+                        if (! length) break;
+                    }
+                }
+                pcSerial.printf("\r\n****\r\n");
+                fona.HTTP_GET_end();
+                break;
+            }
+            
+            case 'W': {
+                // Post data to website
+                uint16_t statuscode;
+                int16_t length;
+                char url[80];
+                char data[80];
+                
+                flushSerial();
+                pcSerial.printf("NOTE: in beta! Use simple websites to post!\r\n");
+                pcSerial.printf("URL to post (e.g. httpbin.org/post):\r\n");
+                pcSerial.printf("http://"); readline(url, 79);
+                pcSerial.printf("%s\r\n", url);
+                pcSerial.printf("Data to post (e.g. \"foo\" or \"{\"simple\":\"json\"}\"):\r\n");
+                readline(data, 79);
+                pcSerial.printf("%s\r\n", data);
+                
+                pcSerial.printf("****\r\n");
+                if (!fona.HTTP_POST_start(url, "text/plain", (uint8_t *) data, strlen(data), &statuscode, (uint16_t *)&length)) {
+                    pcSerial.printf("Failed!\r\n");
+                    break;
+                }
+                while (length > 0) {
+                    while (fona.readable()) {
+                        char c = fona.getc();
+                        pcSerial.putc(c);
+                        length--;
+                        if (! length) break;
+                    }
+                }
+                pcSerial.printf("\r\n****\r\n");
+                fona.HTTP_POST_end();
+                break;
+            }
+            /*****************************************/
+            
+            case 'S': {
+                pcSerial.printf("Creating SERIAL TUBE\r\n");
+                while (1) {
+                    while (pcSerial.readable()) {
+                        wait_ms(1);
+                        fona.putc(pcSerial.getc());
+                    }
+                    if (fona.readable()) {
+                        pcSerial.putc(fona.getc());
+                    }
+                }
+            }
+            
+            default: {
+                pcSerial.printf("Unknown command\r\n");
+                printMenu();
+                break;
+            }
+        } // end switch
+        // flush input
+        flushSerial();
+        while (fona.readable()) {
+            pcSerial.putc(fona.getc());
+        } 
+    } // end while()
+} // end main
+
+void printMenu(void) {
+    pcSerial.printf("-------------------------------------\r\n");
+    pcSerial.printf("[?] Print this menu\r\n");
+    pcSerial.printf("[a] read the ADC (2.8V max)\r\n");
+    pcSerial.printf("[b] read the Battery V and %% charged\r\n");
+    pcSerial.printf("[C] read the SIM CCID\r\n");
+    pcSerial.printf("[U] Unlock SIM with PIN code\r\n");
+    pcSerial.printf("[i] read RSSI\r\n");
+    pcSerial.printf("[n] get Network status\r\n");
+    pcSerial.printf("[v] set audio Volume\r\n");
+    pcSerial.printf("[V] get Volume\r\n");
+    pcSerial.printf("[H] set Headphone audio\r\n");
+    pcSerial.printf("[e] set External audio\r\n");
+    pcSerial.printf("[T] play audio Tone\r\n");
+    pcSerial.printf("[P] PWM/Buzzer out\r\n");
+    
+    // FM (SIM800 only)
+    pcSerial.printf("[f] tune FM radio\r\n");
+    pcSerial.printf("[F] turn off FM\r\n");
+    pcSerial.printf("[m] set FM volume\r\n");
+    pcSerial.printf("[M] get FM volume\r\n");
+    pcSerial.printf("[q] get FM station signal level\r\n");
+    
+    // Phone
+    pcSerial.printf("[c] make phone Call\r\n");
+    pcSerial.printf("[h] Hang up phone\r\n");
+    pcSerial.printf("[p] Pick up phone\r\n");
+    
+    // SMS
+    pcSerial.printf("[N] Number of SMSs\r\n");
+    pcSerial.printf("[r] Read SMS #\r\n");
+    pcSerial.printf("[R] Read All SMS\r\n");
+    pcSerial.printf("[d] Delete SMS #\r\n");
+    pcSerial.printf("[s] Send SMS\r\n");
+    
+    // Time
+    pcSerial.printf("[y] Enable network time sync\r\n");   
+    pcSerial.printf("[Y] Enable NTP time sync (GPRS)\r\n");   
+    pcSerial.printf("[t] Get network time\r\n");
+    
+    // GPRS
+    pcSerial.printf("[G] Enable GPRS\r\n");
+    pcSerial.printf("[g] Disable GPRS\r\n");
+    pcSerial.printf("[l] Query GSMLOC (GPRS)\r\n");
+    pcSerial.printf("[w] Read webpage (GPRS)\r\n");
+    pcSerial.printf("[W] Post to website (GPRS)\r\n");
+    
+    // GPS
+    pcSerial.printf("[O] Turn GPS on (SIM808)\r\n");
+    pcSerial.printf("[o] Turn GPS off (SIM808)\r\n");
+    pcSerial.printf("[x] GPS fix status (SIM808)\r\n");
+    pcSerial.printf("[L] Query GPS location (SIM808)\r\n");
+    pcSerial.printf("[E] Raw NMEA out (SIM808)\r\n");
+    
+    pcSerial.printf("[S] create Serial passthru tunnel\r\n");
+    pcSerial.printf("-------------------------------------\r\n");
+    pcSerial.printf("\r\n");
+}
+
+void flushSerial() {
+    while (pcSerial.readable()) 
+        pcSerial.getc();
+}
+
+char readBlocking() {
+    while (!pcSerial.readable());
+    return pcSerial.getc();
+}
+
+uint16_t readnumber() {
+    uint16_t x = 0;
+    char c;
+    while (! isdigit(c = readBlocking())) {
+        //pcSerial.putc(c);
+    }
+    pcSerial.putc(c);
+    x = c - '0';
+    while (isdigit(c = readBlocking())) {
+        pcSerial.putc(c);
+        x *= 10;
+        x += c - '0';
+    }
+    return x;
+}
+  
+uint8_t readline(char *buff, uint8_t maxbuff, uint16_t timeout) {
+    uint16_t buffidx = 0;
+    bool timeoutvalid = true;
+    if (timeout == 0) timeoutvalid = false;
+    
+    while (true) {
+        if (buffidx > maxbuff) {
+            //pcSerial.printf("SPACE\r\n");
+            break;
+        }
+        
+        while(pcSerial.readable()) {
+            char c =  pcSerial.getc();
+            
+            //pcSerial.printf("%02x#%c\r\n", c, c);
+            
+            if (c == '\r') 
+                    continue;
+                
+            
+            if (c == '\r') { /////////////////////////////////////////// CHANGED BY JONATHAN: (0XA -> '\r')
+                if (buffidx == 0)   // the first 0x0A is ignored
+                    continue;
+                
+                timeout = 0;         // the second 0x0A is the end of the line
+                timeoutvalid = true;
+                break;
+            }
+            buff[buffidx] = c;
+            buffidx++;
+            
+            myCount++; ///////////////////////// ADED BY JONATHAN
+        }
+        
+        if (timeoutvalid && timeout == 0) {
+            //pcSerial.printf("TIMEOUT\r\n");
+            break;
+        }
+        wait_ms(1);
+    }
+    buff[buffidx] = 0;  // null term
+    return buffidx;
+}
+
+long map(long x, long in_min, long in_max, long out_min, long out_max)
+{
+    return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
+}
+
+
+///// ------------------- Below is ESP8266 WiFi Chip Config + Setup COde -------------------- ////
+// Sets new ESP8266 baurate, change the esp.baud(xxxxx) to match your new setting once this has been executed
+void ESPsetbaudrate()
+{
+    strcpy(snd, "AT+CIOBAUD=115200\r\n");   // change the numeric value to the required baudrate
+    SendCMD();
+}
+ 
+ void ESPstart() {
+    wait(3);
+    pcSerial.printf("\f---------- Starting ESP Config ----------\r\n\n");
+        strcpy(snd,".\r\n.\r\n");
+    SendCMD();     
+}
+ 
+void ESPreset() {
+    wait(1);
+    pcSerial.printf("---------- Reset & get Firmware ----------\r\n");
+    strcpy(snd,"node.restart()\r\n");
+    SendCMD();
+    timeout=5;
+    getreply();
+    pcSerial.printf(buf); 
+}
+
+void ESPversion() {
+    wait(1); 
+    pcSerial.printf("\n---------- Get Version ----------\r\n");
+    strcpy(snd,"print(node.info())\r\n");
+    SendCMD();
+    timeout=4;
+    getreply();
+    pcSerial.printf(buf);    
+}
+
+void ESPmode() {
+     wait(2);
+ 
+    // set CWMODE to 1=Station,2=AP,3=BOTH, default mode 1 (Station)
+    pcSerial.printf("\n---------- Setting Mode ----------\r\n");
+    strcpy(snd, "wifi.setmode(wifi.STATION)\r\n");
+    SendCMD();
+    timeout=4;
+    getreply();
+    pcSerial.printf(buf);   
+}
+
+void ESPconnect() {
+    pcSerial.printf("\n---------- Connecting to AP ----------\r\n");
+    pcSerial.printf("ssid = %s   pwd = %s\r\n",ssid,pwd);
+    strcpy(snd, "wifi.sta.config(\"");
+    strcat(snd, ssid);
+    strcat(snd, "\",\"");
+    strcat(snd, pwd);
+    strcat(snd, "\")\r\n");
+    SendCMD();
+    timeout=10;
+    getreply();
+    pcSerial.printf(buf);
+    wait(2);
+}
+
+void ESPip() {
+     pcSerial.printf("\n---------- Get chip's IP ----------\r\n");
+    
+    strcpy(snd, "print(wifi.sta.getip())\r\n");
+    SendCMD(); // get device IP
+    timeout=3;
+    getreply();
+    pcSerial.printf(buf);
+ 
+    wait(1);
+}
+
+void ESPstatus() {
+    pcSerial.printf("\n---------- Get Connection Status ----------\r\n");
+    strcpy(snd, "print(wifi.sta.status())\r\n");
+    SendCMD();
+    timeout=5;
+    getreply();
+    pcSerial.printf(buf);
+ 
+    pcSerial.printf("\n\n\n  If you get a valid (non zero) IP, ESP8266 has been set up.\r\n");
+    pcSerial.printf("  Run this if you want to reconfig the ESP8266 at any time.\r\n");
+    pcSerial.printf("  It saves the SSID and password settings internally\r\n");
+    wait(5);
+}
+ 
+//  +++++++++++++++++++++++++++++++++ This is for ESP8266 config only, run this once to set up the ESP8266 +++++++++++++++
+void ESPconfig()
+{
+    ESPstart();
+    ESPreset();
+    ESPversion();
+    ESPmode();
+    ESPconnect();
+    ESPip();
+    ESPstatus();
+    
+    /*
+    pcSerial.printf("\n---------- Connecting to website -----------\r\n");
+    
+    strcpy(snd, "conn=net.createConnection(net.TCP, 0)\r\n");
+    SendCMD();
+    wait(1);
+    
+        strcpy(snd, "conn:on(\"receive\",function(conn,payload)\r\n");
+        SendCMD();
+        wait(1);
+    
+    strcpy(snd, "end)\r\n");
+    SendCMD();
+    wait(1);
+    
+    strcpy(snd, "conn:connect(80,\"https://sites.google.com/site/4180echo/home\")");
+    SendCMD();
+    wait(1);
+    
+    strcpy("conn:on(\"connection\", function(conn, payload)");
+    SendCMD();
+    wait(1);
+    */
+    
+    
+ 
+   
+        
+   pcSerial.printf("\n---------- Setting up http server ----------\r\n");
+    strcpy(snd, "srv=net.createServer(net.TCP)\r\n");
+        SendCMD();
+        wait(1);
+        strcpy(snd, "srv:listen(80,function(conn)\r\n");
+        SendCMD();
+        wait(1);
+        strcpy(snd, "conn:on(\"receive\",function(conn,payload)\r\n");
+        SendCMD();
+        wait(1);
+        strcpy(snd, "print(payload)\r\n");
+        SendCMD();
+        wait(1);
+        
+        strcpy(snd, "conn:send(\"<!DOCTYPE html>\")\r\n");
+        SendCMD();
+        wait(1);
+        
+        strcpy(snd, "conn:send(\"<html>\")\r\n");
+        SendCMD();
+        wait(1);
+        
+        strcpy(snd, "conn:send(\"<h1> Hi Jonathan!</h1>\")\r\n");
+        SendCMD();
+        wait(1);
+        
+        strcpy(snd, "conn:send(\"<h2> test</h2>\")\r\n");
+        SendCMD();
+        wait(1);
+        
+        strcpy(snd, "conn:send(\"</html>\")\r\n");
+        SendCMD();
+        wait(1);
+        
+        strcpy(snd, "end)\r\n");
+        SendCMD();
+        wait(1);
+        
+        strcpy(snd, "conn:on(\"sent\",function(conn) conn:close() end)\r\n");
+        SendCMD();
+        wait(1);
+        strcpy(snd, "end)\r\n");
+        SendCMD();
+        wait(1);
+        timeout=17;
+        getreply();
+        pcSerial.printf(buf);
+        pcSerial.printf("\r\nDONE");
+}
+ 
+void SendCMD()
+{
+    esp.printf("%s", snd);
+}
+ 
+void getreply()
+{
+    memset(buf, '\0', sizeof(buf));
+    t.start();
+    ended=0;
+    count=0;
+    while(!ended) {
+        if(esp.readable()) {
+            buf[count] = esp.getc();
+            count++;
+        }
+        if(t.read() > timeout) {
+            ended = 1;
+            t.stop();
+            t.reset();
+        }
+    }
+}
\ No newline at end of file