This program connects to the The Things Network backend in OTAA Mode. It logs sensor values from a BME 280 to the backend. Tried adding support for Grove GPS using SerialGPS library but it is not working - conflicting with mbed-rtos, so it commented. Deep Sleep for mDot implemented BUT avoiding reprogramming of the mDot config is NOT working.
Dependencies: BME280 SerialGPS libmDot mbed-rtos mbed
main.cpp@2:866a72c3c3bf, 2016-07-04 (annotated)
- Committer:
- AshuJoshi
- Date:
- Mon Jul 04 22:29:31 2016 +0000
- Revision:
- 2:866a72c3c3bf
- Parent:
- 1:36e336869699
- Child:
- 3:5c2bcba214b5
Added mDot support - logging, init, and sleep. Minor enhancements to BME280 - formatted output as strings.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AshuJoshi | 0:3ec6a7645098 | 1 | /****************************************************** |
AshuJoshi | 0:3ec6a7645098 | 2 | * A Program to interface the Grove Base Shielf V2 |
AshuJoshi | 0:3ec6a7645098 | 3 | * to the mDot UDK. |
AshuJoshi | 0:3ec6a7645098 | 4 | * Additionally sample code to compress the data |
AshuJoshi | 0:3ec6a7645098 | 5 | * for use with LPWANs such as LoRa |
AshuJoshi | 0:3ec6a7645098 | 6 | *****************************************************/ |
AshuJoshi | 0:3ec6a7645098 | 7 | |
AshuJoshi | 0:3ec6a7645098 | 8 | #include "mbed.h" |
AshuJoshi | 0:3ec6a7645098 | 9 | #include <math.h> |
AshuJoshi | 2:866a72c3c3bf | 10 | #include <string> |
AshuJoshi | 2:866a72c3c3bf | 11 | #include "mDot.h" |
AshuJoshi | 2:866a72c3c3bf | 12 | #include "MTSLog.h" |
AshuJoshi | 2:866a72c3c3bf | 13 | #include "MTSText.h" |
AshuJoshi | 1:36e336869699 | 14 | #include "BME280.h" |
AshuJoshi | 0:3ec6a7645098 | 15 | |
AshuJoshi | 2:866a72c3c3bf | 16 | using namespace mts; |
AshuJoshi | 2:866a72c3c3bf | 17 | |
AshuJoshi | 2:866a72c3c3bf | 18 | #define MIN(a,b) (((a)<(b))?(a):(b)) |
AshuJoshi | 2:866a72c3c3bf | 19 | #define MAX(a,b) (((a)>(b))?(a):(b)) |
AshuJoshi | 2:866a72c3c3bf | 20 | |
AshuJoshi | 1:36e336869699 | 21 | // mDot UDK Specific |
AshuJoshi | 1:36e336869699 | 22 | // MDot Pinout: https://developer.mbed.org/platforms/MTS-mDot-F411/#pinout-diagram |
AshuJoshi | 0:3ec6a7645098 | 23 | // Uncomment this line if using a full sized UDK2.0 instead of a Micro UDK |
AshuJoshi | 1:36e336869699 | 24 | |
AshuJoshi | 0:3ec6a7645098 | 25 | #define UDK2 1 |
AshuJoshi | 0:3ec6a7645098 | 26 | #ifdef UDK2 |
AshuJoshi | 0:3ec6a7645098 | 27 | DigitalOut led(LED1); |
AshuJoshi | 0:3ec6a7645098 | 28 | #else |
AshuJoshi | 0:3ec6a7645098 | 29 | DigitalOut led(XBEE_RSSI); |
AshuJoshi | 0:3ec6a7645098 | 30 | #endif |
AshuJoshi | 0:3ec6a7645098 | 31 | |
AshuJoshi | 1:36e336869699 | 32 | //BME280 sensor(I2C_SDA, I2C_SCL) |
AshuJoshi | 1:36e336869699 | 33 | // MDot UDK - I2C_SDA and I2C_SCL connected to PC_9/PA_* |
AshuJoshi | 1:36e336869699 | 34 | BME280 b280(PC_9, PA_8); |
AshuJoshi | 1:36e336869699 | 35 | |
AshuJoshi | 1:36e336869699 | 36 | |
AshuJoshi | 1:36e336869699 | 37 | |
AshuJoshi | 0:3ec6a7645098 | 38 | // Globals |
AshuJoshi | 0:3ec6a7645098 | 39 | Ticker tick; |
AshuJoshi | 2:866a72c3c3bf | 40 | mDot* dot; |
AshuJoshi | 2:866a72c3c3bf | 41 | |
AshuJoshi | 0:3ec6a7645098 | 42 | |
AshuJoshi | 0:3ec6a7645098 | 43 | // Function Declarations |
AshuJoshi | 0:3ec6a7645098 | 44 | void endLessTestLoop(); |
AshuJoshi | 0:3ec6a7645098 | 45 | void setUpLEDBlink(); |
AshuJoshi | 0:3ec6a7645098 | 46 | void blink(); |
AshuJoshi | 1:36e336869699 | 47 | void readandprintBME280(); |
AshuJoshi | 2:866a72c3c3bf | 48 | void mDotConfig(); |
AshuJoshi | 2:866a72c3c3bf | 49 | void mDotGotoDeepSleep(int seconds); |
AshuJoshi | 2:866a72c3c3bf | 50 | |
AshuJoshi | 0:3ec6a7645098 | 51 | |
AshuJoshi | 0:3ec6a7645098 | 52 | |
AshuJoshi | 0:3ec6a7645098 | 53 | /***************************************************** |
AshuJoshi | 0:3ec6a7645098 | 54 | * MAIN |
AshuJoshi | 0:3ec6a7645098 | 55 | *****************************************************/ |
AshuJoshi | 0:3ec6a7645098 | 56 | int main(){ |
AshuJoshi | 0:3ec6a7645098 | 57 | |
AshuJoshi | 0:3ec6a7645098 | 58 | // Simple Test Functions, "Hello World on UDK |
AshuJoshi | 0:3ec6a7645098 | 59 | setUpLEDBlink(); |
AshuJoshi | 2:866a72c3c3bf | 60 | mDotConfig(); |
AshuJoshi | 0:3ec6a7645098 | 61 | endLessTestLoop(); |
AshuJoshi | 0:3ec6a7645098 | 62 | |
AshuJoshi | 0:3ec6a7645098 | 63 | return 0; |
AshuJoshi | 0:3ec6a7645098 | 64 | } |
AshuJoshi | 0:3ec6a7645098 | 65 | |
AshuJoshi | 2:866a72c3c3bf | 66 | /***************************************************** |
AshuJoshi | 2:866a72c3c3bf | 67 | * mDot Functions |
AshuJoshi | 2:866a72c3c3bf | 68 | ****************************************************/ |
AshuJoshi | 2:866a72c3c3bf | 69 | |
AshuJoshi | 2:866a72c3c3bf | 70 | |
AshuJoshi | 2:866a72c3c3bf | 71 | void mDotConfig() { |
AshuJoshi | 2:866a72c3c3bf | 72 | // get a mDot handle |
AshuJoshi | 2:866a72c3c3bf | 73 | dot = mDot::getInstance(); |
AshuJoshi | 2:866a72c3c3bf | 74 | //dot->setLogLevel(mts::MTSLog::INFO_LEVEL); |
AshuJoshi | 2:866a72c3c3bf | 75 | dot->setLogLevel(mts::MTSLog::TRACE_LEVEL); |
AshuJoshi | 2:866a72c3c3bf | 76 | // print library version information |
AshuJoshi | 2:866a72c3c3bf | 77 | logInfo("version: %s", dot->getId().c_str()); |
AshuJoshi | 2:866a72c3c3bf | 78 | } |
AshuJoshi | 2:866a72c3c3bf | 79 | |
AshuJoshi | 2:866a72c3c3bf | 80 | void mDotGotoDeepSleep(int seconds) { |
AshuJoshi | 2:866a72c3c3bf | 81 | // logInfo("input to sleep routine %d", seconds); |
AshuJoshi | 2:866a72c3c3bf | 82 | // Should sleep here and wakeup after a set interval. |
AshuJoshi | 2:866a72c3c3bf | 83 | uint32_t sleep_time = MAX((dot->getNextTxMs() / 1000), seconds); |
AshuJoshi | 2:866a72c3c3bf | 84 | logInfo("going to sleep for %d seconds", sleep_time); |
AshuJoshi | 2:866a72c3c3bf | 85 | // go to sleep and wake up automatically sleep_time seconds later |
AshuJoshi | 2:866a72c3c3bf | 86 | dot->sleep(sleep_time, mDot::RTC_ALARM, false); |
AshuJoshi | 2:866a72c3c3bf | 87 | |
AshuJoshi | 2:866a72c3c3bf | 88 | } |
AshuJoshi | 0:3ec6a7645098 | 89 | |
AshuJoshi | 0:3ec6a7645098 | 90 | |
AshuJoshi | 0:3ec6a7645098 | 91 | |
AshuJoshi | 1:36e336869699 | 92 | /***************************************************** |
AshuJoshi | 1:36e336869699 | 93 | * Sensor Functions |
AshuJoshi | 1:36e336869699 | 94 | ****************************************************/ |
AshuJoshi | 0:3ec6a7645098 | 95 | |
AshuJoshi | 1:36e336869699 | 96 | void readandprintBME280() { |
AshuJoshi | 2:866a72c3c3bf | 97 | float temperature; |
AshuJoshi | 2:866a72c3c3bf | 98 | float pressure; |
AshuJoshi | 2:866a72c3c3bf | 99 | float humidity; |
AshuJoshi | 2:866a72c3c3bf | 100 | char string_buffer[64]; |
AshuJoshi | 2:866a72c3c3bf | 101 | |
AshuJoshi | 2:866a72c3c3bf | 102 | // Temperature |
AshuJoshi | 2:866a72c3c3bf | 103 | temperature = b280.getTemperature(); |
AshuJoshi | 2:866a72c3c3bf | 104 | sprintf(string_buffer, "%s%3.2f", "TC:", temperature); |
AshuJoshi | 2:866a72c3c3bf | 105 | logInfo("The temperature is %s", string_buffer); |
AshuJoshi | 2:866a72c3c3bf | 106 | // Pressure |
AshuJoshi | 2:866a72c3c3bf | 107 | pressure = b280.getPressure(); |
AshuJoshi | 2:866a72c3c3bf | 108 | sprintf(string_buffer, "%s%04.2f", "hPa:", pressure); |
AshuJoshi | 2:866a72c3c3bf | 109 | logInfo("The pressure is %s", string_buffer); |
AshuJoshi | 2:866a72c3c3bf | 110 | // Humidity |
AshuJoshi | 2:866a72c3c3bf | 111 | humidity = b280.getHumidity(); |
AshuJoshi | 2:866a72c3c3bf | 112 | sprintf(string_buffer, "%s%03.2f", "H%:", humidity); |
AshuJoshi | 2:866a72c3c3bf | 113 | logInfo("The humidty is %s", string_buffer); |
AshuJoshi | 2:866a72c3c3bf | 114 | |
AshuJoshi | 2:866a72c3c3bf | 115 | // printf("%2.2f degC, %04.2f hPa, %2.2f %%\n", temperature, pressure, humidity); |
AshuJoshi | 1:36e336869699 | 116 | } |
AshuJoshi | 0:3ec6a7645098 | 117 | |
AshuJoshi | 0:3ec6a7645098 | 118 | |
AshuJoshi | 0:3ec6a7645098 | 119 | |
AshuJoshi | 0:3ec6a7645098 | 120 | /***************************************************** |
AshuJoshi | 1:36e336869699 | 121 | * FUNCTIONS for Simple Testing |
AshuJoshi | 1:36e336869699 | 122 | ****************************************************/ |
AshuJoshi | 0:3ec6a7645098 | 123 | |
AshuJoshi | 0:3ec6a7645098 | 124 | void setUpLEDBlink(){ |
AshuJoshi | 0:3ec6a7645098 | 125 | // configure the Ticker to blink the LED on 500ms interval |
AshuJoshi | 0:3ec6a7645098 | 126 | tick.attach(&blink, 0.5); |
AshuJoshi | 0:3ec6a7645098 | 127 | } |
AshuJoshi | 0:3ec6a7645098 | 128 | |
AshuJoshi | 0:3ec6a7645098 | 129 | void endLessTestLoop() { |
AshuJoshi | 0:3ec6a7645098 | 130 | while(true) { |
AshuJoshi | 1:36e336869699 | 131 | // printf("Hello world!\r\n"); |
AshuJoshi | 1:36e336869699 | 132 | printf("BME280 Sensor: \n"); |
AshuJoshi | 1:36e336869699 | 133 | readandprintBME280(); |
AshuJoshi | 2:866a72c3c3bf | 134 | |
AshuJoshi | 2:866a72c3c3bf | 135 | mDotGotoDeepSleep(10); |
AshuJoshi | 2:866a72c3c3bf | 136 | |
AshuJoshi | 0:3ec6a7645098 | 137 | } |
AshuJoshi | 0:3ec6a7645098 | 138 | } |
AshuJoshi | 0:3ec6a7645098 | 139 | |
AshuJoshi | 0:3ec6a7645098 | 140 | // Callback function to change LED state |
AshuJoshi | 0:3ec6a7645098 | 141 | void blink() { |
AshuJoshi | 0:3ec6a7645098 | 142 | led = !led; |
AshuJoshi | 0:3ec6a7645098 | 143 | } |