Ag demo with soil moisture
Dependencies: DOGS102 ISL29011 MMA845x MPL3115A2 NCP5623B libmDot mbed-rtos mbed-src
Fork of MTDOT-EVBDemo-DRH by
main.cpp@9:daa92715f700, 2016-02-22 (annotated)
- Committer:
- skovarik
- Date:
- Mon Feb 22 16:14:28 2016 +0000
- Revision:
- 9:daa92715f700
- Parent:
- 7:74c027d0353b
added soil moisture to Ag demo
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
falingtrea | 0:bdd16076aaa5 | 1 | /** |
falingtrea | 0:bdd16076aaa5 | 2 | * @file main.cpp |
falingtrea | 0:bdd16076aaa5 | 3 | * @brief Main application for mDot-EVB demo |
falingtrea | 0:bdd16076aaa5 | 4 | * @author Tim Barr MultiTech Systems Inc. |
falingtrea | 3:68e974f5f532 | 5 | * @version 1.03 |
falingtrea | 0:bdd16076aaa5 | 6 | * @see |
falingtrea | 0:bdd16076aaa5 | 7 | * |
falingtrea | 0:bdd16076aaa5 | 8 | * Copyright (c) 2015 |
falingtrea | 0:bdd16076aaa5 | 9 | * |
falingtrea | 0:bdd16076aaa5 | 10 | * Licensed under the Apache License, Version 2.0 (the "License"); |
falingtrea | 0:bdd16076aaa5 | 11 | * you may not use this file except in compliance with the License. |
falingtrea | 0:bdd16076aaa5 | 12 | * You may obtain a copy of the License at |
falingtrea | 0:bdd16076aaa5 | 13 | * |
falingtrea | 0:bdd16076aaa5 | 14 | * http://www.apache.org/licenses/LICENSE-2.0 |
falingtrea | 0:bdd16076aaa5 | 15 | * |
falingtrea | 0:bdd16076aaa5 | 16 | * Unless required by applicable law or agreed to in writing, software |
falingtrea | 0:bdd16076aaa5 | 17 | * distributed under the License is distributed on an "AS IS" BASIS, |
falingtrea | 0:bdd16076aaa5 | 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
falingtrea | 0:bdd16076aaa5 | 19 | * See the License for the specific language governing permissions and |
falingtrea | 0:bdd16076aaa5 | 20 | * limitations under the License. |
falingtrea | 1:ac9595d0f0e7 | 21 | * |
falingtrea | 1:ac9595d0f0e7 | 22 | * 1.01 TAB 7/6/15 Removed NULL pointer from evbAmbientLight creation call. |
falingtrea | 2:75adc72aa6a0 | 23 | * |
falingtrea | 3:68e974f5f532 | 24 | * 1.02 TAB 7/8/15 Send logo to LCD before attempting connection to LoRa network. Added |
falingtrea | 2:75adc72aa6a0 | 25 | * information on setting up for public LoRa network. Moved SW setup to |
falingtrea | 2:75adc72aa6a0 | 26 | * beginning of main. Removed printf call from ISR functions. Added |
falingtrea | 2:75adc72aa6a0 | 27 | * additional checks for exit_program. |
falingtrea | 3:68e974f5f532 | 28 | * |
falingtrea | 3:68e974f5f532 | 29 | * 1.03 TAB 7/15/15 Added threads for push button switch debounce. |
falingtrea | 3:68e974f5f532 | 30 | * |
falingtrea | 0:bdd16076aaa5 | 31 | */ |
falingtrea | 0:bdd16076aaa5 | 32 | |
falingtrea | 0:bdd16076aaa5 | 33 | #include "mbed.h" |
falingtrea | 0:bdd16076aaa5 | 34 | #include "MMA845x.h" |
falingtrea | 0:bdd16076aaa5 | 35 | #include "MPL3115A2.h" |
falingtrea | 0:bdd16076aaa5 | 36 | #include "ISL29011.h" |
falingtrea | 0:bdd16076aaa5 | 37 | #include "NCP5623B.h" |
falingtrea | 0:bdd16076aaa5 | 38 | #include "DOGS102.h" |
falingtrea | 0:bdd16076aaa5 | 39 | #include "font_6x8.h" |
falingtrea | 0:bdd16076aaa5 | 40 | #include "MultiTech_Logo.h" |
falingtrea | 0:bdd16076aaa5 | 41 | #include "mDot.h" |
falingtrea | 0:bdd16076aaa5 | 42 | #include "rtos.h" |
falingtrea | 0:bdd16076aaa5 | 43 | #include <string> |
falingtrea | 0:bdd16076aaa5 | 44 | #include <vector> |
falingtrea | 0:bdd16076aaa5 | 45 | |
falingtrea | 0:bdd16076aaa5 | 46 | enum LED1_COLOR { |
falingtrea | 0:bdd16076aaa5 | 47 | RED = 0, |
falingtrea | 0:bdd16076aaa5 | 48 | GREEN = 1 |
falingtrea | 0:bdd16076aaa5 | 49 | }; |
falingtrea | 0:bdd16076aaa5 | 50 | |
falingtrea | 0:bdd16076aaa5 | 51 | /* |
falingtrea | 0:bdd16076aaa5 | 52 | * union for converting from 32-bit to 4 8-bit values |
falingtrea | 0:bdd16076aaa5 | 53 | */ |
falingtrea | 0:bdd16076aaa5 | 54 | union convert32 { |
falingtrea | 0:bdd16076aaa5 | 55 | int32_t f_s; // convert from signed 32 bit int |
falingtrea | 0:bdd16076aaa5 | 56 | uint32_t f_u; // convert from unsigned 32 bit int |
falingtrea | 0:bdd16076aaa5 | 57 | uint8_t t_u[4]; // convert to 8 bit unsigned array |
falingtrea | 0:bdd16076aaa5 | 58 | }; |
falingtrea | 0:bdd16076aaa5 | 59 | |
falingtrea | 0:bdd16076aaa5 | 60 | /* |
falingtrea | 0:bdd16076aaa5 | 61 | * union for converting from 16- bit to 2 8-bit values |
falingtrea | 0:bdd16076aaa5 | 62 | */ |
falingtrea | 0:bdd16076aaa5 | 63 | union convert16 { |
falingtrea | 0:bdd16076aaa5 | 64 | int16_t f_s; // convert from signed 16 bit int |
falingtrea | 0:bdd16076aaa5 | 65 | uint16_t f_u; // convert from unsigned 16 bit int |
falingtrea | 0:bdd16076aaa5 | 66 | uint8_t t_u[2]; // convert to 8 bit unsigned array |
falingtrea | 0:bdd16076aaa5 | 67 | }; |
falingtrea | 0:bdd16076aaa5 | 68 | |
falingtrea | 0:bdd16076aaa5 | 69 | //DigitalIn mDot02(PA_2); // GPIO/UART_TX |
falingtrea | 0:bdd16076aaa5 | 70 | //DigitalOut mDot03(PA_3); // GPIO/UART_RX |
falingtrea | 0:bdd16076aaa5 | 71 | //DigitalIn mDot04(PA_6); // GPIO/SPI_MISO |
falingtrea | 0:bdd16076aaa5 | 72 | //DigitalIn mDot06(PA_8); // GPIO/I2C_SCL |
falingtrea | 0:bdd16076aaa5 | 73 | //DigitalIn mDot07(PC_9); // GPIO/I2C_SDA |
falingtrea | 0:bdd16076aaa5 | 74 | |
falingtrea | 0:bdd16076aaa5 | 75 | InterruptIn mDot08(PA_12); // GPIO/USB PB S1 on EVB |
falingtrea | 0:bdd16076aaa5 | 76 | InterruptIn mDot09(PA_11); // GPIO/USB PB S2 on EVB |
falingtrea | 0:bdd16076aaa5 | 77 | |
falingtrea | 0:bdd16076aaa5 | 78 | //DigitalIn mDot11(PA_7); // GPIO/SPI_MOSI |
falingtrea | 0:bdd16076aaa5 | 79 | |
falingtrea | 0:bdd16076aaa5 | 80 | InterruptIn mDot12(PA_0); // GPIO/UART_CTS PRESSURE_INT2 on EVB |
falingtrea | 0:bdd16076aaa5 | 81 | DigitalOut mDot13(PC_13,1); // GPIO LCD_C/D |
falingtrea | 0:bdd16076aaa5 | 82 | InterruptIn mDot15(PC_1); // GPIO LIGHT_PROX_INT on EVB |
falingtrea | 0:bdd16076aaa5 | 83 | InterruptIn mDot16(PA_1); // GPIO/UART_RTS ACCEL_INT2 on EVB |
falingtrea | 0:bdd16076aaa5 | 84 | DigitalOut mDot17(PA_4,1); // GPIO/SPI_NCS LCD_CS on EVB |
falingtrea | 0:bdd16076aaa5 | 85 | |
falingtrea | 0:bdd16076aaa5 | 86 | //DigitalIn mDot18(PA_5); // GPIO/SPI_SCK |
falingtrea | 0:bdd16076aaa5 | 87 | |
falingtrea | 0:bdd16076aaa5 | 88 | //DigitalInOut mDot19(PB_0,PIN_INPUT,PullNone,0); // GPIO PushPull LED Low=Red High=Green set MODE=INPUT to turn off |
falingtrea | 0:bdd16076aaa5 | 89 | AnalogIn mDot20(PB_1); // GPIO Current Sense Analog in on EVB |
falingtrea | 0:bdd16076aaa5 | 90 | |
falingtrea | 0:bdd16076aaa5 | 91 | Serial debugUART(PA_9, PA_10); // mDot debug UART |
falingtrea | 0:bdd16076aaa5 | 92 | |
falingtrea | 0:bdd16076aaa5 | 93 | //Serial mDotUART(PA_2, PA_3); // mDot external UART mDot02 and mDot03 |
falingtrea | 0:bdd16076aaa5 | 94 | |
falingtrea | 0:bdd16076aaa5 | 95 | I2C mDoti2c(PC_9,PA_8); // mDot External I2C mDot6 and mDot7 |
falingtrea | 0:bdd16076aaa5 | 96 | |
falingtrea | 0:bdd16076aaa5 | 97 | SPI mDotspi(PA_7,PA_6,PA_5); // mDot external SPI mDot11, mDot4, and mDot18 |
falingtrea | 0:bdd16076aaa5 | 98 | |
daveheitzman | 7:74c027d0353b | 99 | MPL3115A2* resetBaro(const MPL3115A2* oldBaro); |
daveheitzman | 7:74c027d0353b | 100 | |
skovarik | 9:daa92715f700 | 101 | AnalogIn moisture_sensor(PB_1); |
skovarik | 9:daa92715f700 | 102 | |
falingtrea | 2:75adc72aa6a0 | 103 | /* **** replace these values with the proper public or private network settings **** |
falingtrea | 2:75adc72aa6a0 | 104 | * config_network_nameand config_network_pass are for private networks. |
falingtrea | 2:75adc72aa6a0 | 105 | */ |
skovarik | 9:daa92715f700 | 106 | static std::string config_network_name = "MTCDT-18321832"; |
skovarik | 9:daa92715f700 | 107 | static std::string config_network_pass = "MTCDT-18321832"; |
daveheitzman | 4:51d7c24bbf91 | 108 | static uint8_t config_frequency_sub_band = 3; |
falingtrea | 0:bdd16076aaa5 | 109 | |
falingtrea | 2:75adc72aa6a0 | 110 | /* config_app_id and config_app_key are for public networks. |
falingtrea | 2:75adc72aa6a0 | 111 | static uint8_t app_id[8] = {0x00,0x01,0x02,0x03,0x0A,0x0B,0x0C,0x0D}; |
falingtrea | 2:75adc72aa6a0 | 112 | std::vector<uint8_t> config_app_id; |
falingtrea | 2:75adc72aa6a0 | 113 | static uint8_t app_key[16] = {0x00,0x01,0x02,0x03,0x0A,0x0B,0x0C,0x0D}; |
falingtrea | 2:75adc72aa6a0 | 114 | std::vector<uint8_t> config_app_key; |
falingtrea | 2:75adc72aa6a0 | 115 | */ |
falingtrea | 2:75adc72aa6a0 | 116 | |
falingtrea | 0:bdd16076aaa5 | 117 | uint8_t result, pckt_time=10; |
falingtrea | 0:bdd16076aaa5 | 118 | char data; |
falingtrea | 0:bdd16076aaa5 | 119 | unsigned char test; |
falingtrea | 0:bdd16076aaa5 | 120 | char txtstr[17]; |
falingtrea | 0:bdd16076aaa5 | 121 | int32_t num_whole, mdot_ret; |
falingtrea | 0:bdd16076aaa5 | 122 | uint32_t pressure; |
falingtrea | 0:bdd16076aaa5 | 123 | int16_t num_frac; |
skovarik | 9:daa92715f700 | 124 | float moisture; |
falingtrea | 0:bdd16076aaa5 | 125 | |
falingtrea | 0:bdd16076aaa5 | 126 | bool exit_program = false; |
falingtrea | 0:bdd16076aaa5 | 127 | |
falingtrea | 0:bdd16076aaa5 | 128 | MMA845x_DATA accel_data; |
falingtrea | 0:bdd16076aaa5 | 129 | MPL3115A2_DATA baro_data; |
falingtrea | 0:bdd16076aaa5 | 130 | uint16_t lux_data; |
falingtrea | 0:bdd16076aaa5 | 131 | MMA845x* evbAccel; |
falingtrea | 0:bdd16076aaa5 | 132 | MPL3115A2* evbBaro; |
falingtrea | 0:bdd16076aaa5 | 133 | ISL29011* evbAmbLight; |
falingtrea | 0:bdd16076aaa5 | 134 | NCP5623B* evbBackLight; |
falingtrea | 0:bdd16076aaa5 | 135 | DOGS102* evbLCD; |
falingtrea | 0:bdd16076aaa5 | 136 | mDot* mdot_radio; |
falingtrea | 0:bdd16076aaa5 | 137 | |
falingtrea | 0:bdd16076aaa5 | 138 | convert32 convertl; |
falingtrea | 0:bdd16076aaa5 | 139 | convert16 converts; |
falingtrea | 0:bdd16076aaa5 | 140 | |
falingtrea | 3:68e974f5f532 | 141 | // flags for pushbutton debounce code |
falingtrea | 3:68e974f5f532 | 142 | bool pb1_low = false; |
falingtrea | 3:68e974f5f532 | 143 | bool pb2_low = false; |
falingtrea | 3:68e974f5f532 | 144 | |
falingtrea | 0:bdd16076aaa5 | 145 | void pb1ISR(void); |
falingtrea | 0:bdd16076aaa5 | 146 | void pb2ISR(void); |
falingtrea | 3:68e974f5f532 | 147 | void pb1_debounce(void const *args); |
falingtrea | 3:68e974f5f532 | 148 | void pb2_debounce(void const *args); |
falingtrea | 3:68e974f5f532 | 149 | Thread* thread_3; |
falingtrea | 0:bdd16076aaa5 | 150 | |
falingtrea | 0:bdd16076aaa5 | 151 | void log_error(mDot* dot, const char* msg, int32_t retval); |
falingtrea | 0:bdd16076aaa5 | 152 | |
falingtrea | 0:bdd16076aaa5 | 153 | void config_pkt_xmit (void const *args); |
daveheitzman | 5:5a794cedd728 | 154 | int32_t sendString(const std::string text); |
falingtrea | 0:bdd16076aaa5 | 155 | |
daveheitzman | 6:b62e5106b5bc | 156 | bool writeValueOrError(); |
daveheitzman | 6:b62e5106b5bc | 157 | |
daveheitzman | 6:b62e5106b5bc | 158 | char sensor_text[54]; |
daveheitzman | 6:b62e5106b5bc | 159 | char lora_temp_string[7]; |
daveheitzman | 6:b62e5106b5bc | 160 | char lora_alt_string[8]; |
daveheitzman | 6:b62e5106b5bc | 161 | char lora_press_string[9]; |
daveheitzman | 6:b62e5106b5bc | 162 | |
daveheitzman | 6:b62e5106b5bc | 163 | char lora_light_string[6]; |
daveheitzman | 6:b62e5106b5bc | 164 | const int FAIL_MAX=15; |
daveheitzman | 6:b62e5106b5bc | 165 | int failtime=FAIL_MAX; |
daveheitzman | 6:b62e5106b5bc | 166 | |
falingtrea | 0:bdd16076aaa5 | 167 | int main() |
falingtrea | 0:bdd16076aaa5 | 168 | { |
falingtrea | 0:bdd16076aaa5 | 169 | |
falingtrea | 0:bdd16076aaa5 | 170 | std::vector<uint8_t> mdot_data; |
falingtrea | 0:bdd16076aaa5 | 171 | std::vector<uint8_t> mdot_EUI; |
falingtrea | 0:bdd16076aaa5 | 172 | uint16_t i = 0; |
falingtrea | 0:bdd16076aaa5 | 173 | |
daveheitzman | 6:b62e5106b5bc | 174 | debugUART.baud(115200); |
falingtrea | 0:bdd16076aaa5 | 175 | // mDotUART.baud(9600); // mdot UART unused but available on external connector |
falingtrea | 0:bdd16076aaa5 | 176 | |
falingtrea | 3:68e974f5f532 | 177 | Thread thread_1(pb1_debounce); // threads for de-bouncing pushbutton switches |
falingtrea | 3:68e974f5f532 | 178 | Thread thread_2(pb2_debounce); |
falingtrea | 3:68e974f5f532 | 179 | |
falingtrea | 3:68e974f5f532 | 180 | thread_3 = new Thread(config_pkt_xmit); // start thread that sends LoRa packet when SW2 pressed |
falingtrea | 2:75adc72aa6a0 | 181 | |
falingtrea | 1:ac9595d0f0e7 | 182 | evbAccel = new MMA845x(mDoti2c,MMA845x::SA0_VSS); // setup Accelerometer |
daveheitzman | 6:b62e5106b5bc | 183 | evbBaro = new MPL3115A2(mDoti2c); // setup0 Barometric sensor |
falingtrea | 1:ac9595d0f0e7 | 184 | evbAmbLight = new ISL29011(mDoti2c); // Setup Ambient Light Sensor |
falingtrea | 0:bdd16076aaa5 | 185 | evbBackLight = new NCP5623B(mDoti2c); // setup backlight and LED 2 driver chip |
falingtrea | 1:ac9595d0f0e7 | 186 | evbLCD = new DOGS102(mDotspi, mDot17, mDot13); // setup LCD |
falingtrea | 0:bdd16076aaa5 | 187 | |
falingtrea | 2:75adc72aa6a0 | 188 | /* |
falingtrea | 2:75adc72aa6a0 | 189 | * Setup SW1 as program stop function |
falingtrea | 2:75adc72aa6a0 | 190 | */ |
falingtrea | 2:75adc72aa6a0 | 191 | mDot08.disable_irq(); |
falingtrea | 2:75adc72aa6a0 | 192 | mDot08.fall(&pb1ISR); |
falingtrea | 2:75adc72aa6a0 | 193 | |
falingtrea | 2:75adc72aa6a0 | 194 | /* |
falingtrea | 2:75adc72aa6a0 | 195 | * need to call this function after rise or fall because rise/fall sets |
falingtrea | 2:75adc72aa6a0 | 196 | * mode to PullNone |
falingtrea | 2:75adc72aa6a0 | 197 | */ |
falingtrea | 2:75adc72aa6a0 | 198 | mDot08.mode(PullUp); |
falingtrea | 2:75adc72aa6a0 | 199 | |
falingtrea | 2:75adc72aa6a0 | 200 | mDot08.enable_irq(); |
falingtrea | 2:75adc72aa6a0 | 201 | |
falingtrea | 2:75adc72aa6a0 | 202 | /* |
falingtrea | 2:75adc72aa6a0 | 203 | * Setup SW2 as packet time change |
falingtrea | 2:75adc72aa6a0 | 204 | */ |
falingtrea | 2:75adc72aa6a0 | 205 | mDot09.disable_irq(); |
falingtrea | 2:75adc72aa6a0 | 206 | mDot09.fall(&pb2ISR); |
falingtrea | 2:75adc72aa6a0 | 207 | |
falingtrea | 2:75adc72aa6a0 | 208 | /* |
falingtrea | 2:75adc72aa6a0 | 209 | * need to call this function after rise or fall because rise/fall sets |
falingtrea | 2:75adc72aa6a0 | 210 | * mode to PullNone |
falingtrea | 2:75adc72aa6a0 | 211 | */ |
falingtrea | 2:75adc72aa6a0 | 212 | mDot09.mode(PullUp); |
falingtrea | 2:75adc72aa6a0 | 213 | |
falingtrea | 2:75adc72aa6a0 | 214 | mDot09.enable_irq(); |
falingtrea | 2:75adc72aa6a0 | 215 | |
falingtrea | 2:75adc72aa6a0 | 216 | /* |
falingtrea | 2:75adc72aa6a0 | 217 | * Setting other InterruptIn pins with Pull Ups |
falingtrea | 2:75adc72aa6a0 | 218 | */ |
falingtrea | 2:75adc72aa6a0 | 219 | mDot12.mode(PullUp); |
falingtrea | 2:75adc72aa6a0 | 220 | mDot15.mode(PullUp); |
falingtrea | 2:75adc72aa6a0 | 221 | mDot16.mode(PullUp); |
falingtrea | 2:75adc72aa6a0 | 222 | |
falingtrea | 2:75adc72aa6a0 | 223 | printf("font table address %p\n\r",&font_6x8); |
falingtrea | 2:75adc72aa6a0 | 224 | printf("bitmap address %p\n\r",&MultiTech_Logo); |
falingtrea | 2:75adc72aa6a0 | 225 | |
falingtrea | 2:75adc72aa6a0 | 226 | // Setup and display logo on LCD |
falingtrea | 2:75adc72aa6a0 | 227 | evbLCD->startUpdate(); |
falingtrea | 2:75adc72aa6a0 | 228 | |
falingtrea | 2:75adc72aa6a0 | 229 | evbLCD->writeBitmap(0,0,MultiTech_Logo); |
falingtrea | 2:75adc72aa6a0 | 230 | |
falingtrea | 2:75adc72aa6a0 | 231 | sprintf(txtstr,"MTDOT"); |
falingtrea | 2:75adc72aa6a0 | 232 | evbLCD->writeText(24,3,font_6x8,txtstr,strlen(txtstr)); |
falingtrea | 2:75adc72aa6a0 | 233 | sprintf(txtstr,"Evaluation"); |
falingtrea | 2:75adc72aa6a0 | 234 | evbLCD->writeText(24,4,font_6x8,txtstr,strlen(txtstr)); |
falingtrea | 2:75adc72aa6a0 | 235 | sprintf(txtstr,"Board"); |
falingtrea | 2:75adc72aa6a0 | 236 | evbLCD->writeText(24,5,font_6x8,txtstr,strlen(txtstr)); |
falingtrea | 2:75adc72aa6a0 | 237 | |
falingtrea | 2:75adc72aa6a0 | 238 | evbLCD->endUpdate(); |
falingtrea | 2:75adc72aa6a0 | 239 | |
falingtrea | 2:75adc72aa6a0 | 240 | printf("\n\r setup mdot\n\r"); |
falingtrea | 0:bdd16076aaa5 | 241 | |
falingtrea | 0:bdd16076aaa5 | 242 | // get a mDot handle |
falingtrea | 0:bdd16076aaa5 | 243 | mdot_radio = mDot::getInstance(); |
falingtrea | 0:bdd16076aaa5 | 244 | |
falingtrea | 0:bdd16076aaa5 | 245 | if (mdot_radio) { |
falingtrea | 0:bdd16076aaa5 | 246 | // reset to default config so we know what state we're in |
falingtrea | 0:bdd16076aaa5 | 247 | mdot_radio->resetConfig(); |
falingtrea | 0:bdd16076aaa5 | 248 | |
falingtrea | 0:bdd16076aaa5 | 249 | // Setting up LED1 as activity LED |
falingtrea | 0:bdd16076aaa5 | 250 | mdot_radio->setActivityLedPin(PB_0); |
falingtrea | 0:bdd16076aaa5 | 251 | mdot_radio->setActivityLedEnable(true); |
falingtrea | 0:bdd16076aaa5 | 252 | |
falingtrea | 0:bdd16076aaa5 | 253 | // Read node ID |
falingtrea | 0:bdd16076aaa5 | 254 | mdot_EUI = mdot_radio->getDeviceId(); |
falingtrea | 0:bdd16076aaa5 | 255 | printf("mDot EUI = "); |
falingtrea | 0:bdd16076aaa5 | 256 | |
falingtrea | 0:bdd16076aaa5 | 257 | for (i=0; i<mdot_EUI.size(); i++) { |
falingtrea | 0:bdd16076aaa5 | 258 | printf("%02x ", mdot_EUI[i]); |
falingtrea | 0:bdd16076aaa5 | 259 | } |
falingtrea | 0:bdd16076aaa5 | 260 | printf("\n\r"); |
falingtrea | 0:bdd16076aaa5 | 261 | |
falingtrea | 0:bdd16076aaa5 | 262 | |
falingtrea | 2:75adc72aa6a0 | 263 | // Setting up the mDot with network information. |
falingtrea | 2:75adc72aa6a0 | 264 | |
falingtrea | 2:75adc72aa6a0 | 265 | /* |
falingtrea | 2:75adc72aa6a0 | 266 | * This call sets up private or public mode on the MTDOT. Set the function to true if |
falingtrea | 2:75adc72aa6a0 | 267 | * connecting to a public network |
falingtrea | 2:75adc72aa6a0 | 268 | */ |
falingtrea | 2:75adc72aa6a0 | 269 | printf("setting Private Network Mode\r\n"); |
falingtrea | 2:75adc72aa6a0 | 270 | if ((mdot_ret = mdot_radio->setPublicNetwork(false)) != mDot::MDOT_OK) { |
falingtrea | 2:75adc72aa6a0 | 271 | log_error(mdot_radio, "failed to set Public Network Mode", mdot_ret); |
falingtrea | 2:75adc72aa6a0 | 272 | } |
falingtrea | 2:75adc72aa6a0 | 273 | |
falingtrea | 2:75adc72aa6a0 | 274 | /* |
falingtrea | 2:75adc72aa6a0 | 275 | * Frequency sub-band is valid for NAM only and for Private networks should be set to a value |
falingtrea | 2:75adc72aa6a0 | 276 | * between 1-8 that matches the the LoRa gateway setting. Public networks use sub-band 0 only. |
falingtrea | 2:75adc72aa6a0 | 277 | * This function can be commented out for EU networks |
falingtrea | 2:75adc72aa6a0 | 278 | */ |
falingtrea | 0:bdd16076aaa5 | 279 | printf("setting frequency sub band\r\n"); |
falingtrea | 0:bdd16076aaa5 | 280 | if ((mdot_ret = mdot_radio->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) { |
falingtrea | 0:bdd16076aaa5 | 281 | log_error(mdot_radio, "failed to set frequency sub band", mdot_ret); |
falingtrea | 0:bdd16076aaa5 | 282 | } |
falingtrea | 0:bdd16076aaa5 | 283 | |
falingtrea | 2:75adc72aa6a0 | 284 | /* |
falingtrea | 2:75adc72aa6a0 | 285 | * setNetworkName is used for private networks. |
falingtrea | 2:75adc72aa6a0 | 286 | * Use setNetworkID(AppID) for public networks |
falingtrea | 2:75adc72aa6a0 | 287 | */ |
falingtrea | 2:75adc72aa6a0 | 288 | |
falingtrea | 2:75adc72aa6a0 | 289 | // config_app_id.assign(app_id,app_id+7); |
falingtrea | 2:75adc72aa6a0 | 290 | |
falingtrea | 0:bdd16076aaa5 | 291 | printf("setting network name\r\n"); |
falingtrea | 0:bdd16076aaa5 | 292 | if ((mdot_ret = mdot_radio->setNetworkName(config_network_name)) != mDot::MDOT_OK) { |
falingtrea | 2:75adc72aa6a0 | 293 | // if ((mdot_ret = mdot_radio->setNetworkID(config_app_id)) != mDot::MDOT_OK) { |
falingtrea | 0:bdd16076aaa5 | 294 | log_error(mdot_radio, "failed to set network name", mdot_ret); |
falingtrea | 0:bdd16076aaa5 | 295 | } |
falingtrea | 0:bdd16076aaa5 | 296 | |
falingtrea | 2:75adc72aa6a0 | 297 | /* |
falingtrea | 2:75adc72aa6a0 | 298 | * setNetworkPassphrase is used for private networks |
falingtrea | 2:75adc72aa6a0 | 299 | * Use setNetworkKey for public networks |
falingtrea | 2:75adc72aa6a0 | 300 | */ |
falingtrea | 2:75adc72aa6a0 | 301 | |
falingtrea | 2:75adc72aa6a0 | 302 | // config_app_key.assign(app_key,app_key+15); |
falingtrea | 2:75adc72aa6a0 | 303 | |
falingtrea | 0:bdd16076aaa5 | 304 | printf("setting network password\r\n"); |
falingtrea | 0:bdd16076aaa5 | 305 | if ((mdot_ret = mdot_radio->setNetworkPassphrase(config_network_pass)) != mDot::MDOT_OK) { |
falingtrea | 2:75adc72aa6a0 | 306 | // if ((mdot_ret = mdot_radio->setNetworkKey(config_app_key)) != mDot::MDOT_OK) { |
falingtrea | 0:bdd16076aaa5 | 307 | log_error(mdot_radio, "failed to set network password", mdot_ret); |
falingtrea | 0:bdd16076aaa5 | 308 | } |
falingtrea | 0:bdd16076aaa5 | 309 | |
falingtrea | 0:bdd16076aaa5 | 310 | // attempt to join the network |
falingtrea | 0:bdd16076aaa5 | 311 | printf("joining network\r\n"); |
falingtrea | 2:75adc72aa6a0 | 312 | while (((mdot_ret = mdot_radio->joinNetwork()) != mDot::MDOT_OK) && (!exit_program)) { |
falingtrea | 0:bdd16076aaa5 | 313 | log_error(mdot_radio,"failed to join network:", mdot_ret); |
falingtrea | 0:bdd16076aaa5 | 314 | if (mdot_radio->getFrequencyBand() == mDot::FB_868){ |
falingtrea | 0:bdd16076aaa5 | 315 | mdot_ret = mdot_radio->getNextTxMs(); |
falingtrea | 0:bdd16076aaa5 | 316 | } |
falingtrea | 0:bdd16076aaa5 | 317 | else { |
falingtrea | 0:bdd16076aaa5 | 318 | mdot_ret = 0; |
falingtrea | 2:75adc72aa6a0 | 319 | } |
falingtrea | 0:bdd16076aaa5 | 320 | |
falingtrea | 0:bdd16076aaa5 | 321 | printf("delay = %lu\n\r",mdot_ret); |
falingtrea | 0:bdd16076aaa5 | 322 | osDelay(mdot_ret + 1); |
falingtrea | 0:bdd16076aaa5 | 323 | } |
falingtrea | 2:75adc72aa6a0 | 324 | |
falingtrea | 2:75adc72aa6a0 | 325 | /* |
falingtrea | 2:75adc72aa6a0 | 326 | * Check for PB1 press during network join attempt |
falingtrea | 2:75adc72aa6a0 | 327 | */ |
falingtrea | 2:75adc72aa6a0 | 328 | if (exit_program) { |
falingtrea | 2:75adc72aa6a0 | 329 | printf("Exiting program\n\r"); |
falingtrea | 2:75adc72aa6a0 | 330 | evbLCD->clearBuffer(); |
falingtrea | 2:75adc72aa6a0 | 331 | sprintf(txtstr,"Exiting Program"); |
falingtrea | 2:75adc72aa6a0 | 332 | evbLCD->writeText(0,4,font_6x8,txtstr,strlen(txtstr)); |
falingtrea | 2:75adc72aa6a0 | 333 | exit(1); |
falingtrea | 2:75adc72aa6a0 | 334 | } |
falingtrea | 2:75adc72aa6a0 | 335 | |
falingtrea | 0:bdd16076aaa5 | 336 | } else { |
falingtrea | 0:bdd16076aaa5 | 337 | printf("radio setup failed\n\r"); |
falingtrea | 0:bdd16076aaa5 | 338 | //exit(1); |
falingtrea | 0:bdd16076aaa5 | 339 | } |
falingtrea | 0:bdd16076aaa5 | 340 | |
falingtrea | 0:bdd16076aaa5 | 341 | osDelay(200); |
falingtrea | 0:bdd16076aaa5 | 342 | evbBackLight->setPWM(NCP5623B::LED_3,16); // enable LED2 on EVB and set to 50% PWM |
falingtrea | 0:bdd16076aaa5 | 343 | |
falingtrea | 2:75adc72aa6a0 | 344 | // sets LED2 to 50% max current |
falingtrea | 0:bdd16076aaa5 | 345 | evbBackLight->setLEDCurrent(16); |
falingtrea | 0:bdd16076aaa5 | 346 | |
falingtrea | 0:bdd16076aaa5 | 347 | printf("Start of Test\n\r"); |
falingtrea | 0:bdd16076aaa5 | 348 | |
falingtrea | 2:75adc72aa6a0 | 349 | osDelay (500); // allows other threads to process |
falingtrea | 2:75adc72aa6a0 | 350 | printf("shutdown LED:\n\r"); |
falingtrea | 2:75adc72aa6a0 | 351 | evbBackLight->shutdown(); |
falingtrea | 0:bdd16076aaa5 | 352 | |
falingtrea | 0:bdd16076aaa5 | 353 | osDelay (500); // allows other threads to process |
falingtrea | 0:bdd16076aaa5 | 354 | printf("Turn on LED2\n\r"); |
falingtrea | 0:bdd16076aaa5 | 355 | evbBackLight->setLEDCurrent(16); |
falingtrea | 0:bdd16076aaa5 | 356 | |
falingtrea | 0:bdd16076aaa5 | 357 | data = evbAccel->getWhoAmI(); |
falingtrea | 0:bdd16076aaa5 | 358 | printf("Accelerometer who_am_i value = %x \n\r", data); |
falingtrea | 0:bdd16076aaa5 | 359 | |
falingtrea | 0:bdd16076aaa5 | 360 | result = evbAccel->getStatus(); |
falingtrea | 0:bdd16076aaa5 | 361 | printf("status byte = %x \n\r", result); |
falingtrea | 0:bdd16076aaa5 | 362 | |
falingtrea | 0:bdd16076aaa5 | 363 | printf("Barometer who_am_i check = %s \n\r", evbBaro->testWhoAmI() ? "TRUE" : "FALSE"); |
falingtrea | 0:bdd16076aaa5 | 364 | |
falingtrea | 0:bdd16076aaa5 | 365 | result = evbBaro->getStatus(); |
falingtrea | 0:bdd16076aaa5 | 366 | printf("status byte = %x \n\r", result); |
falingtrea | 0:bdd16076aaa5 | 367 | |
falingtrea | 0:bdd16076aaa5 | 368 | /* |
falingtrea | 0:bdd16076aaa5 | 369 | * Setup the Accelerometer for 8g range, 14 bit resolution, Noise reduction off, sample rate 1.56 Hz |
falingtrea | 0:bdd16076aaa5 | 370 | * normal oversample mode, High pass filter off |
falingtrea | 0:bdd16076aaa5 | 371 | */ |
falingtrea | 0:bdd16076aaa5 | 372 | evbAccel->setCommonParameters(MMA845x::RANGE_8g,MMA845x::RES_MAX,MMA845x::LN_OFF, |
falingtrea | 0:bdd16076aaa5 | 373 | MMA845x::DR_1_56,MMA845x::OS_NORMAL,MMA845x::HPF_OFF ); |
falingtrea | 0:bdd16076aaa5 | 374 | |
falingtrea | 0:bdd16076aaa5 | 375 | /* |
falingtrea | 0:bdd16076aaa5 | 376 | * Setup the Barometric sensor for post processed Ambient pressure, 4 samples per data acquisition. |
falingtrea | 0:bdd16076aaa5 | 377 | * and a sample taken every second when in active mode |
falingtrea | 0:bdd16076aaa5 | 378 | */ |
falingtrea | 0:bdd16076aaa5 | 379 | evbBaro->setParameters(MPL3115A2::DATA_NORMAL, MPL3115A2::DM_BAROMETER, MPL3115A2::OR_16, |
falingtrea | 0:bdd16076aaa5 | 380 | MPL3115A2::AT_1); |
falingtrea | 0:bdd16076aaa5 | 381 | /* |
falingtrea | 0:bdd16076aaa5 | 382 | * Setup the Ambient Light Sensor for continuous Ambient Light Sensing, 16 bit resolution, |
falingtrea | 0:bdd16076aaa5 | 383 | * and 16000 lux range |
falingtrea | 0:bdd16076aaa5 | 384 | */ |
falingtrea | 0:bdd16076aaa5 | 385 | |
falingtrea | 0:bdd16076aaa5 | 386 | evbAmbLight->setMode(ISL29011::ALS_CONT); |
falingtrea | 0:bdd16076aaa5 | 387 | evbAmbLight->setResolution(ISL29011::ADC_16BIT); |
falingtrea | 0:bdd16076aaa5 | 388 | evbAmbLight->setRange(ISL29011::RNG_16000); |
falingtrea | 0:bdd16076aaa5 | 389 | |
falingtrea | 0:bdd16076aaa5 | 390 | /* |
falingtrea | 0:bdd16076aaa5 | 391 | * Set the accelerometer for active mode |
falingtrea | 0:bdd16076aaa5 | 392 | */ |
falingtrea | 0:bdd16076aaa5 | 393 | evbAccel->activeMode(); |
falingtrea | 0:bdd16076aaa5 | 394 | |
falingtrea | 0:bdd16076aaa5 | 395 | /* |
falingtrea | 0:bdd16076aaa5 | 396 | * Clear the min-max registers in the Barometric Sensor |
falingtrea | 0:bdd16076aaa5 | 397 | */ |
falingtrea | 0:bdd16076aaa5 | 398 | evbBaro->clearMinMaxRegs(); |
falingtrea | 0:bdd16076aaa5 | 399 | |
falingtrea | 0:bdd16076aaa5 | 400 | evbBackLight->setLEDCurrent(0); |
falingtrea | 0:bdd16076aaa5 | 401 | |
falingtrea | 0:bdd16076aaa5 | 402 | /* |
falingtrea | 2:75adc72aa6a0 | 403 | * Check for PB1 press during network join attempt |
falingtrea | 2:75adc72aa6a0 | 404 | */ |
falingtrea | 2:75adc72aa6a0 | 405 | if (exit_program) { |
falingtrea | 2:75adc72aa6a0 | 406 | printf("Exiting program\n\r"); |
falingtrea | 2:75adc72aa6a0 | 407 | evbLCD->clearBuffer(); |
falingtrea | 2:75adc72aa6a0 | 408 | sprintf(txtstr,"Exiting Program"); |
falingtrea | 2:75adc72aa6a0 | 409 | evbLCD->writeText(0,4,font_6x8,txtstr,strlen(txtstr)); |
falingtrea | 2:75adc72aa6a0 | 410 | exit(1); |
falingtrea | 2:75adc72aa6a0 | 411 | } |
falingtrea | 2:75adc72aa6a0 | 412 | |
falingtrea | 2:75adc72aa6a0 | 413 | /* |
falingtrea | 0:bdd16076aaa5 | 414 | * Main data acquisition loop |
falingtrea | 0:bdd16076aaa5 | 415 | */ |
falingtrea | 0:bdd16076aaa5 | 416 | pckt_time = 10; |
falingtrea | 0:bdd16076aaa5 | 417 | i = 0; |
falingtrea | 0:bdd16076aaa5 | 418 | do { |
falingtrea | 0:bdd16076aaa5 | 419 | evbLCD->startUpdate(); |
falingtrea | 0:bdd16076aaa5 | 420 | evbLCD->clearBuffer(); |
falingtrea | 0:bdd16076aaa5 | 421 | |
falingtrea | 0:bdd16076aaa5 | 422 | /* |
falingtrea | 0:bdd16076aaa5 | 423 | * Test Accelerometer XYZ data ready bit to see if acquisition complete |
falingtrea | 0:bdd16076aaa5 | 424 | */ |
daveheitzman | 6:b62e5106b5bc | 425 | failtime = FAIL_MAX; |
falingtrea | 0:bdd16076aaa5 | 426 | do { |
falingtrea | 0:bdd16076aaa5 | 427 | osDelay(100); // allows other threads to process |
falingtrea | 0:bdd16076aaa5 | 428 | result = evbAccel->getStatus(); |
daveheitzman | 6:b62e5106b5bc | 429 | failtime--; |
daveheitzman | 6:b62e5106b5bc | 430 | } while ((result & MMA845x::XYZDR) == 0 && failtime > 0); |
daveheitzman | 7:74c027d0353b | 431 | if (failtime==0) { |
daveheitzman | 7:74c027d0353b | 432 | evbBaro=resetBaro(evbBaro); |
daveheitzman | 7:74c027d0353b | 433 | continue; |
daveheitzman | 7:74c027d0353b | 434 | } |
falingtrea | 0:bdd16076aaa5 | 435 | |
falingtrea | 0:bdd16076aaa5 | 436 | /* |
falingtrea | 0:bdd16076aaa5 | 437 | * Retrieve and print out accelerometer data |
falingtrea | 0:bdd16076aaa5 | 438 | */ |
falingtrea | 0:bdd16076aaa5 | 439 | accel_data = evbAccel->getXYZ(); |
falingtrea | 0:bdd16076aaa5 | 440 | |
falingtrea | 0:bdd16076aaa5 | 441 | sprintf(txtstr,"Accelerometer"); |
falingtrea | 0:bdd16076aaa5 | 442 | evbLCD->writeText(0,0,font_6x8,txtstr,strlen(txtstr)); |
falingtrea | 0:bdd16076aaa5 | 443 | sprintf(txtstr, "x = %d", accel_data._x); |
falingtrea | 0:bdd16076aaa5 | 444 | evbLCD->writeText(20,1,font_6x8,txtstr,strlen(txtstr)); |
falingtrea | 0:bdd16076aaa5 | 445 | sprintf(txtstr, "y = %d", accel_data._y); |
falingtrea | 0:bdd16076aaa5 | 446 | evbLCD->writeText(20,2,font_6x8,txtstr,strlen(txtstr)); |
falingtrea | 0:bdd16076aaa5 | 447 | sprintf(txtstr, "z = %d", accel_data._z ); |
falingtrea | 0:bdd16076aaa5 | 448 | evbLCD->writeText(20,3,font_6x8,txtstr,strlen(txtstr)); |
falingtrea | 0:bdd16076aaa5 | 449 | |
falingtrea | 0:bdd16076aaa5 | 450 | /* |
falingtrea | 0:bdd16076aaa5 | 451 | * Trigger a Pressure reading |
falingtrea | 0:bdd16076aaa5 | 452 | */ |
falingtrea | 0:bdd16076aaa5 | 453 | evbBaro->setParameters(MPL3115A2::DATA_NORMAL, MPL3115A2::DM_BAROMETER, MPL3115A2::OR_16, |
falingtrea | 0:bdd16076aaa5 | 454 | MPL3115A2::AT_1); |
falingtrea | 0:bdd16076aaa5 | 455 | evbBaro->triggerOneShot(); |
falingtrea | 0:bdd16076aaa5 | 456 | |
falingtrea | 0:bdd16076aaa5 | 457 | /* |
falingtrea | 0:bdd16076aaa5 | 458 | * Test barometer device status to see if acquisition is complete |
falingtrea | 0:bdd16076aaa5 | 459 | */ |
daveheitzman | 6:b62e5106b5bc | 460 | failtime=FAIL_MAX; |
falingtrea | 0:bdd16076aaa5 | 461 | do { |
falingtrea | 0:bdd16076aaa5 | 462 | osDelay(100); // allows other threads to process |
falingtrea | 0:bdd16076aaa5 | 463 | result = evbBaro->getStatus(); |
daveheitzman | 6:b62e5106b5bc | 464 | failtime--; |
daveheitzman | 6:b62e5106b5bc | 465 | } while ((result & MPL3115A2::PTDR) == 0 && failtime > 0 ); |
daveheitzman | 7:74c027d0353b | 466 | if (failtime==0) { |
daveheitzman | 7:74c027d0353b | 467 | evbBaro=resetBaro(evbBaro); |
daveheitzman | 7:74c027d0353b | 468 | continue; |
daveheitzman | 7:74c027d0353b | 469 | } |
daveheitzman | 7:74c027d0353b | 470 | |
daveheitzman | 7:74c027d0353b | 471 | |
falingtrea | 0:bdd16076aaa5 | 472 | /* |
falingtrea | 0:bdd16076aaa5 | 473 | * Retrieve and print out barometric pressure |
falingtrea | 0:bdd16076aaa5 | 474 | */ |
falingtrea | 0:bdd16076aaa5 | 475 | pressure = evbBaro->getBaroData() >> 12; // convert 32 bit signed to 20 bit unsigned value |
falingtrea | 0:bdd16076aaa5 | 476 | num_whole = pressure >> 2; // 18 bit integer significant |
falingtrea | 0:bdd16076aaa5 | 477 | num_frac = (pressure & 0x3) * 25; // 2 bit fractional 0.25 per bit |
daveheitzman | 6:b62e5106b5bc | 478 | // if failtime reached 0 , indicates that the result might be junk. |
falingtrea | 0:bdd16076aaa5 | 479 | sprintf(txtstr,"Press=%ld.%02d Pa", num_whole, num_frac); |
daveheitzman | 5:5a794cedd728 | 480 | |
daveheitzman | 5:5a794cedd728 | 481 | pressure = evbBaro->getBaroData() >> 12; // convert 32 bit signed to 20 bit unsigned value |
daveheitzman | 5:5a794cedd728 | 482 | num_whole = pressure >> 2; // 18 bit integer significant |
daveheitzman | 5:5a794cedd728 | 483 | num_frac = (pressure & 0x3) * 25; // 2 bit fractional 0.25 per bit |
daveheitzman | 5:5a794cedd728 | 484 | |
daveheitzman | 6:b62e5106b5bc | 485 | writeValueOrError(); // will write to lorapresstring and txtstr |
daveheitzman | 5:5a794cedd728 | 486 | |
falingtrea | 0:bdd16076aaa5 | 487 | |
falingtrea | 0:bdd16076aaa5 | 488 | /* |
falingtrea | 0:bdd16076aaa5 | 489 | * Trigger a Altitude reading |
falingtrea | 0:bdd16076aaa5 | 490 | */ |
falingtrea | 0:bdd16076aaa5 | 491 | evbBaro->setParameters(MPL3115A2::DATA_NORMAL, MPL3115A2::DM_ALTIMETER, MPL3115A2::OR_16, |
falingtrea | 0:bdd16076aaa5 | 492 | MPL3115A2::AT_1); |
falingtrea | 0:bdd16076aaa5 | 493 | evbBaro->triggerOneShot(); |
falingtrea | 0:bdd16076aaa5 | 494 | |
falingtrea | 0:bdd16076aaa5 | 495 | /* |
falingtrea | 0:bdd16076aaa5 | 496 | * Test barometer device status to see if acquisition is complete |
falingtrea | 0:bdd16076aaa5 | 497 | */ |
daveheitzman | 6:b62e5106b5bc | 498 | failtime=FAIL_MAX; |
falingtrea | 0:bdd16076aaa5 | 499 | do { |
falingtrea | 0:bdd16076aaa5 | 500 | osDelay(100); // allows other threads to process |
falingtrea | 0:bdd16076aaa5 | 501 | result = evbBaro->getStatus(); |
daveheitzman | 6:b62e5106b5bc | 502 | failtime--; |
daveheitzman | 6:b62e5106b5bc | 503 | } while ((result & MPL3115A2::PTDR) == 0 && failtime > 0 ); |
daveheitzman | 7:74c027d0353b | 504 | if (failtime==0) { |
daveheitzman | 7:74c027d0353b | 505 | evbBaro=resetBaro(evbBaro); |
daveheitzman | 7:74c027d0353b | 506 | continue; |
daveheitzman | 7:74c027d0353b | 507 | } |
falingtrea | 0:bdd16076aaa5 | 508 | |
falingtrea | 0:bdd16076aaa5 | 509 | /* |
falingtrea | 0:bdd16076aaa5 | 510 | * Retrieve and print out altitude and temperature |
daveheitzman | 5:5a794cedd728 | 511 | const std::string lora_temp_string; |
daveheitzman | 5:5a794cedd728 | 512 | const std::string lora_alt_string; |
daveheitzman | 5:5a794cedd728 | 513 | const std::string lora_press_string; |
daveheitzman | 5:5a794cedd728 | 514 | const std::string lora_light_string; |
daveheitzman | 5:5a794cedd728 | 515 | |
falingtrea | 0:bdd16076aaa5 | 516 | */ |
falingtrea | 0:bdd16076aaa5 | 517 | baro_data = evbBaro->getAllData(false); |
falingtrea | 0:bdd16076aaa5 | 518 | baro_data._baro /= 4096; // convert 32 bit signed to 20 bit signed value |
falingtrea | 0:bdd16076aaa5 | 519 | num_whole = baro_data._baro / 16; // 18 bit signed significant integer |
falingtrea | 0:bdd16076aaa5 | 520 | num_frac = (baro_data._baro & 0xF) * 625 / 100; // 4 bit fractional .0625 per bit |
falingtrea | 0:bdd16076aaa5 | 521 | sprintf(txtstr,"Alti=%ld.%03d m", num_whole, num_frac); |
daveheitzman | 5:5a794cedd728 | 522 | sprintf(lora_alt_string,"%ld.%03d", num_whole, num_frac); |
falingtrea | 0:bdd16076aaa5 | 523 | evbLCD->writeText(0,5,font_6x8,txtstr,strlen(txtstr)); |
falingtrea | 0:bdd16076aaa5 | 524 | num_whole = baro_data._temp / 16; // 8 bit signed significant integer |
falingtrea | 0:bdd16076aaa5 | 525 | num_frac = (baro_data._temp & 0x0F) * 625 / 100; // 4 bit fractional .0625 per bit |
falingtrea | 0:bdd16076aaa5 | 526 | sprintf(txtstr,"Temp=%ld.%03d C", num_whole, num_frac); |
daveheitzman | 5:5a794cedd728 | 527 | sprintf(lora_temp_string,"%ld.%03d", num_whole, num_frac); |
falingtrea | 0:bdd16076aaa5 | 528 | evbLCD->writeText(0,6,font_6x8,txtstr,strlen(txtstr)); |
falingtrea | 0:bdd16076aaa5 | 529 | |
falingtrea | 0:bdd16076aaa5 | 530 | /* |
falingtrea | 0:bdd16076aaa5 | 531 | * retrieve and print out Ambient Light level |
falingtrea | 0:bdd16076aaa5 | 532 | */ |
falingtrea | 0:bdd16076aaa5 | 533 | lux_data = evbAmbLight->getData(); |
falingtrea | 0:bdd16076aaa5 | 534 | num_whole = lux_data * 24 / 100; // 16000 lux full scale .24 lux per bit |
falingtrea | 0:bdd16076aaa5 | 535 | num_frac = lux_data * 24 % 100; |
falingtrea | 0:bdd16076aaa5 | 536 | sprintf(txtstr, "Light=%ld.%02d lux", num_whole, num_frac ); |
daveheitzman | 5:5a794cedd728 | 537 | sprintf(lora_light_string, "%ld.%02d", num_whole, num_frac ); |
falingtrea | 0:bdd16076aaa5 | 538 | evbLCD->writeText(0,7,font_6x8,txtstr,strlen(txtstr)); |
skovarik | 9:daa92715f700 | 539 | |
skovarik | 9:daa92715f700 | 540 | /* |
skovarik | 9:daa92715f700 | 541 | * retrieve and print out moisture from Analog moisture sensor |
skovarik | 9:daa92715f700 | 542 | */ |
skovarik | 9:daa92715f700 | 543 | moisture = moisture_sensor; |
skovarik | 9:daa92715f700 | 544 | printf("moisture: %f\r\n", moisture); |
falingtrea | 0:bdd16076aaa5 | 545 | |
falingtrea | 0:bdd16076aaa5 | 546 | evbLCD->endUpdate(); |
falingtrea | 0:bdd16076aaa5 | 547 | printf("finished iteration %d\n\r",(++i)); |
falingtrea | 0:bdd16076aaa5 | 548 | if (i % pckt_time == 0) { // check packet counter will send packet every 2-5-10 data collection loops |
falingtrea | 0:bdd16076aaa5 | 549 | mdot_data.clear(); |
falingtrea | 0:bdd16076aaa5 | 550 | mdot_data.push_back(0x0E); // key for Current Acceleration 3-Axis Value |
falingtrea | 0:bdd16076aaa5 | 551 | converts.f_s = accel_data._x *4; // shift data 2 bits while retaining sign |
falingtrea | 0:bdd16076aaa5 | 552 | mdot_data.push_back(converts.t_u[1]); // get 8 MSB of 14 bit value |
falingtrea | 0:bdd16076aaa5 | 553 | converts.f_s = accel_data._y * 4; // shift data 2 bits while retaining sign |
falingtrea | 0:bdd16076aaa5 | 554 | mdot_data.push_back(converts.t_u[1]); // get 8 MSB of 14 bit value |
falingtrea | 0:bdd16076aaa5 | 555 | converts.f_s = accel_data._z * 4; // shift data 2 bits while retaining sign |
falingtrea | 0:bdd16076aaa5 | 556 | mdot_data.push_back(converts.t_u[1]); // get 8 MSB of 14 bit value |
falingtrea | 0:bdd16076aaa5 | 557 | mdot_data.push_back(0x08); // key for Current Pressure Value |
falingtrea | 0:bdd16076aaa5 | 558 | convertl.f_u = pressure; // pressure data is 20 bits unsigned |
falingtrea | 0:bdd16076aaa5 | 559 | mdot_data.push_back(convertl.t_u[2]); |
falingtrea | 0:bdd16076aaa5 | 560 | mdot_data.push_back(convertl.t_u[1]); |
falingtrea | 0:bdd16076aaa5 | 561 | mdot_data.push_back(convertl.t_u[0]); |
falingtrea | 0:bdd16076aaa5 | 562 | mdot_data.push_back(0x05); // key for Current Ambient Light Value |
falingtrea | 0:bdd16076aaa5 | 563 | converts.f_u = lux_data; // data is 16 bits unsigned |
falingtrea | 0:bdd16076aaa5 | 564 | mdot_data.push_back(converts.t_u[1]); |
falingtrea | 0:bdd16076aaa5 | 565 | mdot_data.push_back(converts.t_u[0]); |
falingtrea | 0:bdd16076aaa5 | 566 | mdot_data.push_back(0x0B); // key for Current Temperature Value |
falingtrea | 0:bdd16076aaa5 | 567 | converts.f_s = baro_data._temp; // temperature is signed 12 bit |
falingtrea | 0:bdd16076aaa5 | 568 | mdot_data.push_back(converts.t_u[1]); |
falingtrea | 0:bdd16076aaa5 | 569 | mdot_data.push_back(converts.t_u[0]); |
daveheitzman | 5:5a794cedd728 | 570 | mdot_data.clear(); |
daveheitzman | 5:5a794cedd728 | 571 | |
daveheitzman | 5:5a794cedd728 | 572 | // if you want to go back to sending byte vector instead: |
daveheitzman | 5:5a794cedd728 | 573 | // if ((mdot_ret = mdot_radio->send(mdot_data)) != mDot::MDOT_OK) { |
daveheitzman | 5:5a794cedd728 | 574 | // sprintf(sensor_text, "x:%d,y:%d,z:%d,p:%s,a:%s,t:%s,l:%s", |
daveheitzman | 5:5a794cedd728 | 575 | |
skovarik | 9:daa92715f700 | 576 | sprintf(sensor_text, "%d,%d,%d,%s,%s,%s,%s,%f", |
daveheitzman | 5:5a794cedd728 | 577 | accel_data._x, |
daveheitzman | 5:5a794cedd728 | 578 | accel_data._y, |
daveheitzman | 5:5a794cedd728 | 579 | accel_data._z, |
daveheitzman | 5:5a794cedd728 | 580 | lora_press_string, |
daveheitzman | 5:5a794cedd728 | 581 | lora_alt_string, |
daveheitzman | 5:5a794cedd728 | 582 | lora_temp_string, |
skovarik | 9:daa92715f700 | 583 | lora_light_string, |
skovarik | 9:daa92715f700 | 584 | moisture); |
daveheitzman | 5:5a794cedd728 | 585 | if ((mdot_ret = sendString((const std::string)sensor_text)) != mDot::MDOT_OK) { |
falingtrea | 0:bdd16076aaa5 | 586 | log_error(mdot_radio, "failed to send", mdot_ret); |
falingtrea | 0:bdd16076aaa5 | 587 | } else { |
falingtrea | 0:bdd16076aaa5 | 588 | printf("successfully sent data to gateway\r\n"); |
falingtrea | 0:bdd16076aaa5 | 589 | } |
falingtrea | 0:bdd16076aaa5 | 590 | } |
daveheitzman | 5:5a794cedd728 | 591 | |
daveheitzman | 4:51d7c24bbf91 | 592 | } while(!exit_program); |
falingtrea | 0:bdd16076aaa5 | 593 | |
falingtrea | 0:bdd16076aaa5 | 594 | evbBaro->triggerOneShot(); |
falingtrea | 0:bdd16076aaa5 | 595 | |
falingtrea | 0:bdd16076aaa5 | 596 | do { |
falingtrea | 0:bdd16076aaa5 | 597 | osDelay(200); // allows other threads to process |
falingtrea | 0:bdd16076aaa5 | 598 | result = evbBaro->getStatus(); |
falingtrea | 0:bdd16076aaa5 | 599 | } while ((result & MPL3115A2::PTDR) == 0 ); |
falingtrea | 0:bdd16076aaa5 | 600 | |
falingtrea | 0:bdd16076aaa5 | 601 | baro_data = evbBaro->getAllData(true); |
falingtrea | 0:bdd16076aaa5 | 602 | printf ("minBaro=%ld maxBaro=%ld minTemp=%d maxTemp=%d\n\r", baro_data._minbaro, baro_data._maxbaro, |
falingtrea | 0:bdd16076aaa5 | 603 | baro_data._mintemp, baro_data._maxtemp); |
falingtrea | 0:bdd16076aaa5 | 604 | |
falingtrea | 0:bdd16076aaa5 | 605 | printf("End of Test\n\r"); |
falingtrea | 0:bdd16076aaa5 | 606 | |
falingtrea | 2:75adc72aa6a0 | 607 | evbLCD->clearBuffer(); |
falingtrea | 2:75adc72aa6a0 | 608 | sprintf(txtstr,"Exiting Program"); |
falingtrea | 2:75adc72aa6a0 | 609 | evbLCD->writeText(0,4,font_6x8,txtstr,strlen(txtstr)); |
falingtrea | 2:75adc72aa6a0 | 610 | |
falingtrea | 0:bdd16076aaa5 | 611 | } |
falingtrea | 0:bdd16076aaa5 | 612 | |
falingtrea | 0:bdd16076aaa5 | 613 | /* |
falingtrea | 3:68e974f5f532 | 614 | * Sets pb1_low flag. Slag is cleared in pb1_debounce thread |
falingtrea | 0:bdd16076aaa5 | 615 | */ |
falingtrea | 0:bdd16076aaa5 | 616 | void pb1ISR(void) |
falingtrea | 0:bdd16076aaa5 | 617 | { |
falingtrea | 3:68e974f5f532 | 618 | if (!pb1_low) |
falingtrea | 3:68e974f5f532 | 619 | pb1_low = true; |
falingtrea | 3:68e974f5f532 | 620 | } |
falingtrea | 0:bdd16076aaa5 | 621 | |
falingtrea | 3:68e974f5f532 | 622 | /* |
falingtrea | 3:68e974f5f532 | 623 | * Debounces pb1. Also exits program if pushbutton 1 is pressed |
falingtrea | 3:68e974f5f532 | 624 | */ |
falingtrea | 3:68e974f5f532 | 625 | void pb1_debounce(void const *args) |
falingtrea | 3:68e974f5f532 | 626 | { |
falingtrea | 3:68e974f5f532 | 627 | |
falingtrea | 3:68e974f5f532 | 628 | static uint8_t count = 0; |
falingtrea | 3:68e974f5f532 | 629 | |
falingtrea | 3:68e974f5f532 | 630 | while (true) { |
falingtrea | 3:68e974f5f532 | 631 | |
falingtrea | 3:68e974f5f532 | 632 | if (pb1_low && (mDot08 == 0)) |
falingtrea | 3:68e974f5f532 | 633 | count++; |
falingtrea | 3:68e974f5f532 | 634 | else { |
falingtrea | 3:68e974f5f532 | 635 | count = 0; |
falingtrea | 3:68e974f5f532 | 636 | pb1_low = false; |
falingtrea | 3:68e974f5f532 | 637 | } |
falingtrea | 3:68e974f5f532 | 638 | |
falingtrea | 3:68e974f5f532 | 639 | if (count == 5) |
falingtrea | 3:68e974f5f532 | 640 | exit_program = true; |
falingtrea | 3:68e974f5f532 | 641 | |
falingtrea | 3:68e974f5f532 | 642 | Thread::wait(5); |
falingtrea | 3:68e974f5f532 | 643 | } |
falingtrea | 0:bdd16076aaa5 | 644 | } |
falingtrea | 0:bdd16076aaa5 | 645 | |
falingtrea | 0:bdd16076aaa5 | 646 | /* |
falingtrea | 3:68e974f5f532 | 647 | * Sets pb2_low flag. Flag is cleared in pb2_debounce thread |
falingtrea | 0:bdd16076aaa5 | 648 | */ |
falingtrea | 0:bdd16076aaa5 | 649 | void pb2ISR(void) |
falingtrea | 0:bdd16076aaa5 | 650 | { |
falingtrea | 3:68e974f5f532 | 651 | if (!pb2_low) |
falingtrea | 3:68e974f5f532 | 652 | pb2_low = true; |
falingtrea | 3:68e974f5f532 | 653 | } |
falingtrea | 0:bdd16076aaa5 | 654 | |
falingtrea | 3:68e974f5f532 | 655 | /* |
falingtrea | 3:68e974f5f532 | 656 | * Debounces pb2. Also changes packet transmit time to every other, |
falingtrea | 3:68e974f5f532 | 657 | * every fifth, or every tenth sample when SW2 pushed |
falingtrea | 3:68e974f5f532 | 658 | * Also triggers a thread to transmit a configuration packet |
falingtrea | 3:68e974f5f532 | 659 | */ |
falingtrea | 3:68e974f5f532 | 660 | void pb2_debounce(void const *args) |
falingtrea | 3:68e974f5f532 | 661 | { |
falingtrea | 3:68e974f5f532 | 662 | |
falingtrea | 3:68e974f5f532 | 663 | static uint8_t count = 0; |
falingtrea | 3:68e974f5f532 | 664 | |
falingtrea | 3:68e974f5f532 | 665 | while (true) { |
falingtrea | 0:bdd16076aaa5 | 666 | |
falingtrea | 3:68e974f5f532 | 667 | if (pb2_low && (mDot09 == 0)) |
falingtrea | 3:68e974f5f532 | 668 | count++; |
falingtrea | 3:68e974f5f532 | 669 | else { |
falingtrea | 3:68e974f5f532 | 670 | count = 0; |
falingtrea | 3:68e974f5f532 | 671 | pb2_low = false; |
falingtrea | 3:68e974f5f532 | 672 | } |
falingtrea | 3:68e974f5f532 | 673 | |
falingtrea | 3:68e974f5f532 | 674 | if (count == 5){ |
falingtrea | 0:bdd16076aaa5 | 675 | |
falingtrea | 3:68e974f5f532 | 676 | if (pckt_time >= 5) |
falingtrea | 3:68e974f5f532 | 677 | pckt_time /= 2; |
falingtrea | 3:68e974f5f532 | 678 | else pckt_time = 20; |
falingtrea | 3:68e974f5f532 | 679 | |
falingtrea | 3:68e974f5f532 | 680 | thread_3->signal_set(0x10); // signal config_pkt_xmit to send packet |
falingtrea | 3:68e974f5f532 | 681 | } |
falingtrea | 3:68e974f5f532 | 682 | |
falingtrea | 3:68e974f5f532 | 683 | Thread::wait(5); |
falingtrea | 3:68e974f5f532 | 684 | } |
falingtrea | 3:68e974f5f532 | 685 | } |
falingtrea | 0:bdd16076aaa5 | 686 | |
falingtrea | 2:75adc72aa6a0 | 687 | /* |
falingtrea | 2:75adc72aa6a0 | 688 | * Function that print clear text verion of mDot errors |
falingtrea | 2:75adc72aa6a0 | 689 | */ |
falingtrea | 0:bdd16076aaa5 | 690 | void log_error(mDot* dot, const char* msg, int32_t retval) |
falingtrea | 0:bdd16076aaa5 | 691 | { |
falingtrea | 0:bdd16076aaa5 | 692 | printf("%s - %ld:%s, %s\r\n", msg, retval, mDot::getReturnCodeString(retval).c_str(), dot->getLastError().c_str()); |
falingtrea | 0:bdd16076aaa5 | 693 | } |
falingtrea | 0:bdd16076aaa5 | 694 | |
falingtrea | 2:75adc72aa6a0 | 695 | /* |
falingtrea | 2:75adc72aa6a0 | 696 | * Thread that is triggered by SW2 ISR. Sends a packet to the LoRa server with the new Packet Transmission time setting |
falingtrea | 2:75adc72aa6a0 | 697 | */ |
falingtrea | 0:bdd16076aaa5 | 698 | void config_pkt_xmit (void const *args) |
falingtrea | 0:bdd16076aaa5 | 699 | { |
falingtrea | 0:bdd16076aaa5 | 700 | |
falingtrea | 0:bdd16076aaa5 | 701 | std::vector<uint8_t> data; |
falingtrea | 0:bdd16076aaa5 | 702 | |
falingtrea | 0:bdd16076aaa5 | 703 | while (true) { |
falingtrea | 0:bdd16076aaa5 | 704 | Thread::signal_wait(0x10); // wait for pb2ISR to signal send |
falingtrea | 0:bdd16076aaa5 | 705 | data.clear(); |
falingtrea | 0:bdd16076aaa5 | 706 | data.push_back(0x0F); // key for Configuration data (packet transmission timer) |
falingtrea | 0:bdd16076aaa5 | 707 | data.push_back(pckt_time); |
falingtrea | 0:bdd16076aaa5 | 708 | |
falingtrea | 0:bdd16076aaa5 | 709 | if ((mdot_ret = mdot_radio->send(data)) != mDot::MDOT_OK) { |
falingtrea | 0:bdd16076aaa5 | 710 | log_error(mdot_radio, "failed to send config data", mdot_ret); |
falingtrea | 0:bdd16076aaa5 | 711 | } else { |
falingtrea | 0:bdd16076aaa5 | 712 | printf("sent config data to gateway\r\n"); |
falingtrea | 0:bdd16076aaa5 | 713 | } |
falingtrea | 0:bdd16076aaa5 | 714 | } |
falingtrea | 0:bdd16076aaa5 | 715 | } |
daveheitzman | 5:5a794cedd728 | 716 | |
daveheitzman | 5:5a794cedd728 | 717 | int32_t sendString(const std::string text) |
daveheitzman | 5:5a794cedd728 | 718 | { |
daveheitzman | 5:5a794cedd728 | 719 | int32_t ret; |
daveheitzman | 5:5a794cedd728 | 720 | if (mdot_radio->getNextTxMs() != 0) { |
daveheitzman | 5:5a794cedd728 | 721 | printf("Sending in %lu ms...\r\n", mdot_radio->getNextTxMs()); |
daveheitzman | 5:5a794cedd728 | 722 | return false; |
daveheitzman | 5:5a794cedd728 | 723 | } |
daveheitzman | 5:5a794cedd728 | 724 | |
daveheitzman | 5:5a794cedd728 | 725 | printf("Sending: '%s' ", text.c_str()); |
daveheitzman | 5:5a794cedd728 | 726 | std::vector<uint8_t> data(text.begin(), text.end()); |
daveheitzman | 5:5a794cedd728 | 727 | if ((ret = mdot_radio->send(data, 1)) != mDot::MDOT_OK) |
daveheitzman | 5:5a794cedd728 | 728 | { |
daveheitzman | 5:5a794cedd728 | 729 | log_error(mdot_radio, "Failed to send data", ret); |
daveheitzman | 5:5a794cedd728 | 730 | |
daveheitzman | 5:5a794cedd728 | 731 | } else { |
daveheitzman | 5:5a794cedd728 | 732 | printf("Data sent!\r\n"); |
daveheitzman | 5:5a794cedd728 | 733 | } |
daveheitzman | 5:5a794cedd728 | 734 | return ret; |
daveheitzman | 6:b62e5106b5bc | 735 | } |
daveheitzman | 6:b62e5106b5bc | 736 | |
daveheitzman | 6:b62e5106b5bc | 737 | bool writeValueOrError() |
daveheitzman | 6:b62e5106b5bc | 738 | { |
daveheitzman | 6:b62e5106b5bc | 739 | bool res; |
daveheitzman | 6:b62e5106b5bc | 740 | if (failtime==0){ |
daveheitzman | 6:b62e5106b5bc | 741 | sprintf(lora_press_string, "%s", "--.--"); |
daveheitzman | 6:b62e5106b5bc | 742 | sprintf(txtstr, "%s", "--.--"); |
daveheitzman | 6:b62e5106b5bc | 743 | evbLCD->writeText(0,4,font_6x8,txtstr, strlen(txtstr)); |
daveheitzman | 6:b62e5106b5bc | 744 | res=false; |
daveheitzman | 6:b62e5106b5bc | 745 | }else{ |
daveheitzman | 6:b62e5106b5bc | 746 | sprintf(lora_press_string, "%ld.%02d", num_whole, num_frac); |
daveheitzman | 6:b62e5106b5bc | 747 | evbLCD->writeText(0,4,font_6x8,txtstr,strlen(txtstr)); |
daveheitzman | 6:b62e5106b5bc | 748 | res=true; |
daveheitzman | 6:b62e5106b5bc | 749 | } |
daveheitzman | 6:b62e5106b5bc | 750 | return res; |
daveheitzman | 6:b62e5106b5bc | 751 | } |
daveheitzman | 6:b62e5106b5bc | 752 | |
daveheitzman | 7:74c027d0353b | 753 | MPL3115A2* resetBaro(const MPL3115A2* oldBaro){ |
daveheitzman | 7:74c027d0353b | 754 | delete oldBaro; |
daveheitzman | 7:74c027d0353b | 755 | MPL3115A2* baro = new MPL3115A2(mDoti2c); |
daveheitzman | 7:74c027d0353b | 756 | baro->testWhoAmI(); |
daveheitzman | 7:74c027d0353b | 757 | |
daveheitzman | 7:74c027d0353b | 758 | printf("Resetting barometer.. %x \n\r", baro->getStatus() ); |
daveheitzman | 7:74c027d0353b | 759 | baro->setParameters(MPL3115A2::DATA_NORMAL, MPL3115A2::DM_BAROMETER, MPL3115A2::OR_16, |
daveheitzman | 7:74c027d0353b | 760 | MPL3115A2::AT_1); |
daveheitzman | 7:74c027d0353b | 761 | evbBaro->clearMinMaxRegs(); |
daveheitzman | 7:74c027d0353b | 762 | return baro; |
daveheitzman | 7:74c027d0353b | 763 | } |