Version del programa usado para la Expo IOT 2018 - Strada Hotel

Dependencies:   DHT HX711 libmDot-mbed5 ISL29011

Fork of Dot-Examples by Rajnish Narayan

Committer:
Santi1990
Date:
Tue Apr 17 18:10:04 2018 +0000
Revision:
27:3f2cb700dd45
Parent:
22:d9bc10bbc433
Esta version usamos para la primera Expo de Xavia (Expo IOT 2018 - Strada Hotel).

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mfiore 0:a151a6350d7f 1 #include "dot_util.h"
Mike Fiore 14:19fae4509473 2 #include "RadioEvent.h"
Santi1990 27:3f2cb700dd45 3 #include "HX711.h"
Santi1990 27:3f2cb700dd45 4 #include "DHT.h"
Santi1990 27:3f2cb700dd45 5 #include <iostream>
Santi1990 27:3f2cb700dd45 6 #include <string>
Santi1990 27:3f2cb700dd45 7 #include <sstream>
mfiore 0:a151a6350d7f 8
mfiore 0:a151a6350d7f 9 #if ACTIVE_EXAMPLE == OTA_EXAMPLE
mfiore 0:a151a6350d7f 10
mfiore 17:d4f82e16de5f 11 /////////////////////////////////////////////////////////////////////////////
mfiore 17:d4f82e16de5f 12 // -------------------- DOT LIBRARY REQUIRED ------------------------------//
mfiore 17:d4f82e16de5f 13 // * Because these example programs can be used for both mDot and xDot //
mfiore 17:d4f82e16de5f 14 // devices, the LoRa stack is not included. The libmDot library should //
mfiore 17:d4f82e16de5f 15 // be imported if building for mDot devices. The libxDot library //
mfiore 17:d4f82e16de5f 16 // should be imported if building for xDot devices. //
mfiore 17:d4f82e16de5f 17 // * https://developer.mbed.org/teams/MultiTech/code/libmDot-dev-mbed5/ //
mfiore 17:d4f82e16de5f 18 // * https://developer.mbed.org/teams/MultiTech/code/libmDot-mbed5/ //
mfiore 17:d4f82e16de5f 19 // * https://developer.mbed.org/teams/MultiTech/code/libxDot-dev-mbed5/ //
mfiore 17:d4f82e16de5f 20 // * https://developer.mbed.org/teams/MultiTech/code/libxDot-mbed5/ //
mfiore 17:d4f82e16de5f 21 /////////////////////////////////////////////////////////////////////////////
mfiore 17:d4f82e16de5f 22
Mike Fiore 5:97ed5f2f099e 23 /////////////////////////////////////////////////////////////
Mike Fiore 5:97ed5f2f099e 24 // * these options must match the settings on your gateway //
Mike Fiore 5:97ed5f2f099e 25 // * edit their values to match your configuration //
Mike Fiore 5:97ed5f2f099e 26 // * frequency sub band is only relevant for the 915 bands //
Mike Fiore 5:97ed5f2f099e 27 // * either the network name and passphrase can be used or //
Mike Fiore 5:97ed5f2f099e 28 // the network ID (8 bytes) and KEY (16 bytes) //
Mike Fiore 5:97ed5f2f099e 29 /////////////////////////////////////////////////////////////
Santi1990 27:3f2cb700dd45 30 static std::string network_name = "xaviaiot";
Santi1990 27:3f2cb700dd45 31 static std::string network_passphrase = "xaviaiot";
Mike Fiore 5:97ed5f2f099e 32 static uint8_t network_id[] = { 0x6C, 0x4E, 0xEF, 0x66, 0xF4, 0x79, 0x86, 0xA6 };
Mike Fiore 5:97ed5f2f099e 33 static uint8_t network_key[] = { 0x1F, 0x33, 0xA1, 0x70, 0xA5, 0xF1, 0xFD, 0xA0, 0xAB, 0x69, 0x7A, 0xAE, 0x2B, 0x95, 0x91, 0x6B };
Santi1990 27:3f2cb700dd45 34 static uint8_t frequency_sub_band = 1;
Santi1990 27:3f2cb700dd45 35 static bool public_network = true;
Mike Fiore 15:364df461110f 36 static uint8_t ack = 0;
Mike Fiore 21:09d05faf0e13 37 static bool adr = true;
mfiore 0:a151a6350d7f 38
mfiore 0:a151a6350d7f 39 // deepsleep consumes slightly less current than sleep
mfiore 0:a151a6350d7f 40 // in sleep mode, IO state is maintained, RAM is retained, and application will resume after waking up
mfiore 0:a151a6350d7f 41 // in deepsleep mode, IOs float, RAM is lost, and application will start from beginning after waking up
mfiore 0:a151a6350d7f 42 // if deep_sleep == true, device will enter deepsleep mode
mfiore 0:a151a6350d7f 43 static bool deep_sleep = false;
mfiore 0:a151a6350d7f 44
mfiore 0:a151a6350d7f 45 mDot* dot = NULL;
Mike Fiore 21:09d05faf0e13 46 lora::ChannelPlan* plan = NULL;
mfiore 0:a151a6350d7f 47
mfiore 0:a151a6350d7f 48 Serial pc(USBTX, USBRX);
mfiore 0:a151a6350d7f 49
mfiore 0:a151a6350d7f 50 int main() {
Mike Fiore 14:19fae4509473 51 // Custom event handler for automatically displaying RX data
Mike Fiore 14:19fae4509473 52 RadioEvent events;
Mike Fiore 14:19fae4509473 53
mfiore 0:a151a6350d7f 54 pc.baud(115200);
mfiore 0:a151a6350d7f 55
mfiore 0:a151a6350d7f 56 mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL);
mfiore 0:a151a6350d7f 57
Mike Fiore 21:09d05faf0e13 58 #if CHANNEL_PLAN == CP_US915
Mike Fiore 21:09d05faf0e13 59 plan = new lora::ChannelPlan_US915();
Mike Fiore 21:09d05faf0e13 60 #elif CHANNEL_PLAN == CP_AU915
Mike Fiore 21:09d05faf0e13 61 plan = new lora::ChannelPlan_AU915();
Mike Fiore 21:09d05faf0e13 62 #elif CHANNEL_PLAN == CP_EU868
Mike Fiore 21:09d05faf0e13 63 plan = new lora::ChannelPlan_EU868();
Mike Fiore 21:09d05faf0e13 64 #elif CHANNEL_PLAN == CP_KR920
Mike Fiore 21:09d05faf0e13 65 plan = new lora::ChannelPlan_KR920();
Mike Fiore 21:09d05faf0e13 66 #elif CHANNEL_PLAN == CP_AS923
Mike Fiore 21:09d05faf0e13 67 plan = new lora::ChannelPlan_AS923();
Mike Fiore 21:09d05faf0e13 68 #elif CHANNEL_PLAN == CP_AS923_JAPAN
Mike Fiore 21:09d05faf0e13 69 plan = new lora::ChannelPlan_AS923_Japan();
mfiore 22:d9bc10bbc433 70 #elif CHANNEL_PLAN == CP_IN865
mfiore 22:d9bc10bbc433 71 plan = new lora::ChannelPlan_IN865();
Mike Fiore 21:09d05faf0e13 72 #endif
Mike Fiore 21:09d05faf0e13 73 assert(plan);
Mike Fiore 21:09d05faf0e13 74
Mike Fiore 21:09d05faf0e13 75 dot = mDot::getInstance(plan);
Mike Fiore 21:09d05faf0e13 76 assert(dot);
mfiore 0:a151a6350d7f 77
Mike Fiore 14:19fae4509473 78 // attach the custom events handler
Mike Fiore 14:19fae4509473 79 dot->setEvents(&events);
Mike Fiore 14:19fae4509473 80
Mike Fiore 12:ec9768677cea 81 if (!dot->getStandbyFlag()) {
Mike Fiore 16:a3832552dfe1 82 logInfo("mbed-os library version: %d", MBED_LIBRARY_VERSION);
Mike Fiore 16:a3832552dfe1 83
Mike Fiore 12:ec9768677cea 84 // start from a well-known state
Mike Fiore 12:ec9768677cea 85 logInfo("defaulting Dot configuration");
Mike Fiore 12:ec9768677cea 86 dot->resetConfig();
Mike Fiore 12:ec9768677cea 87 dot->resetNetworkSession();
mfiore 0:a151a6350d7f 88
Mike Fiore 12:ec9768677cea 89 // make sure library logging is turned on
Mike Fiore 12:ec9768677cea 90 dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
Mike Fiore 12:ec9768677cea 91
Mike Fiore 12:ec9768677cea 92 // update configuration if necessary
Mike Fiore 12:ec9768677cea 93 if (dot->getJoinMode() != mDot::OTA) {
Mike Fiore 12:ec9768677cea 94 logInfo("changing network join mode to OTA");
Mike Fiore 12:ec9768677cea 95 if (dot->setJoinMode(mDot::OTA) != mDot::MDOT_OK) {
Mike Fiore 12:ec9768677cea 96 logError("failed to set network join mode to OTA");
Mike Fiore 12:ec9768677cea 97 }
mfiore 0:a151a6350d7f 98 }
Mike Fiore 12:ec9768677cea 99 // in OTA and AUTO_OTA join modes, the credentials can be passed to the library as a name and passphrase or an ID and KEY
Mike Fiore 12:ec9768677cea 100 // only one method or the other should be used!
Mike Fiore 12:ec9768677cea 101 // network ID = crc64(network name)
Mike Fiore 12:ec9768677cea 102 // network KEY = cmac(network passphrase)
Mike Fiore 12:ec9768677cea 103 update_ota_config_name_phrase(network_name, network_passphrase, frequency_sub_band, public_network, ack);
Mike Fiore 12:ec9768677cea 104 //update_ota_config_id_key(network_id, network_key, frequency_sub_band, public_network, ack);
Mike Fiore 15:364df461110f 105
Mike Fiore 15:364df461110f 106 // configure network link checks
Mike Fiore 15:364df461110f 107 // network link checks are a good alternative to requiring the gateway to ACK every packet and should allow a single gateway to handle more Dots
Mike Fiore 15:364df461110f 108 // check the link every count packets
Mike Fiore 15:364df461110f 109 // declare the Dot disconnected after threshold failed link checks
Mike Fiore 21:09d05faf0e13 110 // for count = 3 and threshold = 5, the Dot will ask for a link check response every 5 packets and will consider the connection lost if it fails to receive 3 responses in a row
Mike Fiore 15:364df461110f 111 update_network_link_check_config(3, 5);
Mike Fiore 21:09d05faf0e13 112
Mike Fiore 21:09d05faf0e13 113 // enable or disable Adaptive Data Rate
Mike Fiore 21:09d05faf0e13 114 dot->setAdr(adr);
Mike Fiore 9:72d3203279b2 115
Mike Fiore 12:ec9768677cea 116 // save changes to configuration
Mike Fiore 12:ec9768677cea 117 logInfo("saving configuration");
Mike Fiore 12:ec9768677cea 118 if (!dot->saveConfig()) {
Mike Fiore 12:ec9768677cea 119 logError("failed to save configuration");
Mike Fiore 12:ec9768677cea 120 }
mfiore 0:a151a6350d7f 121
Mike Fiore 12:ec9768677cea 122 // display configuration
Mike Fiore 12:ec9768677cea 123 display_config();
Mike Fiore 12:ec9768677cea 124 } else {
Mike Fiore 12:ec9768677cea 125 // restore the saved session if the dot woke from deepsleep mode
Mike Fiore 12:ec9768677cea 126 // useful to use with deepsleep because session info is otherwise lost when the dot enters deepsleep
mfiore 0:a151a6350d7f 127 logInfo("restoring network session from NVM");
mfiore 0:a151a6350d7f 128 dot->restoreNetworkSession();
mfiore 0:a151a6350d7f 129 }
Santi1990 27:3f2cb700dd45 130 uint16_t temp;
Santi1990 27:3f2cb700dd45 131 uint16_t hum;
Santi1990 27:3f2cb700dd45 132 uint16_t weight;
Santi1990 27:3f2cb700dd45 133 std::string datos;
Santi1990 27:3f2cb700dd45 134
Santi1990 27:3f2cb700dd45 135
Santi1990 27:3f2cb700dd45 136 while (true) {
Mike Fiore 9:72d3203279b2 137
Santi1990 27:3f2cb700dd45 138 uint8_t array[8];
Santi1990 27:3f2cb700dd45 139 //std::vector<uint8_t> tx_data;
mfiore 0:a151a6350d7f 140
mfiore 0:a151a6350d7f 141 // join network if not joined
mfiore 0:a151a6350d7f 142 if (!dot->getNetworkJoinStatus()) {
mfiore 0:a151a6350d7f 143 join_network();
mfiore 0:a151a6350d7f 144 }
Santi1990 27:3f2cb700dd45 145
mfiore 0:a151a6350d7f 146
Santi1990 27:3f2cb700dd45 147 temp = 20;//dht.ReadTemperature(CELCIUS);
Santi1990 27:3f2cb700dd45 148 hum = 30; //dht.ReadHumidity();
Santi1990 27:3f2cb700dd45 149 weight = 10;
Santi1990 27:3f2cb700dd45 150
Santi1990 27:3f2cb700dd45 151 pc.printf("Temperatura: %d/t Humedad: %d/t Peso: %d\n",temp,hum,weight);
Santi1990 27:3f2cb700dd45 152
Santi1990 27:3f2cb700dd45 153 //for (std::string::iterator it = datos.begin(); it != datos.end(); it++)
Santi1990 27:3f2cb700dd45 154 //tx_data.push_back((temp >> 8) & 0xFF);
Santi1990 27:3f2cb700dd45 155 //tx_data.push_back(temp & 0xFF);
mfiore 0:a151a6350d7f 156
Santi1990 27:3f2cb700dd45 157 int i;
Santi1990 27:3f2cb700dd45 158 for (i = 0; i < 8; i++)
Santi1990 27:3f2cb700dd45 159 {
Santi1990 27:3f2cb700dd45 160 array[i] = (temp >> (8 * i)) & 0xff;
Santi1990 27:3f2cb700dd45 161 }
mfiore 0:a151a6350d7f 162
Santi1990 27:3f2cb700dd45 163 send(array);
Santi1990 27:3f2cb700dd45 164
Santi1990 27:3f2cb700dd45 165 //length = sprintf(pepe, "%d;%d;%d", weight, temp, hum);
Santi1990 27:3f2cb700dd45 166 //std::vector<uint8_t> vec(pepe, pepe+ length / chat);
Santi1990 27:3f2cb700dd45 167
Santi1990 27:3f2cb700dd45 168 //datos.clear();
Santi1990 27:3f2cb700dd45 169
Santi1990 27:3f2cb700dd45 170
Santi1990 27:3f2cb700dd45 171
Santi1990 27:3f2cb700dd45 172
Santi1990 27:3f2cb700dd45 173
Santi1990 27:3f2cb700dd45 174
mfiore 0:a151a6350d7f 175
mfiore 0:a151a6350d7f 176 // if going into deepsleep mode, save the session so we don't need to join again after waking up
mfiore 0:a151a6350d7f 177 // not necessary if going into sleep mode since RAM is retained
Santi1990 27:3f2cb700dd45 178
Santi1990 27:3f2cb700dd45 179 if (deep_sleep) {
mfiore 0:a151a6350d7f 180 logInfo("saving network session to NVM");
mfiore 0:a151a6350d7f 181 dot->saveNetworkSession();
mfiore 0:a151a6350d7f 182 }
mfiore 0:a151a6350d7f 183
mfiore 0:a151a6350d7f 184 // ONLY ONE of the three functions below should be uncommented depending on the desired wakeup method
mfiore 0:a151a6350d7f 185 //sleep_wake_rtc_only(deep_sleep);
mfiore 0:a151a6350d7f 186 //sleep_wake_interrupt_only(deep_sleep);
mfiore 0:a151a6350d7f 187 sleep_wake_rtc_or_interrupt(deep_sleep);
mfiore 0:a151a6350d7f 188 }
mfiore 0:a151a6350d7f 189
mfiore 0:a151a6350d7f 190 return 0;
mfiore 0:a151a6350d7f 191 }
mfiore 0:a151a6350d7f 192
Santi1990 27:3f2cb700dd45 193
mfiore 0:a151a6350d7f 194 #endif