123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581 |
- #define DEBUG 0
- #if defined(__AVR__)
- #include <avr/pgmspace.h>
- #include <Arduino.h>
- #include <Battery.h>
- #elif defined(ARDUINO_ARCH_ESP8266) | defined(ESP32)
- #include <ESP.h>
- #elif defined(__MKL26Z64__)
- #include <Arduino.h>
- #else
- #error Unknown architecture in aes.cpp
- #endif
- #include "LoRaCode.h"
- int ldebug=DEBUG;
- int LoRaCode::eTemperature(float val, byte *msg) {
- int len=0;
- byte i= (byte) (val+100);
- byte f= (byte) ( ((float)(val - (float)i -100)) * 100);
- msg[len++] = ((byte)O_TEMP << 2) | 0x01;
- msg[len++] = i;
- msg[len++] = f;
- #if DEBUG>0
- if (ldebug >=1) {
- Serial.print(F("lcode:: Add Temperature "));
- Serial.print(i-100);
- Serial.print(".");
- Serial.println(f);
- }
- #endif
- return(len);
- }
- int LoRaCode::eHumidity(float val, byte *msg) {
- int len=0;
- byte i = (byte) ((float)val *2);
- msg[len++] = ((byte)O_HUMI << 2) | 0x00;
- msg[len++] = i;
- #if DEBUG>0
- if (ldebug >=1) {
- Serial.print(F("lcode:: Add Humidity "));
- Serial.println((float)(i/2));
- }
- #endif
- return(len);
- }
- int LoRaCode::eAirpressure(float val, byte *msg) {
- int len=0;
- byte i = (byte) ((float)val -850);
- msg[len++] = ((byte)O_AIRP << 2) | 0x00;
- msg[len++] = i;
- #if DEBUG>0
- if (ldebug >=1) {
- Serial.print(F("lcode:: Add Airpressure "));
- Serial.println((float)(i + 850));
- }
- #endif
- return(len);
- }
- int LoRaCode::eGps(double lat, double lng, byte *msg) {
- #if DEBUG>0
- if (ldebug>=1) {
- Serial.print(F(" LAT: ")); Serial.print(lat,6);
- Serial.print(F(" LNG: ")); Serial.println(lng,6);
- }
- #endif
- int len=0;
- long factor = 1000000;
- msg[len++] = ((byte)O_GPS << 2) | 0x00;
- const long calculatedLat = (long)(lat * factor);
- msg[len++] = (calculatedLat >> (8*3)) & 0xFF;
- msg[len++] = (calculatedLat >> (8*2)) & 0xFF;
- msg[len++] = (calculatedLat >> (8*1)) & 0xFF;
- msg[len++] = (calculatedLat >> (8*0)) & 0xFF;
- const long calculatedLng = (long)(lng * factor);
- msg[len++] = (calculatedLng >> (8*3)) & 0xFF;
- msg[len++] = (calculatedLng >> (8*2)) & 0xFF;
- msg[len++] = (calculatedLng >> (8*1)) & 0xFF;
- msg[len++] = (calculatedLng >> (8*0)) & 0xFF;
- return(len);
- }
- int LoRaCode::eGpsL(double lat, double lng, long alt, int sat,
- byte *msg) {
- if (ldebug>=1) {
- Serial.print(F(" LAT: ")); Serial.print(lat,6);
- Serial.print(F(" LNG: ")); Serial.print(lng,6);
- Serial.print(F(" ALT: ")); Serial.print(alt/100);
- Serial.print(F(" SAT: ")); Serial.println(sat);
- }
- int len=0;
- long factor = 1000000;
- msg[len++] = ((byte)O_GPSL << 2) | 0x00;
- const long calculatedLat = (long)(lat * factor);
- msg[len++] = (calculatedLat >> (8*3)) & 0xFF;
- msg[len++] = (calculatedLat >> (8*2)) & 0xFF;
- msg[len++] = (calculatedLat >> (8*1)) & 0xFF;
- msg[len++] = (calculatedLat >> (8*0)) & 0xFF;
- const long calculatedLng = (long)(lng * factor);
- msg[len++] = (calculatedLng >> (8*3)) & 0xFF;
- msg[len++] = (calculatedLng >> (8*2)) & 0xFF;
- msg[len++] = (calculatedLng >> (8*1)) & 0xFF;
- msg[len++] = (calculatedLng >> (8*0)) & 0xFF;
- const long calculatedAlt = (long)(alt);
- msg[len++] = (calculatedAlt >> (8*3)) & 0xFF;
- msg[len++] = (calculatedAlt >> (8*2)) & 0xFF;
- msg[len++] = (calculatedAlt >> (8*1)) & 0xFF;
- msg[len++] = (calculatedAlt >> (8*0)) & 0xFF;
- msg[len++] = sat & 0xFF;
- return(len);
- }
- int LoRaCode::ePir(int val, byte *msg) {
- int i = (byte) ( val );
- int len=0;
- msg[len++] = (O_PIR << 2) | 0x00;
- msg[len++] = i;
- return(len);
- }
- int LoRaCode::eAirquality(int pm25, int pm10, byte *msg) {
- int len=0;
- uint16_t val = (uint16_t) (pm25);
- msg[len++] = (O_AQ << 2) | 0x03;
- msg[len++] = (val >> 8) & 0xFF;
- msg[len++] = val & 0xFF;
-
- val = (uint16_t) (pm10);
- msg[len++] = (val >> 8) & 0xFF;
- msg[len++] = val & 0xFF;
- #if DEBUG>0
- if (ldebug >=1) {
- Serial.print(F("lcode:: Add Airquality <"));
- Serial.print(len);
- Serial.print(F("> "));
- Serial.println(val);
- }
- #endif
- return(len);
- }
- int LoRaCode::eMbuttons(byte val, unsigned long address, unsigned short channel, byte *msg) {
- int len=0;
- msg[len++] = (O_MB << 2) | 0x00;
-
- msg[len++] = val;
-
- msg[len++] = (address >> (8*3)) & 0xFF;
- msg[len++] = (address >> (8*2)) & 0xFF;
- msg[len++] = (address >> (8*1)) & 0xFF;
- msg[len++] = (address >> (8*0)) & 0xFF;
-
- msg[len++] = (channel >> (8*1)) & 0xFF;
- msg[len++] = (channel >> (8*0)) & 0xFF;
-
- #if DEBUG>0
- if (ldebug >=1) {
- Serial.print(F("lcode:: Add Multi-Button "));
- Serial.println(val);
- }
- #endif
- return(len);
- }
- int LoRaCode::eMoist(int val, byte *msg) {
- int len=0;
- msg[len++] = (O_MOIST << 2) | 0x00;
- msg[len++] = (val / 4 ) & 0xFF;
- #if DEBUG>0
- if (ldebug >=1) {
- Serial.print(F("lcode:: Add Moisture "));
- Serial.println(val);
- }
- #endif
- return(len);
- }
- int LoRaCode::eLuminescense(float val, byte *msg) {
- int len=0;
- uint16_t lux = (uint16_t) (val);
-
- msg[len++] = (O_LUMI << 2) | 0x01;
- msg[len++] = (lux >> (8*1)) & 0xFF;
- msg[len++] = (lux >> (8*0)) & 0xFF;
- #if DEBUG>0
- if (ldebug >=1) {
- Serial.print(F("lcode:: Add Lumi "));
- Serial.println(val);
- }
- #endif
- return(len);
- }
- int LoRaCode::eLuminescenseL(float val, byte *msg) {
- int len=0;
- uint16_t lux = (uint16_t) (val);
-
- uint8_t frac = (uint8_t) ((val-lux) * 100);
- msg[len++] = (O_LUMI << 2) | 0x02;
- msg[len++] = (lux >> (8*1)) & 0xFF;
- msg[len++] = (lux >> (8*0)) & 0xFF;
- msg[len++] = (frac) & 0xFF;
- #if DEBUG>0
- if (ldebug >=1) {
- Serial.print(F("lcode:: Add Lumi L="));
- Serial.print(lux);
- Serial.print('.');
- Serial.println(frac);
- }
- #endif
- return(len);
- }
- int LoRaCode::eDistance(int val, byte *msg) {
- int len=0;
- msg[len++] = (O_DIST << 2) | 0x01;
- msg[len++] = (val >> (8*1)) & 0xFF;
- msg[len++] = (val >> (8*0)) & 0xFF;
- #if DEBUG>0
- if (ldebug >=1) {
- Serial.print(F("lcode:: Add Dist "));
- Serial.println(val);
- }
- #endif
- return(len);
- }
- int LoRaCode::eGas(int val, byte *msg) {
- int len=0;
- msg[len++] = (O_GAS << 2) | 0x01;
- msg[len++] = (val >> 8) & 0xFF;
- msg[len++] = val & 0xFF;
- #if DEBUG>0
- if (ldebug >=1) {
- Serial.print(F("lcode:: Add Gas "));
- Serial.println(val);
- }
- #endif
- return(len);
- }
- int LoRaCode::eBattery(float val, byte *msg) {
- int len=0;
- int i = (byte) ((float)val *20);
- msg[len++] = (O_BATT << 2) | 0x00;
- msg[len++] = i;
- #if DEBUG>0
- if (ldebug >=1) {
- Serial.print(F("lcode:: Add Battery "));
- Serial.println((float)(i/20));
- }
- #endif
- return(len);
- }
- int LoRaCode::eAdc0(int val, byte *msg) {
- int len=0;
- msg[len++] = (O_ADC0 << 2) | 0x00;
- msg[len++] = (val / 4 ) & 0xFF;
- #if DEBUG>0
- if (ldebug >=1) {
- Serial.print(F("lcode:: Add Adc0 "));
- Serial.println(val);
- }
- #endif
- return(len);
- }
- int LoRaCode::eAdc1(int val, byte *msg) {
- int len=0;
- msg[len++] = (O_ADC1 << 2) | 0x00;
- msg[len++] = (val / 4 ) & 0xFF;
- #if DEBUG>0
- if (ldebug >=1) {
- Serial.print(F("lcode:: Add Adc1 "));
- Serial.println(val);
- }
- #endif
- return(len);
- }
- int LoRaCode::eVal (int opcode, byte *val, byte *msg) {
- int len=0;
- switch (opcode) {
- case O_TEMP:
- len += eTemperature((float) *val, msg);
- break;
- case O_HUMI:
- len += eHumidity((float) *val, msg);
- break;
- case O_AIRP:
- len += eAirpressure((float) *val, msg);
- break;
- case O_GPS:
-
- break;
- case O_PIR:
- len += ePir((int) *val, msg);
- break;
- case O_MOIST:
- len += eMoist((int) *val, msg);
- break;
- case O_LUMI:
- len += eLuminescense((int) *val, msg);
- break;
- case O_BATT:
- len += eBattery((float) *val, msg);
- break;
- default:
- #if DEBUG>0
- Serial.println("lCode:: Error opcode not known");
- #endif
- break;
- }
- return(len);
- }
- bool LoRaCode::eMsg (byte * msg, int len) {
- unsigned char par = 0;
- if (len > 64) {
- #if DEBUG>0
- Serial.println("lCodeMsg:: Error, string to long");
- #endif
- return(false);
- }
- #if DEBUG>0
- if (ldebug>=1) {Serial.print(F("LCodeMsg:: <")); Serial.print(len); Serial.print(F("> ")); }
- #endif
-
- msg[0] = ( len << 1 ) | (0x80);
-
-
- for (int i=0; i< len; i++) {
- byte cc = msg[i];
- par ^= cc;
- }
-
-
- unsigned char pp = 8;
- while (pp > 1) {
- par ^= par >> (pp/2);
- pp -= pp/2;
- }
- if (par & 0x01) {
- #if DEBUG>0
- if (ldebug>=1) Serial.print(F(" odd "));
- #endif
- msg[0] |= 0x01;
- }
- else {
- #if DEBUG>0
- if (ldebug>=1) Serial.print(F(" even "));
- #endif
- }
- return(true);
- }
- void LoRaCode::lPrint (byte *msg, int len) {
- Serial.print(F("lCode: "));
- for (int i=0; i< len; i++) {
- if (msg[i]<10) Serial.print('0');
- Serial.print(msg[i],HEX);
- Serial.print(" ");
- }
- Serial.println();
- }
- int LoRaCode::dLen (byte *msg) {
- if ( ! (msg[0] & 0x80 )) return(-1);
- int len = msg[0] & 0x7F;
- return (len >> 1);
- }
- int LoRaCode::dMsg (byte *msg, byte *val, byte *mode) {
- float res;
- byte len = msg[0] & 0x03;
- *mode = (byte) (msg[0] >> 2);
- switch (*mode) {
- case O_TEMP:
- *val = (float) msg[1]- 100 + ( (float) msg[2] / 100);
- return(3);
- break;
- case O_HUMI:
- *val = (float) msg[1] / 2;
- return(2);
- break;
- case O_AIRP:
- *val = 0;
- return(2);
- break;
- case O_GPS:
- *val = 0;
- return(9);
- break;
- case O_GPSL:
- *val = 0;
- return(14);
- break;
- case O_PIR:
- *val = msg[1];
- return(2);
- break;
- case O_AQ:
- *val = 0;
- return(3);
- break;
- case O_BATT:
- *val = (float) msg[1] / 20;
- return(2);
- break;
- case O_STAT:
- *val = 0;
- return(1);
- case O_1CH:
- *val = msg[1];
- return(2);
- break;
- case O_SF:
- *val = msg[1];
- return(2);
- break;
- case O_TIM:
- val[0] = msg[1];
- val[1] = msg[2];
- return(3);
- break;
- default:
- return(0);
- }
- return (0);
- }
- LoRaCode lcode;
|