demo1
Dependencies: SHT30-DIS-B WakeUp mbed
Fork of M1DK_Skywire_Demo by
main.cpp@8:060eaa992407, 2015-07-29 (annotated)
- Committer:
- kylerodgers
- Date:
- Wed Jul 29 16:11:41 2015 +0000
- Revision:
- 8:060eaa992407
- Parent:
- 7:00d66e743603
- Child:
- 9:555eb4259fc6
Added code for humidity sensor.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kholland | 4:e7a79d3542e1 | 1 | /* main.cpp */ |
kylerodgers | 8:060eaa992407 | 2 | /* v1.1 |
kylerodgers | 8:060eaa992407 | 3 | * Copyright (C) 2015 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 | 0:3095958bc639 | 21 | #include "mbed.h" |
kholland | 0:3095958bc639 | 22 | #include "LPS331.h" |
kholland | 0:3095958bc639 | 23 | #include "LIS3DH.h" |
kholland | 0:3095958bc639 | 24 | #include "LM75B.h" |
kylerodgers | 7:00d66e743603 | 25 | #include "hts221.h" |
kholland | 0:3095958bc639 | 26 | |
kholland | 0:3095958bc639 | 27 | /* --CHANGE THIS FOR YOUR SETUP" -- */ |
kylerodgers | 7:00d66e743603 | 28 | #define DeviceID "A1000032BD4DAF" //Freeboard DweetIO unique ID |
kholland | 0:3095958bc639 | 29 | |
kholland | 0:3095958bc639 | 30 | DigitalOut myled(LED1); |
kholland | 0:3095958bc639 | 31 | DigitalOut skywire_en(PA_6); |
kholland | 0:3095958bc639 | 32 | DigitalOut skywire_rts(PA_7); |
kholland | 0:3095958bc639 | 33 | |
kholland | 0:3095958bc639 | 34 | Serial skywire(PA_9,PA_10); |
kholland | 0:3095958bc639 | 35 | Serial debug_pc(USBTX, USBRX); |
kholland | 0:3095958bc639 | 36 | |
kholland | 0:3095958bc639 | 37 | I2C i2c(PB_9,PB_8); |
kholland | 0:3095958bc639 | 38 | char msg[3]; |
kholland | 0:3095958bc639 | 39 | |
kholland | 0:3095958bc639 | 40 | LPS331 pressure(i2c); |
kholland | 0:3095958bc639 | 41 | LM75B LM75_temp(PB_9,PB_8); |
kholland | 0:3095958bc639 | 42 | LIS3DH accel(i2c, LIS3DH_V_CHIP_ADDR, LIS3DH_DR_NR_LP_100HZ, LIS3DH_FS_2G); |
kylerodgers | 7:00d66e743603 | 43 | HTS221 humidity(PB_9, PB_8); |
kholland | 0:3095958bc639 | 44 | |
kholland | 0:3095958bc639 | 45 | |
kholland | 3:4b2176f6474c | 46 | char str[255]; |
kholland | 3:4b2176f6474c | 47 | |
kholland | 0:3095958bc639 | 48 | float latitude; |
kholland | 0:3095958bc639 | 49 | float longitude; |
kholland | 0:3095958bc639 | 50 | int number; |
kholland | 0:3095958bc639 | 51 | |
kholland | 3:4b2176f6474c | 52 | volatile int rx_in=0; |
kholland | 3:4b2176f6474c | 53 | volatile int rx_out=0; |
kholland | 3:4b2176f6474c | 54 | const int buffer_size = 255; |
kholland | 3:4b2176f6474c | 55 | char rx_buffer[buffer_size+1]; |
kholland | 3:4b2176f6474c | 56 | |
kholland | 3:4b2176f6474c | 57 | char rx_line[buffer_size]; |
kholland | 3:4b2176f6474c | 58 | |
kholland | 3:4b2176f6474c | 59 | void Skywire_Rx_interrupt() { |
kholland | 3:4b2176f6474c | 60 | // Loop just in case more than one character is in UART's receive FIFO buffer |
kholland | 3:4b2176f6474c | 61 | // Stop if buffer full |
kholland | 3:4b2176f6474c | 62 | while ((skywire.readable()) && (((rx_in + 1) % buffer_size) != rx_out)) { |
kholland | 3:4b2176f6474c | 63 | rx_buffer[rx_in] = skywire.getc(); |
kholland | 3:4b2176f6474c | 64 | rx_in = (rx_in + 1) % buffer_size; |
kholland | 3:4b2176f6474c | 65 | } |
kholland | 3:4b2176f6474c | 66 | return; |
kholland | 3:4b2176f6474c | 67 | } |
kholland | 3:4b2176f6474c | 68 | |
kholland | 3:4b2176f6474c | 69 | void read_line() { |
kholland | 3:4b2176f6474c | 70 | int i; |
kholland | 3:4b2176f6474c | 71 | i = 0; |
kholland | 3:4b2176f6474c | 72 | // Start Critical Section - don't interrupt while changing global buffer variables |
kholland | 3:4b2176f6474c | 73 | __disable_irq(); |
kholland | 3:4b2176f6474c | 74 | // Loop reading rx buffer characters until end of line character |
kholland | 3:4b2176f6474c | 75 | while ((i==0) || (rx_line[i-1] != '\n')) { |
kholland | 3:4b2176f6474c | 76 | // Wait if buffer empty |
kholland | 3:4b2176f6474c | 77 | if (rx_in == rx_out) { |
kholland | 3:4b2176f6474c | 78 | // End Critical Section - need to allow rx interrupt to get new characters for buffer |
kholland | 3:4b2176f6474c | 79 | __enable_irq(); |
kholland | 3:4b2176f6474c | 80 | while (rx_in == rx_out) { |
kholland | 3:4b2176f6474c | 81 | } |
kholland | 3:4b2176f6474c | 82 | // Start Critical Section - don't interrupt while changing global buffer variables |
kholland | 3:4b2176f6474c | 83 | __disable_irq(); |
kholland | 3:4b2176f6474c | 84 | } |
kholland | 3:4b2176f6474c | 85 | rx_line[i] = rx_buffer[rx_out]; |
kholland | 3:4b2176f6474c | 86 | i++; |
kholland | 3:4b2176f6474c | 87 | rx_out = (rx_out + 1) % buffer_size; |
kholland | 3:4b2176f6474c | 88 | } |
kholland | 3:4b2176f6474c | 89 | // End Critical Section |
kholland | 3:4b2176f6474c | 90 | __enable_irq(); |
kholland | 3:4b2176f6474c | 91 | rx_line[i-1] = 0; |
kholland | 3:4b2176f6474c | 92 | return; |
kholland | 3:4b2176f6474c | 93 | } |
kholland | 3:4b2176f6474c | 94 | int WaitForResponse(char* response, int num) { |
kholland | 3:4b2176f6474c | 95 | do { |
kholland | 3:4b2176f6474c | 96 | read_line(); |
kholland | 3:4b2176f6474c | 97 | debug_pc.printf("Waiting for: %s, Recieved: %s\r\n", response, rx_line); |
kholland | 3:4b2176f6474c | 98 | } while (strncmp(rx_line, response, num)); |
kholland | 3:4b2176f6474c | 99 | return 0; |
kholland | 3:4b2176f6474c | 100 | } |
kholland | 0:3095958bc639 | 101 | int main() |
kholland | 0:3095958bc639 | 102 | { |
kholland | 0:3095958bc639 | 103 | float axis[3]; |
kholland | 0:3095958bc639 | 104 | float press; |
kholland | 0:3095958bc639 | 105 | float temp; |
kylerodgers | 7:00d66e743603 | 106 | float humi; |
kylerodgers | 7:00d66e743603 | 107 | float dummy_temp; |
kholland | 0:3095958bc639 | 108 | |
kholland | 3:4b2176f6474c | 109 | |
kholland | 0:3095958bc639 | 110 | debug_pc.baud(115200); |
kholland | 0:3095958bc639 | 111 | skywire.baud(115200); |
kholland | 5:977fa2ac1589 | 112 | debug_pc.printf("SystemCoreClock = %d Hz\n", SystemCoreClock); |
kholland | 5:977fa2ac1589 | 113 | |
kholland | 3:4b2176f6474c | 114 | skywire.attach(&Skywire_Rx_interrupt, Serial::RxIrq); |
kholland | 0:3095958bc639 | 115 | skywire_rts=0; |
kholland | 0:3095958bc639 | 116 | myled=0; |
kholland | 3:4b2176f6474c | 117 | debug_pc.printf("Starting Demo...\r\n"); |
kholland | 3:4b2176f6474c | 118 | debug_pc.printf("Waiting for Skywire to Boot...\r\n"); |
kholland | 0:3095958bc639 | 119 | //Enable Skywire |
kholland | 0:3095958bc639 | 120 | skywire_en=0; |
kholland | 0:3095958bc639 | 121 | wait(2); |
kholland | 0:3095958bc639 | 122 | skywire_en=1; |
kholland | 0:3095958bc639 | 123 | wait(2); |
kholland | 0:3095958bc639 | 124 | skywire_en=0; |
kholland | 3:4b2176f6474c | 125 | |
kholland | 3:4b2176f6474c | 126 | myled=1; |
kholland | 0:3095958bc639 | 127 | wait(5); |
kholland | 3:4b2176f6474c | 128 | |
kholland | 3:4b2176f6474c | 129 | LM75_temp.open(); |
kholland | 0:3095958bc639 | 130 | |
kholland | 3:4b2176f6474c | 131 | //Turn off echo |
kholland | 3:4b2176f6474c | 132 | skywire.printf("ATE0\r\n"); |
kholland | 3:4b2176f6474c | 133 | WaitForResponse("OK", 2); |
kholland | 3:4b2176f6474c | 134 | |
kholland | 3:4b2176f6474c | 135 | debug_pc.printf("Connecting to Network...\r\n"); |
kholland | 0:3095958bc639 | 136 | // get IP address |
kholland | 3:4b2176f6474c | 137 | skywire.printf("AT#SGACT=1,1\r\n"); |
kholland | 3:4b2176f6474c | 138 | WaitForResponse("#SGACT", 6); |
kholland | 3:4b2176f6474c | 139 | WaitForResponse("OK", 2); |
kholland | 0:3095958bc639 | 140 | |
kholland | 0:3095958bc639 | 141 | // connect to dweet.io |
kholland | 3:4b2176f6474c | 142 | skywire.printf("AT#HTTPCFG=1,\"dweet.io\",80,0\r\n"); |
kholland | 3:4b2176f6474c | 143 | WaitForResponse("OK", 2); |
kylerodgers | 7:00d66e743603 | 144 | |
kholland | 0:3095958bc639 | 145 | |
kholland | 0:3095958bc639 | 146 | //get location approximation from cell tower information |
kholland | 0:3095958bc639 | 147 | skywire.printf("AT#AGPSSND\r\n"); |
kholland | 3:4b2176f6474c | 148 | WaitForResponse("#AGPSRING:", 10); |
kholland | 3:4b2176f6474c | 149 | |
kholland | 3:4b2176f6474c | 150 | //debug_pc.printf("Skywire says: %s\r\n", rx_line); |
kholland | 3:4b2176f6474c | 151 | sscanf(rx_line, "%s %d,%f,%f,", str, &number, &latitude, &longitude); |
kholland | 3:4b2176f6474c | 152 | debug_pc.printf("Location: Latt:%f, Long:%f\r\n", latitude, longitude); |
kylerodgers | 7:00d66e743603 | 153 | |
kholland | 3:4b2176f6474c | 154 | wait(3); |
kholland | 3:4b2176f6474c | 155 | |
kholland | 0:3095958bc639 | 156 | while(1) { |
kholland | 0:3095958bc639 | 157 | temp = (float)LM75_temp; |
kholland | 3:4b2176f6474c | 158 | debug_pc.printf("Temp = %.3f\r\n", temp); |
kholland | 0:3095958bc639 | 159 | press=(float)pressure.value() / 4096; |
kholland | 3:4b2176f6474c | 160 | debug_pc.printf("Pressure = %.3f\r\n", press); |
kylerodgers | 7:00d66e743603 | 161 | humidity.ReadTempHumi(&dummy_temp, &humi); |
kylerodgers | 7:00d66e743603 | 162 | debug_pc.printf("Humidity = %.3f\r\n", humi); |
kholland | 0:3095958bc639 | 163 | accel.read_data(axis); |
kholland | 3:4b2176f6474c | 164 | debug_pc.printf("Accel = %.3f, %.3f, %.3f\r\n", axis[0], axis[1], axis[2]); |
kylerodgers | 7:00d66e743603 | 165 | //wait(10); |
kholland | 3:4b2176f6474c | 166 | |
kholland | 0:3095958bc639 | 167 | //Report Sensor Data to dweet.io |
kylerodgers | 7:00d66e743603 | 168 | 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); |
kholland | 3:4b2176f6474c | 169 | WaitForResponse("#HTTPRING", 9); |
kholland | 0:3095958bc639 | 170 | skywire.printf("AT#HTTPRCV=1\r\n"); |
kholland | 3:4b2176f6474c | 171 | WaitForResponse("OK", 2); |
kholland | 0:3095958bc639 | 172 | wait(5); |
kholland | 3:4b2176f6474c | 173 | } |
kholland | 0:3095958bc639 | 174 | |
kholland | 0:3095958bc639 | 175 | } |