Ag demo with soil moisture

Dependencies:   DOGS102 ISL29011 MMA845x MPL3115A2 NCP5623B libmDot mbed-rtos mbed-src

Fork of MTDOT-EVBDemo-DRH by Dave Heitzman

Committer:
daveheitzman
Date:
Mon Aug 24 16:38:59 2015 +0000
Revision:
6:b62e5106b5bc
Parent:
5:5a794cedd728
Child:
7:74c027d0353b
During long-running loop, discards any iterations that cause bad calls, and just continues instead of getting stuck in retry loop. Communicates on baud rate 115200 for ease of use.

Who changed what in which revision?

UserRevisionLine numberNew 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
falingtrea 2:75adc72aa6a0 99 /* **** replace these values with the proper public or private network settings ****
falingtrea 2:75adc72aa6a0 100 * config_network_nameand config_network_pass are for private networks.
falingtrea 2:75adc72aa6a0 101 */
daveheitzman 4:51d7c24bbf91 102 static std::string config_network_name = "Escalation";
daveheitzman 4:51d7c24bbf91 103 static std::string config_network_pass = "Escalation";
daveheitzman 4:51d7c24bbf91 104 static uint8_t config_frequency_sub_band = 3;
falingtrea 0:bdd16076aaa5 105
falingtrea 2:75adc72aa6a0 106 /* config_app_id and config_app_key are for public networks.
falingtrea 2:75adc72aa6a0 107 static uint8_t app_id[8] = {0x00,0x01,0x02,0x03,0x0A,0x0B,0x0C,0x0D};
falingtrea 2:75adc72aa6a0 108 std::vector<uint8_t> config_app_id;
falingtrea 2:75adc72aa6a0 109 static uint8_t app_key[16] = {0x00,0x01,0x02,0x03,0x0A,0x0B,0x0C,0x0D};
falingtrea 2:75adc72aa6a0 110 std::vector<uint8_t> config_app_key;
falingtrea 2:75adc72aa6a0 111 */
falingtrea 2:75adc72aa6a0 112
falingtrea 0:bdd16076aaa5 113 uint8_t result, pckt_time=10;
falingtrea 0:bdd16076aaa5 114 char data;
falingtrea 0:bdd16076aaa5 115 unsigned char test;
falingtrea 0:bdd16076aaa5 116 char txtstr[17];
falingtrea 0:bdd16076aaa5 117 int32_t num_whole, mdot_ret;
falingtrea 0:bdd16076aaa5 118 uint32_t pressure;
falingtrea 0:bdd16076aaa5 119 int16_t num_frac;
falingtrea 0:bdd16076aaa5 120
falingtrea 0:bdd16076aaa5 121 bool exit_program = false;
falingtrea 0:bdd16076aaa5 122
falingtrea 0:bdd16076aaa5 123 MMA845x_DATA accel_data;
falingtrea 0:bdd16076aaa5 124 MPL3115A2_DATA baro_data;
falingtrea 0:bdd16076aaa5 125 uint16_t lux_data;
falingtrea 0:bdd16076aaa5 126 MMA845x* evbAccel;
falingtrea 0:bdd16076aaa5 127 MPL3115A2* evbBaro;
falingtrea 0:bdd16076aaa5 128 ISL29011* evbAmbLight;
falingtrea 0:bdd16076aaa5 129 NCP5623B* evbBackLight;
falingtrea 0:bdd16076aaa5 130 DOGS102* evbLCD;
falingtrea 0:bdd16076aaa5 131 mDot* mdot_radio;
falingtrea 0:bdd16076aaa5 132
falingtrea 0:bdd16076aaa5 133 convert32 convertl;
falingtrea 0:bdd16076aaa5 134 convert16 converts;
falingtrea 0:bdd16076aaa5 135
falingtrea 3:68e974f5f532 136 // flags for pushbutton debounce code
falingtrea 3:68e974f5f532 137 bool pb1_low = false;
falingtrea 3:68e974f5f532 138 bool pb2_low = false;
falingtrea 3:68e974f5f532 139
falingtrea 0:bdd16076aaa5 140 void pb1ISR(void);
falingtrea 0:bdd16076aaa5 141 void pb2ISR(void);
falingtrea 3:68e974f5f532 142 void pb1_debounce(void const *args);
falingtrea 3:68e974f5f532 143 void pb2_debounce(void const *args);
falingtrea 3:68e974f5f532 144 Thread* thread_3;
falingtrea 0:bdd16076aaa5 145
falingtrea 0:bdd16076aaa5 146 void log_error(mDot* dot, const char* msg, int32_t retval);
falingtrea 0:bdd16076aaa5 147
falingtrea 0:bdd16076aaa5 148 void config_pkt_xmit (void const *args);
daveheitzman 5:5a794cedd728 149 int32_t sendString(const std::string text);
falingtrea 0:bdd16076aaa5 150
daveheitzman 6:b62e5106b5bc 151 bool writeValueOrError();
daveheitzman 6:b62e5106b5bc 152
daveheitzman 6:b62e5106b5bc 153 char sensor_text[54];
daveheitzman 6:b62e5106b5bc 154 char lora_temp_string[7];
daveheitzman 6:b62e5106b5bc 155 char lora_alt_string[8];
daveheitzman 6:b62e5106b5bc 156 char lora_press_string[9];
daveheitzman 6:b62e5106b5bc 157
daveheitzman 6:b62e5106b5bc 158 char lora_light_string[6];
daveheitzman 6:b62e5106b5bc 159 const int FAIL_MAX=15;
daveheitzman 6:b62e5106b5bc 160 int failtime=FAIL_MAX;
daveheitzman 6:b62e5106b5bc 161
falingtrea 0:bdd16076aaa5 162 int main()
falingtrea 0:bdd16076aaa5 163 {
falingtrea 0:bdd16076aaa5 164
falingtrea 0:bdd16076aaa5 165 std::vector<uint8_t> mdot_data;
falingtrea 0:bdd16076aaa5 166 std::vector<uint8_t> mdot_EUI;
falingtrea 0:bdd16076aaa5 167 uint16_t i = 0;
falingtrea 0:bdd16076aaa5 168
daveheitzman 6:b62e5106b5bc 169 debugUART.baud(115200);
falingtrea 0:bdd16076aaa5 170 // mDotUART.baud(9600); // mdot UART unused but available on external connector
falingtrea 0:bdd16076aaa5 171
falingtrea 3:68e974f5f532 172 Thread thread_1(pb1_debounce); // threads for de-bouncing pushbutton switches
falingtrea 3:68e974f5f532 173 Thread thread_2(pb2_debounce);
falingtrea 3:68e974f5f532 174
falingtrea 3:68e974f5f532 175 thread_3 = new Thread(config_pkt_xmit); // start thread that sends LoRa packet when SW2 pressed
falingtrea 2:75adc72aa6a0 176
falingtrea 1:ac9595d0f0e7 177 evbAccel = new MMA845x(mDoti2c,MMA845x::SA0_VSS); // setup Accelerometer
daveheitzman 6:b62e5106b5bc 178 evbBaro = new MPL3115A2(mDoti2c); // setup0 Barometric sensor
falingtrea 1:ac9595d0f0e7 179 evbAmbLight = new ISL29011(mDoti2c); // Setup Ambient Light Sensor
falingtrea 0:bdd16076aaa5 180 evbBackLight = new NCP5623B(mDoti2c); // setup backlight and LED 2 driver chip
falingtrea 1:ac9595d0f0e7 181 evbLCD = new DOGS102(mDotspi, mDot17, mDot13); // setup LCD
falingtrea 0:bdd16076aaa5 182
falingtrea 2:75adc72aa6a0 183 /*
falingtrea 2:75adc72aa6a0 184 * Setup SW1 as program stop function
falingtrea 2:75adc72aa6a0 185 */
falingtrea 2:75adc72aa6a0 186 mDot08.disable_irq();
falingtrea 2:75adc72aa6a0 187 mDot08.fall(&pb1ISR);
falingtrea 2:75adc72aa6a0 188
falingtrea 2:75adc72aa6a0 189 /*
falingtrea 2:75adc72aa6a0 190 * need to call this function after rise or fall because rise/fall sets
falingtrea 2:75adc72aa6a0 191 * mode to PullNone
falingtrea 2:75adc72aa6a0 192 */
falingtrea 2:75adc72aa6a0 193 mDot08.mode(PullUp);
falingtrea 2:75adc72aa6a0 194
falingtrea 2:75adc72aa6a0 195 mDot08.enable_irq();
falingtrea 2:75adc72aa6a0 196
falingtrea 2:75adc72aa6a0 197 /*
falingtrea 2:75adc72aa6a0 198 * Setup SW2 as packet time change
falingtrea 2:75adc72aa6a0 199 */
falingtrea 2:75adc72aa6a0 200 mDot09.disable_irq();
falingtrea 2:75adc72aa6a0 201 mDot09.fall(&pb2ISR);
falingtrea 2:75adc72aa6a0 202
falingtrea 2:75adc72aa6a0 203 /*
falingtrea 2:75adc72aa6a0 204 * need to call this function after rise or fall because rise/fall sets
falingtrea 2:75adc72aa6a0 205 * mode to PullNone
falingtrea 2:75adc72aa6a0 206 */
falingtrea 2:75adc72aa6a0 207 mDot09.mode(PullUp);
falingtrea 2:75adc72aa6a0 208
falingtrea 2:75adc72aa6a0 209 mDot09.enable_irq();
falingtrea 2:75adc72aa6a0 210
falingtrea 2:75adc72aa6a0 211 /*
falingtrea 2:75adc72aa6a0 212 * Setting other InterruptIn pins with Pull Ups
falingtrea 2:75adc72aa6a0 213 */
falingtrea 2:75adc72aa6a0 214 mDot12.mode(PullUp);
falingtrea 2:75adc72aa6a0 215 mDot15.mode(PullUp);
falingtrea 2:75adc72aa6a0 216 mDot16.mode(PullUp);
falingtrea 2:75adc72aa6a0 217
falingtrea 2:75adc72aa6a0 218 printf("font table address %p\n\r",&font_6x8);
falingtrea 2:75adc72aa6a0 219 printf("bitmap address %p\n\r",&MultiTech_Logo);
falingtrea 2:75adc72aa6a0 220
falingtrea 2:75adc72aa6a0 221 // Setup and display logo on LCD
falingtrea 2:75adc72aa6a0 222 evbLCD->startUpdate();
falingtrea 2:75adc72aa6a0 223
falingtrea 2:75adc72aa6a0 224 evbLCD->writeBitmap(0,0,MultiTech_Logo);
falingtrea 2:75adc72aa6a0 225
falingtrea 2:75adc72aa6a0 226 sprintf(txtstr,"MTDOT");
falingtrea 2:75adc72aa6a0 227 evbLCD->writeText(24,3,font_6x8,txtstr,strlen(txtstr));
falingtrea 2:75adc72aa6a0 228 sprintf(txtstr,"Evaluation");
falingtrea 2:75adc72aa6a0 229 evbLCD->writeText(24,4,font_6x8,txtstr,strlen(txtstr));
falingtrea 2:75adc72aa6a0 230 sprintf(txtstr,"Board");
falingtrea 2:75adc72aa6a0 231 evbLCD->writeText(24,5,font_6x8,txtstr,strlen(txtstr));
falingtrea 2:75adc72aa6a0 232
falingtrea 2:75adc72aa6a0 233 evbLCD->endUpdate();
falingtrea 2:75adc72aa6a0 234
falingtrea 2:75adc72aa6a0 235 printf("\n\r setup mdot\n\r");
falingtrea 0:bdd16076aaa5 236
falingtrea 0:bdd16076aaa5 237 // get a mDot handle
falingtrea 0:bdd16076aaa5 238 mdot_radio = mDot::getInstance();
falingtrea 0:bdd16076aaa5 239
falingtrea 0:bdd16076aaa5 240 if (mdot_radio) {
falingtrea 0:bdd16076aaa5 241 // reset to default config so we know what state we're in
falingtrea 0:bdd16076aaa5 242 mdot_radio->resetConfig();
falingtrea 0:bdd16076aaa5 243
falingtrea 0:bdd16076aaa5 244 // Setting up LED1 as activity LED
falingtrea 0:bdd16076aaa5 245 mdot_radio->setActivityLedPin(PB_0);
falingtrea 0:bdd16076aaa5 246 mdot_radio->setActivityLedEnable(true);
falingtrea 0:bdd16076aaa5 247
falingtrea 0:bdd16076aaa5 248 // Read node ID
falingtrea 0:bdd16076aaa5 249 mdot_EUI = mdot_radio->getDeviceId();
falingtrea 0:bdd16076aaa5 250 printf("mDot EUI = ");
falingtrea 0:bdd16076aaa5 251
falingtrea 0:bdd16076aaa5 252 for (i=0; i<mdot_EUI.size(); i++) {
falingtrea 0:bdd16076aaa5 253 printf("%02x ", mdot_EUI[i]);
falingtrea 0:bdd16076aaa5 254 }
falingtrea 0:bdd16076aaa5 255 printf("\n\r");
falingtrea 0:bdd16076aaa5 256
falingtrea 0:bdd16076aaa5 257
falingtrea 2:75adc72aa6a0 258 // Setting up the mDot with network information.
falingtrea 2:75adc72aa6a0 259
falingtrea 2:75adc72aa6a0 260 /*
falingtrea 2:75adc72aa6a0 261 * This call sets up private or public mode on the MTDOT. Set the function to true if
falingtrea 2:75adc72aa6a0 262 * connecting to a public network
falingtrea 2:75adc72aa6a0 263 */
falingtrea 2:75adc72aa6a0 264 printf("setting Private Network Mode\r\n");
falingtrea 2:75adc72aa6a0 265 if ((mdot_ret = mdot_radio->setPublicNetwork(false)) != mDot::MDOT_OK) {
falingtrea 2:75adc72aa6a0 266 log_error(mdot_radio, "failed to set Public Network Mode", mdot_ret);
falingtrea 2:75adc72aa6a0 267 }
falingtrea 2:75adc72aa6a0 268
falingtrea 2:75adc72aa6a0 269 /*
falingtrea 2:75adc72aa6a0 270 * Frequency sub-band is valid for NAM only and for Private networks should be set to a value
falingtrea 2:75adc72aa6a0 271 * between 1-8 that matches the the LoRa gateway setting. Public networks use sub-band 0 only.
falingtrea 2:75adc72aa6a0 272 * This function can be commented out for EU networks
falingtrea 2:75adc72aa6a0 273 */
falingtrea 0:bdd16076aaa5 274 printf("setting frequency sub band\r\n");
falingtrea 0:bdd16076aaa5 275 if ((mdot_ret = mdot_radio->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) {
falingtrea 0:bdd16076aaa5 276 log_error(mdot_radio, "failed to set frequency sub band", mdot_ret);
falingtrea 0:bdd16076aaa5 277 }
falingtrea 0:bdd16076aaa5 278
falingtrea 2:75adc72aa6a0 279 /*
falingtrea 2:75adc72aa6a0 280 * setNetworkName is used for private networks.
falingtrea 2:75adc72aa6a0 281 * Use setNetworkID(AppID) for public networks
falingtrea 2:75adc72aa6a0 282 */
falingtrea 2:75adc72aa6a0 283
falingtrea 2:75adc72aa6a0 284 // config_app_id.assign(app_id,app_id+7);
falingtrea 2:75adc72aa6a0 285
falingtrea 0:bdd16076aaa5 286 printf("setting network name\r\n");
falingtrea 0:bdd16076aaa5 287 if ((mdot_ret = mdot_radio->setNetworkName(config_network_name)) != mDot::MDOT_OK) {
falingtrea 2:75adc72aa6a0 288 // if ((mdot_ret = mdot_radio->setNetworkID(config_app_id)) != mDot::MDOT_OK) {
falingtrea 0:bdd16076aaa5 289 log_error(mdot_radio, "failed to set network name", mdot_ret);
falingtrea 0:bdd16076aaa5 290 }
falingtrea 0:bdd16076aaa5 291
falingtrea 2:75adc72aa6a0 292 /*
falingtrea 2:75adc72aa6a0 293 * setNetworkPassphrase is used for private networks
falingtrea 2:75adc72aa6a0 294 * Use setNetworkKey for public networks
falingtrea 2:75adc72aa6a0 295 */
falingtrea 2:75adc72aa6a0 296
falingtrea 2:75adc72aa6a0 297 // config_app_key.assign(app_key,app_key+15);
falingtrea 2:75adc72aa6a0 298
falingtrea 0:bdd16076aaa5 299 printf("setting network password\r\n");
falingtrea 0:bdd16076aaa5 300 if ((mdot_ret = mdot_radio->setNetworkPassphrase(config_network_pass)) != mDot::MDOT_OK) {
falingtrea 2:75adc72aa6a0 301 // if ((mdot_ret = mdot_radio->setNetworkKey(config_app_key)) != mDot::MDOT_OK) {
falingtrea 0:bdd16076aaa5 302 log_error(mdot_radio, "failed to set network password", mdot_ret);
falingtrea 0:bdd16076aaa5 303 }
falingtrea 0:bdd16076aaa5 304
falingtrea 0:bdd16076aaa5 305 // attempt to join the network
falingtrea 0:bdd16076aaa5 306 printf("joining network\r\n");
falingtrea 2:75adc72aa6a0 307 while (((mdot_ret = mdot_radio->joinNetwork()) != mDot::MDOT_OK) && (!exit_program)) {
falingtrea 0:bdd16076aaa5 308 log_error(mdot_radio,"failed to join network:", mdot_ret);
falingtrea 0:bdd16076aaa5 309 if (mdot_radio->getFrequencyBand() == mDot::FB_868){
falingtrea 0:bdd16076aaa5 310 mdot_ret = mdot_radio->getNextTxMs();
falingtrea 0:bdd16076aaa5 311 }
falingtrea 0:bdd16076aaa5 312 else {
falingtrea 0:bdd16076aaa5 313 mdot_ret = 0;
falingtrea 2:75adc72aa6a0 314 }
falingtrea 0:bdd16076aaa5 315
falingtrea 0:bdd16076aaa5 316 printf("delay = %lu\n\r",mdot_ret);
falingtrea 0:bdd16076aaa5 317 osDelay(mdot_ret + 1);
falingtrea 0:bdd16076aaa5 318 }
falingtrea 2:75adc72aa6a0 319
falingtrea 2:75adc72aa6a0 320 /*
falingtrea 2:75adc72aa6a0 321 * Check for PB1 press during network join attempt
falingtrea 2:75adc72aa6a0 322 */
falingtrea 2:75adc72aa6a0 323 if (exit_program) {
falingtrea 2:75adc72aa6a0 324 printf("Exiting program\n\r");
falingtrea 2:75adc72aa6a0 325 evbLCD->clearBuffer();
falingtrea 2:75adc72aa6a0 326 sprintf(txtstr,"Exiting Program");
falingtrea 2:75adc72aa6a0 327 evbLCD->writeText(0,4,font_6x8,txtstr,strlen(txtstr));
falingtrea 2:75adc72aa6a0 328 exit(1);
falingtrea 2:75adc72aa6a0 329 }
falingtrea 2:75adc72aa6a0 330
falingtrea 0:bdd16076aaa5 331 } else {
falingtrea 0:bdd16076aaa5 332 printf("radio setup failed\n\r");
falingtrea 0:bdd16076aaa5 333 //exit(1);
falingtrea 0:bdd16076aaa5 334 }
falingtrea 0:bdd16076aaa5 335
falingtrea 0:bdd16076aaa5 336 osDelay(200);
falingtrea 0:bdd16076aaa5 337 evbBackLight->setPWM(NCP5623B::LED_3,16); // enable LED2 on EVB and set to 50% PWM
falingtrea 0:bdd16076aaa5 338
falingtrea 2:75adc72aa6a0 339 // sets LED2 to 50% max current
falingtrea 0:bdd16076aaa5 340 evbBackLight->setLEDCurrent(16);
falingtrea 0:bdd16076aaa5 341
falingtrea 0:bdd16076aaa5 342 printf("Start of Test\n\r");
falingtrea 0:bdd16076aaa5 343
falingtrea 2:75adc72aa6a0 344 osDelay (500); // allows other threads to process
falingtrea 2:75adc72aa6a0 345 printf("shutdown LED:\n\r");
falingtrea 2:75adc72aa6a0 346 evbBackLight->shutdown();
falingtrea 0:bdd16076aaa5 347
falingtrea 0:bdd16076aaa5 348 osDelay (500); // allows other threads to process
falingtrea 0:bdd16076aaa5 349 printf("Turn on LED2\n\r");
falingtrea 0:bdd16076aaa5 350 evbBackLight->setLEDCurrent(16);
falingtrea 0:bdd16076aaa5 351
falingtrea 0:bdd16076aaa5 352 data = evbAccel->getWhoAmI();
falingtrea 0:bdd16076aaa5 353 printf("Accelerometer who_am_i value = %x \n\r", data);
falingtrea 0:bdd16076aaa5 354
falingtrea 0:bdd16076aaa5 355 result = evbAccel->getStatus();
falingtrea 0:bdd16076aaa5 356 printf("status byte = %x \n\r", result);
falingtrea 0:bdd16076aaa5 357
falingtrea 0:bdd16076aaa5 358 printf("Barometer who_am_i check = %s \n\r", evbBaro->testWhoAmI() ? "TRUE" : "FALSE");
falingtrea 0:bdd16076aaa5 359
falingtrea 0:bdd16076aaa5 360 result = evbBaro->getStatus();
falingtrea 0:bdd16076aaa5 361 printf("status byte = %x \n\r", result);
falingtrea 0:bdd16076aaa5 362
falingtrea 0:bdd16076aaa5 363 /*
falingtrea 0:bdd16076aaa5 364 * Setup the Accelerometer for 8g range, 14 bit resolution, Noise reduction off, sample rate 1.56 Hz
falingtrea 0:bdd16076aaa5 365 * normal oversample mode, High pass filter off
falingtrea 0:bdd16076aaa5 366 */
falingtrea 0:bdd16076aaa5 367 evbAccel->setCommonParameters(MMA845x::RANGE_8g,MMA845x::RES_MAX,MMA845x::LN_OFF,
falingtrea 0:bdd16076aaa5 368 MMA845x::DR_1_56,MMA845x::OS_NORMAL,MMA845x::HPF_OFF );
falingtrea 0:bdd16076aaa5 369
falingtrea 0:bdd16076aaa5 370 /*
falingtrea 0:bdd16076aaa5 371 * Setup the Barometric sensor for post processed Ambient pressure, 4 samples per data acquisition.
falingtrea 0:bdd16076aaa5 372 * and a sample taken every second when in active mode
falingtrea 0:bdd16076aaa5 373 */
falingtrea 0:bdd16076aaa5 374 evbBaro->setParameters(MPL3115A2::DATA_NORMAL, MPL3115A2::DM_BAROMETER, MPL3115A2::OR_16,
falingtrea 0:bdd16076aaa5 375 MPL3115A2::AT_1);
falingtrea 0:bdd16076aaa5 376 /*
falingtrea 0:bdd16076aaa5 377 * Setup the Ambient Light Sensor for continuous Ambient Light Sensing, 16 bit resolution,
falingtrea 0:bdd16076aaa5 378 * and 16000 lux range
falingtrea 0:bdd16076aaa5 379 */
falingtrea 0:bdd16076aaa5 380
falingtrea 0:bdd16076aaa5 381 evbAmbLight->setMode(ISL29011::ALS_CONT);
falingtrea 0:bdd16076aaa5 382 evbAmbLight->setResolution(ISL29011::ADC_16BIT);
falingtrea 0:bdd16076aaa5 383 evbAmbLight->setRange(ISL29011::RNG_16000);
falingtrea 0:bdd16076aaa5 384
falingtrea 0:bdd16076aaa5 385 /*
falingtrea 0:bdd16076aaa5 386 * Set the accelerometer for active mode
falingtrea 0:bdd16076aaa5 387 */
falingtrea 0:bdd16076aaa5 388 evbAccel->activeMode();
falingtrea 0:bdd16076aaa5 389
falingtrea 0:bdd16076aaa5 390 /*
falingtrea 0:bdd16076aaa5 391 * Clear the min-max registers in the Barometric Sensor
falingtrea 0:bdd16076aaa5 392 */
falingtrea 0:bdd16076aaa5 393 evbBaro->clearMinMaxRegs();
falingtrea 0:bdd16076aaa5 394
falingtrea 0:bdd16076aaa5 395 evbBackLight->setLEDCurrent(0);
falingtrea 0:bdd16076aaa5 396
falingtrea 0:bdd16076aaa5 397 /*
falingtrea 2:75adc72aa6a0 398 * Check for PB1 press during network join attempt
falingtrea 2:75adc72aa6a0 399 */
falingtrea 2:75adc72aa6a0 400 if (exit_program) {
falingtrea 2:75adc72aa6a0 401 printf("Exiting program\n\r");
falingtrea 2:75adc72aa6a0 402 evbLCD->clearBuffer();
falingtrea 2:75adc72aa6a0 403 sprintf(txtstr,"Exiting Program");
falingtrea 2:75adc72aa6a0 404 evbLCD->writeText(0,4,font_6x8,txtstr,strlen(txtstr));
falingtrea 2:75adc72aa6a0 405 exit(1);
falingtrea 2:75adc72aa6a0 406 }
falingtrea 2:75adc72aa6a0 407
falingtrea 2:75adc72aa6a0 408 /*
falingtrea 0:bdd16076aaa5 409 * Main data acquisition loop
falingtrea 0:bdd16076aaa5 410 */
falingtrea 0:bdd16076aaa5 411 pckt_time = 10;
falingtrea 0:bdd16076aaa5 412 i = 0;
falingtrea 0:bdd16076aaa5 413 do {
falingtrea 0:bdd16076aaa5 414 evbLCD->startUpdate();
falingtrea 0:bdd16076aaa5 415 evbLCD->clearBuffer();
falingtrea 0:bdd16076aaa5 416
falingtrea 0:bdd16076aaa5 417 /*
falingtrea 0:bdd16076aaa5 418 * Test Accelerometer XYZ data ready bit to see if acquisition complete
falingtrea 0:bdd16076aaa5 419 */
daveheitzman 6:b62e5106b5bc 420 failtime = FAIL_MAX;
falingtrea 0:bdd16076aaa5 421 do {
falingtrea 0:bdd16076aaa5 422 osDelay(100); // allows other threads to process
falingtrea 0:bdd16076aaa5 423 result = evbAccel->getStatus();
daveheitzman 6:b62e5106b5bc 424 failtime--;
daveheitzman 6:b62e5106b5bc 425 } while ((result & MMA845x::XYZDR) == 0 && failtime > 0);
daveheitzman 6:b62e5106b5bc 426 if (failtime==0) {continue; }
falingtrea 0:bdd16076aaa5 427
falingtrea 0:bdd16076aaa5 428 /*
falingtrea 0:bdd16076aaa5 429 * Retrieve and print out accelerometer data
falingtrea 0:bdd16076aaa5 430 */
falingtrea 0:bdd16076aaa5 431 accel_data = evbAccel->getXYZ();
falingtrea 0:bdd16076aaa5 432
falingtrea 0:bdd16076aaa5 433 sprintf(txtstr,"Accelerometer");
falingtrea 0:bdd16076aaa5 434 evbLCD->writeText(0,0,font_6x8,txtstr,strlen(txtstr));
falingtrea 0:bdd16076aaa5 435 sprintf(txtstr, "x = %d", accel_data._x);
falingtrea 0:bdd16076aaa5 436 evbLCD->writeText(20,1,font_6x8,txtstr,strlen(txtstr));
falingtrea 0:bdd16076aaa5 437 sprintf(txtstr, "y = %d", accel_data._y);
falingtrea 0:bdd16076aaa5 438 evbLCD->writeText(20,2,font_6x8,txtstr,strlen(txtstr));
falingtrea 0:bdd16076aaa5 439 sprintf(txtstr, "z = %d", accel_data._z );
falingtrea 0:bdd16076aaa5 440 evbLCD->writeText(20,3,font_6x8,txtstr,strlen(txtstr));
falingtrea 0:bdd16076aaa5 441
falingtrea 0:bdd16076aaa5 442 /*
falingtrea 0:bdd16076aaa5 443 * Trigger a Pressure reading
falingtrea 0:bdd16076aaa5 444 */
falingtrea 0:bdd16076aaa5 445 evbBaro->setParameters(MPL3115A2::DATA_NORMAL, MPL3115A2::DM_BAROMETER, MPL3115A2::OR_16,
falingtrea 0:bdd16076aaa5 446 MPL3115A2::AT_1);
falingtrea 0:bdd16076aaa5 447 evbBaro->triggerOneShot();
falingtrea 0:bdd16076aaa5 448
falingtrea 0:bdd16076aaa5 449 /*
falingtrea 0:bdd16076aaa5 450 * Test barometer device status to see if acquisition is complete
falingtrea 0:bdd16076aaa5 451 */
daveheitzman 6:b62e5106b5bc 452 failtime=FAIL_MAX;
falingtrea 0:bdd16076aaa5 453 do {
falingtrea 0:bdd16076aaa5 454 osDelay(100); // allows other threads to process
falingtrea 0:bdd16076aaa5 455 result = evbBaro->getStatus();
daveheitzman 6:b62e5106b5bc 456 failtime--;
daveheitzman 6:b62e5106b5bc 457 } while ((result & MPL3115A2::PTDR) == 0 && failtime > 0 );
daveheitzman 6:b62e5106b5bc 458 if (failtime==0) {continue; }
falingtrea 0:bdd16076aaa5 459 /*
falingtrea 0:bdd16076aaa5 460 * Retrieve and print out barometric pressure
falingtrea 0:bdd16076aaa5 461 */
falingtrea 0:bdd16076aaa5 462 pressure = evbBaro->getBaroData() >> 12; // convert 32 bit signed to 20 bit unsigned value
falingtrea 0:bdd16076aaa5 463 num_whole = pressure >> 2; // 18 bit integer significant
falingtrea 0:bdd16076aaa5 464 num_frac = (pressure & 0x3) * 25; // 2 bit fractional 0.25 per bit
daveheitzman 6:b62e5106b5bc 465 // if failtime reached 0 , indicates that the result might be junk.
falingtrea 0:bdd16076aaa5 466 sprintf(txtstr,"Press=%ld.%02d Pa", num_whole, num_frac);
daveheitzman 5:5a794cedd728 467
daveheitzman 5:5a794cedd728 468 pressure = evbBaro->getBaroData() >> 12; // convert 32 bit signed to 20 bit unsigned value
daveheitzman 5:5a794cedd728 469 num_whole = pressure >> 2; // 18 bit integer significant
daveheitzman 5:5a794cedd728 470 num_frac = (pressure & 0x3) * 25; // 2 bit fractional 0.25 per bit
daveheitzman 5:5a794cedd728 471
daveheitzman 6:b62e5106b5bc 472 writeValueOrError(); // will write to lorapresstring and txtstr
daveheitzman 5:5a794cedd728 473
falingtrea 0:bdd16076aaa5 474
falingtrea 0:bdd16076aaa5 475 /*
falingtrea 0:bdd16076aaa5 476 * Trigger a Altitude reading
falingtrea 0:bdd16076aaa5 477 */
falingtrea 0:bdd16076aaa5 478 evbBaro->setParameters(MPL3115A2::DATA_NORMAL, MPL3115A2::DM_ALTIMETER, MPL3115A2::OR_16,
falingtrea 0:bdd16076aaa5 479 MPL3115A2::AT_1);
falingtrea 0:bdd16076aaa5 480 evbBaro->triggerOneShot();
falingtrea 0:bdd16076aaa5 481
falingtrea 0:bdd16076aaa5 482 /*
falingtrea 0:bdd16076aaa5 483 * Test barometer device status to see if acquisition is complete
falingtrea 0:bdd16076aaa5 484 */
daveheitzman 6:b62e5106b5bc 485 failtime=FAIL_MAX;
falingtrea 0:bdd16076aaa5 486 do {
falingtrea 0:bdd16076aaa5 487 osDelay(100); // allows other threads to process
falingtrea 0:bdd16076aaa5 488 result = evbBaro->getStatus();
daveheitzman 6:b62e5106b5bc 489 failtime--;
daveheitzman 6:b62e5106b5bc 490 } while ((result & MPL3115A2::PTDR) == 0 && failtime > 0 );
daveheitzman 6:b62e5106b5bc 491 if (failtime==0) {continue; }
falingtrea 0:bdd16076aaa5 492
falingtrea 0:bdd16076aaa5 493 /*
falingtrea 0:bdd16076aaa5 494 * Retrieve and print out altitude and temperature
daveheitzman 5:5a794cedd728 495 const std::string lora_temp_string;
daveheitzman 5:5a794cedd728 496 const std::string lora_alt_string;
daveheitzman 5:5a794cedd728 497 const std::string lora_press_string;
daveheitzman 5:5a794cedd728 498 const std::string lora_light_string;
daveheitzman 5:5a794cedd728 499
falingtrea 0:bdd16076aaa5 500 */
falingtrea 0:bdd16076aaa5 501 baro_data = evbBaro->getAllData(false);
falingtrea 0:bdd16076aaa5 502 baro_data._baro /= 4096; // convert 32 bit signed to 20 bit signed value
falingtrea 0:bdd16076aaa5 503 num_whole = baro_data._baro / 16; // 18 bit signed significant integer
falingtrea 0:bdd16076aaa5 504 num_frac = (baro_data._baro & 0xF) * 625 / 100; // 4 bit fractional .0625 per bit
falingtrea 0:bdd16076aaa5 505 sprintf(txtstr,"Alti=%ld.%03d m", num_whole, num_frac);
daveheitzman 5:5a794cedd728 506 sprintf(lora_alt_string,"%ld.%03d", num_whole, num_frac);
falingtrea 0:bdd16076aaa5 507 evbLCD->writeText(0,5,font_6x8,txtstr,strlen(txtstr));
falingtrea 0:bdd16076aaa5 508 num_whole = baro_data._temp / 16; // 8 bit signed significant integer
falingtrea 0:bdd16076aaa5 509 num_frac = (baro_data._temp & 0x0F) * 625 / 100; // 4 bit fractional .0625 per bit
falingtrea 0:bdd16076aaa5 510 sprintf(txtstr,"Temp=%ld.%03d C", num_whole, num_frac);
daveheitzman 5:5a794cedd728 511 sprintf(lora_temp_string,"%ld.%03d", num_whole, num_frac);
falingtrea 0:bdd16076aaa5 512 evbLCD->writeText(0,6,font_6x8,txtstr,strlen(txtstr));
falingtrea 0:bdd16076aaa5 513
falingtrea 0:bdd16076aaa5 514 /*
falingtrea 0:bdd16076aaa5 515 * retrieve and print out Ambient Light level
falingtrea 0:bdd16076aaa5 516 */
falingtrea 0:bdd16076aaa5 517 lux_data = evbAmbLight->getData();
falingtrea 0:bdd16076aaa5 518 num_whole = lux_data * 24 / 100; // 16000 lux full scale .24 lux per bit
falingtrea 0:bdd16076aaa5 519 num_frac = lux_data * 24 % 100;
falingtrea 0:bdd16076aaa5 520 sprintf(txtstr, "Light=%ld.%02d lux", num_whole, num_frac );
daveheitzman 5:5a794cedd728 521 sprintf(lora_light_string, "%ld.%02d", num_whole, num_frac );
falingtrea 0:bdd16076aaa5 522 evbLCD->writeText(0,7,font_6x8,txtstr,strlen(txtstr));
falingtrea 0:bdd16076aaa5 523
falingtrea 0:bdd16076aaa5 524 evbLCD->endUpdate();
falingtrea 0:bdd16076aaa5 525 printf("finished iteration %d\n\r",(++i));
falingtrea 0:bdd16076aaa5 526 if (i % pckt_time == 0) { // check packet counter will send packet every 2-5-10 data collection loops
falingtrea 0:bdd16076aaa5 527 mdot_data.clear();
falingtrea 0:bdd16076aaa5 528 mdot_data.push_back(0x0E); // key for Current Acceleration 3-Axis Value
falingtrea 0:bdd16076aaa5 529 converts.f_s = accel_data._x *4; // shift data 2 bits while retaining sign
falingtrea 0:bdd16076aaa5 530 mdot_data.push_back(converts.t_u[1]); // get 8 MSB of 14 bit value
falingtrea 0:bdd16076aaa5 531 converts.f_s = accel_data._y * 4; // shift data 2 bits while retaining sign
falingtrea 0:bdd16076aaa5 532 mdot_data.push_back(converts.t_u[1]); // get 8 MSB of 14 bit value
falingtrea 0:bdd16076aaa5 533 converts.f_s = accel_data._z * 4; // shift data 2 bits while retaining sign
falingtrea 0:bdd16076aaa5 534 mdot_data.push_back(converts.t_u[1]); // get 8 MSB of 14 bit value
falingtrea 0:bdd16076aaa5 535 mdot_data.push_back(0x08); // key for Current Pressure Value
falingtrea 0:bdd16076aaa5 536 convertl.f_u = pressure; // pressure data is 20 bits unsigned
falingtrea 0:bdd16076aaa5 537 mdot_data.push_back(convertl.t_u[2]);
falingtrea 0:bdd16076aaa5 538 mdot_data.push_back(convertl.t_u[1]);
falingtrea 0:bdd16076aaa5 539 mdot_data.push_back(convertl.t_u[0]);
falingtrea 0:bdd16076aaa5 540 mdot_data.push_back(0x05); // key for Current Ambient Light Value
falingtrea 0:bdd16076aaa5 541 converts.f_u = lux_data; // data is 16 bits unsigned
falingtrea 0:bdd16076aaa5 542 mdot_data.push_back(converts.t_u[1]);
falingtrea 0:bdd16076aaa5 543 mdot_data.push_back(converts.t_u[0]);
falingtrea 0:bdd16076aaa5 544 mdot_data.push_back(0x0B); // key for Current Temperature Value
falingtrea 0:bdd16076aaa5 545 converts.f_s = baro_data._temp; // temperature is signed 12 bit
falingtrea 0:bdd16076aaa5 546 mdot_data.push_back(converts.t_u[1]);
falingtrea 0:bdd16076aaa5 547 mdot_data.push_back(converts.t_u[0]);
daveheitzman 5:5a794cedd728 548 mdot_data.clear();
daveheitzman 5:5a794cedd728 549
daveheitzman 5:5a794cedd728 550 // if you want to go back to sending byte vector instead:
daveheitzman 5:5a794cedd728 551 // if ((mdot_ret = mdot_radio->send(mdot_data)) != mDot::MDOT_OK) {
daveheitzman 5:5a794cedd728 552 // sprintf(sensor_text, "x:%d,y:%d,z:%d,p:%s,a:%s,t:%s,l:%s",
daveheitzman 5:5a794cedd728 553
daveheitzman 5:5a794cedd728 554 sprintf(sensor_text, "%d,%d,%d,%s,%s,%s,%s",
daveheitzman 5:5a794cedd728 555 accel_data._x,
daveheitzman 5:5a794cedd728 556 accel_data._y,
daveheitzman 5:5a794cedd728 557 accel_data._z,
daveheitzman 5:5a794cedd728 558 lora_press_string,
daveheitzman 5:5a794cedd728 559 lora_alt_string,
daveheitzman 5:5a794cedd728 560 lora_temp_string,
daveheitzman 5:5a794cedd728 561 lora_light_string);
daveheitzman 5:5a794cedd728 562 if ((mdot_ret = sendString((const std::string)sensor_text)) != mDot::MDOT_OK) {
falingtrea 0:bdd16076aaa5 563 log_error(mdot_radio, "failed to send", mdot_ret);
falingtrea 0:bdd16076aaa5 564 } else {
falingtrea 0:bdd16076aaa5 565 printf("successfully sent data to gateway\r\n");
falingtrea 0:bdd16076aaa5 566 }
falingtrea 0:bdd16076aaa5 567 }
daveheitzman 5:5a794cedd728 568
daveheitzman 4:51d7c24bbf91 569 } while(!exit_program);
falingtrea 0:bdd16076aaa5 570
falingtrea 0:bdd16076aaa5 571 evbBaro->triggerOneShot();
falingtrea 0:bdd16076aaa5 572
falingtrea 0:bdd16076aaa5 573 do {
falingtrea 0:bdd16076aaa5 574 osDelay(200); // allows other threads to process
falingtrea 0:bdd16076aaa5 575 result = evbBaro->getStatus();
falingtrea 0:bdd16076aaa5 576 } while ((result & MPL3115A2::PTDR) == 0 );
falingtrea 0:bdd16076aaa5 577
falingtrea 0:bdd16076aaa5 578 baro_data = evbBaro->getAllData(true);
falingtrea 0:bdd16076aaa5 579 printf ("minBaro=%ld maxBaro=%ld minTemp=%d maxTemp=%d\n\r", baro_data._minbaro, baro_data._maxbaro,
falingtrea 0:bdd16076aaa5 580 baro_data._mintemp, baro_data._maxtemp);
falingtrea 0:bdd16076aaa5 581
falingtrea 0:bdd16076aaa5 582 printf("End of Test\n\r");
falingtrea 0:bdd16076aaa5 583
falingtrea 2:75adc72aa6a0 584 evbLCD->clearBuffer();
falingtrea 2:75adc72aa6a0 585 sprintf(txtstr,"Exiting Program");
falingtrea 2:75adc72aa6a0 586 evbLCD->writeText(0,4,font_6x8,txtstr,strlen(txtstr));
falingtrea 2:75adc72aa6a0 587
falingtrea 0:bdd16076aaa5 588 }
falingtrea 0:bdd16076aaa5 589
falingtrea 0:bdd16076aaa5 590 /*
falingtrea 3:68e974f5f532 591 * Sets pb1_low flag. Slag is cleared in pb1_debounce thread
falingtrea 0:bdd16076aaa5 592 */
falingtrea 0:bdd16076aaa5 593 void pb1ISR(void)
falingtrea 0:bdd16076aaa5 594 {
falingtrea 3:68e974f5f532 595 if (!pb1_low)
falingtrea 3:68e974f5f532 596 pb1_low = true;
falingtrea 3:68e974f5f532 597 }
falingtrea 0:bdd16076aaa5 598
falingtrea 3:68e974f5f532 599 /*
falingtrea 3:68e974f5f532 600 * Debounces pb1. Also exits program if pushbutton 1 is pressed
falingtrea 3:68e974f5f532 601 */
falingtrea 3:68e974f5f532 602 void pb1_debounce(void const *args)
falingtrea 3:68e974f5f532 603 {
falingtrea 3:68e974f5f532 604
falingtrea 3:68e974f5f532 605 static uint8_t count = 0;
falingtrea 3:68e974f5f532 606
falingtrea 3:68e974f5f532 607 while (true) {
falingtrea 3:68e974f5f532 608
falingtrea 3:68e974f5f532 609 if (pb1_low && (mDot08 == 0))
falingtrea 3:68e974f5f532 610 count++;
falingtrea 3:68e974f5f532 611 else {
falingtrea 3:68e974f5f532 612 count = 0;
falingtrea 3:68e974f5f532 613 pb1_low = false;
falingtrea 3:68e974f5f532 614 }
falingtrea 3:68e974f5f532 615
falingtrea 3:68e974f5f532 616 if (count == 5)
falingtrea 3:68e974f5f532 617 exit_program = true;
falingtrea 3:68e974f5f532 618
falingtrea 3:68e974f5f532 619 Thread::wait(5);
falingtrea 3:68e974f5f532 620 }
falingtrea 0:bdd16076aaa5 621 }
falingtrea 0:bdd16076aaa5 622
falingtrea 0:bdd16076aaa5 623 /*
falingtrea 3:68e974f5f532 624 * Sets pb2_low flag. Flag is cleared in pb2_debounce thread
falingtrea 0:bdd16076aaa5 625 */
falingtrea 0:bdd16076aaa5 626 void pb2ISR(void)
falingtrea 0:bdd16076aaa5 627 {
falingtrea 3:68e974f5f532 628 if (!pb2_low)
falingtrea 3:68e974f5f532 629 pb2_low = true;
falingtrea 3:68e974f5f532 630 }
falingtrea 0:bdd16076aaa5 631
falingtrea 3:68e974f5f532 632 /*
falingtrea 3:68e974f5f532 633 * Debounces pb2. Also changes packet transmit time to every other,
falingtrea 3:68e974f5f532 634 * every fifth, or every tenth sample when SW2 pushed
falingtrea 3:68e974f5f532 635 * Also triggers a thread to transmit a configuration packet
falingtrea 3:68e974f5f532 636 */
falingtrea 3:68e974f5f532 637 void pb2_debounce(void const *args)
falingtrea 3:68e974f5f532 638 {
falingtrea 3:68e974f5f532 639
falingtrea 3:68e974f5f532 640 static uint8_t count = 0;
falingtrea 3:68e974f5f532 641
falingtrea 3:68e974f5f532 642 while (true) {
falingtrea 0:bdd16076aaa5 643
falingtrea 3:68e974f5f532 644 if (pb2_low && (mDot09 == 0))
falingtrea 3:68e974f5f532 645 count++;
falingtrea 3:68e974f5f532 646 else {
falingtrea 3:68e974f5f532 647 count = 0;
falingtrea 3:68e974f5f532 648 pb2_low = false;
falingtrea 3:68e974f5f532 649 }
falingtrea 3:68e974f5f532 650
falingtrea 3:68e974f5f532 651 if (count == 5){
falingtrea 0:bdd16076aaa5 652
falingtrea 3:68e974f5f532 653 if (pckt_time >= 5)
falingtrea 3:68e974f5f532 654 pckt_time /= 2;
falingtrea 3:68e974f5f532 655 else pckt_time = 20;
falingtrea 3:68e974f5f532 656
falingtrea 3:68e974f5f532 657 thread_3->signal_set(0x10); // signal config_pkt_xmit to send packet
falingtrea 3:68e974f5f532 658 }
falingtrea 3:68e974f5f532 659
falingtrea 3:68e974f5f532 660 Thread::wait(5);
falingtrea 3:68e974f5f532 661 }
falingtrea 3:68e974f5f532 662 }
falingtrea 0:bdd16076aaa5 663
falingtrea 2:75adc72aa6a0 664 /*
falingtrea 2:75adc72aa6a0 665 * Function that print clear text verion of mDot errors
falingtrea 2:75adc72aa6a0 666 */
falingtrea 0:bdd16076aaa5 667 void log_error(mDot* dot, const char* msg, int32_t retval)
falingtrea 0:bdd16076aaa5 668 {
falingtrea 0:bdd16076aaa5 669 printf("%s - %ld:%s, %s\r\n", msg, retval, mDot::getReturnCodeString(retval).c_str(), dot->getLastError().c_str());
falingtrea 0:bdd16076aaa5 670 }
falingtrea 0:bdd16076aaa5 671
falingtrea 2:75adc72aa6a0 672 /*
falingtrea 2:75adc72aa6a0 673 * Thread that is triggered by SW2 ISR. Sends a packet to the LoRa server with the new Packet Transmission time setting
falingtrea 2:75adc72aa6a0 674 */
falingtrea 0:bdd16076aaa5 675 void config_pkt_xmit (void const *args)
falingtrea 0:bdd16076aaa5 676 {
falingtrea 0:bdd16076aaa5 677
falingtrea 0:bdd16076aaa5 678 std::vector<uint8_t> data;
falingtrea 0:bdd16076aaa5 679
falingtrea 0:bdd16076aaa5 680 while (true) {
falingtrea 0:bdd16076aaa5 681 Thread::signal_wait(0x10); // wait for pb2ISR to signal send
falingtrea 0:bdd16076aaa5 682 data.clear();
falingtrea 0:bdd16076aaa5 683 data.push_back(0x0F); // key for Configuration data (packet transmission timer)
falingtrea 0:bdd16076aaa5 684 data.push_back(pckt_time);
falingtrea 0:bdd16076aaa5 685
falingtrea 0:bdd16076aaa5 686 if ((mdot_ret = mdot_radio->send(data)) != mDot::MDOT_OK) {
falingtrea 0:bdd16076aaa5 687 log_error(mdot_radio, "failed to send config data", mdot_ret);
falingtrea 0:bdd16076aaa5 688 } else {
falingtrea 0:bdd16076aaa5 689 printf("sent config data to gateway\r\n");
falingtrea 0:bdd16076aaa5 690 }
falingtrea 0:bdd16076aaa5 691 }
falingtrea 0:bdd16076aaa5 692 }
daveheitzman 5:5a794cedd728 693
daveheitzman 5:5a794cedd728 694 int32_t sendString(const std::string text)
daveheitzman 5:5a794cedd728 695 {
daveheitzman 5:5a794cedd728 696 int32_t ret;
daveheitzman 5:5a794cedd728 697 if (mdot_radio->getNextTxMs() != 0) {
daveheitzman 5:5a794cedd728 698 printf("Sending in %lu ms...\r\n", mdot_radio->getNextTxMs());
daveheitzman 5:5a794cedd728 699 return false;
daveheitzman 5:5a794cedd728 700 }
daveheitzman 5:5a794cedd728 701
daveheitzman 5:5a794cedd728 702 printf("Sending: '%s' ", text.c_str());
daveheitzman 5:5a794cedd728 703 std::vector<uint8_t> data(text.begin(), text.end());
daveheitzman 5:5a794cedd728 704 if ((ret = mdot_radio->send(data, 1)) != mDot::MDOT_OK)
daveheitzman 5:5a794cedd728 705 {
daveheitzman 5:5a794cedd728 706 log_error(mdot_radio, "Failed to send data", ret);
daveheitzman 5:5a794cedd728 707
daveheitzman 5:5a794cedd728 708 } else {
daveheitzman 5:5a794cedd728 709 printf("Data sent!\r\n");
daveheitzman 5:5a794cedd728 710 }
daveheitzman 5:5a794cedd728 711 return ret;
daveheitzman 6:b62e5106b5bc 712 }
daveheitzman 6:b62e5106b5bc 713
daveheitzman 6:b62e5106b5bc 714 bool writeValueOrError()
daveheitzman 6:b62e5106b5bc 715 {
daveheitzman 6:b62e5106b5bc 716 bool res;
daveheitzman 6:b62e5106b5bc 717 if (failtime==0){
daveheitzman 6:b62e5106b5bc 718 sprintf(lora_press_string, "%s", "--.--");
daveheitzman 6:b62e5106b5bc 719 sprintf(txtstr, "%s", "--.--");
daveheitzman 6:b62e5106b5bc 720 evbLCD->writeText(0,4,font_6x8,txtstr, strlen(txtstr));
daveheitzman 6:b62e5106b5bc 721 res=false;
daveheitzman 6:b62e5106b5bc 722 }else{
daveheitzman 6:b62e5106b5bc 723 sprintf(lora_press_string, "%ld.%02d", num_whole, num_frac);
daveheitzman 6:b62e5106b5bc 724 evbLCD->writeText(0,4,font_6x8,txtstr,strlen(txtstr));
daveheitzman 6:b62e5106b5bc 725 res=true;
daveheitzman 6:b62e5106b5bc 726 }
daveheitzman 6:b62e5106b5bc 727 return res;
daveheitzman 6:b62e5106b5bc 728 }
daveheitzman 6:b62e5106b5bc 729