ADC Sensor LoRa Demo
Dependencies: libmDot mbed-rtos mbed-src
main.cpp@0:d9c1041494ec, 2015-07-18 (annotated)
- Committer:
- jamescmaki
- Date:
- Sat Jul 18 00:02:04 2015 +0000
- Revision:
- 0:d9c1041494ec
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jamescmaki | 0:d9c1041494ec | 1 | /* |
jamescmaki | 0:d9c1041494ec | 2 | * ADC Sensor Demo |
jamescmaki | 0:d9c1041494ec | 3 | * |
jamescmaki | 0:d9c1041494ec | 4 | * Analog inputs are read and pushed to the LoRa gateway. |
jamescmaki | 0:d9c1041494ec | 5 | * |
jamescmaki | 0:d9c1041494ec | 6 | * Analog sensors should be wired to mDot pins 16 and 17. |
jamescmaki | 0:d9c1041494ec | 7 | * |
jamescmaki | 0:d9c1041494ec | 8 | */ |
jamescmaki | 0:d9c1041494ec | 9 | |
jamescmaki | 0:d9c1041494ec | 10 | #include "mbed.h" |
jamescmaki | 0:d9c1041494ec | 11 | #include "mDot.h" |
jamescmaki | 0:d9c1041494ec | 12 | #include <string> |
jamescmaki | 0:d9c1041494ec | 13 | #include <vector> |
jamescmaki | 0:d9c1041494ec | 14 | |
jamescmaki | 0:d9c1041494ec | 15 | /* |
jamescmaki | 0:d9c1041494ec | 16 | * LoRa settings |
jamescmaki | 0:d9c1041494ec | 17 | * |
jamescmaki | 0:d9c1041494ec | 18 | * For Conduit AEP settings can be found at: |
jamescmaki | 0:d9c1041494ec | 19 | * |
jamescmaki | 0:d9c1041494ec | 20 | * https://<ip-address>/lora_network.html |
jamescmaki | 0:d9c1041494ec | 21 | * |
jamescmaki | 0:d9c1041494ec | 22 | * For Conduit mlinux: |
jamescmaki | 0:d9c1041494ec | 23 | * |
jamescmaki | 0:d9c1041494ec | 24 | * /var/config/lora/lora-network-server.conf |
jamescmaki | 0:d9c1041494ec | 25 | * |
jamescmaki | 0:d9c1041494ec | 26 | */ |
jamescmaki | 0:d9c1041494ec | 27 | static std::string config_network_name = "testtest"; |
jamescmaki | 0:d9c1041494ec | 28 | static std::string config_network_pass = "memememe"; |
jamescmaki | 0:d9c1041494ec | 29 | static uint8_t config_frequency_sub_band = 5; |
jamescmaki | 0:d9c1041494ec | 30 | |
jamescmaki | 0:d9c1041494ec | 31 | enum { |
jamescmaki | 0:d9c1041494ec | 32 | MSG_TYPE_ADC0, |
jamescmaki | 0:d9c1041494ec | 33 | MSG_TYPE_ADC1, |
jamescmaki | 0:d9c1041494ec | 34 | }; |
jamescmaki | 0:d9c1041494ec | 35 | |
jamescmaki | 0:d9c1041494ec | 36 | AnalogIn adc0(PA_1); // PIN16 on mDot |
jamescmaki | 0:d9c1041494ec | 37 | AnalogIn adc1(PA_4); // PIN17 on mDot |
jamescmaki | 0:d9c1041494ec | 38 | |
jamescmaki | 0:d9c1041494ec | 39 | Serial debug(USBTX, USBRX); |
jamescmaki | 0:d9c1041494ec | 40 | |
jamescmaki | 0:d9c1041494ec | 41 | int main() { |
jamescmaki | 0:d9c1041494ec | 42 | int32_t rc; |
jamescmaki | 0:d9c1041494ec | 43 | |
jamescmaki | 0:d9c1041494ec | 44 | debug.baud(115200); |
jamescmaki | 0:d9c1041494ec | 45 | |
jamescmaki | 0:d9c1041494ec | 46 | mDot *mdot = mDot::getInstance(); |
jamescmaki | 0:d9c1041494ec | 47 | |
jamescmaki | 0:d9c1041494ec | 48 | mdot->resetConfig(); |
jamescmaki | 0:d9c1041494ec | 49 | |
jamescmaki | 0:d9c1041494ec | 50 | printf("mDot version: %s\r\n", mdot->getId().c_str()); |
jamescmaki | 0:d9c1041494ec | 51 | |
jamescmaki | 0:d9c1041494ec | 52 | // Activity Status on UDK2 |
jamescmaki | 0:d9c1041494ec | 53 | mdot->setActivityLedPin(LED1); |
jamescmaki | 0:d9c1041494ec | 54 | mdot->setActivityLedEnable(true); |
jamescmaki | 0:d9c1041494ec | 55 | |
jamescmaki | 0:d9c1041494ec | 56 | if ((rc = mdot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) { |
jamescmaki | 0:d9c1041494ec | 57 | printf("failed to set frequency sub-band: %d\r\n", rc); |
jamescmaki | 0:d9c1041494ec | 58 | return 1; |
jamescmaki | 0:d9c1041494ec | 59 | } |
jamescmaki | 0:d9c1041494ec | 60 | |
jamescmaki | 0:d9c1041494ec | 61 | if ((rc = mdot->setNetworkName(config_network_name)) != mDot::MDOT_OK) { |
jamescmaki | 0:d9c1041494ec | 62 | printf("failed to set network name: %d\r\n", rc); |
jamescmaki | 0:d9c1041494ec | 63 | return 1; |
jamescmaki | 0:d9c1041494ec | 64 | } |
jamescmaki | 0:d9c1041494ec | 65 | |
jamescmaki | 0:d9c1041494ec | 66 | if ((rc = mdot->setNetworkPassphrase(config_network_pass)) != mDot::MDOT_OK) { |
jamescmaki | 0:d9c1041494ec | 67 | printf("failed to set network pass phrase: %d\r\n", rc); |
jamescmaki | 0:d9c1041494ec | 68 | return 1; |
jamescmaki | 0:d9c1041494ec | 69 | } |
jamescmaki | 0:d9c1041494ec | 70 | |
jamescmaki | 0:d9c1041494ec | 71 | while ((rc = mdot->joinNetwork()) != mDot::MDOT_OK) { |
jamescmaki | 0:d9c1041494ec | 72 | printf("failed to join network: %d\r\n", rc); |
jamescmaki | 0:d9c1041494ec | 73 | if (mdot->getFrequencyBand() == mDot::FB_868){ |
jamescmaki | 0:d9c1041494ec | 74 | rc = mdot->getNextTxMs(); |
jamescmaki | 0:d9c1041494ec | 75 | } else { |
jamescmaki | 0:d9c1041494ec | 76 | rc = 0; |
jamescmaki | 0:d9c1041494ec | 77 | } |
jamescmaki | 0:d9c1041494ec | 78 | |
jamescmaki | 0:d9c1041494ec | 79 | wait(1); |
jamescmaki | 0:d9c1041494ec | 80 | } |
jamescmaki | 0:d9c1041494ec | 81 | |
jamescmaki | 0:d9c1041494ec | 82 | while (true) { |
jamescmaki | 0:d9c1041494ec | 83 | std::vector<uint8_t> lora_pl; |
jamescmaki | 0:d9c1041494ec | 84 | |
jamescmaki | 0:d9c1041494ec | 85 | // Read analog inputs |
jamescmaki | 0:d9c1041494ec | 86 | uint16_t adc0_value = adc0.read_u16(); |
jamescmaki | 0:d9c1041494ec | 87 | uint16_t adc1_value = adc1.read_u16(); |
jamescmaki | 0:d9c1041494ec | 88 | |
jamescmaki | 0:d9c1041494ec | 89 | // Pack ADC0 TLV |
jamescmaki | 0:d9c1041494ec | 90 | lora_pl.clear(); |
jamescmaki | 0:d9c1041494ec | 91 | lora_pl.push_back(MSG_TYPE_ADC0); |
jamescmaki | 0:d9c1041494ec | 92 | lora_pl.push_back(2); |
jamescmaki | 0:d9c1041494ec | 93 | lora_pl.push_back(((char *) &adc0_value)[1]); |
jamescmaki | 0:d9c1041494ec | 94 | lora_pl.push_back(((char *) &adc0_value)[0]); |
jamescmaki | 0:d9c1041494ec | 95 | |
jamescmaki | 0:d9c1041494ec | 96 | // Pack ADC1 TLV |
jamescmaki | 0:d9c1041494ec | 97 | lora_pl.push_back(MSG_TYPE_ADC1); |
jamescmaki | 0:d9c1041494ec | 98 | lora_pl.push_back(2); |
jamescmaki | 0:d9c1041494ec | 99 | lora_pl.push_back(((char *) &adc1_value)[1]); |
jamescmaki | 0:d9c1041494ec | 100 | lora_pl.push_back(((char *) &adc1_value)[0]); |
jamescmaki | 0:d9c1041494ec | 101 | |
jamescmaki | 0:d9c1041494ec | 102 | // Send message |
jamescmaki | 0:d9c1041494ec | 103 | while ((rc = mdot->send(lora_pl)) != mDot::MDOT_OK) { |
jamescmaki | 0:d9c1041494ec | 104 | printf("failed to send message: %d\r\n", rc); |
jamescmaki | 0:d9c1041494ec | 105 | wait(1); |
jamescmaki | 0:d9c1041494ec | 106 | } |
jamescmaki | 0:d9c1041494ec | 107 | |
jamescmaki | 0:d9c1041494ec | 108 | wait(5); |
jamescmaki | 0:d9c1041494ec | 109 | } |
jamescmaki | 0:d9c1041494ec | 110 | } |