Easylube
Dependencies: DS1820 libmDot mbed-rtos mbed
Diff: main.cpp
- Revision:
- 2:26ffaed90e4f
- Parent:
- 1:0fe7bd229e28
- Child:
- 3:323401b1b227
--- a/main.cpp Tue Aug 23 08:47:09 2016 +0000 +++ b/main.cpp Wed Aug 31 21:01:30 2016 +0000 @@ -4,431 +4,125 @@ #include <string> #include <vector> #include <algorithm> - -typedef enum {NONE, STARTING, TURNING, ERR, ALIVE, NO_SIGNAL, POWER_UP} activity_type; - - +#include "EasyLube.h" +#include "Lora.h" +#include "DS1820.h" + +#define TEMP_DATA_PIN PC_1 +#define TEMP_ERROR -1000 + +#define MESSAGE_PREFIX "HTD-LUBRICANT:" + +#define LORA_NETWORK_NAME "MultiTech" +#define LORA_NETWORK_PASS "MultiTech" +#define LORA_NETWORK_FREQUENCY_SUB_BAND 1 +#define LORA_MAX_ATTEMPTS 1 + -// 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; +static std::string message_prefix = MESSAGE_PREFIX; -void PrintID() +EasyLube easyLube; +mDot* dot; + +void init() { - mDot* dot = mDot::getInstance(); - std::vector<uint8_t> unitID = dot->getDeviceId(); - char EUI[20]; - char Id[2]; - string Ident; + // get a mDot handle + dot = mDot::getInstance(); - for(int n = 0; n < 8; n++) - { - sprintf(Id,"%02X",unitID[n]); - strcat (EUI,Id); - } - Ident = EUI; - logInfo("UID = %s \r\n",Ident.c_str()); -} - -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(); + // Set log level to info, so everything will be sent to the output? dot->setLogLevel(mts::MTSLog::INFO_LEVEL); - - PrintID(); - - // 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(); + // print library version information + logInfo("V: %s", dot->getId().c_str()); } - -void resetPulse(mDot* dot) +//Maak een bericht van de huidige activiteit met temperatuur +std::vector<uint8_t> createMessage(EasyLube::Activity activity, float temp) { - 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; + //Eerst de prefix kopieren zodat het bericht aan de andere kant te herkennen is + for (std::string::iterator it = message_prefix.begin(); it != message_prefix.end(); it++) + data.push_back((uint8_t) *it); - if(activity == NONE) + //Afhankelijk van de activiteit voegen we een code toe + switch(activity) { - 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); + case EasyLube::STARTING: + data.push_back((uint8_t) 'S'); data.push_back((uint8_t) 'T'); + break; + case EasyLube::DOSAGE: + data.push_back((uint8_t) 'D'); data.push_back((uint8_t) 'O'); + break; + case EasyLube::ERROR: + data.push_back((uint8_t) 'E'); data.push_back((uint8_t) 'R'); + break; + case EasyLube::ALIVE: + data.push_back((uint8_t) 'A'); data.push_back((uint8_t) 'L'); + break; + case EasyLube::NO_SIGNAL: + case EasyLube::POWER_UP: + data.push_back((uint8_t) 'P'); data.push_back((uint8_t) 'U'); + break; + default: + data.push_back((uint8_t) 'O'); data.push_back((uint8_t) 'N'); } - 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) + //Als de temperatuur anders is dan de TEMP_ERROR, dan voegen we deze waarde ook toe aan het resultaat + if(temp > TEMP_ERROR) { - 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()); + //Format is TEMP=123.4, met een comma vooraf om onderscheid te maken met de code + char buffer[50]; + uint8_t n=sprintf (buffer, ",TEMP=%3.1f", temp); + logInfo("It is %3.1foC", temp); + for (uint8_t i = 0; i < n; i++) + { + data.push_back((uint8_t) buffer[i]); } } - 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..."); + return data; } int main() { - mDot* dot; - DigitalIn input(PA_0); - DigitalIn data0(PB_0); - DigitalIn data1(PB_1); - DigitalIn data2(PA_1); + init(); + + //De lora instantie kan pas aangemaakt worden er een instantie is van de mDot welke in de init wordt gemaakt + Lora lora(dot, LORA_NETWORK_NAME, LORA_NETWORK_PASS, LORA_NETWORK_FREQUENCY_SUB_BAND); - // get a mDot handle - dot = mDot::getInstance(); + //De huidige activiteit opvragen + EasyLube::Activity activity = easyLube.getActivity(); - // print library version information - logInfo("V: %s", dot->getId().c_str()); - - - if(input) + //Als er geen activiteit is dan zijn we snel klaar, is wel een bijzondere situatie + if(activity != EasyLube::NONE) { - uint8_t data = 0; - if(data0) - { - data |= (1 << 0); - } - if(data1) - { - data |= (1 << 1); - } - if(data2) - { - data |= (1 << 2); - } + std::vector<uint8_t> data; - 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) + //Controleren of er een DS1820 aanwezig is. + if(DS1820::unassignedProbe(TEMP_DATA_PIN)) { - 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; + //Zo ja, de temperatuur uitlezen + DS1820 probe(TEMP_DATA_PIN); + probe.convertTemperature(true); + float temp = probe.temperature(); + //Get message with temp + data = createMessage(activity, temp); } 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); - } + logInfo("Geen temperatuur sensor gevonden"); + //Get message without temp + data = createMessage(activity, TEMP_ERROR); } - 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); - } - + //De gegevens versturen + lora.SendData(data, LORA_MAX_ATTEMPTS); } - - logInfo("Nu nergens meer op reageren....."); - - dot->sleep(20, mDot::INTERRUPT, true); - */ + + //32 minuten slapen of wachten op een interrupt + dot->sleep(1920, mDot::RTC_ALARM_OR_INTERRUPT, true); + return 0; }