LoRaCode.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // LoRa encoding and decoding functions
  2. // Copyright (c) 2016 Maarten Westenberg
  3. // Version 1.1.0
  4. // Date: 2016-10-23
  5. //
  6. // All rights reserved. This program and the accompanying materials
  7. // are made available under the terms of the MIT License
  8. // which accompanies this distribution, and is available at
  9. // https://opensource.org/licenses/mit-license.php
  10. //
  11. // Author: Maarten Westenberg
  12. //
  13. // The protocols used in this code:
  14. // 1. LoRA Specification version V1.0 and V1.1 for Gateway-Node communication
  15. //
  16. // 2. Semtech Basic communication protocol between Lora gateway and server version 3.0.0
  17. // https://github.com/Lora-net/packet_forwarder/blob/master/PROTOCOL.TXT
  18. //
  19. // Notes:
  20. // The lCode specification is documented on a sparate page on github.
  21. //
  22. // Todo:
  23. // The luminescense is read as a 16-bit value in the library and converted to
  24. // a float value in order to get proper scaling etc. In the lCode lib it is
  25. // coded as a 2-byte value over the air, which might be incorrect for lux values
  26. // over 650 lux (which IS posible since bright daylight has more than 1000 lux).
  27. // So XXX we have to add another byte to cover values above 65
  28. // ----------------------------------------------------------------------------------------
  29. #ifndef LoRaCode_h
  30. #define LoRaCode_h
  31. // Op Codes
  32. #define O_TEMP 0x01 // Temperature is a one-byte code
  33. #define O_HUMI 0x02 // Humidity is a one-byte code
  34. #define O_AIRP 0x03 // Air pressure is a one-byte code
  35. #define O_GPS 0x04 // Short version: ONLY 3 bytes LAT and 3 bytes LONG
  36. #define O_GPSL 0x05 // Long GPS
  37. #define O_PIR 0x06 // Movement, 1 bit (=1 byte)
  38. #define O_AQ 0x07 // Airquality
  39. #define O_RTC 0x08 // Real Time Clock
  40. #define O_COMPASS 0x09 // Compass
  41. #define O_MB 0x0A // Multi Sensors 433
  42. #define O_MOIST 0x0B // Moisture is one-byte
  43. #define O_LUMI 0x0C // Luminescense u16
  44. #define O_DIST 0x0D // Distance is 2-byte
  45. #define O_GAS 0x0E // GAS
  46. // 0x0F
  47. // 0x10 // 16 values
  48. // 0x11
  49. // ..
  50. // 0x1F
  51. #define O_BATT 0x20 // Internal Battery
  52. #define O_ADC0 0x21 // AD converter on pin 0
  53. #define O_ADC1 0x22
  54. // Reserved for LoRa messages (especially downstream)
  55. #define O_STAT 0x30 // Ask for status message from node
  56. #define O_SF 0x31 // Spreading factor change OFF=0, values 7-12
  57. #define O_TIM 0x32 // Timing of the wait cyclus (20 to 7200 seconds)
  58. #define O_1CH 0x33 // Single channel: Channel Value=0-9, OFF==255
  59. #define O_LOC 0x34 // Ask for the location. Responds with GPS (if available)
  60. // ..
  61. // 0x3F
  62. class LoRaCode
  63. {
  64. public:
  65. int eVal(int opcode, byte *val, byte *msg);
  66. int eTemperature(float val, byte *msg);
  67. int eHumidity(float val, byte *msg);
  68. int eAirpressure(float val, byte *msg);
  69. int eGps(double lat, double lng, byte *msg);
  70. int eGpsL(double lat, double lng, long alt, int sat, byte *msg);
  71. int ePir(int val, byte *msg);
  72. int eAirquality(int pm25, int pm10, byte *msg); // value 0 (good) -1024 (gas)
  73. int eMbuttons(byte val, unsigned long address, unsigned short channel, byte *msg); // concentrator for multi-buttons
  74. int eMoist(int val, byte *msg); // 255 is dry, 0 is wet
  75. int eLuminescense(float val, byte *msg); // val contains light intensity
  76. int eLuminescenseL(float val, byte *msg); // long contains light intensity
  77. int eDistance(int val, byte *msg);
  78. int eGas(int val, byte *msg);
  79. // opcodes 0x0F until 0x1F
  80. int eBattery(float val, byte *msg);
  81. int eAdc0(int val, byte *msg); // Pin A0 has 1024 values, we use 256
  82. int eAdc1(int val, byte *msg); // Pin A1 has 1024 values, we use 256
  83. bool eMsg(byte *msg, int len);
  84. void lPrint(byte *msg, int len);
  85. //Decoding (downstream)
  86. int dLen (byte *msg);
  87. int dMsg (byte *msg, byte *val, byte *mode);
  88. };
  89. extern LoRaCode lcode;
  90. #endif