demo1
Dependencies: SHT30-DIS-B WakeUp mbed
Fork of M1DK_Skywire_Demo by
main.cpp
00001 /* main.cpp */ 00002 /* Copyright (C) 2017 nimbelink.com, MIT License 00003 * 00004 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 00005 * and associated documentation files (the "Software"), to deal in the Software without restriction, 00006 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 00007 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 00008 * furnished to do so, subject to the following conditions: 00009 * 00010 * The above copyright notice and this permission notice shall be included in all copies or 00011 * substantial portions of the Software. 00012 * 00013 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 00014 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00015 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 00016 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00017 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00018 */ 00019 00020 /* 00021 * DESCRIPTION 00022 * This code sends sensor data to Dweet.io on the NL-M1DK. 00023 */ 00024 00025 /* 00026 * INSTRUCTIONS FOR USING THIS CODE 00027 * 1. Change the "DeviceID" to a unique identifier for your Nucleo board. One recommendation 00028 * would be to use the MEID/IMEI of your Skywire Modem. 00029 */ 00030 00031 #include "mbed.h" // mbed Library 00032 #include "pinmap.h" // pinmap needed for hardware flow control 00033 00034 #include "SHT30DISB.h" 00035 00036 #include "WakeUp.h" 00037 00038 #include "LowPowerTimeout.h" 00039 #include "sleep_api.h" 00040 #include "cmsis.h" 00041 00042 // --CHANGE THIS FOR YOUR SETUP-- 00043 #define DeviceID "CP Prototype001" //DweetIO unique IDA 00044 00045 // --CHANGE THIS FOR YOUR SETUP (IF APPLICABLE)-- 00046 const char *APN = "NIMBLINK.GW12.VZWENTP"; 00047 00048 DigitalOut myled(LED1); // Main LED 00049 DigitalOut skywire_rts(PB_5); // Skywire Send 00050 DigitalOut green_LED(D7); // Green LED 00051 DigitalOut red_LED(D10); // Red LED 00052 00053 DigitalOut nRESET(PB_4); // Skywire Reset line 00054 00055 AnalogIn photo_trans(A3); // Photo Transistor 00056 DigitalOut photo_trans_nEN(D11); // Photo Transistor Enable 00057 DigitalIn button1(PA_5); // Button 1 00058 00059 Serial skywire(PA_9,PA_10); // Serial comms to Skywire 00060 Serial debug_pc(USBTX, USBRX); // USB connection to PC 00061 00062 I2C i2c(PB_9,PB_8); // Setup I2C bus for sensors 00063 00064 // SHT30 Sensor Setup 00065 SHT30DISB SHT30(i2c); 00066 00067 // variable to switch state 00068 bool sw1; 00069 00070 // char array for reading from Skywire 00071 char str[255]; 00072 00073 // Variables for checking CSQ 00074 char csq[3]="99"; 00075 int csq_val = 99; 00076 00077 // Variables for UART comms 00078 volatile int rx_in=0; 00079 volatile int rx_out=0; 00080 const int buffer_size = 600; 00081 char rx_buffer[buffer_size+1]; 00082 char rx_line[buffer_size]; 00083 00084 // Interrupt for the Skywire 00085 void Skywire_Rx_interrupt() 00086 { 00087 // Loop just in case more than one character is in UART's receive FIFO buffer 00088 // Stop if buffer full 00089 while ((skywire.readable()) && (((rx_in + 1) % buffer_size) != rx_out)) { 00090 rx_buffer[rx_in] = skywire.getc(); 00091 rx_in = (rx_in + 1) % buffer_size; 00092 } 00093 return; 00094 } 00095 00096 // Read line from the UART 00097 void read_line() 00098 { 00099 int i; 00100 i = 0; 00101 // Start Critical Section - don't interrupt while changing global buffer variables 00102 __disable_irq(); 00103 // Loop reading rx buffer characters until end of line character 00104 while ((i==0) || (rx_line[i-1] != '\n')) { 00105 // Wait if buffer empty 00106 if (rx_in == rx_out) { 00107 // End Critical Section - need to allow rx interrupt to get new characters for buffer 00108 __enable_irq(); 00109 while (rx_in == rx_out) { 00110 } 00111 // Start Critical Section - don't interrupt while changing global buffer variables 00112 __disable_irq(); 00113 } 00114 rx_line[i] = rx_buffer[rx_out]; 00115 i++; 00116 rx_out = (rx_out + 1) % buffer_size; 00117 } 00118 // End Critical Section 00119 __enable_irq(); 00120 rx_line[i-1] = 0; 00121 return; 00122 } 00123 00124 // Wait for specific response 00125 int WaitForResponse(const char *response) 00126 { 00127 debug_pc.printf("Command sent. Waiting for: %s\r\n", response); 00128 do { 00129 read_line(); 00130 // If we get ERROR, return with 1 00131 if (strncmp(rx_line, "ERROR", strlen("ERROR")) == 0) { 00132 debug_pc.printf("ERROR\r\n"); 00133 return 1; 00134 } 00135 debug_pc.printf("Waiting for: %s, Recieved: %s\r\n", response, rx_line); 00136 } while (strncmp(rx_line, response, strlen(response))); 00137 return 0; 00138 } 00139 00140 // Send AT+CSQ until we get a good signal 00141 int GetCSQResponse() 00142 { 00143 do { 00144 skywire.printf("AT+CSQ\r"); 00145 WaitForResponse("OK"); 00146 csq[0]=rx_line[6]; 00147 csq[1]=rx_line[7]; 00148 csq_val=atoi(csq); 00149 } while (!strncmp(rx_line, "CSQ: 99,", 8)); 00150 return csq_val; 00151 } 00152 00153 00154 00155 00156 //LowPowerTimeout timeout; 00157 00158 00159 00160 00161 00162 00163 00164 int main() 00165 { 00166 00167 00168 debug_pc.baud(115200); 00169 /* sleep mode testing. 00170 00171 red_LED = 1; 00172 float temp; 00173 float humi; 00174 float photo; 00175 char *CSQValue; 00176 00177 // Setup serial comms with Skywire and PC 00178 debug_pc.baud(115200); 00179 skywire.baud(115200); 00180 skywire_rts = 0; 00181 debug_pc.printf("SystemCoreClock = %d Hz\r\n", SystemCoreClock); 00182 debug_pc.printf("Attaching interrupt...\r\n"); 00183 skywire.attach(&Skywire_Rx_interrupt, Serial::RxIrq); 00184 debug_pc.printf("Interrupt attached...\r\n"); 00185 00186 // Reset the Skywire to get AT commands, and recover from any previous state 00187 debug_pc.printf("Resetting Skywire...\r\n"); 00188 nRESET = 1; 00189 wait_ms(100); 00190 nRESET = 0; 00191 wait(6); 00192 debug_pc.printf("Resetting baud...\r\n"); 00193 skywire.printf("\rAT+IPR=115200\r"); 00194 wait_ms(100); 00195 skywire.baud(115200); 00196 wait_ms(100); 00197 debug_pc.printf("Baud reset!\r\n"); 00198 myled=0; 00199 debug_pc.printf("Starting Demo...\r\n"); 00200 debug_pc.printf("Waiting for Skywire to Boot...\r\n"); 00201 green_LED = !green_LED; 00202 myled=1; 00203 //wait(120); 00204 00205 00206 // Make them comments for debugging: 00207 debug_pc.printf("Sending AT\r\n"); 00208 skywire.printf("\rAT\r"); 00209 WaitForResponse("OK"); 00210 //WaitForResponse("+CEREG: 1"); 00211 debug_pc.printf("Sending AT+CFUN=1\r\n"); 00212 skywire.printf("AT+CFUN=1\r"); 00213 WaitForResponse("OK"); 00214 //wait_ms(2000); 00215 00216 // Check CSQ 00217 debug_pc.printf("Getting CSQ...\r\n"); 00218 GetCSQResponse(); 00219 green_LED = !green_LED; 00220 while(csq_val==99 || csq_val==0) 00221 { 00222 00223 debug_pc.printf("CSQ Value: %i\r\n",csq_val); 00224 debug_pc.printf("No network sginal detected. \r\n"); 00225 debug_pc.printf("Waiting for device to connect to the network. \r\n"); 00226 debug_pc.printf("Please check antenna connections if network is not found after 30 seconds. \r\n"); 00227 wait(1); 00228 //add elapsed time 00229 debug_pc.printf("Checking network connectrion. \r\n"); 00230 GetCSQResponse(); 00231 red_LED = !red_LED; 00232 } 00233 00234 debug_pc.printf("Network detected. Checking authorization...\r\n"); 00235 skywire.printf("AT+CEREG?\r"); 00236 WaitForResponse("OK"); 00237 00238 //debug_pc.printf("Connecting to Network...\r\n"); 00239 debug_pc.printf("Setting up socket...\r\n"); 00240 skywire.printf("AT+SQNSCFG=3,3,300,90,600,50\r"); 00241 WaitForResponse("OK"); 00242 00243 red_LED = 1; 00244 green_LED = 0; 00245 00246 00247 debug_pc.printf("Check Version...\r\n"); 00248 skywire.printf("ATI1\r"); 00249 WaitForResponse("OK"); 00250 00251 00252 00253 ////////////////Added on April 18, 2018 00254 00255 SPI spi(PB_15, PB_14,PB_13); // mosi, miso, sclk 00256 DigitalOut cs(PB_12); 00257 00258 // Select the device by setting chip select low 00259 cs = 0; 00260 00261 // Send 0x8f, the command to read the WHOAMI register 00262 //spi.write(0x80); 00263 00264 // Send a dummy byte to receive the contents of the WHOAMI register 00265 long whoami = spi.write(0x80); 00266 00267 debug_pc.printf("Read SPI information...\r\n"); 00268 debug_pc.printf("SPI Value: %i\r\n",whoami ); 00269 long whoami1 = spi.write(0x00); 00270 debug_pc.printf("SPI Value: %i\r\n",whoami1 ); 00271 long whoami2 = spi.write(0x00); 00272 debug_pc.printf("SPI Value: %i\r\n",whoami2 ); 00273 long whoami3 = spi.write(0x00); 00274 debug_pc.printf("SPI Value: %i\r\n",whoami3 ); 00275 long whoami4 = spi.write(0x00); 00276 debug_pc.printf("SPI Value: %i\r\n",whoami4 ); 00277 00278 // Deselect the device 00279 cs = 1; 00280 //// Added on April 18, 2018 00281 00282 wait_ms(100); 00283 00284 00285 00286 while(1) { 00287 // Green on to indicate code position 00288 // Start of loop. Either entered loop for the first time or just sent to dweet.io 00289 red_LED = 0; 00290 green_LED = 1; 00291 //* Comments for debugging 00292 int retries = 0; 00293 while (retries < 5) { 00294 //skywire.printf("AT+SQNSD=3,0,80,\"dweet.io\"\r"); 00295 skywire.printf("AT+SQNSD=3,0,59001,\"12.193.202.234\"\r"); 00296 //skywire.printf("AT+SQNSD=3,0,60001,\"<12.25.92.72>\"\r"); 00297 if (WaitForResponse("CONNECT") == 1) { 00298 retries += 1; 00299 wait(1); 00300 } else 00301 break; 00302 } 00303 00304 //*/ 00305 00306 00307 00308 /* sleep mode testing. 00309 00310 00311 //get temp and humi 00312 temp=SHT30.cTemp(); 00313 humi=SHT30.humidity(); 00314 photo_trans_nEN=0; 00315 photo=photo_trans*200; 00316 00317 00318 00319 //humi=csq_val; 00320 wait(1); 00321 00322 00323 // SPI Reading Procedure Added on April 18 2018 00324 wait(2); 00325 // Select the device by setting chip select low 00326 cs = 0; 00327 00328 // Send a dummy byte to receive the contents of the WHOAMI register 00329 char whoami = spi.write(0x00); 00330 00331 debug_pc.printf("Read SPI information...\r\n"); 00332 debug_pc.printf("SPI Value: %i\r\n",whoami ); 00333 00334 //long whoami1 = spi.write(0x00); 00335 char whoami1 = spi.write(0x00); 00336 debug_pc.printf("SPI Value: %i\r\n",whoami1 ); 00337 char whoami2 = spi.write(0x00); 00338 debug_pc.printf("SPI Value: %i\r\n",whoami2 ); 00339 char whoami3 = spi.write(0x00); 00340 debug_pc.printf("SPI Value: %i\r\n",whoami3 ); 00341 char whoami4 = spi.write(0x00); 00342 debug_pc.printf("SPI Value: %i\r\n",whoami4 ); 00343 00344 photo=whoami1/14.5; 00345 00346 unsigned char mask=1<<5; 00347 int sign=mask&whoami; 00348 00349 long tempLong1=0; 00350 long tempLong2=0; 00351 long tempLong3=0; 00352 long tempLong4=0; 00353 00354 char tempChar; 00355 long convertedResults=0;// converted results 00356 00357 float output; 00358 00359 if (sign==0) // negative readings: 00360 { 00361 00362 tempChar=whoami; 00363 tempChar=tempChar<<3; 00364 tempChar=tempChar>>3; 00365 debug_pc.printf("test1 Value is: %i\r\n", tempChar ); 00366 00367 tempLong1=tempLong1|tempChar; 00368 debug_pc.printf("test2 Value is: %i\r\n", tempLong1 ); 00369 00370 tempLong1=tempLong1<<24; 00371 debug_pc.printf("test3 Value is: %i\r\n", tempLong1 ); 00372 00373 00374 tempChar=whoami1; 00375 tempLong2=tempLong2|tempChar; 00376 tempLong2=tempLong2<<16; 00377 00378 tempChar=whoami2; 00379 tempLong3=tempLong3|tempChar; 00380 tempLong3=tempLong3<<8; 00381 00382 tempChar=whoami3; 00383 tempChar=tempChar>>5; 00384 tempChar=tempChar<<5; 00385 tempLong4=tempLong4&tempChar; 00386 00387 convertedResults=tempLong1|tempLong2|tempLong3|tempLong4; 00388 convertedResults=~ convertedResults; 00389 convertedResults=convertedResults<<3; 00390 convertedResults=convertedResults>>3; 00391 convertedResults= convertedResults>>5; 00392 00393 00394 00395 debug_pc.printf("Long1 Value is: %i\r\n", tempLong1 ); 00396 debug_pc.printf("Long2 Value is: %i\r\n", tempLong2 ); 00397 debug_pc.printf("Long3 Value is: %i\r\n", tempLong3 ); 00398 debug_pc.printf("Long4 Value is: %i\r\n", tempLong4 ); 00399 00400 output = -convertedResults*0.0000120; 00401 00402 00403 00404 } 00405 else // positive readings: 00406 { 00407 tempChar=whoami; 00408 tempChar=tempChar<<3; 00409 tempChar=tempChar>>3; 00410 debug_pc.printf("test1 Value is: %i\r\n", tempChar ); 00411 00412 tempLong1=tempLong1|tempChar; 00413 debug_pc.printf("test2 Value is: %i\r\n", tempLong1 ); 00414 00415 tempLong1=tempLong1<<24; 00416 debug_pc.printf("test3 Value is: %i\r\n", tempLong1 ); 00417 00418 00419 tempChar=whoami1; 00420 tempLong2=tempLong2|tempChar; 00421 tempLong2=tempLong2<<16; 00422 00423 tempChar=whoami2; 00424 tempLong3=tempLong3|tempChar; 00425 tempLong3=tempLong3<<8; 00426 00427 tempChar=whoami3; 00428 tempChar=tempChar>>5; 00429 tempChar=tempChar<<5; 00430 tempLong4=tempLong4&tempChar; 00431 00432 convertedResults=tempLong1|tempLong2|tempLong3|tempLong4; 00433 convertedResults= convertedResults>>5; 00434 00435 00436 00437 debug_pc.printf("Long1 Value is: %i\r\n", tempLong1 ); 00438 debug_pc.printf("Long2 Value is: %i\r\n", tempLong2 ); 00439 debug_pc.printf("Long3 Value is: %i\r\n", tempLong3 ); 00440 debug_pc.printf("Long4 Value is: %i\r\n", tempLong4 ); 00441 output = convertedResults*0.0000120; 00442 } 00443 00444 00445 debug_pc.printf("Measured Value Sign is: %i\r\n",sign ); 00446 00447 00448 debug_pc.printf("Converted Value is: %i\r\n", convertedResults ); 00449 debug_pc.printf("Converted True Value is: %f\r\n", output ); 00450 00451 00452 // Deselect the device 00453 00454 00455 cs = 1; 00456 //// End of SPI Reading Procedure Added on April 18, 2018 00457 00458 00459 00460 00461 00462 wait(2); 00463 00464 00465 00466 // Check buttons for presses 00467 if (button1 == 0) 00468 sw1 = 1; 00469 else 00470 sw1 = 0; 00471 00472 // Green on to indicate code position: 00473 // Sensors updated, have not sent to dweet.io 00474 red_LED = 1; 00475 green_LED = 0; 00476 00477 // /* 00478 photo=output; 00479 float para1; 00480 float para2; 00481 float para3; 00482 float para4; 00483 float para5; 00484 float para6; 00485 float para7; 00486 float para8; 00487 float para9; 00488 float para10; 00489 float para11; 00490 para1=csq_val; 00491 para2=temp/1.4; 00492 para3=output; 00493 para4=humi; 00494 para5=0; 00495 para6=0; 00496 para7=0; 00497 para8=0; 00498 para9=0; 00499 para10=0; 00500 para11=0; 00501 if (retries != 5) { 00502 debug_pc.printf("Sending information...\r\n"); 00503 // Report the sensor data to dweet.io 00504 //skywire.printf("POST /dweet/for/%s?temp=%.3f&sw1=%d&photo=%.3f&CSQ=%.3f HTTP/1.0\r\n\r\n", DeviceID, temp, sw1, photo, humi); 00505 //skywire.printf("POST %s,%.3f,%.3f,%.3f,%.3f HTTP/1.0\r\n\r\n", DeviceID, temp, sw1, photo, humi); 00506 skywire.printf("%s,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f \r\n\r\n", DeviceID, para1, para2, para3, para4,para5,para6,para7,para8,para9,para10,para11); 00507 WaitForResponse("NO CARRIER"); 00508 } 00509 // } 00510 debug_pc.printf("Closing socket...\r\n"); 00511 skywire.printf("AT+SQNSH=3\r"); 00512 WaitForResponse("OK"); 00513 wait(2); 00514 skywire.printf("AT+CGATT=0\r"); 00515 WaitForResponse("OK"); 00516 wait(2); 00517 skywire.printf("AT+CGATT=1\r"); 00518 WaitForResponse("+CEREG: 1"); 00519 wait(2); 00520 red_LED = 0; 00521 green_LED = 1; 00522 00523 wait(3); 00524 skywire.printf("AT+CSQ\r"); 00525 WaitForResponse("OK"); 00526 csq[0]=rx_line[6]; 00527 csq[1]=rx_line[7]; 00528 csq_val=atoi(csq); 00529 wait(6); 00530 00531 // sleeping testing 00532 00533 00534 // after data sending: 00535 00536 */ //Sleep mode testing 00537 00538 00539 00540 00541 00542 00543 wait(5); 00544 00545 //The low-power oscillator can be quite inaccurate on some targets 00546 //this function calibrates it against the main clock 00547 WakeUp::calibrate(); 00548 00549 00550 while(1) { 00551 //Set LED to zero 00552 red_LED = 0; 00553 green_LED = 1; 00554 //Set wakeup time for 2 seconds 00555 WakeUp::set_ms(2000); 00556 00557 //Enter deepsleep, the program won't go beyond this point until it is woken up 00558 deepsleep(); 00559 00560 //Set LED for 1 second to one 00561 red_LED = 1; 00562 green_LED = 0; 00563 wait(1); 00564 } 00565 00566 00567 00568 00569 00570 00571 00572 // End of sleeping, get back to work 00573 00574 00575 00576 00577 00578 00579 00580 00581 }
Generated on Sat Jul 23 2022 02:42:35 by 1.7.2