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: DS1820 libmDot mbed-rtos mbed
main.cpp
- Committer:
- TataLora
- Date:
- 2016-07-07
- Revision:
- 0:7e7bd4f937c7
- Child:
- 1:0fe7bd229e28
File content as of revision 0:7e7bd4f937c7:
#include "mbed.h"
#include "mDot.h"
#include "MTSLog.h"
#include <string>
#include <vector>
#include <algorithm>
typedef enum {NONE, STARTING, TURNING, ERR, ALIVE, NO_SIGNAL, POWER_UP} activity_type;
// these options must match the settings on your Conduit
// uncomment the following lines and edit their values to match your configuration
static std::string config_network_name = "MultiTech";
static std::string config_network_pass = "MultiTech";
static uint8_t config_frequency_sub_band = 1;
Timer t;
int isPulseActive = 0;
int isWaiting = 1;
activity_type activity = NONE;
int pulseCount = 0;
bool initial = 1;
void ConfigLora(mDot* dot)
{
int32_t ret;
Timer configTimer;
configTimer.reset();
configTimer.start();
//*******************************************
// configuration
//*******************************************
// reset to default config so we know what state we're in
dot->resetConfig();
dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
// frequency sub band is only applicable in the 915 (US) frequency band
// if using a MultiTech Conduit gateway, use the same sub band as your Conduit (1-8) - the mDot will use the 8 channels in that sub band
// if using a gateway that supports all 64 channels, use sub band 0 - the mDot will use all 64 channels
logInfo("setting frequency sub band");
if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) {
logError("failed to set frequency sub band %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
}
logInfo("setting network name");
if ((ret = dot->setNetworkName(config_network_name)) != mDot::MDOT_OK) {
logError("failed to set network name %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
}
logInfo("setting network password");
if ((ret = dot->setNetworkPassphrase(config_network_pass)) != mDot::MDOT_OK) {
logError("failed to set network password %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
}
// a higher spreading factor allows for longer range but lower throughput
// in the 915 (US) frequency band, spreading factors 7 - 10 are available
// in the 868 (EU) frequency band, spreading factors 7 - 12 are available
logInfo("setting TX spreading factor");
if ((ret = dot->setTxDataRate(mDot::SF_10)) != mDot::MDOT_OK) {
logError("failed to set TX datarate %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
}
// request receive confirmation of packets from the gateway
logInfo("enabling ACKs");
if ((ret = dot->setAck(1)) != mDot::MDOT_OK) {
logError("failed to enable ACKs %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
}
// set join mode to AUTO_OTA so the mDot doesn't have to rejoin after sleeping
logInfo("setting join mode to AUTO_OTA");
if ((ret = dot->setJoinMode(mDot::AUTO_OTA)) != mDot::MDOT_OK) {
logError("failed to set join mode %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
}
// save this configuration to the mDot's NVM
logInfo("saving config");
if (! dot->saveConfig()) {
logError("failed to save configuration");
}
//*******************************************
// end of configuration
//*******************************************
logInfo("Configuratie duurde: %f seconds", configTimer.read());
configTimer.stop();
}
void resetPulse(mDot* dot)
{
int32_t ret;
std::vector<uint8_t> data;
std::string data_Starten = "Starten";
std::string data_Draaien = "Draaien";
std::string data_Error = "Error!";
std::string data_Alive = "Alive";
std::string data_NoSignal = "NoSignal";
std::string data_PowerUp = "PowerUp";
std::string data_Onbekend = "Onbekend";
bool stuurBericht = 1;
isPulseActive = 0;
if(activity == NONE)
{
logInfo("Einde pulse %d stuks - Onbekend", pulseCount);
// format data for sending to the gateway
for (std::string::iterator it = data_Onbekend.begin(); it != data_Onbekend.end(); it++)
data.push_back((uint8_t) *it);
stuurBericht = 0;
}
if(activity == STARTING)
{
logInfo("Einde pulse %d stuks - Starten", pulseCount);
// format data for sending to the gateway
for (std::string::iterator it = data_Starten.begin(); it != data_Starten.end(); it++)
data.push_back((uint8_t) *it);
}
if(activity == TURNING)
{
logInfo("Einde pulse %d stuks - Draaien", pulseCount);
// format data for sending to the gateway
for (std::string::iterator it = data_Draaien.begin(); it != data_Draaien.end(); it++)
data.push_back((uint8_t) *it);
}
if(activity == ERR)
{
logInfo("Einde pulse %d stuks - Storing", pulseCount);
// format data for sending to the gateway
for (std::string::iterator it = data_Error.begin(); it != data_Error.end(); it++)
data.push_back((uint8_t) *it);
}
if(activity == NO_SIGNAL)
{
logInfo("Einde pulse %d stuks - No signal", pulseCount);
// format data for sending to the gateway
for (std::string::iterator it = data_NoSignal.begin(); it != data_NoSignal.end(); it++)
data.push_back((uint8_t) *it);
}
if(activity == ALIVE)
{
logInfo("Einde pulse %d stuks - Alive", pulseCount);
// format data for sending to the gateway
for (std::string::iterator it = data_Alive.begin(); it != data_Alive.end(); it++)
data.push_back((uint8_t) *it);
}
if(activity == POWER_UP)
{
logInfo("Einde pulse %d stuks - Power up", pulseCount);
// format data for sending to the gateway
for (std::string::iterator it = data_PowerUp.begin(); it != data_PowerUp.end(); it++)
data.push_back((uint8_t) *it);
}
pulseCount = 0;
isWaiting = 1;
if(!stuurBericht)
{
logInfo("Geen bericht versturen");
return;
}
ConfigLora(dot);
// join the network if not joined
/*if (!dot->getNetworkJoinStatus()) {
logInfo("network not joined, joining network");
if ((ret = dot->joinNetwork()) != mDot::MDOT_OK) {
logError("failed to join network %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
}
}
if (dot->getNetworkJoinStatus()) {
// send the data
// ACKs are enabled by default, so we're expecting to get one back
if ((ret = dot->send(data)) != mDot::MDOT_OK) {
logError("failed to send %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
} else {
logInfo("successfully sent data to gateway");
}
}
*/
logInfo("joining network");
while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) {
logError("failed to join network %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
// in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again
//osDelay(std::max((uint32_t)1000, (uint32_t)dot->getNextTxMs()));
// in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again
uint32_t sleep_time = std::max((uint32_t)10000, (uint32_t)dot->getNextTxMs()) / 1000;
logInfo("going to sleep...");
osDelay(std::max((uint32_t)5000, (uint32_t)dot->getNextTxMs()));
}
while (true) {
// send the data to the gateway
if ((ret = dot->send(data)) != mDot::MDOT_OK) {
logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str());
} else {
logInfo("successfully sent data to gateway");
return;
}
// in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again
osDelay(std::max((uint32_t)5000, (uint32_t)dot->getNextTxMs()));
}
// in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again
//uint32_t sleep_time = std::max((uint32_t)10000, (uint32_t)dot->getNextTxMs()) / 1000;
//logInfo("going to sleep...");
}
int main() {
mDot* dot;
DigitalIn input(PA_0);
DigitalIn data0(PB_0);
DigitalIn data1(PB_1);
DigitalIn data2(PA_1);
// get a mDot handle
dot = mDot::getInstance();
// print library version information
logInfo("V: %s", dot->getId().c_str());
if(input)
{
uint8_t data = 0;
if(data0)
{
data |= (1 << 0);
}
if(data1)
{
data |= (1 << 1);
}
if(data2)
{
data |= (1 << 2);
}
if(data == 0)
{
activity = POWER_UP;
logInfo("Starting with data: POWER_UP");
}
if(data == 1)
{
activity = ERR;
logInfo("Starting with data: ERROR");
}
if(data == 2)
{
activity = TURNING;
logInfo("Starting with data: TURNING");
}
if(data == 3)
{
activity = STARTING;
logInfo("Starting with data: STARTING");
}
if(data == 4)
{
activity = ALIVE;
logInfo("Starting with data: ALIVE");
}
resetPulse(dot);
}
else
{
activity = NO_SIGNAL;
logInfo("Signal-pin was niet hoog");
resetPulse(dot);
}
//32 minuten slapen of wachten op een interrupt
dot->sleep(1920, mDot::RTC_ALARM_OR_INTERRUPT, true);
//logInfo("Pulsen inspecteren");
/*t.start();
int oldValue = 2;
while (true) {
int newLedValue = input;
int newValue = input.read();
int diff;
if(newValue > oldValue)
{
diff = newValue - oldValue;
}
else
{
diff = oldValue - newValue;
}
if(isPulseActive)
{
if(t.read_ms() > 1200)
{
if(pulseCount < 3)
{
activity = NONE;
}
t.stop();
resetPulse(dot);
//printf("Slapen\r\n");
// go to deepsleep and wake up automatically sleep_time seconds later
dot->sleep(20, mDot::INTERRUPT, true);
}
else if(activity == ERR)
{
if(pulseCount > 5)
{
t.stop();
resetPulse(dot);
break;
}
}
}
if(initial)
{
if(t.read_ms() > 1200)
{
logInfo("Geen pulsen gedetecteerd, slapen");
dot->sleep(0, mDot::INTERRUPT, true);
}
}
if(diff >= 1)
{
if(newValue)
{
initial = 0;
t.stop();
t.reset();
t.start();
isPulseActive = 1;
//logInfo("Start ");
//dot->sleep(0, mDot::INTERRUPT, false);
}
else if(isPulseActive)
{
t.stop();
logInfo("Positieve puls duur %f seconds", t.read());
int timeInMs = t.read_ms();
if(timeInMs > 990 && timeInMs < 1020)
{
activity = STARTING;
}
else if(timeInMs > 220 && timeInMs < 260)
{
activity = TURNING;
}
else if(timeInMs > 88 && timeInMs < 92)
{
activity = ALIVE;
dot->sleep(20, mDot::INTERRUPT, true);
}
else if(timeInMs > 32 && timeInMs < 40)
{
activity = ERR;
}
else if(timeInMs > 20)
{
activity = NONE;
}
pulseCount++;
t.reset();
t.start();
//dot->sleep(2, mDot::RTC_ALARM, false);
}
//printf("analog value: %d\r\n", newValue);
}
oldValue = newValue;
if(!isWaiting)
{
//printf("Slapen\r\n");
//dot->sleep(4, mDot::RTC_ALARM);
}
}
logInfo("Nu nergens meer op reageren.....");
dot->sleep(20, mDot::INTERRUPT, true);
*/
return 0;
}