Utility for reading temperature on DHT22 and sending to LoRa network

Dependencies:   DHT22 libmDot-mbed5

Committer:
kellybs1
Date:
Tue Oct 31 03:12:37 2017 +0000
Revision:
0:e0037d88baa2
initial commit - with junk keys

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kellybs1 0:e0037d88baa2 1 /*
kellybs1 0:e0037d88baa2 2 This program:
kellybs1 0:e0037d88baa2 3 - connects to a LoRaWAN by ABP/MANUAL
kellybs1 0:e0037d88baa2 4 - reads light data from an analogue Light Dependent Resistor
kellybs1 0:e0037d88baa2 5 - reads temperaure and humidity from a DHT22 sensor
kellybs1 0:e0037d88baa2 6 - sends the recorded data onto the LoRaWAN
kellybs1 0:e0037d88baa2 7 - sets the mDot to sleep
kellybs1 0:e0037d88baa2 8 - repeats these operations in a loop
kellybs1 0:e0037d88baa2 9 */
kellybs1 0:e0037d88baa2 10
kellybs1 0:e0037d88baa2 11 #include "mbed.h"
kellybs1 0:e0037d88baa2 12 #include "mDot.h"
kellybs1 0:e0037d88baa2 13 #include "ChannelPlans.h"
kellybs1 0:e0037d88baa2 14 #include "MTSLog.h"
kellybs1 0:e0037d88baa2 15 #include "dot_util.h"
kellybs1 0:e0037d88baa2 16 #include "mbed.h"
kellybs1 0:e0037d88baa2 17 #include "DHT22.h"
kellybs1 0:e0037d88baa2 18 #include <string>
kellybs1 0:e0037d88baa2 19 #include <vector>
kellybs1 0:e0037d88baa2 20 #include <algorithm>
kellybs1 0:e0037d88baa2 21 #include <sstream>
kellybs1 0:e0037d88baa2 22
kellybs1 0:e0037d88baa2 23 //dht 22 pin (D6 on UDK 2)
kellybs1 0:e0037d88baa2 24 DHT22 dht22(PA_1);
kellybs1 0:e0037d88baa2 25
kellybs1 0:e0037d88baa2 26 // these options must match the settings on your gateway/server
kellybs1 0:e0037d88baa2 27 /*
kellybs1 0:e0037d88baa2 28
kellybs1 0:e0037d88baa2 29 */
kellybs1 0:e0037d88baa2 30 //device address
kellybs1 0:e0037d88baa2 31 static uint8_t network_address[] = { 0xaa, 0xaa, 0xaa, 0xaa };
kellybs1 0:e0037d88baa2 32 //network session key
kellybs1 0:e0037d88baa2 33 static uint8_t network_session_key[] = { 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
kellybs1 0:e0037d88baa2 34 //application sesssion or data session key
kellybs1 0:e0037d88baa2 35 static uint8_t data_session_key[] = { 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
kellybs1 0:e0037d88baa2 36 static uint8_t frequency_sub_band = 2; //VFI
kellybs1 0:e0037d88baa2 37 static bool public_network = true;
kellybs1 0:e0037d88baa2 38 //enable receipt of acknowledge packets 0 = No, 1 = Yes
kellybs1 0:e0037d88baa2 39 static uint8_t ack = 0;
kellybs1 0:e0037d88baa2 40 //adaptive data rate enabler
kellybs1 0:e0037d88baa2 41 static bool adr = false;
kellybs1 0:e0037d88baa2 42 //how many seconds to sleep for
kellybs1 0:e0037d88baa2 43 uint32_t sleep_time = 300;
kellybs1 0:e0037d88baa2 44
kellybs1 0:e0037d88baa2 45 //USB serial
kellybs1 0:e0037d88baa2 46 Serial pc(USBTX, USBRX);
kellybs1 0:e0037d88baa2 47
kellybs1 0:e0037d88baa2 48 //get ourselves an mDot pointer - we will assign to it in main()
kellybs1 0:e0037d88baa2 49 mDot* dot = NULL;
kellybs1 0:e0037d88baa2 50
kellybs1 0:e0037d88baa2 51 //converts value to string
kellybs1 0:e0037d88baa2 52 template <typename T>
kellybs1 0:e0037d88baa2 53 string ToString(T val) {
kellybs1 0:e0037d88baa2 54 stringstream stream;
kellybs1 0:e0037d88baa2 55 stream << val;
kellybs1 0:e0037d88baa2 56 return stream.str();
kellybs1 0:e0037d88baa2 57 }
kellybs1 0:e0037d88baa2 58
kellybs1 0:e0037d88baa2 59 int main() {
kellybs1 0:e0037d88baa2 60 //setting serial rate
kellybs1 0:e0037d88baa2 61 pc.baud(9600);
kellybs1 0:e0037d88baa2 62
kellybs1 0:e0037d88baa2 63 // use AU915 plan
kellybs1 0:e0037d88baa2 64 lora::ChannelPlan* plan = new lora::ChannelPlan_AU915();
kellybs1 0:e0037d88baa2 65 assert(plan);
kellybs1 0:e0037d88baa2 66 // get a mDot handle with the plan we chose
kellybs1 0:e0037d88baa2 67 dot = mDot::getInstance(plan);
kellybs1 0:e0037d88baa2 68 assert(dot);
kellybs1 0:e0037d88baa2 69
kellybs1 0:e0037d88baa2 70 if (!dot->getStandbyFlag()) {
kellybs1 0:e0037d88baa2 71 logInfo("mbed-os library version: %d", MBED_LIBRARY_VERSION);
kellybs1 0:e0037d88baa2 72
kellybs1 0:e0037d88baa2 73 // start from a well-known state
kellybs1 0:e0037d88baa2 74 logInfo("defaulting Dot configuration");
kellybs1 0:e0037d88baa2 75 dot->resetConfig();
kellybs1 0:e0037d88baa2 76 dot->resetNetworkSession();
kellybs1 0:e0037d88baa2 77
kellybs1 0:e0037d88baa2 78 // make sure library logging is turned on
kellybs1 0:e0037d88baa2 79 dot->setLogLevel(mts::MTSLog::DEBUG_LEVEL);
kellybs1 0:e0037d88baa2 80
kellybs1 0:e0037d88baa2 81 // update configuration if necessary
kellybs1 0:e0037d88baa2 82 if (dot->getJoinMode() != mDot::MANUAL) {
kellybs1 0:e0037d88baa2 83 logInfo("changing network join mode to MANUAL");
kellybs1 0:e0037d88baa2 84 if (dot->setJoinMode(mDot::MANUAL) != mDot::MDOT_OK) {
kellybs1 0:e0037d88baa2 85 logError("failed to set network join mode to MANUAL");
kellybs1 0:e0037d88baa2 86 }
kellybs1 0:e0037d88baa2 87 }
kellybs1 0:e0037d88baa2 88 // in MANUAL join mode there is no join request/response transaction
kellybs1 0:e0037d88baa2 89 // as long as the Dot is configured correctly and provisioned correctly on the gateway, it should be able to communicate
kellybs1 0:e0037d88baa2 90 // network address - 4 bytes (00000001 - FFFFFFFE)
kellybs1 0:e0037d88baa2 91 // network session key - 16 bytes
kellybs1 0:e0037d88baa2 92 // data session key - 16 bytes
kellybs1 0:e0037d88baa2 93 // to provision your Dot with a Conduit gateway, follow the following steps
kellybs1 0:e0037d88baa2 94 // * ssh into the Conduit
kellybs1 0:e0037d88baa2 95 // * provision the Dot using the lora-query application: http://www.multitech.net/developer/software/lora/lora-network-server/
kellybs1 0:e0037d88baa2 96 // lora-query -a 01020304 A 0102030401020304 <your Dot's device ID> 01020304010203040102030401020304 01020304010203040102030401020304
kellybs1 0:e0037d88baa2 97 // * if you change the network address, network session key, or data session key, make sure you update them on the gateway
kellybs1 0:e0037d88baa2 98 // to provision your Dot with a 3rd party gateway, see the gateway or network provider documentation
kellybs1 0:e0037d88baa2 99 update_manual_config(network_address, network_session_key, data_session_key, frequency_sub_band, public_network, ack);
kellybs1 0:e0037d88baa2 100
kellybs1 0:e0037d88baa2 101
kellybs1 0:e0037d88baa2 102 // enable or disable Adaptive Data Rate
kellybs1 0:e0037d88baa2 103 dot->setAdr(adr);
kellybs1 0:e0037d88baa2 104
kellybs1 0:e0037d88baa2 105 //* AU915 Datarates
kellybs1 0:e0037d88baa2 106 //* ---------------
kellybs1 0:e0037d88baa2 107 //* DR0 - SF10BW125 -- 11 bytes
kellybs1 0:e0037d88baa2 108 //* DR1 - SF9BW125 -- 53 bytes
kellybs1 0:e0037d88baa2 109 //* DR2 - SF8BW125 -- 129 byte
kellybs1 0:e0037d88baa2 110 //* DR3 - SF7BW125 -- 242 bytes
kellybs1 0:e0037d88baa2 111 //* DR4 - SF8BW500 -- 242 bytes
kellybs1 0:e0037d88baa2 112 dot->setTxDataRate(mDot::DR2);
kellybs1 0:e0037d88baa2 113
kellybs1 0:e0037d88baa2 114 // save changes to configuration
kellybs1 0:e0037d88baa2 115 logInfo("saving configuration");
kellybs1 0:e0037d88baa2 116 if (!dot->saveConfig()) {
kellybs1 0:e0037d88baa2 117 logError("failed to save configuration");
kellybs1 0:e0037d88baa2 118 }
kellybs1 0:e0037d88baa2 119
kellybs1 0:e0037d88baa2 120 // display configuration
kellybs1 0:e0037d88baa2 121 display_config();
kellybs1 0:e0037d88baa2 122 } else {
kellybs1 0:e0037d88baa2 123 // restore the saved session if the dot woke from deepsleep mode
kellybs1 0:e0037d88baa2 124 // useful to use with deepsleep because session info is otherwise lost when the dot enters deepsleep
kellybs1 0:e0037d88baa2 125 logInfo("restoring network session from NVM");
kellybs1 0:e0037d88baa2 126 dot->restoreNetworkSession();
kellybs1 0:e0037d88baa2 127 }
kellybs1 0:e0037d88baa2 128
kellybs1 0:e0037d88baa2 129 //this is where the magic happens
kellybs1 0:e0037d88baa2 130 while (true) {
kellybs1 0:e0037d88baa2 131
kellybs1 0:e0037d88baa2 132 //wake up
kellybs1 0:e0037d88baa2 133 wait(3);
kellybs1 0:e0037d88baa2 134 //init data variable
kellybs1 0:e0037d88baa2 135 std::vector<uint8_t> data;
kellybs1 0:e0037d88baa2 136
kellybs1 0:e0037d88baa2 137
kellybs1 0:e0037d88baa2 138 //read DHT22
kellybs1 0:e0037d88baa2 139 //get dht22 sample
kellybs1 0:e0037d88baa2 140 logInfo("Stopped");
kellybs1 0:e0037d88baa2 141 int error = dht22.sample();
kellybs1 0:e0037d88baa2 142
kellybs1 0:e0037d88baa2 143 //sampling is a little slow - give it time
kellybs1 0:e0037d88baa2 144 wait_ms(100);
kellybs1 0:e0037d88baa2 145 //it's required to divide these values by ten for some reason
kellybs1 0:e0037d88baa2 146 float t = (float)dht22.getTemperature() / 10;
kellybs1 0:e0037d88baa2 147
kellybs1 0:e0037d88baa2 148 //build our output string now
kellybs1 0:e0037d88baa2 149 string output = ToString(t);
kellybs1 0:e0037d88baa2 150
kellybs1 0:e0037d88baa2 151 //serial output for debugging
kellybs1 0:e0037d88baa2 152 logInfo("Sending %s", output.c_str());
kellybs1 0:e0037d88baa2 153
kellybs1 0:e0037d88baa2 154 // format data for sending to the gateway
kellybs1 0:e0037d88baa2 155 for (std::string::iterator it = output.begin(); it != output.end(); it++)
kellybs1 0:e0037d88baa2 156 data.push_back((uint8_t) *it);
kellybs1 0:e0037d88baa2 157
kellybs1 0:e0037d88baa2 158 //now send
kellybs1 0:e0037d88baa2 159 send_data(data);
kellybs1 0:e0037d88baa2 160
kellybs1 0:e0037d88baa2 161 // go to sleep and wake up automatically sleep_time seconds later
kellybs1 0:e0037d88baa2 162 //false is "don't deep sleep" - mDot doesn't do that
kellybs1 0:e0037d88baa2 163 dot->sleep(sleep_time, mDot::RTC_ALARM, false);
kellybs1 0:e0037d88baa2 164 }
kellybs1 0:e0037d88baa2 165
kellybs1 0:e0037d88baa2 166 return 0; //shouldn't happen
kellybs1 0:e0037d88baa2 167 }
kellybs1 0:e0037d88baa2 168