![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
FRDM-K64F, Avnet M14A2A, Grove Shield, to create smart home system. In use with AT&Ts M2x & Flow.
Dependencies: mbed FXOS8700CQ MODSERIAL
main.cpp@85:0cf65ceb4492, 2019-04-29 (annotated)
- Committer:
- jwhammel
- Date:
- Mon Apr 29 04:24:38 2019 +0000
- Revision:
- 85:0cf65ceb4492
- Parent:
- 84:fc8c9b39723a
We have added an alarm trigger that gets sent to AT&T Flow.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
fkellermavnet | 68:6e311c747045 | 1 | /* =================================================================== |
fkellermavnet | 68:6e311c747045 | 2 | Copyright © 2016, AVNET Inc. |
fkellermavnet | 68:6e311c747045 | 3 | |
fkellermavnet | 68:6e311c747045 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
fkellermavnet | 68:6e311c747045 | 5 | you may not use this file except in compliance with the License. |
fkellermavnet | 68:6e311c747045 | 6 | You may obtain a copy of the License at |
fkellermavnet | 68:6e311c747045 | 7 | |
fkellermavnet | 68:6e311c747045 | 8 | http://www.apache.org/licenses/LICENSE-2.0 |
fkellermavnet | 68:6e311c747045 | 9 | |
fkellermavnet | 68:6e311c747045 | 10 | Unless required by applicable law or agreed to in writing, |
fkellermavnet | 68:6e311c747045 | 11 | software distributed under the License is distributed on an |
fkellermavnet | 68:6e311c747045 | 12 | "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, |
fkellermavnet | 68:6e311c747045 | 13 | either express or implied. See the License for the specific |
fkellermavnet | 68:6e311c747045 | 14 | language governing permissions and limitations under the License. |
fkellermavnet | 68:6e311c747045 | 15 | |
fkellermavnet | 68:6e311c747045 | 16 | ======================================================================== */ |
fkellermavnet | 68:6e311c747045 | 17 | |
stefanrousseau | 55:3abf9e3f42e6 | 18 | #include "mbed.h" |
JMF | 0:9d5134074d84 | 19 | #include <cctype> |
JMF | 0:9d5134074d84 | 20 | #include <string> |
JMF | 2:0e2ef866af95 | 21 | #include "config_me.h" |
stefanrousseau | 4:f83bedd9cab4 | 22 | #include "sensors.h" |
stefanrousseau | 61:f6b93129f954 | 23 | #include "cell_modem.h" |
stefanrousseau | 11:e6602513730f | 24 | #include "hardware.h" |
stefanrousseau | 11:e6602513730f | 25 | |
jwhammel | 84:fc8c9b39723a | 26 | #include "DHT.h" |
jwhammel | 84:fc8c9b39723a | 27 | float current_temp = 0.0f, rel_humid = 0.0f; |
jwhammel | 84:fc8c9b39723a | 28 | |
stefanrousseau | 61:f6b93129f954 | 29 | I2C i2c(PTC11, PTC10); //SDA, SCL -- define the I2C pins being used |
stefanrousseau | 56:cb42ff383dab | 30 | MODSERIAL pc(USBTX, USBRX, 256, 256); // tx, rx with default tx, rx buffer sizes |
stefanrousseau | 63:90d7c69993cd | 31 | MODSERIAL mdm(PTD3, PTD2, 4096, 4096); |
stefanrousseau | 16:17c5916f2d12 | 32 | DigitalOut led_green(LED_GREEN); |
stefanrousseau | 16:17c5916f2d12 | 33 | DigitalOut led_red(LED_RED); |
stefanrousseau | 16:17c5916f2d12 | 34 | DigitalOut led_blue(LED_BLUE); |
JMF | 0:9d5134074d84 | 35 | |
JMF | 0:9d5134074d84 | 36 | |
stefanrousseau | 3:26b3cc155f39 | 37 | //******************************************************************************************************************************************** |
stefanrousseau | 12:7c94ec5069dc | 38 | //* Create string with sensor readings that can be sent to flow as an HTTP get |
stefanrousseau | 3:26b3cc155f39 | 39 | //******************************************************************************************************************************************** |
stefanrousseau | 12:7c94ec5069dc | 40 | K64F_Sensors_t SENSOR_DATA = |
stefanrousseau | 3:26b3cc155f39 | 41 | { |
stefanrousseau | 12:7c94ec5069dc | 42 | .Temperature = "0", |
stefanrousseau | 12:7c94ec5069dc | 43 | .Humidity = "0", |
stefanrousseau | 12:7c94ec5069dc | 44 | .AccelX = "0", |
stefanrousseau | 12:7c94ec5069dc | 45 | .AccelY = "0", |
stefanrousseau | 12:7c94ec5069dc | 46 | .AccelZ = "0", |
stefanrousseau | 12:7c94ec5069dc | 47 | .MagnetometerX = "0", |
stefanrousseau | 12:7c94ec5069dc | 48 | .MagnetometerY = "0", |
stefanrousseau | 12:7c94ec5069dc | 49 | .MagnetometerZ = "0", |
stefanrousseau | 12:7c94ec5069dc | 50 | .AmbientLightVis = "0", |
stefanrousseau | 12:7c94ec5069dc | 51 | .AmbientLightIr = "0", |
stefanrousseau | 12:7c94ec5069dc | 52 | .UVindex = "0", |
stefanrousseau | 12:7c94ec5069dc | 53 | .Proximity = "0", |
stefanrousseau | 12:7c94ec5069dc | 54 | .Temperature_Si7020 = "0", |
stefanrousseau | 55:3abf9e3f42e6 | 55 | .Humidity_Si7020 = "0", |
stefanrousseau | 55:3abf9e3f42e6 | 56 | .Virtual_Sensor1 = "0", |
stefanrousseau | 55:3abf9e3f42e6 | 57 | .Virtual_Sensor2 = "0", |
stefanrousseau | 55:3abf9e3f42e6 | 58 | .Virtual_Sensor3 = "0", |
stefanrousseau | 55:3abf9e3f42e6 | 59 | .Virtual_Sensor4 = "0", |
stefanrousseau | 55:3abf9e3f42e6 | 60 | .Virtual_Sensor5 = "0", |
stefanrousseau | 55:3abf9e3f42e6 | 61 | .Virtual_Sensor6 = "0", |
stefanrousseau | 55:3abf9e3f42e6 | 62 | .Virtual_Sensor7 = "0", |
stefanrousseau | 71:45a5e426df81 | 63 | .Virtual_Sensor8 = "0", |
stefanrousseau | 72:b500e1507b5f | 64 | .GPS_Satellites = "0", |
stefanrousseau | 71:45a5e426df81 | 65 | .GPS_Latitude = "0", |
stefanrousseau | 71:45a5e426df81 | 66 | .GPS_Longitude = "0", |
stefanrousseau | 71:45a5e426df81 | 67 | .GPS_Altitude = "0", |
stefanrousseau | 71:45a5e426df81 | 68 | .GPS_Speed = "0", |
stefanrousseau | 71:45a5e426df81 | 69 | .GPS_Course = "0" |
stefanrousseau | 3:26b3cc155f39 | 70 | }; |
stefanrousseau | 12:7c94ec5069dc | 71 | |
fkellermavnet | 77:c65eae5b9958 | 72 | void display_app_firmware_version(void) |
fkellermavnet | 77:c65eae5b9958 | 73 | { |
fkellermavnet | 77:c65eae5b9958 | 74 | PUTS("\r\n\r\nApp Firmware: Release 1.0 - built: "__DATE__" "__TIME__"\r\n\r\n"); |
fkellermavnet | 77:c65eae5b9958 | 75 | } |
fkellermavnet | 77:c65eae5b9958 | 76 | |
stefanrousseau | 3:26b3cc155f39 | 77 | void GenerateModemString(char * modem_string) |
stefanrousseau | 3:26b3cc155f39 | 78 | { |
stefanrousseau | 12:7c94ec5069dc | 79 | switch(iSensorsToReport) |
stefanrousseau | 12:7c94ec5069dc | 80 | { |
stefanrousseau | 12:7c94ec5069dc | 81 | case TEMP_HUMIDITY_ONLY: |
stefanrousseau | 12:7c94ec5069dc | 82 | { |
stefanrousseau | 12:7c94ec5069dc | 83 | sprintf(modem_string, "GET %s%s?serial=%s&temp=%s&humidity=%s %s%s\r\n\r\n", FLOW_BASE_URL, FLOW_INPUT_NAME, FLOW_DEVICE_NAME, SENSOR_DATA.Temperature, SENSOR_DATA.Humidity, FLOW_URL_TYPE, MY_SERVER_URL); |
stefanrousseau | 12:7c94ec5069dc | 84 | break; |
stefanrousseau | 12:7c94ec5069dc | 85 | } |
stefanrousseau | 12:7c94ec5069dc | 86 | case TEMP_HUMIDITY_ACCELEROMETER: |
stefanrousseau | 12:7c94ec5069dc | 87 | { |
jwhammel | 85:0cf65ceb4492 | 88 | sprintf(modem_string, "GET %s%s?serial=%s&temp=%s&humidity=%s&accelX=%s&accelY=%s&accelZ=%s&alarm=%s %s%s\r\n\r\n", FLOW_BASE_URL, FLOW_INPUT_NAME, FLOW_DEVICE_NAME, SENSOR_DATA.Temperature, SENSOR_DATA.Humidity, SENSOR_DATA.AccelX,SENSOR_DATA.AccelY,SENSOR_DATA.AccelZ,SENSOR_DATA.Proximity, FLOW_URL_TYPE, MY_SERVER_URL); |
stefanrousseau | 12:7c94ec5069dc | 89 | break; |
stefanrousseau | 12:7c94ec5069dc | 90 | } |
stefanrousseau | 70:24d5800f27be | 91 | case TEMP_HUMIDITY_ACCELEROMETER_GPS: |
stefanrousseau | 70:24d5800f27be | 92 | { |
stefanrousseau | 72:b500e1507b5f | 93 | sprintf(modem_string, "GET %s%s?serial=%s&temp=%s&humidity=%s&accelX=%s&accelY=%s&accelZ=%s&gps_satellites=%s&latitude=%s&longitude=%s&altitude=%s&speed=%s&course=%s %s%s\r\n\r\n", FLOW_BASE_URL, FLOW_INPUT_NAME, FLOW_DEVICE_NAME, SENSOR_DATA.Temperature, SENSOR_DATA.Humidity, SENSOR_DATA.AccelX,SENSOR_DATA.AccelY,SENSOR_DATA.AccelZ,SENSOR_DATA.GPS_Satellites,SENSOR_DATA.GPS_Latitude,SENSOR_DATA.GPS_Longitude,SENSOR_DATA.GPS_Altitude,SENSOR_DATA.GPS_Speed,SENSOR_DATA.GPS_Course, FLOW_URL_TYPE, MY_SERVER_URL); |
stefanrousseau | 70:24d5800f27be | 94 | break; |
stefanrousseau | 70:24d5800f27be | 95 | } |
stefanrousseau | 12:7c94ec5069dc | 96 | case TEMP_HUMIDITY_ACCELEROMETER_PMODSENSORS: |
stefanrousseau | 12:7c94ec5069dc | 97 | { |
stefanrousseau | 12:7c94ec5069dc | 98 | sprintf(modem_string, "GET %s%s?serial=%s&temp=%s&humidity=%s&accelX=%s&accelY=%s&accelZ=%s&proximity=%s&light_uv=%s&light_vis=%s&light_ir=%s %s%s\r\n\r\n", FLOW_BASE_URL, FLOW_INPUT_NAME, FLOW_DEVICE_NAME, SENSOR_DATA.Temperature, SENSOR_DATA.Humidity, SENSOR_DATA.AccelX,SENSOR_DATA.AccelY,SENSOR_DATA.AccelZ, SENSOR_DATA.Proximity, SENSOR_DATA.UVindex, SENSOR_DATA.AmbientLightVis, SENSOR_DATA.AmbientLightIr, FLOW_URL_TYPE, MY_SERVER_URL); |
stefanrousseau | 12:7c94ec5069dc | 99 | break; |
stefanrousseau | 12:7c94ec5069dc | 100 | } |
stefanrousseau | 55:3abf9e3f42e6 | 101 | case TEMP_HUMIDITY_ACCELEROMETER_PMODSENSORS_VIRTUALSENSORS: |
stefanrousseau | 55:3abf9e3f42e6 | 102 | { |
stefanrousseau | 55:3abf9e3f42e6 | 103 | sprintf(modem_string, "GET %s%s?serial=%s&temp=%s&humidity=%s&accelX=%s&accelY=%s&accelZ=%s&proximity=%s&light_uv=%s&light_vis=%s&light_ir=%s&virt_sens1=%s&virt_sens2=%s&virt_sens3=%s&virt_sens4=%s&virt_sens5=%s&virt_sens6=%s&virt_sens7=%s&virt_sens8=%s %s%s\r\n\r\n", FLOW_BASE_URL, FLOW_INPUT_NAME, FLOW_DEVICE_NAME, SENSOR_DATA.Temperature, SENSOR_DATA.Humidity, SENSOR_DATA.AccelX,SENSOR_DATA.AccelY,SENSOR_DATA.AccelZ, SENSOR_DATA.Proximity, SENSOR_DATA.UVindex, SENSOR_DATA.AmbientLightVis, SENSOR_DATA.AmbientLightIr, SENSOR_DATA.Virtual_Sensor1, SENSOR_DATA.Virtual_Sensor2, SENSOR_DATA.Virtual_Sensor3, SENSOR_DATA.Virtual_Sensor4, SENSOR_DATA.Virtual_Sensor5, SENSOR_DATA.Virtual_Sensor6, SENSOR_DATA.Virtual_Sensor7, SENSOR_DATA.Virtual_Sensor8, FLOW_URL_TYPE, MY_SERVER_URL); |
stefanrousseau | 55:3abf9e3f42e6 | 104 | break; |
stefanrousseau | 55:3abf9e3f42e6 | 105 | } |
stefanrousseau | 12:7c94ec5069dc | 106 | default: |
stefanrousseau | 12:7c94ec5069dc | 107 | { |
stefanrousseau | 12:7c94ec5069dc | 108 | sprintf(modem_string, "Invalid sensor selected\r\n\r\n"); |
stefanrousseau | 12:7c94ec5069dc | 109 | break; |
stefanrousseau | 12:7c94ec5069dc | 110 | } |
stefanrousseau | 16:17c5916f2d12 | 111 | } //switch(iSensorsToReport) |
stefanrousseau | 3:26b3cc155f39 | 112 | } //GenerateModemString |
stefanrousseau | 3:26b3cc155f39 | 113 | |
stefanrousseau | 3:26b3cc155f39 | 114 | |
stefanrousseau | 3:26b3cc155f39 | 115 | //Periodic timer |
stefanrousseau | 3:26b3cc155f39 | 116 | Ticker OneMsTicker; |
stefanrousseau | 3:26b3cc155f39 | 117 | volatile bool bTimerExpiredFlag = false; |
stefanrousseau | 3:26b3cc155f39 | 118 | int OneMsTicks = 0; |
stefanrousseau | 3:26b3cc155f39 | 119 | int iTimer1Interval_ms = 1000; |
stefanrousseau | 3:26b3cc155f39 | 120 | //******************************************************************************************************************************************** |
stefanrousseau | 3:26b3cc155f39 | 121 | //* Periodic 1ms timer tick |
stefanrousseau | 3:26b3cc155f39 | 122 | //******************************************************************************************************************************************** |
stefanrousseau | 3:26b3cc155f39 | 123 | void OneMsFunction() |
stefanrousseau | 3:26b3cc155f39 | 124 | { |
stefanrousseau | 3:26b3cc155f39 | 125 | OneMsTicks++; |
stefanrousseau | 3:26b3cc155f39 | 126 | if ((OneMsTicks % iTimer1Interval_ms) == 0) |
stefanrousseau | 3:26b3cc155f39 | 127 | { |
stefanrousseau | 3:26b3cc155f39 | 128 | bTimerExpiredFlag = true; |
stefanrousseau | 3:26b3cc155f39 | 129 | } |
stefanrousseau | 3:26b3cc155f39 | 130 | } //OneMsFunction() |
stefanrousseau | 3:26b3cc155f39 | 131 | |
stefanrousseau | 16:17c5916f2d12 | 132 | //******************************************************************************************************************************************** |
stefanrousseau | 16:17c5916f2d12 | 133 | //* Set the RGB LED's Color |
stefanrousseau | 16:17c5916f2d12 | 134 | //* LED Color 0=Off to 7=White. 3 bits represent BGR (bit0=Red, bit1=Green, bit2=Blue) |
stefanrousseau | 16:17c5916f2d12 | 135 | //******************************************************************************************************************************************** |
stefanrousseau | 16:17c5916f2d12 | 136 | void SetLedColor(unsigned char ucColor) |
stefanrousseau | 16:17c5916f2d12 | 137 | { |
stefanrousseau | 16:17c5916f2d12 | 138 | //Note that when an LED is on, you write a 0 to it: |
stefanrousseau | 16:17c5916f2d12 | 139 | led_red = !(ucColor & 0x1); //bit 0 |
stefanrousseau | 16:17c5916f2d12 | 140 | led_green = !(ucColor & 0x2); //bit 1 |
stefanrousseau | 16:17c5916f2d12 | 141 | led_blue = !(ucColor & 0x4); //bit 2 |
stefanrousseau | 16:17c5916f2d12 | 142 | } //SetLedColor() |
stefanrousseau | 16:17c5916f2d12 | 143 | |
stefanrousseau | 16:17c5916f2d12 | 144 | //******************************************************************************************************************************************** |
stefanrousseau | 61:f6b93129f954 | 145 | //* Process the JSON response. In this example we are only extracting a LED color. |
stefanrousseau | 16:17c5916f2d12 | 146 | //******************************************************************************************************************************************** |
stefanrousseau | 16:17c5916f2d12 | 147 | bool parse_JSON(char* json_string) |
stefanrousseau | 16:17c5916f2d12 | 148 | { |
stefanrousseau | 16:17c5916f2d12 | 149 | char* beginquote; |
stefanrousseau | 16:17c5916f2d12 | 150 | char token[] = "\"LED\":\""; |
stefanrousseau | 16:17c5916f2d12 | 151 | beginquote = strstr(json_string, token ); |
stefanrousseau | 16:17c5916f2d12 | 152 | if ((beginquote != 0)) |
stefanrousseau | 16:17c5916f2d12 | 153 | { |
stefanrousseau | 16:17c5916f2d12 | 154 | char cLedColor = beginquote[strlen(token)]; |
stefanrousseau | 64:09004cd610df | 155 | PRINTF(GRN "LED Found : %c" DEF "\r\n", cLedColor); |
stefanrousseau | 16:17c5916f2d12 | 156 | switch(cLedColor) |
stefanrousseau | 16:17c5916f2d12 | 157 | { |
stefanrousseau | 16:17c5916f2d12 | 158 | case 'O': |
stefanrousseau | 16:17c5916f2d12 | 159 | { //Off |
stefanrousseau | 16:17c5916f2d12 | 160 | SetLedColor(0); |
stefanrousseau | 16:17c5916f2d12 | 161 | break; |
stefanrousseau | 16:17c5916f2d12 | 162 | } |
stefanrousseau | 16:17c5916f2d12 | 163 | case 'R': |
stefanrousseau | 16:17c5916f2d12 | 164 | { //Red |
stefanrousseau | 16:17c5916f2d12 | 165 | SetLedColor(1); |
stefanrousseau | 16:17c5916f2d12 | 166 | break; |
stefanrousseau | 16:17c5916f2d12 | 167 | } |
stefanrousseau | 16:17c5916f2d12 | 168 | case 'G': |
stefanrousseau | 16:17c5916f2d12 | 169 | { //Green |
stefanrousseau | 16:17c5916f2d12 | 170 | SetLedColor(2); |
stefanrousseau | 16:17c5916f2d12 | 171 | break; |
stefanrousseau | 16:17c5916f2d12 | 172 | } |
stefanrousseau | 16:17c5916f2d12 | 173 | case 'Y': |
stefanrousseau | 16:17c5916f2d12 | 174 | { //Yellow |
stefanrousseau | 16:17c5916f2d12 | 175 | SetLedColor(3); |
stefanrousseau | 16:17c5916f2d12 | 176 | break; |
stefanrousseau | 16:17c5916f2d12 | 177 | } |
stefanrousseau | 16:17c5916f2d12 | 178 | case 'B': |
stefanrousseau | 16:17c5916f2d12 | 179 | { //Blue |
stefanrousseau | 16:17c5916f2d12 | 180 | SetLedColor(4); |
stefanrousseau | 16:17c5916f2d12 | 181 | break; |
stefanrousseau | 16:17c5916f2d12 | 182 | } |
stefanrousseau | 16:17c5916f2d12 | 183 | case 'M': |
stefanrousseau | 16:17c5916f2d12 | 184 | { //Magenta |
stefanrousseau | 16:17c5916f2d12 | 185 | SetLedColor(5); |
stefanrousseau | 16:17c5916f2d12 | 186 | break; |
stefanrousseau | 16:17c5916f2d12 | 187 | } |
stefanrousseau | 16:17c5916f2d12 | 188 | case 'T': |
stefanrousseau | 16:17c5916f2d12 | 189 | { //Turquoise |
stefanrousseau | 16:17c5916f2d12 | 190 | SetLedColor(6); |
stefanrousseau | 16:17c5916f2d12 | 191 | break; |
stefanrousseau | 16:17c5916f2d12 | 192 | } |
stefanrousseau | 16:17c5916f2d12 | 193 | case 'W': |
stefanrousseau | 16:17c5916f2d12 | 194 | { //White |
stefanrousseau | 16:17c5916f2d12 | 195 | SetLedColor(7); |
stefanrousseau | 16:17c5916f2d12 | 196 | break; |
stefanrousseau | 16:17c5916f2d12 | 197 | } |
stefanrousseau | 16:17c5916f2d12 | 198 | default: |
stefanrousseau | 16:17c5916f2d12 | 199 | { |
stefanrousseau | 16:17c5916f2d12 | 200 | break; |
stefanrousseau | 16:17c5916f2d12 | 201 | } |
stefanrousseau | 16:17c5916f2d12 | 202 | } //switch(cLedColor) |
stefanrousseau | 16:17c5916f2d12 | 203 | return true; |
stefanrousseau | 16:17c5916f2d12 | 204 | } |
stefanrousseau | 16:17c5916f2d12 | 205 | else |
stefanrousseau | 16:17c5916f2d12 | 206 | { |
stefanrousseau | 16:17c5916f2d12 | 207 | return false; |
stefanrousseau | 16:17c5916f2d12 | 208 | } |
stefanrousseau | 16:17c5916f2d12 | 209 | } //parse_JSON |
stefanrousseau | 16:17c5916f2d12 | 210 | |
jwhammel | 84:fc8c9b39723a | 211 | //DigitalIn btn_temp_up(D4); |
jwhammel | 84:fc8c9b39723a | 212 | //DigitalIn btn_temp_down(D8); |
jwhammel | 85:0cf65ceb4492 | 213 | DigitalIn btn_silence(D4); |
jwhammel | 84:fc8c9b39723a | 214 | DigitalIn motion(D3); |
jwhammel | 84:fc8c9b39723a | 215 | DigitalOut buzzer(D5); |
jwhammel | 84:fc8c9b39723a | 216 | |
jwhammel | 85:0cf65ceb4492 | 217 | AnalogIn pot_temp_set(A2); |
jwhammel | 85:0cf65ceb4492 | 218 | AnalogIn Alarm_Off(A3); |
jwhammel | 85:0cf65ceb4492 | 219 | //DHT sensor(D4, DHT11); |
jwhammel | 84:fc8c9b39723a | 220 | |
JMF | 0:9d5134074d84 | 221 | int main() { |
stefanrousseau | 61:f6b93129f954 | 222 | static unsigned ledOnce = 0; |
jwhammel | 84:fc8c9b39723a | 223 | int error = 0; |
jwhammel | 84:fc8c9b39723a | 224 | float h = 0.0f, c = 0.0f, f = 0.0f, k = 0.0f, dp = 0.0f, dpf = 0.0f; |
jwhammel | 84:fc8c9b39723a | 225 | float value = 0.0f; |
jwhammel | 85:0cf65ceb4492 | 226 | float alm_off; |
jwhammel | 84:fc8c9b39723a | 227 | float temp_setting = 0.0f, diff = 0.0f; |
jwhammel | 84:fc8c9b39723a | 228 | int mycolor = 0x0; |
jwhammel | 85:0cf65ceb4492 | 229 | bool alarms = false; |
jwhammel | 85:0cf65ceb4492 | 230 | int mycounter = 0; |
jwhammel | 85:0cf65ceb4492 | 231 | int movement; |
jwhammel | 84:fc8c9b39723a | 232 | |
stefanrousseau | 72:b500e1507b5f | 233 | //delay so that the debug terminal can open after power-on reset: |
fkellermavnet | 77:c65eae5b9958 | 234 | wait (5.0); |
stefanrousseau | 61:f6b93129f954 | 235 | pc.baud(115200); |
fkellermavnet | 77:c65eae5b9958 | 236 | |
fkellermavnet | 77:c65eae5b9958 | 237 | display_app_firmware_version(); |
fkellermavnet | 77:c65eae5b9958 | 238 | |
stefanrousseau | 64:09004cd610df | 239 | PRINTF(GRN "Hello World from the Cellular IoT Kit!\r\n\r\n"); |
JMF | 0:9d5134074d84 | 240 | |
stefanrousseau | 61:f6b93129f954 | 241 | //Initialize the I2C sensors that are present |
stefanrousseau | 11:e6602513730f | 242 | sensors_init(); |
stefanrousseau | 12:7c94ec5069dc | 243 | read_sensors(); |
stefanrousseau | 11:e6602513730f | 244 | |
stefanrousseau | 61:f6b93129f954 | 245 | // Set LED to RED until init finishes |
stefanrousseau | 61:f6b93129f954 | 246 | SetLedColor(0x1); //Red |
JMF | 0:9d5134074d84 | 247 | // Initialize the modem |
stefanrousseau | 64:09004cd610df | 248 | PRINTF("\r\n"); |
stefanrousseau | 61:f6b93129f954 | 249 | cell_modem_init(); |
fkellermavnet | 77:c65eae5b9958 | 250 | display_wnc_firmware_rev(); |
fkellermavnet | 77:c65eae5b9958 | 251 | |
stefanrousseau | 61:f6b93129f954 | 252 | // Set LED BLUE for partial init |
stefanrousseau | 61:f6b93129f954 | 253 | SetLedColor(0x4); //Blue |
JMF | 0:9d5134074d84 | 254 | |
stefanrousseau | 3:26b3cc155f39 | 255 | //Create a 1ms timer tick function: |
stefanrousseau | 61:f6b93129f954 | 256 | iTimer1Interval_ms = SENSOR_UPDATE_INTERVAL_MS; |
stefanrousseau | 3:26b3cc155f39 | 257 | OneMsTicker.attach(OneMsFunction, 0.001f) ; |
fkellermavnet | 26:8d6e7e7cdcae | 258 | |
JMF | 2:0e2ef866af95 | 259 | // Send and receive data perpetually |
jwhammel | 84:fc8c9b39723a | 260 | |
jwhammel | 84:fc8c9b39723a | 261 | mycolor = 0x0; |
jwhammel | 84:fc8c9b39723a | 262 | SetLedColor(mycolor); |
jwhammel | 84:fc8c9b39723a | 263 | |
jwhammel | 84:fc8c9b39723a | 264 | buzzer = 0; |
jwhammel | 84:fc8c9b39723a | 265 | |
JMF | 2:0e2ef866af95 | 266 | while(1) { |
stefanrousseau | 55:3abf9e3f42e6 | 267 | #ifdef USE_VIRTUAL_SENSORS |
stefanrousseau | 55:3abf9e3f42e6 | 268 | ProcessUsbInterface(); |
stefanrousseau | 55:3abf9e3f42e6 | 269 | #endif |
stefanrousseau | 3:26b3cc155f39 | 270 | if (bTimerExpiredFlag) |
stefanrousseau | 3:26b3cc155f39 | 271 | { |
stefanrousseau | 3:26b3cc155f39 | 272 | bTimerExpiredFlag = false; |
stefanrousseau | 4:f83bedd9cab4 | 273 | read_sensors(); //read available external sensors from a PMOD and the on-board motion sensor |
stefanrousseau | 3:26b3cc155f39 | 274 | char modem_string[512]; |
stefanrousseau | 3:26b3cc155f39 | 275 | GenerateModemString(&modem_string[0]); |
stefanrousseau | 61:f6b93129f954 | 276 | char myJsonResponse[512]; |
stefanrousseau | 61:f6b93129f954 | 277 | if (cell_modem_Sendreceive(&modem_string[0], &myJsonResponse[0])) |
fkellermavnet | 20:27a4f27254d0 | 278 | { |
stefanrousseau | 41:9b14c40eebf6 | 279 | if (!ledOnce) |
stefanrousseau | 41:9b14c40eebf6 | 280 | { |
stefanrousseau | 41:9b14c40eebf6 | 281 | ledOnce = 1; |
stefanrousseau | 61:f6b93129f954 | 282 | SetLedColor(0x2); //Green |
stefanrousseau | 41:9b14c40eebf6 | 283 | } |
jwhammel | 84:fc8c9b39723a | 284 | //parse_JSON(&myJsonResponse[0]); |
stefanrousseau | 16:17c5916f2d12 | 285 | } |
stefanrousseau | 3:26b3cc155f39 | 286 | } //bTimerExpiredFlag |
jwhammel | 85:0cf65ceb4492 | 287 | //------------------------------------------------------------------------------ |
jwhammel | 85:0cf65ceb4492 | 288 | //------------------------------------------------------------------------------ |
jwhammel | 85:0cf65ceb4492 | 289 | //------------------------------------------------------------------------------ |
jwhammel | 85:0cf65ceb4492 | 290 | //-------------------------------BEGIN NEW CODE--------------------------------- |
jwhammel | 85:0cf65ceb4492 | 291 | //------------------------------------------------------------------------------ |
jwhammel | 85:0cf65ceb4492 | 292 | //------------------------------------------------------------------------------ |
jwhammel | 85:0cf65ceb4492 | 293 | /* error = sensor.readData(); |
jwhammel | 84:fc8c9b39723a | 294 | if (0 == error) |
jwhammel | 84:fc8c9b39723a | 295 | { |
jwhammel | 84:fc8c9b39723a | 296 | c = sensor.ReadTemperature(CELCIUS); |
jwhammel | 84:fc8c9b39723a | 297 | f = sensor.ReadTemperature(FARENHEIT); |
jwhammel | 84:fc8c9b39723a | 298 | k = sensor.ReadTemperature(KELVIN); |
jwhammel | 84:fc8c9b39723a | 299 | h = sensor.ReadHumidity(); |
jwhammel | 84:fc8c9b39723a | 300 | dp = sensor.CalcdewPoint(c, h); |
jwhammel | 84:fc8c9b39723a | 301 | dpf = sensor.CalcdewPointFast(c, h); |
jwhammel | 84:fc8c9b39723a | 302 | //printf("Temperature in Kelvin: %4.2f, Celcius: %4.2f, Farenheit %4.2f\n", k, c, f); |
jwhammel | 84:fc8c9b39723a | 303 | //printf("Humidity is %4.2f, Dewpoint: %4.2f, Dewpoint fast: %4.2f\n", h, dp, dpf); |
jwhammel | 84:fc8c9b39723a | 304 | } |
jwhammel | 84:fc8c9b39723a | 305 | else |
jwhammel | 84:fc8c9b39723a | 306 | { |
jwhammel | 84:fc8c9b39723a | 307 | //printf("Error: %d\n", error); |
jwhammel | 84:fc8c9b39723a | 308 | } |
jwhammel | 85:0cf65ceb4492 | 309 | */ |
jwhammel | 84:fc8c9b39723a | 310 | value = pot_temp_set; |
jwhammel | 84:fc8c9b39723a | 311 | //printf("Slide location %3.6f\r\n", value); |
jwhammel | 84:fc8c9b39723a | 312 | temp_setting = value * 40.0f + 50.0f; |
jwhammel | 85:0cf65ceb4492 | 313 | // printf(WHT "Thermostat is set to %3.2f degrees.\r\n", temp_setting); |
jwhammel | 84:fc8c9b39723a | 314 | |
jwhammel | 84:fc8c9b39723a | 315 | //current_temp and rel_humid are assigned in sensors.cpp |
jwhammel | 85:0cf65ceb4492 | 316 | // printf(WHT "Current temperature is %3.2f degrees.\r\n", current_temp); |
jwhammel | 85:0cf65ceb4492 | 317 | // printf(WHT "Relative humidity is %3.2f percent.\r\n", rel_humid); |
jwhammel | 84:fc8c9b39723a | 318 | |
jwhammel | 84:fc8c9b39723a | 319 | printf(WHT "\r\n"); |
jwhammel | 84:fc8c9b39723a | 320 | |
jwhammel | 84:fc8c9b39723a | 321 | diff = current_temp - temp_setting; |
jwhammel | 84:fc8c9b39723a | 322 | if ((diff > -2.0f && diff < 2.0f && rel_humid < 40.0f) && mycolor != 0x0) //if temperature is near optimal, and it is not humid, turn off heat and AC. |
jwhammel | 84:fc8c9b39723a | 323 | { |
jwhammel | 84:fc8c9b39723a | 324 | mycolor = 0x0; |
jwhammel | 84:fc8c9b39723a | 325 | SetLedColor(mycolor); |
jwhammel | 84:fc8c9b39723a | 326 | } |
jwhammel | 84:fc8c9b39723a | 327 | if ((diff > -2.0f && diff < 2.0f && rel_humid > 40.0f) && mycolor != 0x2) //if temperature is near optimal, and it is humid, use fan only. |
jwhammel | 84:fc8c9b39723a | 328 | { |
jwhammel | 84:fc8c9b39723a | 329 | mycolor = 0x2; |
jwhammel | 84:fc8c9b39723a | 330 | SetLedColor(mycolor); |
jwhammel | 84:fc8c9b39723a | 331 | } |
jwhammel | 84:fc8c9b39723a | 332 | if (diff > 4.0f && mycolor != 0x4) //if it is too hot, turn on AC. |
jwhammel | 84:fc8c9b39723a | 333 | { |
jwhammel | 84:fc8c9b39723a | 334 | mycolor = 0x4; |
jwhammel | 84:fc8c9b39723a | 335 | SetLedColor(mycolor); |
jwhammel | 84:fc8c9b39723a | 336 | } |
jwhammel | 84:fc8c9b39723a | 337 | if (diff < -4.0f && mycolor != 0x1) //if it is too cold, turn on heat. |
jwhammel | 84:fc8c9b39723a | 338 | { |
jwhammel | 84:fc8c9b39723a | 339 | mycolor = 0x1; |
jwhammel | 84:fc8c9b39723a | 340 | SetLedColor(mycolor); |
jwhammel | 84:fc8c9b39723a | 341 | } |
jwhammel | 84:fc8c9b39723a | 342 | |
jwhammel | 84:fc8c9b39723a | 343 | //if motion sensor detects motion, sound an alarm |
jwhammel | 85:0cf65ceb4492 | 344 | |
jwhammel | 85:0cf65ceb4492 | 345 | if (motion && !alarms) |
jwhammel | 84:fc8c9b39723a | 346 | { |
jwhammel | 85:0cf65ceb4492 | 347 | alarms = true; |
jwhammel | 85:0cf65ceb4492 | 348 | strcpy(SENSOR_DATA.Proximity, "1"); |
jwhammel | 84:fc8c9b39723a | 349 | printf(WHT "--------------------------------\r\n"); |
jwhammel | 84:fc8c9b39723a | 350 | printf(WHT "Alert! Home intrusion detected!\r\n"); |
jwhammel | 84:fc8c9b39723a | 351 | printf(WHT "--------------------------------\r\n"); |
jwhammel | 84:fc8c9b39723a | 352 | printf(WHT "\r\n"); |
jwhammel | 84:fc8c9b39723a | 353 | } |
jwhammel | 85:0cf65ceb4492 | 354 | alm_off = Alarm_Off;//Switching to integer because AnalogIn acts weird in If statement |
jwhammel | 85:0cf65ceb4492 | 355 | if((alm_off > 0.5f) && alarms) |
jwhammel | 84:fc8c9b39723a | 356 | { |
jwhammel | 85:0cf65ceb4492 | 357 | alarms = false; |
jwhammel | 85:0cf65ceb4492 | 358 | strcpy(SENSOR_DATA.Proximity, "0"); |
jwhammel | 84:fc8c9b39723a | 359 | buzzer = 0; |
jwhammel | 84:fc8c9b39723a | 360 | printf(WHT "Alarm disengaged. Resuming normal operation.\r\n"); |
jwhammel | 84:fc8c9b39723a | 361 | printf(WHT "\r\n"); |
jwhammel | 84:fc8c9b39723a | 362 | } |
jwhammel | 85:0cf65ceb4492 | 363 | if (alarms) |
jwhammel | 84:fc8c9b39723a | 364 | { |
jwhammel | 84:fc8c9b39723a | 365 | buzzer = !buzzer; |
jwhammel | 84:fc8c9b39723a | 366 | } |
jwhammel | 84:fc8c9b39723a | 367 | |
jwhammel | 85:0cf65ceb4492 | 368 | |
jwhammel | 85:0cf65ceb4492 | 369 | /*wait(1.0); |
jwhammel | 85:0cf65ceb4492 | 370 | |
jwhammel | 85:0cf65ceb4492 | 371 | mycounter = mycounter + 1; |
jwhammel | 85:0cf65ceb4492 | 372 | if (mycounter >= 20) |
jwhammel | 85:0cf65ceb4492 | 373 | { |
jwhammel | 85:0cf65ceb4492 | 374 | mycounter = 0; |
jwhammel | 85:0cf65ceb4492 | 375 | } |
jwhammel | 85:0cf65ceb4492 | 376 | if (mycounter >= 15) |
jwhammel | 85:0cf65ceb4492 | 377 | { |
jwhammel | 85:0cf65ceb4492 | 378 | strcpy(SENSOR_DATA.Proximity, "1"); |
jwhammel | 85:0cf65ceb4492 | 379 | } |
jwhammel | 85:0cf65ceb4492 | 380 | else |
jwhammel | 85:0cf65ceb4492 | 381 | { |
jwhammel | 85:0cf65ceb4492 | 382 | strcpy(SENSOR_DATA.Proximity, "0"); |
jwhammel | 85:0cf65ceb4492 | 383 | } */ |
jwhammel | 84:fc8c9b39723a | 384 | |
stefanrousseau | 3:26b3cc155f39 | 385 | } //forever loop |
JMF | 0:9d5134074d84 | 386 | } |