Release 1.0 supporting the Kinetis Freedom K64F board and NimbeLink NL-AB-ST-NCL and NimbeLink NL-SWSK. Tested with NL-SW-1xRTT-V and NL-SW-EVDO-V modems.

Dependencies:   LIS3DH LM75B LPS331 mbed hts221

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* main.cpp */
00002 /* v1.1
00003  * Copyright (C) 2015 nimbelink.com, MIT License
00004  *
00005  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00006  * and associated documentation files (the "Software"), to deal in the Software without restriction,
00007  * including without limitation the rights to use, copy, modify, merge, publish, distribute,
00008  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
00009  * furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included in all copies or
00012  * substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00015  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00016  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00017  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00018  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00019  */
00020 
00021 #include "mbed.h"
00022 #include "LPS331.h"
00023 #include "LIS3DH.h"
00024 #include "LM75B.h"
00025 #include "hts221.h"
00026 
00027 //  #define DeviceID "A100003E4226B3"  //Freeboard DweetIO unique ID
00028 // dynamically assigning this based on modem's unique identifier below
00029 char DeviceID[15];
00030 
00031 
00032 DigitalOut skywire_en(PTD3);    //K64 FRDM
00033 DigitalOut skywire_rts(PTD2);    //K64 FRDM
00034 DigitalOut skywire_dtr(PTD0);    //K64 FRDM
00035 
00036 DigitalOut led_red(LED_RED);
00037 DigitalOut led_green(LED_GREEN);
00038 DigitalIn sw2(SW2);
00039 DigitalIn sw3(SW3);
00040 Serial pc(USBTX, USBRX);
00041 Serial skywire(PTC17, PTC16);    //K64 FRDM
00042 
00043 I2C i2c(PTE25,PTE24);
00044 char msg[3];
00045 
00046 LPS331 pressure(i2c);
00047 LM75B LM75_temp(PTE25,PTE24);
00048 LIS3DH accel(i2c, LIS3DH_V_CHIP_ADDR, LIS3DH_DR_NR_LP_100HZ, LIS3DH_FS_2G);
00049 HTS221 humidity(PTE25, PTE24);
00050 
00051 char str[255];
00052 
00053 float latitude;
00054 float longitude;
00055 int number;
00056 
00057 volatile int rx_in=0;
00058 volatile int rx_out=0;
00059 const int buffer_size = 255;
00060 char rx_buffer[buffer_size+1];
00061 
00062 char rx_line[buffer_size];
00063 
00064 void Skywire_Rx_interrupt() {
00065 // Loop just in case more than one character is in UART's receive FIFO buffer
00066 // Stop if buffer full
00067     while ((skywire.readable()) && (((rx_in + 1) % buffer_size) != rx_out)) {
00068         rx_buffer[rx_in] = skywire.getc();
00069         rx_in = (rx_in + 1) % buffer_size;
00070     }
00071     return;
00072 }
00073 
00074 void read_line() {
00075     int i;
00076     i = 0;
00077 // Start Critical Section - don't interrupt while changing global buffer variables
00078     __disable_irq();
00079 // Loop reading rx buffer characters until end of line character
00080     while ((i==0) || (rx_line[i-1] != '\n')) {
00081 // Wait if buffer empty
00082         if (rx_in == rx_out) {
00083 // End Critical Section - need to allow rx interrupt to get new characters for buffer
00084             __enable_irq();
00085             while (rx_in == rx_out) {
00086               //pc.printf("..Read_line entered2.5\r\n");               
00087             }
00088 // Start Critical Section - don't interrupt while changing global buffer variables
00089             __disable_irq();
00090         }
00091         rx_line[i] = rx_buffer[rx_out];
00092         i++;
00093         rx_out = (rx_out + 1) % buffer_size;       
00094     }
00095 // End Critical Section
00096     __enable_irq();       
00097     rx_line[i-1] = 0;
00098     return;
00099 }
00100 int WaitForResponse(char* response, int num) {
00101     do {
00102         read_line();
00103         pc.printf("Waiting for: %s, Recieved: %s\r\n", response, rx_line);
00104     } while (strncmp(rx_line, response, num));
00105     return 0;
00106 }
00107 
00108 void check_sw2(void)
00109 {
00110     if (sw2 == 0) {
00111         pc.printf("SW2 button pressed. \r\n");
00112         // turn RED LED on to indicate provisioning process has started
00113         led_red = 0;
00114         led_green = 1;
00115         wait(1);
00116         // turn LEDs off
00117         led_red = 1;
00118         led_green = 1;      
00119         skywire.printf("ATD*22899;\r\n");
00120         WaitForResponse("#OTASP: 0", 9);
00121         pc.printf("Provisioning: #OTASP: 0 reached. \r\n");        
00122         // turn RED LED on
00123         led_red = 0;
00124         led_green = 1;       
00125         WaitForResponse("#OTASP: 1", 9);
00126         pc.printf("Provisioning: #OTASP: 1 reached. \r\n");          
00127         // turn both LEDs on
00128         led_red = 0;
00129         led_green = 0;            
00130         WaitForResponse("#OTASP: 2", 9);
00131         pc.printf("Provisioning: #OTASP: 2 reached. \r\n");         
00132         // turn GREEN LED on
00133         led_red = 1;
00134         led_green = 0;           
00135         WaitForResponse("NO CARRIER", 10);
00136         pc.printf("Provisioning successfully completed. \r\n");
00137  
00138         skywire.printf("AT#REBOOT");
00139         // wait 10 seconds 
00140         wait(10);       
00141         
00142         
00143     }
00144 }
00145 void blinkRG(int blinkduration)
00146 {
00147     // blinkduration is measured in seconds
00148     led_red=1;
00149     led_green=0;
00150     while (blinkduration-- >=1){
00151         led_red = led_green;
00152         led_green=!led_red;
00153         wait(1);
00154     }
00155     // leave the function with LEDs both off
00156     led_red=1;
00157     led_green=1;
00158     
00159 }
00160 void check_sw3(void)
00161 {
00162     if (sw3 == 0) {
00163         pc.printf("SW3 button pressed. \r\n");
00164         // this button press is used to pause transmissions (to save data if the device is on forever);
00165         pc.printf("Transmissions are paused. \r\n");
00166         // turn on RED LED
00167         led_green = 1;
00168         led_red = 0;
00169         // wait a few seconds for user to release the button.
00170         wait(3);
00171         
00172         // wait until user presses the button again to unpause
00173         while(sw3 ==1);
00174        pc.printf("Transmissions resumed. \r\n");        
00175          // turn LEDs off
00176         led_green = 1;
00177         led_red = 1;       
00178     }
00179 }
00180 
00181 int main() {
00182     
00183     led_red = 1;
00184     led_green = 1;
00185     float axis[3];
00186     float press;
00187     float temp;
00188     float humi;
00189     float dummy_temp;  
00190     
00191     // turn on Skywire modem
00192     skywire_en = 1;
00193     
00194     // setup skywire UART
00195     skywire_rts=0;
00196     skywire_dtr=0;   
00197     skywire.baud(115200); 
00198     skywire.format(8, Serial::None, 1); 
00199       
00200     // setup Skywire UART interrupt
00201     skywire.attach(&Skywire_Rx_interrupt, skywire.RxIrq);
00202    
00203     pc.baud(115200);
00204     pc.printf("Hello World from FRDM-K64F board.\r\n");
00205     pc.printf("Starting Demo...\r\n");
00206     pc.printf("Waiting for Skywire to Boot...\r\n");
00207   
00208     // wait 10 seconds for modem to boot up and connect to network
00209     blinkRG(10);
00210     pc.printf("Waiting complete...\r\n");    
00211 
00212     LM75_temp.open();
00213 
00214     //Turn off echo
00215     skywire.printf("ATE0\r\n");
00216     WaitForResponse("OK", 2);
00217     
00218     // check to see if switch 2 is pressed, if so, execute provisioning sequence
00219     check_sw2();
00220 
00221 
00222     // read out MEID number to use as unique identifier
00223     skywire.printf("AT#MEIDESN?\r\n");
00224     wait(2);
00225     read_line(); 
00226     read_line(); 
00227     sscanf(rx_line, "%*s %14s,", DeviceID);
00228 
00229     pc.printf("Device MEID: %s \r\n", DeviceID); 
00230 
00231     pc.printf("Connecting to Network...\r\n");
00232     // get IP address
00233     skywire.printf("AT#SGACT=1,1\r\n");
00234     WaitForResponse("#SGACT", 6);
00235     WaitForResponse("OK", 2);
00236     // use LEDs to indicate we're connected to the newtork
00237     led_red=0;
00238     led_green=0; 
00239        
00240     // connect to dweet.io
00241     skywire.printf("AT#HTTPCFG=1,\"dweet.io\",80,0\r\n");
00242     WaitForResponse("OK", 2);
00243     
00244     //get location approximation from cell tower information
00245     skywire.printf("AT#AGPSSND\r\n");
00246     WaitForResponse("#AGPSRING:", 10);
00247 
00248     //debug_pc.printf("Skywire says: %s\r\n", rx_line);
00249     sscanf(rx_line, "%s %d,%f,%f,", str, &number, &latitude, &longitude);
00250     //debug_pc.printf("Location: Latt:%f, Long:%f\r\n", latitude, longitude);
00251     wait(3);
00252     
00253     while(1) {
00254         temp = (float)LM75_temp;
00255         temp = temp *9 /5 + 32;
00256         pc.printf("Temp = %.3f\r\n", temp);
00257         press=(float)pressure.value() / 4096;
00258         pc.printf("Pressure = %.3f\r\n", press);
00259         humidity.ReadTempHumi(&dummy_temp, &humi);
00260         pc.printf("Humidity = %.3f\r\n", humi);
00261         accel.read_data(axis);
00262         pc.printf("Accel = %.3f, %.3f, %.3f\r\n", axis[0], axis[1], axis[2]);
00263         
00264         wait(0.25);
00265         // turn LED Green to indicate transmission
00266         led_red=1;
00267         led_green = 0;  
00268           
00269         //Report Sensor Data to dweet.io
00270         skywire.printf("AT#HTTPQRY=1,0,\"/dweet/for/%s?temp=%.3f&press=%.3f&humi=%.3f&X=%.3f&Y=%.3f&Z=%.3f&Latitude=%f&Longitude=%f\"\r\n", DeviceID, temp, press, humi, axis[0], axis[1], axis[2], latitude, longitude);
00271         WaitForResponse("#HTTPRING", 9);
00272         skywire.printf("AT#HTTPRCV=1\r\n");
00273         WaitForResponse("OK", 2);
00274         led_green = 1;  
00275         wait(2);
00276         // see if user has requested to pause transmissions
00277         check_sw3();
00278     }
00279 
00280 
00281     
00282     
00283 }