demo1

Dependencies:   SHT30-DIS-B WakeUp mbed

Fork of M1DK_Skywire_Demo by NimbeLink

Committer:
kholland
Date:
Tue Mar 03 18:23:30 2015 +0000
Revision:
5:977fa2ac1589
Parent:
4:e7a79d3542e1
Child:
7:00d66e743603
Updated mbed library

Who changed what in which revision?

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