Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: libmDot-mbed5 ISL29011
Diff: examples/src/ota_example.cpp
- Revision:
- 44:89b8ed62c32c
- Parent:
- 43:231de26f0302
- Child:
- 45:0d541f98d058
diff -r 231de26f0302 -r 89b8ed62c32c examples/src/ota_example.cpp
--- a/examples/src/ota_example.cpp Tue Aug 25 02:48:32 2020 +0000
+++ b/examples/src/ota_example.cpp Wed Nov 11 00:27:58 2020 +0000
@@ -1,7 +1,18 @@
#include "dot_util.h"
#include "RadioEvent.h"
+#include "InterruptIn.h"
+#include "Callback.h"
-#if ACTIVE_EXAMPLE == OTA_EXAMPLE
+#define CHANNEL_PLAN CP_AS923 // Uncomment for different Freq. plans.
+//#define CHANNEL_PLAN CP_AU915
+static float batMAx = 4.18; // Change for different battery set up.
+static int uplinkInterval = 15; // Value of MINUTES between transmissions
+static uint16_t locationID = 001; // Change for different locations. (1 --> 65535)
+static float rawLatitude = -34.406000; // Change for different location
+static float rawLongitude = 150.880429; // left here as a potential feature
+//// Update if different TTN application is used. ////
+static uint8_t network_id[] = { 0x70, 0xB3, 0xD5, 0x7E, 0xD0, 0x03, 0x3B, 0xB9 };
+static uint8_t network_key[] = { 0x12, 0x1A, 0x81, 0x8F, 0x10, 0x6B, 0x18, 0x67, 0x54, 0xE6, 0x9E, 0x70, 0x53, 0x7C, 0xAD, 0xCF };
/////////////////////////////////////////////////////////////////////////////
// -------------------- DOT LIBRARY REQUIRED ------------------------------//
@@ -24,11 +35,9 @@
/////////////////////////////////////////////////////////////
static std::string network_name = "MultiTech";
static std::string network_passphrase = "MultiTech";
-static uint8_t network_id[] = { 0x70, 0xB3, 0xD5, 0x7E, 0xD0, 0x03, 0x3B, 0xB9 };
-static uint8_t network_key[] = { 0x12, 0x1A, 0x81, 0x8F, 0x10, 0x6B, 0x18, 0x67, 0x54, 0xE6, 0x9E, 0x70, 0x53, 0x7C, 0xAD, 0xCF };
static uint8_t frequency_sub_band = 0;
static lora::NetworkType network_type = lora::PUBLIC_LORAWAN;
-static uint8_t join_delay = 5000;
+static uint8_t join_delay = 5;
static uint8_t ack = 0;
static bool adr = true;
@@ -37,31 +46,51 @@
// in deepsleep mode, IOs float, RAM is lost, and application will start from beginning after waking up
// if deep_sleep == true, device will enter deepsleep mode
static bool deep_sleep = false;
+I2C i2c(PC_9, PA_8 );
mDot* dot = NULL;
lora::ChannelPlan* plan = NULL;
-Serial pc(USBTX, USBRX);
+BufferedSerial pc(USBTX, USBRX);
-#if defined(TARGET_XDOT_L151CC)
-I2C i2c(I2C_SDA, I2C_SCL);
-ISL29011 lux(i2c);
-#else
-AnalogIn lux(XBEE_AD0);
-#endif
+// Initialize I2C
+const int addr7bit = 0x40; // 7 bit I2C address
+const int addr8bit = addr7bit << 1; // 8bit I2C address, 0x80
+
+volatile int _count = 0;
-int main() {
- // Custom event handler for automatically displaying RX data
- RadioEvent events;
-
- pc.baud(115200);
+void incrementCounter(){
+ _count++;
+ wait_us(200000); // delay for debouncing
+}
+void voidCounter(){
+ wait_us(200000); // delay for debouncing
+}
+int readCounter(){
+ return _count;
+}
+void clearCounter(){
+ logInfo("Setting counter to 0");
+ _count = 0;
+}
-#if defined(TARGET_XDOT_L151CC)
+int main(){
+ AnalogIn bat(A0); // Battery % analog input pin
+ DigitalIn tamp(PA_3);
+
+ InterruptIn in(PA_11);
+ in.fall(&incrementCounter); // rise OR fall
+ in.rise(&voidCounter);
+ in.mode(PullUp);
+ __enable_irq();
+
+ RadioEvent events;
+
+ pc.set_baud(115200);
i2c.frequency(400000);
-#endif
-
+
mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL);
-
+
#if CHANNEL_PLAN == CP_AU915
plan = new lora::ChannelPlan_AU915();
#elif CHANNEL_PLAN == CP_AS923
@@ -97,12 +126,7 @@
// To preserve session over power-off or reset enable this flag
// dot->setPreserveSession(true);
- // 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
- // only one method or the other should be used!
- // network ID = crc64(network name)
- // network KEY = cmac(network passphrase)
- update_ota_config_name_phrase(network_name, network_passphrase, frequency_sub_band, network_type, ack);
- //update_ota_config_id_key(network_id, network_key, frequency_sub_band, network_type, ack);
+ update_ota_config_id_key(network_id, network_key, frequency_sub_band, network_type, ack);
// configure network link checks
// 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
@@ -134,51 +158,114 @@
while (true) {
uint16_t light;
+ uint16_t rain;
+ uint16_t temp;
+ uint16_t humid;
+ uint32_t lat;
+ uint32_t lon;
+ uint8_t tamper;
+
+ char cmd[2]; // I2C command address byte 8-bit
+ char my_data[2]; // I2C return address bytes 16-bit
+
std::vector<uint8_t> tx_data;
- // join network if not joined
+ // join network if not joined
if (!dot->getNetworkJoinStatus()) {
join_network();
}
-
-#if defined(TARGET_XDOT_L151CC)
- // configure the ISL29011 sensor on the xDot-DK for continuous ambient light sampling, 16 bit conversion, and maximum range
- lux.setMode(ISL29011::ALS_CONT);
- lux.setResolution(ISL29011::ADC_16BIT);
- lux.setRange(ISL29011::RNG_64000);
-
- // get the latest light sample and send it to the gateway
- light = lux.getData();
- tx_data.push_back((light >> 8) & 0xFF);
- tx_data.push_back(light & 0xFF);
- logInfo("light: %lu [0x%04X]", light, light);
- send_data(tx_data);
+
+ // Wake sensor /////////////////////////////////////////////////////////
+ cmd[0] = 0x01;
+ cmd[1] = 0x00;
+ i2c.write(addr8bit, cmd, 2);
+
+ // Read TEMERATURE
+ cmd[0] = 0xE3; // For
+ i2c.write(addr8bit, cmd, 1);
+ i2c.read(addr8bit, my_data, 2);
+ wait_us(20000);
+
+ uint16_t amb = (my_data[0] << 8) | my_data[1];
+ float temperature = amb;
+ temperature *= 175.72;
+ temperature /= 65536;
+ temperature -= 46.85;
+ logInfo("Temp = %4.2f*C", temperature);
+
+ wait_us(6000);
+
+ // Read HUMIDITY
+ cmd[0] = 0xE5; // For
+ i2c.write(addr8bit, cmd, 1);
+ i2c.read( addr8bit, my_data, 2);
+ wait_us(20000);
- // put the LSL29011 ambient light sensor into a low power state
- lux.setMode(ISL29011::PWR_DOWN);
-#else
- // get some dummy data and send it to the gateway
- light = lux.read_u16();
- tx_data.push_back((light >> 8) & 0xFF);
- tx_data.push_back(light & 0xFF);
- logInfo("light: %lu [0x%04X]", light, light);
+ uint16_t hum = (my_data[0] << 8) | my_data[1];
+ float humidity = hum;
+ humidity *= 125;
+ humidity /= 65536;
+ humidity -= 6;
+ if(humidity > 100.00){
+ humidity = 100.00;
+ }
+ logInfo("Humidity = %4.2f%", humidity);
+
+ // Shutdown temp sensor
+ cmd[0] = 0x00;
+ cmd[1] = 0x00;
+ i2c.write(addr8bit, cmd, 2);
+ ////////////////////////////////////////////////////////////////////////
+
+ float battery = bat.read();
+ battery = ((3*battery)*2) / batMAx * 100; // Turn battery V to a %
+ logInfo("Battery = %f", battery);
+ if(battery > 100){
+ battery = 100;
+ }
+
+ // Send it to the Cloudtracker
+ rain = readCounter(); // Read the pulse count
+ tx_data.push_back((rain >> 8) & 0xFF);
+ tx_data.push_back(rain & 0xFF);
+ clearCounter(); // Set pulse count back to 0
+
+ uint16_t battery_send = battery * 10;
+ tx_data.push_back((battery_send >> 8) & 0xFF);
+ tx_data.push_back(battery_send & 0xFF);
+
+ humid = humidity * 10;
+ tx_data.push_back((humid >> 8) & 0xFF);
+ tx_data.push_back(humid & 0xFF);
+
+ temp = temperature * 10;
+ tx_data.push_back((temp >> 8) & 0xFF);
+ tx_data.push_back(temp & 0xFF);
+
+ tamper = 0;
+ if(tamp == 0){
+ tamper = 1;
+ }
+ tx_data.push_back(tamper & 0xFF);
+
+ tx_data.push_back((locationID >> 8) & 0xFF);
+ tx_data.push_back(locationID & 0xFF);
+
+ // lat = (rawLatitude * 10000) + 900000;
+ // tx_data.push_back((lat >> 16) & 0xFF);
+ // tx_data.push_back((lat >> 8) & 0xFF);
+ // tx_data.push_back(lat & 0xFF);
+
+ // lon = (rawLongitude * 10000) + 1800000;
+ // tx_data.push_back((lon >> 16) & 0xFF);
+ // tx_data.push_back((lon >> 8) & 0xFF);
+ // tx_data.push_back(lon & 0xFF);
+
send_data(tx_data);
-#endif
-
- // if going into deepsleep mode, save the session so we don't need to join again after waking up
- // not necessary if going into sleep mode since RAM is retained
- if (deep_sleep) {
- logInfo("saving network session to NVM");
- dot->saveNetworkSession();
- }
-
- // ONLY ONE of the three functions below should be uncommented depending on the desired wakeup method
- //sleep_wake_rtc_only(deep_sleep);
- //sleep_wake_interrupt_only(deep_sleep);
- sleep_wake_rtc_or_interrupt(deep_sleep);
+
+ // Send to sleep for desired time.
+ ThisThread::sleep_for(uplinkInterval * 60s); // seconds
}
-
+
return 0;
}
-
-#endif