Easylube
Dependencies: DS1820 libmDot mbed-rtos mbed
main.cpp@3:323401b1b227, 2016-10-14 (annotated)
- Committer:
- TataLora
- Date:
- Fri Oct 14 08:43:33 2016 +0000
- Revision:
- 3:323401b1b227
- Parent:
- 2:26ffaed90e4f
EasyLube
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
TataLora | 0:7e7bd4f937c7 | 1 | #include "mbed.h" |
TataLora | 0:7e7bd4f937c7 | 2 | #include "mDot.h" |
TataLora | 0:7e7bd4f937c7 | 3 | #include "MTSLog.h" |
TataLora | 0:7e7bd4f937c7 | 4 | #include <string> |
TataLora | 0:7e7bd4f937c7 | 5 | #include <vector> |
TataLora | 0:7e7bd4f937c7 | 6 | #include <algorithm> |
TataLora | 2:26ffaed90e4f | 7 | #include "EasyLube.h" |
TataLora | 2:26ffaed90e4f | 8 | #include "Lora.h" |
TataLora | 2:26ffaed90e4f | 9 | #include "DS1820.h" |
TataLora | 2:26ffaed90e4f | 10 | |
TataLora | 2:26ffaed90e4f | 11 | #define TEMP_DATA_PIN PC_1 |
TataLora | 2:26ffaed90e4f | 12 | #define TEMP_ERROR -1000 |
TataLora | 2:26ffaed90e4f | 13 | |
TataLora | 2:26ffaed90e4f | 14 | #define MESSAGE_PREFIX "HTD-LUBRICANT:" |
TataLora | 2:26ffaed90e4f | 15 | |
TataLora | 2:26ffaed90e4f | 16 | #define LORA_NETWORK_NAME "MultiTech" |
TataLora | 2:26ffaed90e4f | 17 | #define LORA_NETWORK_PASS "MultiTech" |
TataLora | 2:26ffaed90e4f | 18 | #define LORA_NETWORK_FREQUENCY_SUB_BAND 1 |
TataLora | 3:323401b1b227 | 19 | #define LORA_MAX_ATTEMPTS 2 |
TataLora | 2:26ffaed90e4f | 20 | |
TataLora | 0:7e7bd4f937c7 | 21 | |
TataLora | 2:26ffaed90e4f | 22 | static std::string message_prefix = MESSAGE_PREFIX; |
TataLora | 0:7e7bd4f937c7 | 23 | |
TataLora | 2:26ffaed90e4f | 24 | EasyLube easyLube; |
TataLora | 2:26ffaed90e4f | 25 | mDot* dot; |
TataLora | 2:26ffaed90e4f | 26 | |
TataLora | 2:26ffaed90e4f | 27 | void init() |
TataLora | 1:0fe7bd229e28 | 28 | { |
TataLora | 2:26ffaed90e4f | 29 | // get a mDot handle |
TataLora | 2:26ffaed90e4f | 30 | dot = mDot::getInstance(); |
TataLora | 1:0fe7bd229e28 | 31 | |
TataLora | 0:7e7bd4f937c7 | 32 | // reset to default config so we know what state we're in |
TataLora | 0:7e7bd4f937c7 | 33 | dot->resetConfig(); |
TataLora | 0:7e7bd4f937c7 | 34 | |
TataLora | 2:26ffaed90e4f | 35 | // Set log level to info, so everything will be sent to the output? |
TataLora | 0:7e7bd4f937c7 | 36 | dot->setLogLevel(mts::MTSLog::INFO_LEVEL); |
TataLora | 0:7e7bd4f937c7 | 37 | |
TataLora | 2:26ffaed90e4f | 38 | // print library version information |
TataLora | 2:26ffaed90e4f | 39 | logInfo("V: %s", dot->getId().c_str()); |
TataLora | 0:7e7bd4f937c7 | 40 | } |
TataLora | 0:7e7bd4f937c7 | 41 | |
TataLora | 2:26ffaed90e4f | 42 | //Maak een bericht van de huidige activiteit met temperatuur |
TataLora | 2:26ffaed90e4f | 43 | std::vector<uint8_t> createMessage(EasyLube::Activity activity, float temp) |
TataLora | 0:7e7bd4f937c7 | 44 | { |
TataLora | 0:7e7bd4f937c7 | 45 | std::vector<uint8_t> data; |
TataLora | 0:7e7bd4f937c7 | 46 | |
TataLora | 2:26ffaed90e4f | 47 | //Eerst de prefix kopieren zodat het bericht aan de andere kant te herkennen is |
TataLora | 2:26ffaed90e4f | 48 | for (std::string::iterator it = message_prefix.begin(); it != message_prefix.end(); it++) |
TataLora | 2:26ffaed90e4f | 49 | data.push_back((uint8_t) *it); |
TataLora | 0:7e7bd4f937c7 | 50 | |
TataLora | 2:26ffaed90e4f | 51 | //Afhankelijk van de activiteit voegen we een code toe |
TataLora | 2:26ffaed90e4f | 52 | switch(activity) |
TataLora | 0:7e7bd4f937c7 | 53 | { |
TataLora | 2:26ffaed90e4f | 54 | case EasyLube::STARTING: |
TataLora | 2:26ffaed90e4f | 55 | data.push_back((uint8_t) 'S'); data.push_back((uint8_t) 'T'); |
TataLora | 2:26ffaed90e4f | 56 | break; |
TataLora | 2:26ffaed90e4f | 57 | case EasyLube::DOSAGE: |
TataLora | 2:26ffaed90e4f | 58 | data.push_back((uint8_t) 'D'); data.push_back((uint8_t) 'O'); |
TataLora | 2:26ffaed90e4f | 59 | break; |
TataLora | 2:26ffaed90e4f | 60 | case EasyLube::ERROR: |
TataLora | 2:26ffaed90e4f | 61 | data.push_back((uint8_t) 'E'); data.push_back((uint8_t) 'R'); |
TataLora | 2:26ffaed90e4f | 62 | break; |
TataLora | 2:26ffaed90e4f | 63 | case EasyLube::ALIVE: |
TataLora | 2:26ffaed90e4f | 64 | data.push_back((uint8_t) 'A'); data.push_back((uint8_t) 'L'); |
TataLora | 2:26ffaed90e4f | 65 | break; |
TataLora | 2:26ffaed90e4f | 66 | case EasyLube::NO_SIGNAL: |
TataLora | 3:323401b1b227 | 67 | //01-09-2016 21:32 toegevoegd, werd eerst PU gestuurd |
TataLora | 3:323401b1b227 | 68 | data.push_back((uint8_t) 'N'); data.push_back((uint8_t) 'S'); |
TataLora | 3:323401b1b227 | 69 | break; |
TataLora | 2:26ffaed90e4f | 70 | case EasyLube::POWER_UP: |
TataLora | 2:26ffaed90e4f | 71 | data.push_back((uint8_t) 'P'); data.push_back((uint8_t) 'U'); |
TataLora | 2:26ffaed90e4f | 72 | break; |
TataLora | 2:26ffaed90e4f | 73 | default: |
TataLora | 2:26ffaed90e4f | 74 | data.push_back((uint8_t) 'O'); data.push_back((uint8_t) 'N'); |
TataLora | 0:7e7bd4f937c7 | 75 | } |
TataLora | 0:7e7bd4f937c7 | 76 | |
TataLora | 2:26ffaed90e4f | 77 | //Als de temperatuur anders is dan de TEMP_ERROR, dan voegen we deze waarde ook toe aan het resultaat |
TataLora | 2:26ffaed90e4f | 78 | if(temp > TEMP_ERROR) |
TataLora | 0:7e7bd4f937c7 | 79 | { |
TataLora | 2:26ffaed90e4f | 80 | //Format is TEMP=123.4, met een comma vooraf om onderscheid te maken met de code |
TataLora | 2:26ffaed90e4f | 81 | char buffer[50]; |
TataLora | 2:26ffaed90e4f | 82 | uint8_t n=sprintf (buffer, ",TEMP=%3.1f", temp); |
TataLora | 2:26ffaed90e4f | 83 | logInfo("It is %3.1foC", temp); |
TataLora | 2:26ffaed90e4f | 84 | for (uint8_t i = 0; i < n; i++) |
TataLora | 2:26ffaed90e4f | 85 | { |
TataLora | 2:26ffaed90e4f | 86 | data.push_back((uint8_t) buffer[i]); |
TataLora | 0:7e7bd4f937c7 | 87 | } |
TataLora | 0:7e7bd4f937c7 | 88 | } |
TataLora | 2:26ffaed90e4f | 89 | return data; |
TataLora | 0:7e7bd4f937c7 | 90 | } |
TataLora | 0:7e7bd4f937c7 | 91 | |
TataLora | 0:7e7bd4f937c7 | 92 | int main() { |
TataLora | 2:26ffaed90e4f | 93 | init(); |
TataLora | 2:26ffaed90e4f | 94 | |
TataLora | 2:26ffaed90e4f | 95 | //De lora instantie kan pas aangemaakt worden er een instantie is van de mDot welke in de init wordt gemaakt |
TataLora | 2:26ffaed90e4f | 96 | Lora lora(dot, LORA_NETWORK_NAME, LORA_NETWORK_PASS, LORA_NETWORK_FREQUENCY_SUB_BAND); |
TataLora | 0:7e7bd4f937c7 | 97 | |
TataLora | 2:26ffaed90e4f | 98 | //De huidige activiteit opvragen |
TataLora | 2:26ffaed90e4f | 99 | EasyLube::Activity activity = easyLube.getActivity(); |
TataLora | 0:7e7bd4f937c7 | 100 | |
TataLora | 2:26ffaed90e4f | 101 | //Als er geen activiteit is dan zijn we snel klaar, is wel een bijzondere situatie |
TataLora | 2:26ffaed90e4f | 102 | if(activity != EasyLube::NONE) |
TataLora | 0:7e7bd4f937c7 | 103 | { |
TataLora | 2:26ffaed90e4f | 104 | std::vector<uint8_t> data; |
TataLora | 0:7e7bd4f937c7 | 105 | |
TataLora | 2:26ffaed90e4f | 106 | //Controleren of er een DS1820 aanwezig is. |
TataLora | 2:26ffaed90e4f | 107 | if(DS1820::unassignedProbe(TEMP_DATA_PIN)) |
TataLora | 0:7e7bd4f937c7 | 108 | { |
TataLora | 2:26ffaed90e4f | 109 | //Zo ja, de temperatuur uitlezen |
TataLora | 2:26ffaed90e4f | 110 | DS1820 probe(TEMP_DATA_PIN); |
TataLora | 2:26ffaed90e4f | 111 | probe.convertTemperature(true); |
TataLora | 2:26ffaed90e4f | 112 | float temp = probe.temperature(); |
TataLora | 3:323401b1b227 | 113 | if(temp != temp) |
TataLora | 3:323401b1b227 | 114 | { |
TataLora | 3:323401b1b227 | 115 | temp = TEMP_ERROR; |
TataLora | 3:323401b1b227 | 116 | } |
TataLora | 2:26ffaed90e4f | 117 | //Get message with temp |
TataLora | 2:26ffaed90e4f | 118 | data = createMessage(activity, temp); |
TataLora | 0:7e7bd4f937c7 | 119 | } |
TataLora | 0:7e7bd4f937c7 | 120 | else |
TataLora | 0:7e7bd4f937c7 | 121 | { |
TataLora | 2:26ffaed90e4f | 122 | logInfo("Geen temperatuur sensor gevonden"); |
TataLora | 2:26ffaed90e4f | 123 | //Get message without temp |
TataLora | 2:26ffaed90e4f | 124 | data = createMessage(activity, TEMP_ERROR); |
TataLora | 0:7e7bd4f937c7 | 125 | } |
TataLora | 0:7e7bd4f937c7 | 126 | |
TataLora | 2:26ffaed90e4f | 127 | //De gegevens versturen |
TataLora | 2:26ffaed90e4f | 128 | lora.SendData(data, LORA_MAX_ATTEMPTS); |
TataLora | 0:7e7bd4f937c7 | 129 | } |
TataLora | 2:26ffaed90e4f | 130 | |
TataLora | 2:26ffaed90e4f | 131 | //32 minuten slapen of wachten op een interrupt |
TataLora | 2:26ffaed90e4f | 132 | dot->sleep(1920, mDot::RTC_ALARM_OR_INTERRUPT, true); |
TataLora | 2:26ffaed90e4f | 133 | |
TataLora | 0:7e7bd4f937c7 | 134 | return 0; |
TataLora | 0:7e7bd4f937c7 | 135 | } |