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