demo1

Dependencies:   SHT30-DIS-B WakeUp mbed

Fork of M1DK_Skywire_Demo by NimbeLink

Committer:
kylerodgers
Date:
Tue Oct 24 20:43:32 2017 +0000
Revision:
13:f827f384f0a1
Parent:
11:78a28ca6409f
Child:
14:44eb72d9995e
Demo for FTW Roadshow.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kholland 4:e7a79d3542e1 1 /* main.cpp */
kylerodgers 13:f827f384f0a1 2 /* Copyright (C) 2017 nimbelink.com, MIT License
kholland 4:e7a79d3542e1 3 *
kholland 4:e7a79d3542e1 4 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
kholland 4:e7a79d3542e1 5 * and associated documentation files (the "Software"), to deal in the Software without restriction,
kholland 4:e7a79d3542e1 6 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
kholland 4:e7a79d3542e1 7 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
kholland 4:e7a79d3542e1 8 * furnished to do so, subject to the following conditions:
kholland 4:e7a79d3542e1 9 *
kholland 4:e7a79d3542e1 10 * The above copyright notice and this permission notice shall be included in all copies or
kholland 4:e7a79d3542e1 11 * substantial portions of the Software.
kholland 4:e7a79d3542e1 12 *
kholland 4:e7a79d3542e1 13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
kholland 4:e7a79d3542e1 14 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
kholland 4:e7a79d3542e1 15 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
kholland 4:e7a79d3542e1 16 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
kholland 4:e7a79d3542e1 17 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
kholland 4:e7a79d3542e1 18 */
kholland 0:3095958bc639 19
kholland 10:5974a7b2cf38 20 /*
kholland 10:5974a7b2cf38 21 * DESCRIPTION
kylerodgers 13:f827f384f0a1 22 * This code sends sensor data to Dweet.io on the NL-M1DK.
kholland 10:5974a7b2cf38 23 */
kholland 10:5974a7b2cf38 24
kholland 10:5974a7b2cf38 25 /*
kholland 10:5974a7b2cf38 26 * INSTRUCTIONS FOR USING THIS CODE
kylerodgers 13:f827f384f0a1 27 * 1. Change the "DeviceID" to a unique identifier for your Nucleo board. One recommendation
kholland 10:5974a7b2cf38 28 * would be to use the MEID/IMEI of your Skywire Modem.
kholland 10:5974a7b2cf38 29 */
kholland 10:5974a7b2cf38 30
GregNash 11:78a28ca6409f 31 #include "mbed.h" // mbed Library
GregNash 11:78a28ca6409f 32 #include "pinmap.h" // pinmap needed for hardware flow control
GregNash 11:78a28ca6409f 33
GregNash 11:78a28ca6409f 34 #include "SHT30DISB.h"
kholland 0:3095958bc639 35
GregNash 11:78a28ca6409f 36 // --CHANGE THIS FOR YOUR SETUP--
kylerodgers 13:f827f384f0a1 37 #define DeviceID "<your Thingname here>" //DweetIO unique ID
GregNash 11:78a28ca6409f 38
GregNash 11:78a28ca6409f 39 // --CHANGE THIS FOR YOUR SETUP (IF APPLICABLE)--
kylerodgers 13:f827f384f0a1 40 const char *APN = "NIMBLINK.GW12.VZWENTP";
kholland 0:3095958bc639 41
kholland 10:5974a7b2cf38 42 DigitalOut myled(LED1); // Main LED
kylerodgers 13:f827f384f0a1 43 DigitalOut skywire_rts(PB_5); // Skywire Send
kylerodgers 13:f827f384f0a1 44 DigitalOut green_LED(D7); // Green LED
kylerodgers 13:f827f384f0a1 45 DigitalOut red_LED(D10); // Red LED
kholland 0:3095958bc639 46
kylerodgers 13:f827f384f0a1 47 DigitalOut nRESET(PB_4); // Skywire Reset line
kylerodgers 13:f827f384f0a1 48
kylerodgers 13:f827f384f0a1 49 AnalogIn photo_trans(A3); // Photo Transistor
kylerodgers 13:f827f384f0a1 50 DigitalOut photo_trans_nEN(D11); // Photo Transistor Enable
kylerodgers 13:f827f384f0a1 51 DigitalIn button1(PA_5); // Button 1
kholland 10:5974a7b2cf38 52
kholland 10:5974a7b2cf38 53 Serial skywire(PA_9,PA_10); // Serial comms to Skywire
kholland 10:5974a7b2cf38 54 Serial debug_pc(USBTX, USBRX); // USB connection to PC
kholland 10:5974a7b2cf38 55
kholland 10:5974a7b2cf38 56 I2C i2c(PB_9,PB_8); // Setup I2C bus for sensors
GregNash 11:78a28ca6409f 57
kylerodgers 13:f827f384f0a1 58 // SHT30 Sensor Setup
kylerodgers 13:f827f384f0a1 59 SHT30DISB SHT30(i2c);
kholland 10:5974a7b2cf38 60
kylerodgers 13:f827f384f0a1 61 // variable to switch state
kylerodgers 13:f827f384f0a1 62 bool sw1;
kholland 0:3095958bc639 63
kholland 10:5974a7b2cf38 64 // char array for reading from Skywire
kholland 3:4b2176f6474c 65 char str[255];
kylerodgers 13:f827f384f0a1 66
kylerodgers 13:f827f384f0a1 67 // Variables for checking CSQ
GregNash 11:78a28ca6409f 68 char csq[3]="99";
GregNash 11:78a28ca6409f 69 int csq_val = 99;
kholland 3:4b2176f6474c 70
kholland 10:5974a7b2cf38 71 // Variables for UART comms
kholland 3:4b2176f6474c 72 volatile int rx_in=0;
kholland 3:4b2176f6474c 73 volatile int rx_out=0;
kholland 10:5974a7b2cf38 74 const int buffer_size = 600;
kholland 3:4b2176f6474c 75 char rx_buffer[buffer_size+1];
kholland 3:4b2176f6474c 76 char rx_line[buffer_size];
kholland 3:4b2176f6474c 77
kholland 10:5974a7b2cf38 78 // Interrupt for the Skywire
kholland 10:5974a7b2cf38 79 void Skywire_Rx_interrupt()
kholland 10:5974a7b2cf38 80 {
kholland 3:4b2176f6474c 81 // Loop just in case more than one character is in UART's receive FIFO buffer
kholland 3:4b2176f6474c 82 // Stop if buffer full
kholland 3:4b2176f6474c 83 while ((skywire.readable()) && (((rx_in + 1) % buffer_size) != rx_out)) {
kholland 3:4b2176f6474c 84 rx_buffer[rx_in] = skywire.getc();
kholland 3:4b2176f6474c 85 rx_in = (rx_in + 1) % buffer_size;
kholland 3:4b2176f6474c 86 }
kholland 3:4b2176f6474c 87 return;
kholland 3:4b2176f6474c 88 }
kholland 3:4b2176f6474c 89
kholland 10:5974a7b2cf38 90 // Read line from the UART
kholland 10:5974a7b2cf38 91 void read_line()
kholland 10:5974a7b2cf38 92 {
kholland 3:4b2176f6474c 93 int i;
kholland 3:4b2176f6474c 94 i = 0;
kholland 3:4b2176f6474c 95 // Start Critical Section - don't interrupt while changing global buffer variables
kholland 3:4b2176f6474c 96 __disable_irq();
kholland 3:4b2176f6474c 97 // Loop reading rx buffer characters until end of line character
kholland 3:4b2176f6474c 98 while ((i==0) || (rx_line[i-1] != '\n')) {
kholland 3:4b2176f6474c 99 // Wait if buffer empty
kholland 3:4b2176f6474c 100 if (rx_in == rx_out) {
kholland 3:4b2176f6474c 101 // End Critical Section - need to allow rx interrupt to get new characters for buffer
kholland 3:4b2176f6474c 102 __enable_irq();
kholland 3:4b2176f6474c 103 while (rx_in == rx_out) {
kholland 3:4b2176f6474c 104 }
kholland 3:4b2176f6474c 105 // Start Critical Section - don't interrupt while changing global buffer variables
kholland 3:4b2176f6474c 106 __disable_irq();
kholland 3:4b2176f6474c 107 }
kholland 3:4b2176f6474c 108 rx_line[i] = rx_buffer[rx_out];
kholland 3:4b2176f6474c 109 i++;
kholland 3:4b2176f6474c 110 rx_out = (rx_out + 1) % buffer_size;
kholland 3:4b2176f6474c 111 }
kholland 3:4b2176f6474c 112 // End Critical Section
kholland 3:4b2176f6474c 113 __enable_irq();
kholland 3:4b2176f6474c 114 rx_line[i-1] = 0;
kholland 3:4b2176f6474c 115 return;
kholland 3:4b2176f6474c 116 }
kholland 10:5974a7b2cf38 117
kholland 10:5974a7b2cf38 118 // Wait for specific response
GregNash 11:78a28ca6409f 119 int WaitForResponse(const char *response)
kholland 10:5974a7b2cf38 120 {
GregNash 11:78a28ca6409f 121 debug_pc.printf("Command sent. Waiting for: %s\r\n", response);
kholland 3:4b2176f6474c 122 do {
kholland 3:4b2176f6474c 123 read_line();
kylerodgers 13:f827f384f0a1 124 // If we get ERROR, return with 1
kylerodgers 13:f827f384f0a1 125 if (strncmp(rx_line, "ERROR", strlen("ERROR")) == 0) {
kylerodgers 13:f827f384f0a1 126 debug_pc.printf("ERROR\r\n");
kylerodgers 13:f827f384f0a1 127 return 1;
kylerodgers 13:f827f384f0a1 128 }
kholland 3:4b2176f6474c 129 debug_pc.printf("Waiting for: %s, Recieved: %s\r\n", response, rx_line);
GregNash 11:78a28ca6409f 130 } while (strncmp(rx_line, response, strlen(response)));
GregNash 11:78a28ca6409f 131 return 0;
GregNash 11:78a28ca6409f 132 }
GregNash 11:78a28ca6409f 133
kylerodgers 13:f827f384f0a1 134 // Send AT+CSQ until we get a good signal
GregNash 11:78a28ca6409f 135 int GetCSQResponse()
GregNash 11:78a28ca6409f 136 {
GregNash 11:78a28ca6409f 137 do {
GregNash 11:78a28ca6409f 138 skywire.printf("AT+CSQ\r");
GregNash 11:78a28ca6409f 139 WaitForResponse("OK");
GregNash 11:78a28ca6409f 140 csq[0]=rx_line[6];
GregNash 11:78a28ca6409f 141 csq[1]=rx_line[7];
GregNash 11:78a28ca6409f 142 csq_val=atoi(csq);
GregNash 11:78a28ca6409f 143 } while (!strncmp(rx_line, "CSQ: 99,", 8));
GregNash 11:78a28ca6409f 144 return csq_val;
GregNash 11:78a28ca6409f 145 }
GregNash 11:78a28ca6409f 146
kholland 10:5974a7b2cf38 147 int main()
kholland 0:3095958bc639 148 {
GregNash 11:78a28ca6409f 149 red_LED = 1;
GregNash 11:78a28ca6409f 150
kholland 0:3095958bc639 151 float temp;
kylerodgers 7:00d66e743603 152 float humi;
GregNash 11:78a28ca6409f 153 float photo;
kholland 0:3095958bc639 154
kholland 10:5974a7b2cf38 155 // Setup serial comms with Skywire and PC
kholland 0:3095958bc639 156 debug_pc.baud(115200);
kylerodgers 13:f827f384f0a1 157 skywire.baud(921600);
kylerodgers 13:f827f384f0a1 158 skywire_rts = 0;
kholland 10:5974a7b2cf38 159 debug_pc.printf("SystemCoreClock = %d Hz\r\n", SystemCoreClock);
kylerodgers 13:f827f384f0a1 160 debug_pc.printf("Attaching interrupt...\r\n");
kholland 3:4b2176f6474c 161 skywire.attach(&Skywire_Rx_interrupt, Serial::RxIrq);
kylerodgers 13:f827f384f0a1 162 debug_pc.printf("Interrupt attached...\r\n");
kholland 10:5974a7b2cf38 163
kylerodgers 13:f827f384f0a1 164 // Reset the Skywire to get AT commands, and recover from any previous state
kylerodgers 13:f827f384f0a1 165 debug_pc.printf("Resetting Skywire...\r\n");
kylerodgers 13:f827f384f0a1 166 nRESET = 1;
kylerodgers 13:f827f384f0a1 167 wait_ms(100);
kylerodgers 13:f827f384f0a1 168 nRESET = 0;
kylerodgers 13:f827f384f0a1 169 wait(3);
kylerodgers 13:f827f384f0a1 170 debug_pc.printf("Resetting baud...\r\n");
kylerodgers 13:f827f384f0a1 171 skywire.printf("\rAT+IPR=115200\r");
kylerodgers 13:f827f384f0a1 172 wait_ms(100);
kylerodgers 13:f827f384f0a1 173 skywire.baud(115200);
kylerodgers 13:f827f384f0a1 174 wait_ms(100);
kylerodgers 13:f827f384f0a1 175 debug_pc.printf("Baud reset!\r\n");
kholland 0:3095958bc639 176 myled=0;
kholland 3:4b2176f6474c 177 debug_pc.printf("Starting Demo...\r\n");
kholland 3:4b2176f6474c 178 debug_pc.printf("Waiting for Skywire to Boot...\r\n");
kylerodgers 13:f827f384f0a1 179 green_LED = !green_LED;
kholland 3:4b2176f6474c 180 myled=1;
kylerodgers 13:f827f384f0a1 181 wait_ms(100);
kholland 10:5974a7b2cf38 182
kylerodgers 13:f827f384f0a1 183 debug_pc.printf("Sending AT\r\n");
kylerodgers 13:f827f384f0a1 184 skywire.printf("\rAT\r");
GregNash 11:78a28ca6409f 185 WaitForResponse("OK");
kylerodgers 13:f827f384f0a1 186 WaitForResponse("+CEREG: 1");
GregNash 11:78a28ca6409f 187
kylerodgers 13:f827f384f0a1 188 // Check CSQ
GregNash 11:78a28ca6409f 189 debug_pc.printf("Getting CSQ...\r\n");
GregNash 11:78a28ca6409f 190 GetCSQResponse();
kylerodgers 13:f827f384f0a1 191 green_LED = !green_LED;
GregNash 11:78a28ca6409f 192 while(csq_val==99 || csq_val==0)
GregNash 11:78a28ca6409f 193 {
GregNash 11:78a28ca6409f 194
GregNash 11:78a28ca6409f 195 debug_pc.printf("CSQ Value: %i\r\n",csq_val);
GregNash 11:78a28ca6409f 196 debug_pc.printf("No network sginal detected. \r\n");
GregNash 11:78a28ca6409f 197 debug_pc.printf("Waiting for device to connect to the network. \r\n");
GregNash 11:78a28ca6409f 198 debug_pc.printf("Please check antenna connections if network is not found after 30 seconds. \r\n");
GregNash 11:78a28ca6409f 199 wait(1);
GregNash 11:78a28ca6409f 200 //add elapsed time
GregNash 11:78a28ca6409f 201 debug_pc.printf("Checking network connectrion. \r\n");
GregNash 11:78a28ca6409f 202 GetCSQResponse();
GregNash 11:78a28ca6409f 203 red_LED = !red_LED;
GregNash 11:78a28ca6409f 204 }
GregNash 11:78a28ca6409f 205
GregNash 11:78a28ca6409f 206 debug_pc.printf("Network detected. Checking authorization...\r\n");
kylerodgers 13:f827f384f0a1 207 skywire.printf("AT+CEREG?\r");
GregNash 11:78a28ca6409f 208 WaitForResponse("OK");
kylerodgers 7:00d66e743603 209
kylerodgers 13:f827f384f0a1 210 //debug_pc.printf("Connecting to Network...\r\n");
kylerodgers 13:f827f384f0a1 211 debug_pc.printf("Setting up socket...\r\n");
kylerodgers 13:f827f384f0a1 212 skywire.printf("AT+SQNSCFG=3,3,300,90,600,50\r");
kylerodgers 13:f827f384f0a1 213 WaitForResponse("OK");
kholland 10:5974a7b2cf38 214
GregNash 11:78a28ca6409f 215 red_LED = 1;
GregNash 11:78a28ca6409f 216 green_LED = 0;
GregNash 11:78a28ca6409f 217
kholland 0:3095958bc639 218 while(1) {
kholland 10:5974a7b2cf38 219 // Green on to indicate code position
kholland 10:5974a7b2cf38 220 // Start of loop. Either entered loop for the first time or just sent to dweet.io
kholland 10:5974a7b2cf38 221 red_LED = 0;
kholland 10:5974a7b2cf38 222 green_LED = 1;
kylerodgers 13:f827f384f0a1 223
kylerodgers 13:f827f384f0a1 224 int retries = 0;
kylerodgers 13:f827f384f0a1 225 while (retries < 5) {
kylerodgers 13:f827f384f0a1 226 skywire.printf("AT+SQNSD=3,0,80,\"dweet.io\"\r");
kylerodgers 13:f827f384f0a1 227 if (WaitForResponse("CONNECT") == 1) {
kylerodgers 13:f827f384f0a1 228 retries += 1;
kylerodgers 13:f827f384f0a1 229 wait(1);
kylerodgers 13:f827f384f0a1 230 } else
kylerodgers 13:f827f384f0a1 231 break;
GregNash 11:78a28ca6409f 232 }
kylerodgers 13:f827f384f0a1 233
kylerodgers 13:f827f384f0a1 234 //get temp and humi
GregNash 11:78a28ca6409f 235 temp=SHT30.cTemp();
GregNash 11:78a28ca6409f 236 humi=SHT30.humidity();
GregNash 11:78a28ca6409f 237 photo_trans_nEN=0;
GregNash 11:78a28ca6409f 238 photo=photo_trans*200;
kholland 10:5974a7b2cf38 239 wait(1);
kholland 10:5974a7b2cf38 240
kholland 10:5974a7b2cf38 241 // Check buttons for presses
kholland 10:5974a7b2cf38 242 if (button1 == 0)
kylerodgers 13:f827f384f0a1 243 sw1 = 1;
kholland 10:5974a7b2cf38 244 else
kylerodgers 13:f827f384f0a1 245 sw1 = 0;
kholland 3:4b2176f6474c 246
kholland 10:5974a7b2cf38 247 // Green on to indicate code position:
kholland 10:5974a7b2cf38 248 // Sensors updated, have not sent to dweet.io
kholland 10:5974a7b2cf38 249 red_LED = 1;
kholland 10:5974a7b2cf38 250 green_LED = 0;
kholland 10:5974a7b2cf38 251
kylerodgers 13:f827f384f0a1 252 if (retries != 5) {
GregNash 11:78a28ca6409f 253 debug_pc.printf("Sending information...\r\n");
GregNash 11:78a28ca6409f 254 // Report the sensor data to dweet.io
GregNash 11:78a28ca6409f 255 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);
GregNash 11:78a28ca6409f 256 WaitForResponse("NO CARRIER");
kylerodgers 13:f827f384f0a1 257 }
kylerodgers 13:f827f384f0a1 258 // }
kylerodgers 13:f827f384f0a1 259 debug_pc.printf("Closing socket...\r\n");
kylerodgers 13:f827f384f0a1 260 skywire.printf("AT+SQNSH=3\r");
kylerodgers 13:f827f384f0a1 261 WaitForResponse("OK");
kylerodgers 13:f827f384f0a1 262 skywire.printf("AT+CGATT=0\r");
kylerodgers 13:f827f384f0a1 263 WaitForResponse("OK");
kylerodgers 13:f827f384f0a1 264 skywire.printf("AT+CGATT=1\r");
kylerodgers 13:f827f384f0a1 265 WaitForResponse("+CEREG: 1");
kholland 10:5974a7b2cf38 266 red_LED = 0;
GregNash 11:78a28ca6409f 267 green_LED = 1;
kholland 3:4b2176f6474c 268 }
GregNash 11:78a28ca6409f 269 }