M1DK Skywire Demo
Dependencies: SHT30-DIS-B mbed
Fork of M1DK_Skywire_Demo by
main.cpp@11:78a28ca6409f, 2017-02-01 (annotated)
- Committer:
- GregNash
- Date:
- Wed Feb 01 18:36:43 2017 +0000
- Revision:
- 11:78a28ca6409f
- Parent:
- 10:5974a7b2cf38
- Child:
- 13:f827f384f0a1
Code Share
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kholland | 4:e7a79d3542e1 | 1 | /* main.cpp */ |
GregNash | 11:78a28ca6409f | 2 | /* v5.0 |
kholland | 10:5974a7b2cf38 | 3 | * Copyright (C) 2016 nimbelink.com, MIT License |
kholland | 4:e7a79d3542e1 | 4 | * |
kholland | 4:e7a79d3542e1 | 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
kholland | 4:e7a79d3542e1 | 6 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
kholland | 4:e7a79d3542e1 | 7 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
kholland | 4:e7a79d3542e1 | 8 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
kholland | 4:e7a79d3542e1 | 9 | * furnished to do so, subject to the following conditions: |
kholland | 4:e7a79d3542e1 | 10 | * |
kholland | 4:e7a79d3542e1 | 11 | * The above copyright notice and this permission notice shall be included in all copies or |
kholland | 4:e7a79d3542e1 | 12 | * substantial portions of the Software. |
kholland | 4:e7a79d3542e1 | 13 | * |
kholland | 4:e7a79d3542e1 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
kholland | 4:e7a79d3542e1 | 15 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
kholland | 4:e7a79d3542e1 | 16 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
kholland | 4:e7a79d3542e1 | 17 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
kholland | 4:e7a79d3542e1 | 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
kholland | 4:e7a79d3542e1 | 19 | */ |
kholland | 0:3095958bc639 | 20 | |
kholland | 10:5974a7b2cf38 | 21 | /* |
kholland | 10:5974a7b2cf38 | 22 | * DESCRIPTION |
kholland | 10:5974a7b2cf38 | 23 | * This code updated sensor data on the Nimbelink ST Development Kit (NL-AB-ST-NCL) and sends |
kholland | 10:5974a7b2cf38 | 24 | * the information to www.dweet.io using the Thingname "DeviceID". That information can be |
kholland | 10:5974a7b2cf38 | 25 | * viewed using a Freeboard at www.freeboard.io (account required). Depending on your |
kholland | 10:5974a7b2cf38 | 26 | * ST Development Kit version, clone the following freeboard: |
kholland | 10:5974a7b2cf38 | 27 | * Rev A (Legacy): https://freeboard.io/board/jqlneI |
kholland | 10:5974a7b2cf38 | 28 | * Rev B (Current): https://freeboard.io/board/LhnbrX |
kholland | 10:5974a7b2cf38 | 29 | * |
GregNash | 11:78a28ca6409f | 30 | * Please consult the wiki for more information. |
kholland | 10:5974a7b2cf38 | 31 | */ |
kholland | 10:5974a7b2cf38 | 32 | |
kholland | 10:5974a7b2cf38 | 33 | /* |
kholland | 10:5974a7b2cf38 | 34 | * INSTRUCTIONS FOR USING THIS CODE |
GregNash | 11:78a28ca6409f | 35 | * 1. This code will automatically detect the modem you are using. |
kholland | 10:5974a7b2cf38 | 36 | * |
kholland | 10:5974a7b2cf38 | 37 | * 2. Change the "DeviceID" to a unique identifier for your Nucleo board. One recommendation |
kholland | 10:5974a7b2cf38 | 38 | * would be to use the MEID/IMEI of your Skywire Modem. |
kholland | 10:5974a7b2cf38 | 39 | * |
kholland | 10:5974a7b2cf38 | 40 | * 3. If applicable, change the APN for your Skywire Modem. |
kholland | 10:5974a7b2cf38 | 41 | */ |
kholland | 10:5974a7b2cf38 | 42 | |
GregNash | 11:78a28ca6409f | 43 | #include "mbed.h" // mbed Library |
GregNash | 11:78a28ca6409f | 44 | #include "pinmap.h" // pinmap needed for hardware flow control |
GregNash | 11:78a28ca6409f | 45 | |
GregNash | 11:78a28ca6409f | 46 | #include "SHT30DISB.h" |
kholland | 0:3095958bc639 | 47 | |
GregNash | 11:78a28ca6409f | 48 | enum Skywire_Modem { |
GregNash | 11:78a28ca6409f | 49 | NL_SW_1xRTT_V, // Verizon 2G Modem - CE910-DUAL |
GregNash | 11:78a28ca6409f | 50 | NL_SW_1xRTT_S, // Sprint 2G Modem - CE910-DUAL |
GregNash | 11:78a28ca6409f | 51 | NL_SW_1xRTT_A, // Aeris 2G Modem - CE910-DUAL |
GregNash | 11:78a28ca6409f | 52 | NL_SW_GPRS, // AT&T/T-Mobile 2G Modem |
GregNash | 11:78a28ca6409f | 53 | NL_SW_EVDO_V, // Verizon 3G Modem |
GregNash | 11:78a28ca6409f | 54 | NL_SW_EVDO_A, // Aeris 3G Modem |
GregNash | 11:78a28ca6409f | 55 | NL_SW_HSPAP, // AT&T/T-Mobile 3G Modem |
GregNash | 11:78a28ca6409f | 56 | NL_SW_HSPAPG, // AT&T/T-Mobile 3G Modem w/ GPS |
GregNash | 11:78a28ca6409f | 57 | NL_SW_HSPAPE, // GSM 3G Modem, EU |
GregNash | 11:78a28ca6409f | 58 | NL_SW_LTE_TSVG, // Verizon 4G LTE Modem |
GregNash | 11:78a28ca6409f | 59 | NL_SW_LTE_TNAG, // AT&T/T-Mobile 4G LTE Modem |
GregNash | 11:78a28ca6409f | 60 | NL_SW_LTE_TEUG, // GSM 4G LTE Modem, EU |
GregNash | 11:78a28ca6409f | 61 | NL_SW_LTE_GELS3, // VZW LTE CAT 1 Modem |
GregNash | 11:78a28ca6409f | 62 | NL_SW_LTE_S7588 // VZW LTE CAT 4 Modem |
GregNash | 11:78a28ca6409f | 63 | }; |
kholland | 10:5974a7b2cf38 | 64 | |
GregNash | 11:78a28ca6409f | 65 | // Our modem |
GregNash | 11:78a28ca6409f | 66 | Skywire_Modem MODEM; |
kholland | 0:3095958bc639 | 67 | |
GregNash | 11:78a28ca6409f | 68 | // --CHANGE THIS FOR YOUR SETUP-- |
GregNash | 11:78a28ca6409f | 69 | #define DeviceID "M1DK-UNIT1" //DweetIO unique ID |
GregNash | 11:78a28ca6409f | 70 | |
GregNash | 11:78a28ca6409f | 71 | // --CHANGE THIS FOR YOUR SETUP (IF APPLICABLE)-- |
GregNash | 11:78a28ca6409f | 72 | char const *APN = "NIMBLINK.GW12.VZWENTP"; |
GregNash | 11:78a28ca6409f | 73 | //char const *APN = "mw01.VZWSTATIC"; |
GregNash | 11:78a28ca6409f | 74 | //char const *APN = "VZWINTERNET"; |
kholland | 0:3095958bc639 | 75 | |
kholland | 10:5974a7b2cf38 | 76 | DigitalOut myled(LED1); // Main LED |
GregNash | 11:78a28ca6409f | 77 | DigitalOut skywire_en(A2); // Skywire Enable |
GregNash | 11:78a28ca6409f | 78 | PinName skywire_rts(D5); // Skywire Send |
GregNash | 11:78a28ca6409f | 79 | DigitalOut green_LED(D7); // Green LED |
GregNash | 11:78a28ca6409f | 80 | DigitalOut red_LED(D10); // Red LED |
kholland | 0:3095958bc639 | 81 | |
GregNash | 11:78a28ca6409f | 82 | AnalogIn photo_trans(A3); // Photo Transistor |
GregNash | 11:78a28ca6409f | 83 | DigitalOut photo_trans_nEN(D11); // Photo Transistor |
GregNash | 11:78a28ca6409f | 84 | DigitalIn button1(D13); // Button 1 |
kholland | 10:5974a7b2cf38 | 85 | |
kholland | 10:5974a7b2cf38 | 86 | Serial skywire(PA_9,PA_10); // Serial comms to Skywire |
kholland | 10:5974a7b2cf38 | 87 | Serial debug_pc(USBTX, USBRX); // USB connection to PC |
kholland | 10:5974a7b2cf38 | 88 | |
kholland | 10:5974a7b2cf38 | 89 | I2C i2c(PB_9,PB_8); // Setup I2C bus for sensors |
kholland | 10:5974a7b2cf38 | 90 | bool sw1; // Boolean to check if button 1 is pressed |
GregNash | 11:78a28ca6409f | 91 | |
kholland | 10:5974a7b2cf38 | 92 | |
GregNash | 11:78a28ca6409f | 93 | SHT30DISB SHT30(i2c); |
kholland | 0:3095958bc639 | 94 | |
kholland | 10:5974a7b2cf38 | 95 | // char array for reading from Skywire |
kholland | 3:4b2176f6474c | 96 | char str[255]; |
GregNash | 11:78a28ca6409f | 97 | char csq[3]="99"; |
GregNash | 11:78a28ca6409f | 98 | int csq_val = 99; |
kholland | 3:4b2176f6474c | 99 | |
kholland | 10:5974a7b2cf38 | 100 | // Variables for GPS |
kholland | 0:3095958bc639 | 101 | float latitude; |
kholland | 0:3095958bc639 | 102 | float longitude; |
kholland | 0:3095958bc639 | 103 | int number; |
kholland | 0:3095958bc639 | 104 | |
kholland | 10:5974a7b2cf38 | 105 | // Variables for UART comms |
kholland | 3:4b2176f6474c | 106 | volatile int rx_in=0; |
kholland | 3:4b2176f6474c | 107 | volatile int rx_out=0; |
kholland | 10:5974a7b2cf38 | 108 | const int buffer_size = 600; |
kholland | 3:4b2176f6474c | 109 | char rx_buffer[buffer_size+1]; |
kholland | 3:4b2176f6474c | 110 | char rx_line[buffer_size]; |
kholland | 3:4b2176f6474c | 111 | |
GregNash | 11:78a28ca6409f | 112 | // Provisioning Flag for CE910 and DE910 only |
GregNash | 11:78a28ca6409f | 113 | int prov_flag = 0; |
GregNash | 11:78a28ca6409f | 114 | |
kholland | 10:5974a7b2cf38 | 115 | // Interrupt for the Skywire |
kholland | 10:5974a7b2cf38 | 116 | void Skywire_Rx_interrupt() |
kholland | 10:5974a7b2cf38 | 117 | { |
kholland | 3:4b2176f6474c | 118 | // Loop just in case more than one character is in UART's receive FIFO buffer |
kholland | 3:4b2176f6474c | 119 | // Stop if buffer full |
kholland | 3:4b2176f6474c | 120 | while ((skywire.readable()) && (((rx_in + 1) % buffer_size) != rx_out)) { |
kholland | 3:4b2176f6474c | 121 | rx_buffer[rx_in] = skywire.getc(); |
kholland | 3:4b2176f6474c | 122 | rx_in = (rx_in + 1) % buffer_size; |
kholland | 3:4b2176f6474c | 123 | } |
kholland | 3:4b2176f6474c | 124 | return; |
kholland | 3:4b2176f6474c | 125 | } |
kholland | 3:4b2176f6474c | 126 | |
GregNash | 11:78a28ca6409f | 127 | |
GregNash | 11:78a28ca6409f | 128 | // Function to "pretty print" the delay time |
GregNash | 11:78a28ca6409f | 129 | void wait_print(int time) |
GregNash | 11:78a28ca6409f | 130 | { |
GregNash | 11:78a28ca6409f | 131 | for (int i = time; i >= 0; i--) |
GregNash | 11:78a28ca6409f | 132 | { |
GregNash | 11:78a28ca6409f | 133 | if (i == 1) { |
GregNash | 11:78a28ca6409f | 134 | debug_pc.printf("Delay Time Remaining: %d second \r", i); |
GregNash | 11:78a28ca6409f | 135 | } |
GregNash | 11:78a28ca6409f | 136 | else { |
GregNash | 11:78a28ca6409f | 137 | debug_pc.printf("Delay Time Remaining: %d seconds \r", i); |
GregNash | 11:78a28ca6409f | 138 | } |
GregNash | 11:78a28ca6409f | 139 | green_LED = 0; |
GregNash | 11:78a28ca6409f | 140 | wait(0.5); |
GregNash | 11:78a28ca6409f | 141 | green_LED = 1; |
GregNash | 11:78a28ca6409f | 142 | wait(0.5); |
GregNash | 11:78a28ca6409f | 143 | } |
GregNash | 11:78a28ca6409f | 144 | debug_pc.printf("Delay finished! \r\n"); |
GregNash | 11:78a28ca6409f | 145 | return; |
GregNash | 11:78a28ca6409f | 146 | } |
GregNash | 11:78a28ca6409f | 147 | |
GregNash | 11:78a28ca6409f | 148 | |
GregNash | 11:78a28ca6409f | 149 | |
kholland | 10:5974a7b2cf38 | 150 | // Function to blink LEDs for debugging |
kholland | 10:5974a7b2cf38 | 151 | // NOTE: Currently not used |
kholland | 10:5974a7b2cf38 | 152 | void blink_leds(int num) |
kholland | 10:5974a7b2cf38 | 153 | { |
kholland | 10:5974a7b2cf38 | 154 | for (int i = 0; i < num; i++) { |
GregNash | 11:78a28ca6409f | 155 | green_LED = 0; |
GregNash | 11:78a28ca6409f | 156 | myled=0; |
GregNash | 11:78a28ca6409f | 157 | wait(0.25); |
GregNash | 11:78a28ca6409f | 158 | green_LED = 1; |
GregNash | 11:78a28ca6409f | 159 | myled=1; |
GregNash | 11:78a28ca6409f | 160 | wait(0.25); |
kholland | 10:5974a7b2cf38 | 161 | } |
kholland | 10:5974a7b2cf38 | 162 | } |
GregNash | 11:78a28ca6409f | 163 | void blink_redled(int num) |
GregNash | 11:78a28ca6409f | 164 | { |
GregNash | 11:78a28ca6409f | 165 | for (int i = 0; i < num; i++) { |
GregNash | 11:78a28ca6409f | 166 | red_LED = 0; |
GregNash | 11:78a28ca6409f | 167 | wait(0.25); |
GregNash | 11:78a28ca6409f | 168 | red_LED = 1; |
GregNash | 11:78a28ca6409f | 169 | wait(0.25); |
GregNash | 11:78a28ca6409f | 170 | } |
GregNash | 11:78a28ca6409f | 171 | } |
kholland | 10:5974a7b2cf38 | 172 | // Read line from the UART |
kholland | 10:5974a7b2cf38 | 173 | void read_line() |
kholland | 10:5974a7b2cf38 | 174 | { |
kholland | 3:4b2176f6474c | 175 | int i; |
kholland | 3:4b2176f6474c | 176 | i = 0; |
kholland | 3:4b2176f6474c | 177 | // Start Critical Section - don't interrupt while changing global buffer variables |
kholland | 3:4b2176f6474c | 178 | __disable_irq(); |
kholland | 3:4b2176f6474c | 179 | // Loop reading rx buffer characters until end of line character |
kholland | 3:4b2176f6474c | 180 | while ((i==0) || (rx_line[i-1] != '\n')) { |
kholland | 3:4b2176f6474c | 181 | // Wait if buffer empty |
kholland | 3:4b2176f6474c | 182 | if (rx_in == rx_out) { |
kholland | 3:4b2176f6474c | 183 | // End Critical Section - need to allow rx interrupt to get new characters for buffer |
kholland | 3:4b2176f6474c | 184 | __enable_irq(); |
kholland | 3:4b2176f6474c | 185 | while (rx_in == rx_out) { |
kholland | 3:4b2176f6474c | 186 | } |
kholland | 3:4b2176f6474c | 187 | // Start Critical Section - don't interrupt while changing global buffer variables |
kholland | 3:4b2176f6474c | 188 | __disable_irq(); |
kholland | 3:4b2176f6474c | 189 | } |
kholland | 3:4b2176f6474c | 190 | rx_line[i] = rx_buffer[rx_out]; |
kholland | 3:4b2176f6474c | 191 | i++; |
kholland | 3:4b2176f6474c | 192 | rx_out = (rx_out + 1) % buffer_size; |
kholland | 3:4b2176f6474c | 193 | } |
kholland | 3:4b2176f6474c | 194 | // End Critical Section |
kholland | 3:4b2176f6474c | 195 | __enable_irq(); |
kholland | 3:4b2176f6474c | 196 | rx_line[i-1] = 0; |
kholland | 3:4b2176f6474c | 197 | return; |
kholland | 3:4b2176f6474c | 198 | } |
kholland | 10:5974a7b2cf38 | 199 | |
kholland | 10:5974a7b2cf38 | 200 | // Wait for specific response |
GregNash | 11:78a28ca6409f | 201 | int WaitForResponse(const char *response) |
kholland | 10:5974a7b2cf38 | 202 | { |
GregNash | 11:78a28ca6409f | 203 | debug_pc.printf("Command sent. Waiting for: %s\r\n", response); |
kholland | 3:4b2176f6474c | 204 | do { |
kholland | 3:4b2176f6474c | 205 | read_line(); |
kholland | 3:4b2176f6474c | 206 | debug_pc.printf("Waiting for: %s, Recieved: %s\r\n", response, rx_line); |
GregNash | 11:78a28ca6409f | 207 | } while (strncmp(rx_line, response, strlen(response))); |
GregNash | 11:78a28ca6409f | 208 | return 0; |
GregNash | 11:78a28ca6409f | 209 | } |
GregNash | 11:78a28ca6409f | 210 | |
GregNash | 11:78a28ca6409f | 211 | // Get and parse the AT+GMM response |
GregNash | 11:78a28ca6409f | 212 | // Depending on what it gets, parse and return |
GregNash | 11:78a28ca6409f | 213 | int GetGMMResponse() |
GregNash | 11:78a28ca6409f | 214 | { |
GregNash | 11:78a28ca6409f | 215 | //int ret_val = 0; |
GregNash | 11:78a28ca6409f | 216 | do { |
GregNash | 11:78a28ca6409f | 217 | read_line(); |
GregNash | 11:78a28ca6409f | 218 | debug_pc.printf("Waiting for: %s, Received: %s\r\n", "OK", rx_line); |
GregNash | 11:78a28ca6409f | 219 | if (!strncmp(rx_line, "GE910-QUAD-V3", 13)) { |
GregNash | 11:78a28ca6409f | 220 | MODEM = NL_SW_GPRS; |
GregNash | 11:78a28ca6409f | 221 | debug_pc.printf("Modem detected: NL-SW-GPRS\r\n"); |
GregNash | 11:78a28ca6409f | 222 | //ret_val = 1; |
GregNash | 11:78a28ca6409f | 223 | return 1; |
GregNash | 11:78a28ca6409f | 224 | } |
GregNash | 11:78a28ca6409f | 225 | if (!strncmp(rx_line, "CE910-DUAL", 10)) { |
GregNash | 11:78a28ca6409f | 226 | MODEM = NL_SW_1xRTT_V; |
GregNash | 11:78a28ca6409f | 227 | debug_pc.printf("Modem detected: NL-SW-1xRTT\r\n"); |
GregNash | 11:78a28ca6409f | 228 | //ret_val = 1; |
GregNash | 11:78a28ca6409f | 229 | return 1; |
GregNash | 11:78a28ca6409f | 230 | } |
GregNash | 11:78a28ca6409f | 231 | if (!strncmp(rx_line, "DE910-DUAL", 10)) { |
GregNash | 11:78a28ca6409f | 232 | MODEM = NL_SW_EVDO_V; |
GregNash | 11:78a28ca6409f | 233 | debug_pc.printf("Modem detected: NL-SW-EVDO\r\n"); |
GregNash | 11:78a28ca6409f | 234 | //ret_val = 1; |
GregNash | 11:78a28ca6409f | 235 | return 1; |
GregNash | 11:78a28ca6409f | 236 | } |
GregNash | 11:78a28ca6409f | 237 | if (!strncmp(rx_line, "HE910-NAD", 9)) { |
GregNash | 11:78a28ca6409f | 238 | MODEM = NL_SW_HSPAP; |
GregNash | 11:78a28ca6409f | 239 | debug_pc.printf("Modem detected: NL-SW-HSPAP\r\n"); |
GregNash | 11:78a28ca6409f | 240 | //ret_val = 1; |
GregNash | 11:78a28ca6409f | 241 | return 1; |
GregNash | 11:78a28ca6409f | 242 | } |
GregNash | 11:78a28ca6409f | 243 | if (!strncmp(rx_line, "HE910-D", 7)) { |
GregNash | 11:78a28ca6409f | 244 | MODEM = NL_SW_HSPAPG; |
GregNash | 11:78a28ca6409f | 245 | debug_pc.printf("Modem detected: NL-SW-HSPAPG\r\n"); |
GregNash | 11:78a28ca6409f | 246 | //ret_val = 1; |
GregNash | 11:78a28ca6409f | 247 | return 1; |
GregNash | 11:78a28ca6409f | 248 | } |
GregNash | 11:78a28ca6409f | 249 | if (!strncmp(rx_line, "LE910-SVG", 9)) { |
GregNash | 11:78a28ca6409f | 250 | MODEM = NL_SW_LTE_TSVG; |
GregNash | 11:78a28ca6409f | 251 | debug_pc.printf("Modem detected: NL-SW-LTE-TSVG\r\n"); |
GregNash | 11:78a28ca6409f | 252 | //ret_val = 1; |
GregNash | 11:78a28ca6409f | 253 | return 1; |
GregNash | 11:78a28ca6409f | 254 | } |
GregNash | 11:78a28ca6409f | 255 | if (!strncmp(rx_line, "LE910-NAG", 9)) { |
GregNash | 11:78a28ca6409f | 256 | MODEM = NL_SW_LTE_TNAG; |
GregNash | 11:78a28ca6409f | 257 | debug_pc.printf("Modem detected: NL-SW-LTE-TNAG\r\n"); |
GregNash | 11:78a28ca6409f | 258 | //ret_val = 1; |
GregNash | 11:78a28ca6409f | 259 | return 1; |
GregNash | 11:78a28ca6409f | 260 | } |
GregNash | 11:78a28ca6409f | 261 | if (!strncmp(rx_line, "HL7588", 6)) { |
GregNash | 11:78a28ca6409f | 262 | MODEM = NL_SW_LTE_S7588; |
GregNash | 11:78a28ca6409f | 263 | debug_pc.printf("Modem detected: NL-SW-LTE-S7588\r\n"); |
GregNash | 11:78a28ca6409f | 264 | //ret_val = 1; |
GregNash | 11:78a28ca6409f | 265 | return 1; |
GregNash | 11:78a28ca6409f | 266 | } |
GregNash | 11:78a28ca6409f | 267 | } while (strncmp(rx_line, "OK", 2)); |
kholland | 3:4b2176f6474c | 268 | return 0; |
kholland | 3:4b2176f6474c | 269 | } |
kholland | 10:5974a7b2cf38 | 270 | |
GregNash | 11:78a28ca6409f | 271 | // Get and parse the AT+GMM response |
GregNash | 11:78a28ca6409f | 272 | // Depending on what it gets, parse and return |
GregNash | 11:78a28ca6409f | 273 | int GetATIResponse() |
GregNash | 11:78a28ca6409f | 274 | { |
GregNash | 11:78a28ca6409f | 275 | int ret_val = 0; |
GregNash | 11:78a28ca6409f | 276 | do { |
GregNash | 11:78a28ca6409f | 277 | read_line(); |
GregNash | 11:78a28ca6409f | 278 | debug_pc.printf("Waiting for: %s, Received: %s\r\n", "OK", rx_line); |
GregNash | 11:78a28ca6409f | 279 | if (!strncmp(rx_line, "ELS31-V", 7)) { |
GregNash | 11:78a28ca6409f | 280 | MODEM = NL_SW_LTE_GELS3; |
GregNash | 11:78a28ca6409f | 281 | debug_pc.printf("Modem detected: NL-SW-LTE-GELS3\r\n"); |
GregNash | 11:78a28ca6409f | 282 | ret_val = 1; |
GregNash | 11:78a28ca6409f | 283 | } |
GregNash | 11:78a28ca6409f | 284 | } while (strncmp(rx_line, "OK", 2)); |
GregNash | 11:78a28ca6409f | 285 | return ret_val; |
GregNash | 11:78a28ca6409f | 286 | } |
GregNash | 11:78a28ca6409f | 287 | |
GregNash | 11:78a28ca6409f | 288 | // Get the Skywire Model |
GregNash | 11:78a28ca6409f | 289 | int getSkywireModel() |
GregNash | 11:78a28ca6409f | 290 | { |
GregNash | 11:78a28ca6409f | 291 | // Send ATI. Prints everything for CAT1, so we will check that first |
GregNash | 11:78a28ca6409f | 292 | debug_pc.printf("Sending ATI...\r\n"); |
GregNash | 11:78a28ca6409f | 293 | skywire.printf("ATI\r"); |
GregNash | 11:78a28ca6409f | 294 | if (GetATIResponse()) { |
GregNash | 11:78a28ca6409f | 295 | return 0; |
GregNash | 11:78a28ca6409f | 296 | } |
GregNash | 11:78a28ca6409f | 297 | // If you get here, then we have a non-CAT1 modem |
GregNash | 11:78a28ca6409f | 298 | debug_pc.printf("Sending AT+GMM...\r\n"); |
GregNash | 11:78a28ca6409f | 299 | skywire.printf("AT+GMM\r"); |
GregNash | 11:78a28ca6409f | 300 | if (GetGMMResponse()) { |
GregNash | 11:78a28ca6409f | 301 | return 0; |
GregNash | 11:78a28ca6409f | 302 | } |
GregNash | 11:78a28ca6409f | 303 | // Otherwise, we have an error (or no modem), so sit here and blink |
GregNash | 11:78a28ca6409f | 304 | else { |
GregNash | 11:78a28ca6409f | 305 | debug_pc.printf("Unable to detect modem: ERROR\r\n"); |
GregNash | 11:78a28ca6409f | 306 | while(1) { |
GregNash | 11:78a28ca6409f | 307 | blink_leds(10); |
GregNash | 11:78a28ca6409f | 308 | } |
GregNash | 11:78a28ca6409f | 309 | } |
GregNash | 11:78a28ca6409f | 310 | } |
GregNash | 11:78a28ca6409f | 311 | |
GregNash | 11:78a28ca6409f | 312 | |
GregNash | 11:78a28ca6409f | 313 | |
GregNash | 11:78a28ca6409f | 314 | int bootSkywire() |
GregNash | 11:78a28ca6409f | 315 | { |
GregNash | 11:78a28ca6409f | 316 | int ret_val = 0; |
GregNash | 11:78a28ca6409f | 317 | |
GregNash | 11:78a28ca6409f | 318 | |
GregNash | 11:78a28ca6409f | 319 | //Enable Skywire |
GregNash | 11:78a28ca6409f | 320 | skywire.printf("AT\r"); |
GregNash | 11:78a28ca6409f | 321 | read_line(); |
GregNash | 11:78a28ca6409f | 322 | debug_pc.printf("Waiting for: %s, Received: %s\r\n", "OK", rx_line); |
GregNash | 11:78a28ca6409f | 323 | if (strncmp(rx_line, "OK", 2)) { |
GregNash | 11:78a28ca6409f | 324 | ret_val=1; |
GregNash | 11:78a28ca6409f | 325 | debug_pc.printf("retval Value: %i\r\n", ret_val); |
GregNash | 11:78a28ca6409f | 326 | } |
GregNash | 11:78a28ca6409f | 327 | else { |
GregNash | 11:78a28ca6409f | 328 | debug_pc.printf("Modem not detected; Attempt reboot.\r\n"); |
GregNash | 11:78a28ca6409f | 329 | skywire_en=0; |
GregNash | 11:78a28ca6409f | 330 | debug_pc.printf("Toggling enable pin low\r\n"); |
GregNash | 11:78a28ca6409f | 331 | wait_print(4); |
GregNash | 11:78a28ca6409f | 332 | skywire_en=1; |
GregNash | 11:78a28ca6409f | 333 | debug_pc.printf("Toggling enable pin high\r\n"); |
GregNash | 11:78a28ca6409f | 334 | wait_print(10); |
GregNash | 11:78a28ca6409f | 335 | debug_pc.printf("retval Value2: %i\r\n", ret_val); |
GregNash | 11:78a28ca6409f | 336 | skywire.printf("AT\r"); |
GregNash | 11:78a28ca6409f | 337 | read_line(); |
GregNash | 11:78a28ca6409f | 338 | debug_pc.printf("Waiting for: %s, Received: %s\r\n", "OK", rx_line); |
GregNash | 11:78a28ca6409f | 339 | if (strncmp(rx_line, "OK", 2)) { |
GregNash | 11:78a28ca6409f | 340 | ret_val=1; |
GregNash | 11:78a28ca6409f | 341 | debug_pc.printf("retval Value: %i\r\n", ret_val); |
GregNash | 11:78a28ca6409f | 342 | } |
GregNash | 11:78a28ca6409f | 343 | |
GregNash | 11:78a28ca6409f | 344 | } |
GregNash | 11:78a28ca6409f | 345 | |
GregNash | 11:78a28ca6409f | 346 | return ret_val; |
GregNash | 11:78a28ca6409f | 347 | } |
GregNash | 11:78a28ca6409f | 348 | |
GregNash | 11:78a28ca6409f | 349 | |
GregNash | 11:78a28ca6409f | 350 | int GetCSQResponse() |
GregNash | 11:78a28ca6409f | 351 | { |
GregNash | 11:78a28ca6409f | 352 | do { |
GregNash | 11:78a28ca6409f | 353 | skywire.printf("AT+CSQ\r"); |
GregNash | 11:78a28ca6409f | 354 | WaitForResponse("OK"); |
GregNash | 11:78a28ca6409f | 355 | csq[0]=rx_line[6]; |
GregNash | 11:78a28ca6409f | 356 | csq[1]=rx_line[7]; |
GregNash | 11:78a28ca6409f | 357 | csq_val=atoi(csq); |
GregNash | 11:78a28ca6409f | 358 | // debug_pc.printf("Waiting for CSQ: %s\r\n", csq); |
GregNash | 11:78a28ca6409f | 359 | // debug_pc.printf("CSQ Value: %i\r\n", csq_val); |
GregNash | 11:78a28ca6409f | 360 | |
GregNash | 11:78a28ca6409f | 361 | } while (!strncmp(rx_line, "CSQ: 99,", 8)); |
GregNash | 11:78a28ca6409f | 362 | return csq_val; |
GregNash | 11:78a28ca6409f | 363 | } |
GregNash | 11:78a28ca6409f | 364 | |
GregNash | 11:78a28ca6409f | 365 | |
GregNash | 11:78a28ca6409f | 366 | |
GregNash | 11:78a28ca6409f | 367 | int Provision(void) |
GregNash | 11:78a28ca6409f | 368 | { |
GregNash | 11:78a28ca6409f | 369 | // 1x green means entered the provisioning process |
GregNash | 11:78a28ca6409f | 370 | |
GregNash | 11:78a28ca6409f | 371 | debug_pc.printf("\r\n\r\n"); |
GregNash | 11:78a28ca6409f | 372 | |
GregNash | 11:78a28ca6409f | 373 | blink_leds(1); |
GregNash | 11:78a28ca6409f | 374 | debug_pc.printf("Provisioning request detected\r\n"); |
GregNash | 11:78a28ca6409f | 375 | debug_pc.printf("Sending provisioning command...\r\n"); |
GregNash | 11:78a28ca6409f | 376 | skywire.printf("ATD*22899;\r\n"); |
GregNash | 11:78a28ca6409f | 377 | WaitForResponse("OK"); |
GregNash | 11:78a28ca6409f | 378 | |
GregNash | 11:78a28ca6409f | 379 | blink_leds(2); |
GregNash | 11:78a28ca6409f | 380 | debug_pc.printf("Command sent, OK received!\r\n"); |
GregNash | 11:78a28ca6409f | 381 | debug_pc.printf("Waiting for additional responses...\r\n"); |
GregNash | 11:78a28ca6409f | 382 | WaitForResponse("#OTASP: 0"); |
GregNash | 11:78a28ca6409f | 383 | WaitForResponse("#OTASP: 1"); |
GregNash | 11:78a28ca6409f | 384 | WaitForResponse("#OTASP: 2"); |
GregNash | 11:78a28ca6409f | 385 | |
GregNash | 11:78a28ca6409f | 386 | blink_leds(3); |
GregNash | 11:78a28ca6409f | 387 | debug_pc.printf("Waiting for NO CARRIER...\r\n"); |
GregNash | 11:78a28ca6409f | 388 | WaitForResponse("NO CARRIER"); |
GregNash | 11:78a28ca6409f | 389 | |
GregNash | 11:78a28ca6409f | 390 | blink_leds(4); |
GregNash | 11:78a28ca6409f | 391 | debug_pc.printf("\r\nReceived NO CARRIER! Rebooting...\r\n"); |
GregNash | 11:78a28ca6409f | 392 | skywire.printf("AT#REBOOT\r\n"); |
GregNash | 11:78a28ca6409f | 393 | //WaitForResponse("OK", 2); |
GregNash | 11:78a28ca6409f | 394 | |
GregNash | 11:78a28ca6409f | 395 | green_LED = 0; |
GregNash | 11:78a28ca6409f | 396 | red_LED = 0; |
GregNash | 11:78a28ca6409f | 397 | |
GregNash | 11:78a28ca6409f | 398 | // wait 60 seconds for reboot, blink LEDs to indicate something is happening |
GregNash | 11:78a28ca6409f | 399 | wait_print(60); |
GregNash | 11:78a28ca6409f | 400 | |
GregNash | 11:78a28ca6409f | 401 | return 0; |
GregNash | 11:78a28ca6409f | 402 | |
GregNash | 11:78a28ca6409f | 403 | } |
GregNash | 11:78a28ca6409f | 404 | |
kholland | 10:5974a7b2cf38 | 405 | int main() |
kholland | 0:3095958bc639 | 406 | { |
GregNash | 11:78a28ca6409f | 407 | |
GregNash | 11:78a28ca6409f | 408 | |
GregNash | 11:78a28ca6409f | 409 | // First, check if the button is being held, set flag |
GregNash | 11:78a28ca6409f | 410 | // and turn on green LED |
GregNash | 11:78a28ca6409f | 411 | if (button1 == 0) { |
GregNash | 11:78a28ca6409f | 412 | prov_flag = 1; |
GregNash | 11:78a28ca6409f | 413 | green_LED = 1; |
GregNash | 11:78a28ca6409f | 414 | red_LED = 0; |
GregNash | 11:78a28ca6409f | 415 | } else { |
GregNash | 11:78a28ca6409f | 416 | green_LED = 0; |
GregNash | 11:78a28ca6409f | 417 | red_LED = 0; |
GregNash | 11:78a28ca6409f | 418 | } |
GregNash | 11:78a28ca6409f | 419 | |
GregNash | 11:78a28ca6409f | 420 | debug_pc.printf("Prov Flag: %d\r\n", prov_flag); |
GregNash | 11:78a28ca6409f | 421 | |
GregNash | 11:78a28ca6409f | 422 | red_LED = 1; |
GregNash | 11:78a28ca6409f | 423 | |
kholland | 0:3095958bc639 | 424 | float temp; |
kylerodgers | 7:00d66e743603 | 425 | float humi; |
GregNash | 11:78a28ca6409f | 426 | float photo; |
kholland | 0:3095958bc639 | 427 | |
kholland | 10:5974a7b2cf38 | 428 | // Setup serial comms with Skywire and PC |
kholland | 0:3095958bc639 | 429 | debug_pc.baud(115200); |
kholland | 0:3095958bc639 | 430 | skywire.baud(115200); |
kholland | 10:5974a7b2cf38 | 431 | debug_pc.printf("SystemCoreClock = %d Hz\r\n", SystemCoreClock); |
kholland | 3:4b2176f6474c | 432 | skywire.attach(&Skywire_Rx_interrupt, Serial::RxIrq); |
kholland | 10:5974a7b2cf38 | 433 | |
GregNash | 11:78a28ca6409f | 434 | //skywire_rts=0; |
GregNash | 11:78a28ca6409f | 435 | |
GregNash | 11:78a28ca6409f | 436 | // On the NL-SW-LTE-GELS3, flow control is required for consistent results |
GregNash | 11:78a28ca6409f | 437 | //pin_function(skywire_rts, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF7_USART1)); |
GregNash | 11:78a28ca6409f | 438 | pin_function(skywire_rts, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF7_USART1)); |
kholland | 10:5974a7b2cf38 | 439 | |
GregNash | 11:78a28ca6409f | 440 | UART_HandleTypeDef handle; |
GregNash | 11:78a28ca6409f | 441 | handle.Instance = (USART_TypeDef *)USART1_BASE; |
GregNash | 11:78a28ca6409f | 442 | __HAL_USART_DISABLE(&handle); |
GregNash | 11:78a28ca6409f | 443 | |
GregNash | 11:78a28ca6409f | 444 | __HAL_UART_HWCONTROL_CTS_ENABLE(&handle); |
GregNash | 11:78a28ca6409f | 445 | |
GregNash | 11:78a28ca6409f | 446 | __HAL_UART_HWCONTROL_RTS_ENABLE(&handle); |
GregNash | 11:78a28ca6409f | 447 | __HAL_USART_ENABLE(&handle); |
GregNash | 11:78a28ca6409f | 448 | |
kholland | 0:3095958bc639 | 449 | myled=0; |
GregNash | 11:78a28ca6409f | 450 | blink_leds(30); |
kholland | 3:4b2176f6474c | 451 | debug_pc.printf("Starting Demo...\r\n"); |
kholland | 3:4b2176f6474c | 452 | debug_pc.printf("Waiting for Skywire to Boot...\r\n"); |
GregNash | 11:78a28ca6409f | 453 | green_LED = !green_LED; |
kholland | 10:5974a7b2cf38 | 454 | |
GregNash | 11:78a28ca6409f | 455 | // while(!bootSkywire()){ |
GregNash | 11:78a28ca6409f | 456 | // debug_pc.printf("Waiting for Skywire to Boot...\r\n"); |
GregNash | 11:78a28ca6409f | 457 | // |
GregNash | 11:78a28ca6409f | 458 | // } |
GregNash | 11:78a28ca6409f | 459 | // |
GregNash | 11:78a28ca6409f | 460 | // skywire.printf("AT\r"); |
GregNash | 11:78a28ca6409f | 461 | // WaitForResponse("OK"); |
kholland | 3:4b2176f6474c | 462 | |
kholland | 3:4b2176f6474c | 463 | myled=1; |
GregNash | 11:78a28ca6409f | 464 | |
GregNash | 11:78a28ca6409f | 465 | blink_leds(1); |
GregNash | 11:78a28ca6409f | 466 | // Wait time is different for each modem, so wait up to one minute |
GregNash | 11:78a28ca6409f | 467 | // wait_print(10); |
kholland | 3:4b2176f6474c | 468 | |
GregNash | 11:78a28ca6409f | 469 | // Start temp reading/////////////////////////////////////////////////////////////// |
kholland | 10:5974a7b2cf38 | 470 | |
GregNash | 11:78a28ca6409f | 471 | // Turn off echo |
kholland | 10:5974a7b2cf38 | 472 | // Helps with checking responses from Skywire |
kholland | 10:5974a7b2cf38 | 473 | debug_pc.printf("Turning off echo...\r\n"); |
GregNash | 11:78a28ca6409f | 474 | skywire.printf("ATE0\r"); |
GregNash | 11:78a28ca6409f | 475 | WaitForResponse("OK"); |
GregNash | 11:78a28ca6409f | 476 | green_LED = !green_LED; |
GregNash | 11:78a28ca6409f | 477 | |
GregNash | 11:78a28ca6409f | 478 | // Get the modem from the Skywire, and set MODEM |
GregNash | 11:78a28ca6409f | 479 | getSkywireModel(); |
GregNash | 11:78a28ca6409f | 480 | |
GregNash | 11:78a28ca6409f | 481 | // If you have a CDMA-based 2G or 3G Skywire, and you pressed the provision button, |
GregNash | 11:78a28ca6409f | 482 | // provision the modem |
GregNash | 11:78a28ca6409f | 483 | if ((MODEM == NL_SW_1xRTT_V || MODEM == NL_SW_EVDO_V) && prov_flag == 1) { |
GregNash | 11:78a28ca6409f | 484 | Provision(); |
GregNash | 11:78a28ca6409f | 485 | } |
kholland | 10:5974a7b2cf38 | 486 | |
GregNash | 11:78a28ca6409f | 487 | // Debug stuff - get signal quality |
GregNash | 11:78a28ca6409f | 488 | |
GregNash | 11:78a28ca6409f | 489 | debug_pc.printf("Getting CSQ...\r\n"); |
GregNash | 11:78a28ca6409f | 490 | GetCSQResponse(); |
GregNash | 11:78a28ca6409f | 491 | green_LED = !green_LED; |
GregNash | 11:78a28ca6409f | 492 | while(csq_val==99 || csq_val==0) |
GregNash | 11:78a28ca6409f | 493 | { |
GregNash | 11:78a28ca6409f | 494 | |
GregNash | 11:78a28ca6409f | 495 | debug_pc.printf("CSQ Value: %i\r\n",csq_val); |
GregNash | 11:78a28ca6409f | 496 | debug_pc.printf("No network sginal detected. \r\n"); |
GregNash | 11:78a28ca6409f | 497 | debug_pc.printf("Waiting for device to connect to the network. \r\n"); |
GregNash | 11:78a28ca6409f | 498 | debug_pc.printf("Please check antenna connections if network is not found after 30 seconds. \r\n"); |
GregNash | 11:78a28ca6409f | 499 | wait(1); |
GregNash | 11:78a28ca6409f | 500 | //add elapsed time |
GregNash | 11:78a28ca6409f | 501 | debug_pc.printf("Checking network connectrion. \r\n"); |
GregNash | 11:78a28ca6409f | 502 | GetCSQResponse(); |
GregNash | 11:78a28ca6409f | 503 | red_LED = !red_LED; |
GregNash | 11:78a28ca6409f | 504 | } |
GregNash | 11:78a28ca6409f | 505 | |
GregNash | 11:78a28ca6409f | 506 | debug_pc.printf("Network detected. Checking authorization...\r\n"); |
GregNash | 11:78a28ca6409f | 507 | |
GregNash | 11:78a28ca6409f | 508 | |
GregNash | 11:78a28ca6409f | 509 | if (MODEM == NL_SW_LTE_TSVG || MODEM == NL_SW_LTE_TNAG) { |
GregNash | 11:78a28ca6409f | 510 | green_LED = !green_LED; |
GregNash | 11:78a28ca6409f | 511 | debug_pc.printf("Sending AT+CGREG...\r\n"); |
GregNash | 11:78a28ca6409f | 512 | skywire.printf("AT+CGREG?\r"); |
GregNash | 11:78a28ca6409f | 513 | } else if (MODEM == NL_SW_LTE_GELS3) { |
GregNash | 11:78a28ca6409f | 514 | green_LED = !green_LED; |
GregNash | 11:78a28ca6409f | 515 | debug_pc.printf("Sending AT+CEREG...\r\n"); |
GregNash | 11:78a28ca6409f | 516 | skywire.printf("AT+CEREG?\r"); |
GregNash | 11:78a28ca6409f | 517 | } else { |
GregNash | 11:78a28ca6409f | 518 | green_LED = !green_LED; |
GregNash | 11:78a28ca6409f | 519 | debug_pc.printf("Sending AT+CREG...\r\n"); |
GregNash | 11:78a28ca6409f | 520 | skywire.printf("AT+CREG?\r"); |
GregNash | 11:78a28ca6409f | 521 | } |
GregNash | 11:78a28ca6409f | 522 | WaitForResponse("OK"); |
GregNash | 11:78a28ca6409f | 523 | |
kholland | 10:5974a7b2cf38 | 524 | // Turn on DNS Response Caching |
kholland | 10:5974a7b2cf38 | 525 | // Used on the Telit-based Skywires |
GregNash | 11:78a28ca6409f | 526 | if (MODEM != NL_SW_LTE_GELS3) { |
GregNash | 11:78a28ca6409f | 527 | green_LED = !green_LED; |
GregNash | 11:78a28ca6409f | 528 | debug_pc.printf("Turning on DNS Cacheing to improve speed...\r\n"); |
GregNash | 11:78a28ca6409f | 529 | skywire.printf("AT#CACHEDNS=1\r"); |
GregNash | 11:78a28ca6409f | 530 | WaitForResponse("OK"); |
GregNash | 11:78a28ca6409f | 531 | } |
kholland | 10:5974a7b2cf38 | 532 | |
kholland | 3:4b2176f6474c | 533 | debug_pc.printf("Connecting to Network...\r\n"); |
kholland | 0:3095958bc639 | 534 | // get IP address |
GregNash | 11:78a28ca6409f | 535 | if (MODEM == NL_SW_LTE_GELS3) { |
GregNash | 11:78a28ca6409f | 536 | green_LED = !green_LED; |
GregNash | 11:78a28ca6409f | 537 | // set CFUN |
GregNash | 11:78a28ca6409f | 538 | debug_pc.printf("Sending AT+CFUN=1\r\n"); |
GregNash | 11:78a28ca6409f | 539 | skywire.printf("AT+CFUN=1\r"); |
GregNash | 11:78a28ca6409f | 540 | WaitForResponse("OK"); |
GregNash | 11:78a28ca6409f | 541 | |
GregNash | 11:78a28ca6409f | 542 | // Setup AT+CGDCONT |
GregNash | 11:78a28ca6409f | 543 | debug_pc.printf("Sending AT+CGDCONT...\r\n"); |
GregNash | 11:78a28ca6409f | 544 | skywire.printf("AT+CGDCONT=3,\"IPV4V6\",\"%s\"\r", APN); |
GregNash | 11:78a28ca6409f | 545 | WaitForResponse("OK"); |
GregNash | 11:78a28ca6409f | 546 | |
GregNash | 11:78a28ca6409f | 547 | // Turn on AT+CGATT |
GregNash | 11:78a28ca6409f | 548 | debug_pc.printf("Sending AT+CGATT=1\r\n"); |
GregNash | 11:78a28ca6409f | 549 | skywire.printf("AT+CGATT=1\r"); |
GregNash | 11:78a28ca6409f | 550 | WaitForResponse("OK"); |
GregNash | 11:78a28ca6409f | 551 | |
GregNash | 11:78a28ca6409f | 552 | debug_pc.printf("AT^SISS=0, srvType...\r\n"); |
GregNash | 11:78a28ca6409f | 553 | skywire.printf("AT^SISS=0,\"srvType\",\"Socket\"\r"); |
GregNash | 11:78a28ca6409f | 554 | WaitForResponse("OK"); |
GregNash | 11:78a28ca6409f | 555 | wait(1); |
GregNash | 11:78a28ca6409f | 556 | debug_pc.printf("AT^SISS=0, conId...\r\n"); |
GregNash | 11:78a28ca6409f | 557 | skywire.printf("AT^SISS=0,\"conId\",3\r"); |
GregNash | 11:78a28ca6409f | 558 | WaitForResponse("OK"); |
GregNash | 11:78a28ca6409f | 559 | wait(1); |
GregNash | 11:78a28ca6409f | 560 | debug_pc.printf("AT^SISS=0,address...\r\n"); |
GregNash | 11:78a28ca6409f | 561 | skywire.printf("AT^SISS=0,\"address\",\"socktcp://dweet.io:80\"\r"); |
GregNash | 11:78a28ca6409f | 562 | //skywire.printf("AT^SISS=0,\"address\",\"socktcp://52.205.164.103:80\"\r"); |
GregNash | 11:78a28ca6409f | 563 | WaitForResponse("OK"); |
GregNash | 11:78a28ca6409f | 564 | wait(1); |
GregNash | 11:78a28ca6409f | 565 | |
GregNash | 11:78a28ca6409f | 566 | // AT^SICA activation |
GregNash | 11:78a28ca6409f | 567 | debug_pc.printf("Activating context...\r\n"); |
GregNash | 11:78a28ca6409f | 568 | skywire.printf("AT^SICA=1,3\r"); |
GregNash | 11:78a28ca6409f | 569 | WaitForResponse("OK"); |
GregNash | 11:78a28ca6409f | 570 | wait(1); |
GregNash | 11:78a28ca6409f | 571 | |
GregNash | 11:78a28ca6409f | 572 | // AT^SICA? query |
GregNash | 11:78a28ca6409f | 573 | debug_pc.printf("AT^SICA?\r\n"); |
GregNash | 11:78a28ca6409f | 574 | skywire.printf("AT^SICA?\r"); |
GregNash | 11:78a28ca6409f | 575 | //WaitForResponse("OK"); |
GregNash | 11:78a28ca6409f | 576 | //wait(1); |
GregNash | 11:78a28ca6409f | 577 | |
GregNash | 11:78a28ca6409f | 578 | } else if (MODEM == NL_SW_LTE_TSVG) { |
GregNash | 11:78a28ca6409f | 579 | green_LED = !green_LED; |
GregNash | 11:78a28ca6409f | 580 | // The last parameter in AT#SCFG sets the timeout if transmit buffer is not full |
GregNash | 11:78a28ca6409f | 581 | // Time is in hundreds of ms: so, a value of 5 = 500 ms |
kholland | 10:5974a7b2cf38 | 582 | debug_pc.printf("Configuring context part 1...\r\n"); |
GregNash | 11:78a28ca6409f | 583 | skywire.printf("AT#SCFG=3,3,300,90,600,5\r"); |
GregNash | 11:78a28ca6409f | 584 | WaitForResponse("OK"); |
GregNash | 11:78a28ca6409f | 585 | green_LED = !green_LED; |
kholland | 10:5974a7b2cf38 | 586 | wait(1); |
kholland | 10:5974a7b2cf38 | 587 | debug_pc.printf("Configuring context part 2...\r\n"); |
GregNash | 11:78a28ca6409f | 588 | skywire.printf("AT+CGDCONT=3,\"IP\",\"%s\"\r", APN); |
GregNash | 11:78a28ca6409f | 589 | WaitForResponse("OK");// |
GregNash | 11:78a28ca6409f | 590 | green_LED = !green_LED; |
GregNash | 11:78a28ca6409f | 591 | // wait(1); |
GregNash | 11:78a28ca6409f | 592 | // debug_pc.printf("Configuring context...\r\n"); |
GregNash | 11:78a28ca6409f | 593 | // skywire.printf("AT#OTAUIDM=0\r", APN); |
GregNash | 11:78a28ca6409f | 594 | // WaitForResponse("#DREL"); |
kholland | 10:5974a7b2cf38 | 595 | wait(1); |
GregNash | 11:78a28ca6409f | 596 | debug_pc.printf("activating context ...\r\n"); |
GregNash | 11:78a28ca6409f | 597 | skywire.printf("AT#SGACT=3,1\r"); |
GregNash | 11:78a28ca6409f | 598 | WaitForResponse("#SGACT"); |
GregNash | 11:78a28ca6409f | 599 | green_LED = !green_LED; |
GregNash | 11:78a28ca6409f | 600 | } else if (MODEM == NL_SW_LTE_TNAG || MODEM == NL_SW_LTE_TEUG || MODEM == NL_SW_HSPAP || MODEM == NL_SW_HSPAPG || MODEM == NL_SW_HSPAPE || MODEM == NL_SW_GPRS) { |
GregNash | 11:78a28ca6409f | 601 | debug_pc.printf("Configuring context part 1...\r\n"); |
GregNash | 11:78a28ca6409f | 602 | skywire.printf("AT#SCFG=1,1,300,90,600,5\r"); |
GregNash | 11:78a28ca6409f | 603 | WaitForResponse("OK"); |
GregNash | 11:78a28ca6409f | 604 | green_LED = !green_LED; |
GregNash | 11:78a28ca6409f | 605 | wait(1); |
GregNash | 11:78a28ca6409f | 606 | debug_pc.printf("Configuring context part 2...\r\n"); |
GregNash | 11:78a28ca6409f | 607 | skywire.printf("AT+CGDCONT=1,\"IP\",\"%s\"\r", APN); |
GregNash | 11:78a28ca6409f | 608 | WaitForResponse("OK"); |
GregNash | 11:78a28ca6409f | 609 | green_LED = !green_LED; |
GregNash | 11:78a28ca6409f | 610 | wait(1); |
GregNash | 11:78a28ca6409f | 611 | skywire.printf("AT#SGACT=1,1\r"); |
GregNash | 11:78a28ca6409f | 612 | WaitForResponse("#SGACT"); |
GregNash | 11:78a28ca6409f | 613 | green_LED = !green_LED; |
GregNash | 11:78a28ca6409f | 614 | } else { |
kholland | 10:5974a7b2cf38 | 615 | // The last parameter in AT#SCFG sets the timeout if transmit buffer is not full |
kholland | 10:5974a7b2cf38 | 616 | // Time is in hundreds of ms: so, a value of 5 = 500 ms |
GregNash | 11:78a28ca6409f | 617 | skywire.printf("AT#SCFG=1,1,300,90,600,5\r"); |
GregNash | 11:78a28ca6409f | 618 | green_LED = !green_LED; |
GregNash | 11:78a28ca6409f | 619 | WaitForResponse("OK"); |
GregNash | 11:78a28ca6409f | 620 | green_LED = !green_LED; |
GregNash | 11:78a28ca6409f | 621 | skywire.printf("AT#SGACT=1,1\r"); |
GregNash | 11:78a28ca6409f | 622 | WaitForResponse("#SGACT"); |
GregNash | 11:78a28ca6409f | 623 | green_LED = !green_LED; |
GregNash | 11:78a28ca6409f | 624 | } |
GregNash | 11:78a28ca6409f | 625 | WaitForResponse("OK"); |
kylerodgers | 7:00d66e743603 | 626 | |
GregNash | 11:78a28ca6409f | 627 | green_LED = !green_LED; |
kholland | 10:5974a7b2cf38 | 628 | // Get triangulation data |
kholland | 10:5974a7b2cf38 | 629 | // NOTE: This only works on the below modems! |
GregNash | 11:78a28ca6409f | 630 | if (MODEM == NL_SW_1xRTT_V || MODEM == NL_SW_1xRTT_S || MODEM == NL_SW_1xRTT_A || MODEM == NL_SW_GPRS || MODEM == NL_SW_EVDO_V || MODEM == NL_SW_EVDO_A || MODEM == NL_SW_HSPAP || MODEM == NL_SW_HSPAPG || MODEM == NL_SW_HSPAPE) { |
kholland | 10:5974a7b2cf38 | 631 | //get location approximation from cell tower information |
GregNash | 11:78a28ca6409f | 632 | skywire.printf("AT#AGPSSND\r"); |
GregNash | 11:78a28ca6409f | 633 | WaitForResponse("#AGPSRING:"); |
kholland | 10:5974a7b2cf38 | 634 | sscanf(rx_line, "%s %d,%f,%f,", str, &number, &latitude, &longitude); |
kholland | 10:5974a7b2cf38 | 635 | debug_pc.printf("Location: Latt:%f, Long:%f\r\n", latitude, longitude); |
GregNash | 11:78a28ca6409f | 636 | } |
kholland | 10:5974a7b2cf38 | 637 | |
GregNash | 11:78a28ca6409f | 638 | red_LED = 1; |
GregNash | 11:78a28ca6409f | 639 | green_LED = 0; |
GregNash | 11:78a28ca6409f | 640 | |
GregNash | 11:78a28ca6409f | 641 | char post[256]; |
kholland | 3:4b2176f6474c | 642 | |
kholland | 0:3095958bc639 | 643 | while(1) { |
kholland | 10:5974a7b2cf38 | 644 | // Green on to indicate code position |
kholland | 10:5974a7b2cf38 | 645 | // Start of loop. Either entered loop for the first time or just sent to dweet.io |
kholland | 10:5974a7b2cf38 | 646 | red_LED = 0; |
kholland | 10:5974a7b2cf38 | 647 | green_LED = 1; |
kholland | 10:5974a7b2cf38 | 648 | // connect to dweet.io |
GregNash | 11:78a28ca6409f | 649 | if (MODEM == NL_SW_LTE_GELS3) { |
GregNash | 11:78a28ca6409f | 650 | debug_pc.printf("Opening socket...\r\n"); |
GregNash | 11:78a28ca6409f | 651 | skywire.printf("AT^SISO=0\r"); |
GregNash | 11:78a28ca6409f | 652 | WaitForResponse("OK"); |
GregNash | 11:78a28ca6409f | 653 | } |
GregNash | 11:78a28ca6409f | 654 | else if (MODEM == NL_SW_LTE_TSVG) { |
GregNash | 11:78a28ca6409f | 655 | skywire.printf("AT#SD=3,0,80,\"dweet.io\"\r"); |
GregNash | 11:78a28ca6409f | 656 | WaitForResponse("CONNECT"); |
GregNash | 11:78a28ca6409f | 657 | } |
GregNash | 11:78a28ca6409f | 658 | else { |
GregNash | 11:78a28ca6409f | 659 | skywire.printf("AT#SD=1,0,80,\"dweet.io\"\r"); |
GregNash | 11:78a28ca6409f | 660 | WaitForResponse("CONNECT"); |
GregNash | 11:78a28ca6409f | 661 | } |
GregNash | 11:78a28ca6409f | 662 | |
kholland | 10:5974a7b2cf38 | 663 | // Update the sensors |
GregNash | 11:78a28ca6409f | 664 | // temp = (float)LM75_temp; |
kholland | 10:5974a7b2cf38 | 665 | //temp = (temp * 9)/5 + 32; // convert C to F |
GregNash | 11:78a28ca6409f | 666 | // debug_pc.printf("Temp = %.3f\r\n", temp); |
GregNash | 11:78a28ca6409f | 667 | // press=(float)pressure.value() / 4096; |
GregNash | 11:78a28ca6409f | 668 | // debug_pc.printf("Pressure = %.3f\r\n", press); |
GregNash | 11:78a28ca6409f | 669 | // humidity.ReadTempHumi(&dummy_temp, &humi); |
GregNash | 11:78a28ca6409f | 670 | //debug_pc.printf("Humidity = %.3f\r\n", humi); |
GregNash | 11:78a28ca6409f | 671 | temp=SHT30.cTemp(); |
GregNash | 11:78a28ca6409f | 672 | humi=SHT30.humidity(); |
GregNash | 11:78a28ca6409f | 673 | photo_trans_nEN=0; |
GregNash | 11:78a28ca6409f | 674 | photo=photo_trans*200; |
kholland | 10:5974a7b2cf38 | 675 | wait(1); |
kholland | 10:5974a7b2cf38 | 676 | |
kholland | 10:5974a7b2cf38 | 677 | // Check buttons for presses |
kholland | 10:5974a7b2cf38 | 678 | if (button1 == 0) |
kholland | 10:5974a7b2cf38 | 679 | sw1 = 0; |
kholland | 10:5974a7b2cf38 | 680 | else |
kholland | 10:5974a7b2cf38 | 681 | sw1 = 1; |
kholland | 3:4b2176f6474c | 682 | |
kholland | 10:5974a7b2cf38 | 683 | // Green on to indicate code position: |
kholland | 10:5974a7b2cf38 | 684 | // Sensors updated, have not sent to dweet.io |
kholland | 10:5974a7b2cf38 | 685 | red_LED = 1; |
kholland | 10:5974a7b2cf38 | 686 | green_LED = 0; |
kholland | 10:5974a7b2cf38 | 687 | |
GregNash | 11:78a28ca6409f | 688 | if (MODEM == NL_SW_LTE_GELS3) { |
GregNash | 11:78a28ca6409f | 689 | sprintf(post, "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 | 690 | debug_pc.printf("Writing...\r\n"); |
GregNash | 11:78a28ca6409f | 691 | skywire.printf("AT^SISW=0,%d\r", strlen(post)); |
GregNash | 11:78a28ca6409f | 692 | WaitForResponse("^SISW:"); |
GregNash | 11:78a28ca6409f | 693 | debug_pc.printf("Sending information...\r\n"); |
GregNash | 11:78a28ca6409f | 694 | skywire.printf("%s", post); |
GregNash | 11:78a28ca6409f | 695 | WaitForResponse("^SISR:"); |
GregNash | 11:78a28ca6409f | 696 | debug_pc.printf("Reading...\r\n"); |
GregNash | 11:78a28ca6409f | 697 | skywire.printf("AT^SISR=0,1000\r"); |
GregNash | 11:78a28ca6409f | 698 | WaitForResponse("OK"); |
GregNash | 11:78a28ca6409f | 699 | skywire.printf("AT^SISC=0\r"); |
GregNash | 11:78a28ca6409f | 700 | WaitForResponse("OK"); |
GregNash | 11:78a28ca6409f | 701 | } |
GregNash | 11:78a28ca6409f | 702 | else { |
GregNash | 11:78a28ca6409f | 703 | debug_pc.printf("Sending information...\r\n"); |
GregNash | 11:78a28ca6409f | 704 | // Report the sensor data to dweet.io |
GregNash | 11:78a28ca6409f | 705 | 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 | 706 | WaitForResponse("NO CARRIER"); |
GregNash | 11:78a28ca6409f | 707 | } |
GregNash | 11:78a28ca6409f | 708 | |
GregNash | 11:78a28ca6409f | 709 | // green on to indicate code position |
kholland | 10:5974a7b2cf38 | 710 | // Data sent to dweet |
kholland | 10:5974a7b2cf38 | 711 | red_LED = 0; |
GregNash | 11:78a28ca6409f | 712 | green_LED = 1; |
GregNash | 11:78a28ca6409f | 713 | |
GregNash | 11:78a28ca6409f | 714 | |
kholland | 3:4b2176f6474c | 715 | } |
GregNash | 11:78a28ca6409f | 716 | } |