ESP-sc-gway.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. // 1-channel LoRa Gateway for ESP8266
  2. // Copyright (c) 2016, 2017, 2018, 2019 Maarten Westenberg version for ESP8266
  3. // Version 6.1.0 E EU868
  4. // Date: 2019-10-20
  5. //
  6. // Based on work done by Thomas Telkamp for Raspberry PI 1ch gateway and many others.
  7. // Contibutions of Dorijan Morelj and Andreas Spies for OLED support.
  8. //
  9. // All rights reserved. This program and the accompanying materials
  10. // are made available under the terms of the MIT License
  11. // which accompanies this distribution, and is available at
  12. // https://opensource.org/licenses/mit-license.php
  13. //
  14. // NO WARRANTY OF ANY KIND IS PROVIDED
  15. //
  16. // Author: Maarten Westenberg (mw12554@hotmail.com)
  17. //
  18. // This file contains a number of compile-time settings that can be set on (=1) or off (=0)
  19. // The disadvantage of compile time is minor compared to the memory gain of not having
  20. // too much code compiled and loaded on your ESP8266.
  21. //
  22. // ----------------------------------------------------------------------------------------
  23. // NOTE: Compile with ESP32 setting and board "ESP32 Dev Module" or "Heltec WiFi Lora 32"
  24. #define VERSION "V.6.1.0.E.EU868; 191020a"
  25. // This value of DEBUG determines whether some parts of code get compiled.
  26. // Also this is the initial value of debug parameter.
  27. // The value can be changed using the admin webserver
  28. // For operational use, set initial DEBUG vaulue 0
  29. #define DEBUG 1
  30. // Debug message will be put on Serial is this one is set.
  31. // If set to 0, not USB Serial prints are done
  32. // Set to 1 it will prinr all user level messages (with correct debug set)
  33. // If set to 2 it will also print interrupt messages (not recommended)
  34. #define DUSB 1
  35. // Define whether we should do a formatting of SPIFFS when starting the gateway
  36. // This is usually a good idea if the webserver is interrupted halfway a writing
  37. // operation. Also to be used when software is upgraded
  38. // Normally, value 0 is a good default.
  39. #define _SPIFF_FORMAT 0
  40. // Define the frequency band the gateway will listen on. Valid options are
  41. // EU863_870 (Europe), US902_928 (North America) & AU925_928 (Australia), CN470_510 (China).
  42. // See https://www.thethingsnetwork.org/docs/lorawan/frequency-plans.html
  43. #define EU863_870 1
  44. // Define whether to use the old Semtech gateway API, which is still supported by TTN,
  45. // but is more lightweight than the new TTN tcp based protocol.
  46. // NOTE: Only one of the two should be defined!
  47. //
  48. #define _UDPROUTER 1
  49. //#define _TTNROUTER 1
  50. // The spreading factor is the most important parameter to set for a single channel
  51. // gateway. It specifies the speed/datarate in which the gateway and node communicate.
  52. // As the name says, in principle the single channel gateway listens to one channel/frequency
  53. // and to one spreading factor only.
  54. // This parameters contains the default value of SF, the actual version can be set with
  55. // the webserver and it will be stored in SPIFF
  56. // NOTE: The frequency is set in the loraModem.h file and is default 868.100000 MHz.
  57. #define _SPREADING SF9
  58. // Channel Activity Detection
  59. // This function will scan for valid LoRa headers and determine the Spreading
  60. // factor accordingly. If set to 1 we will use this function which means the
  61. // 1-channel gateway will become even more versatile. If set to 0 we will use the
  62. // continuous listen mode.
  63. // Using this function means that we HAVE to use more dio pins on the RFM95/sx1276
  64. // device and also connect enable dio1 to detect this state.
  65. #define _CAD 1
  66. // Definitions for the admin webserver.
  67. // A_SERVER determines whether or not the admin webpage is included in the sketch.
  68. // Normally, leave it in!
  69. #define A_SERVER 1 // Define local WebServer only if this define is set
  70. #define A_REFRESH 1 // Allow the webserver refresh or not?
  71. #define A_SERVERPORT 80 // Local webserver port (normally 80)
  72. #define A_MAXBUFSIZE 192 // Must be larger than 128, but small enough to work
  73. // Definitions for over the air updates. At the moment we support OTA with IDE
  74. // Make sure that tou have installed Python version 2.7 and have Bonjour in your network.
  75. // Bonjour is included in iTunes (which is free) and OTA is recommended to install
  76. // the firmware on your router witout having to be really close to the gateway and
  77. // connect with USB.
  78. #define A_OTA 1
  79. // We support a few pin-out configurations out-of-the-box: HALLARD, COMPRESULT and TTGO ESP32.
  80. // If you use one of these two, just set the parameter to the right value.
  81. // If your pin definitions are different, update the loraModem.h file to reflect these settings.
  82. // 1: HALLARD
  83. // 2: COMRESULT pin out
  84. // 3: ESP32 Wemos pin out
  85. // 4: ESP32 TTGO pinning (should work for 433 and OLED too).
  86. // 5: ESP32 TTGO EU433 MHz with OLED
  87. // 6: Other, define your own in loraModem.h
  88. #define _PIN_OUT 4
  89. // Gather statistics on sensor and Wifi status
  90. // 0= No statistics
  91. // 1= Keep track of messages statistics, number determined by MAX_STAT
  92. // 2= Option 1 + Keep track of messages received PER each SF (default)
  93. // 3= See Option 2, but with extra channel info (Do not use when no Hopping is selected)
  94. #define STATISTICS 3
  95. // Maximum number of statistics records gathered. 20 is a good maximum (memory intensive)
  96. // For ESP32 maybe 30 could be used as well
  97. #define MAX_STAT 20
  98. // Single channel gateways if they behave strict should only use one frequency
  99. // channel and one spreading factor. However, the TTN backend replies on RX2
  100. // timeslot for spreading factors SF9-SF12.
  101. // Also, the server will respond with SF12 in the RX2 timeslot.
  102. // If the 1ch gateway is working in and for nodes that ONLY transmit and receive on the set
  103. // and agreed frequency and spreading factor. make sure to set STRICT to 1.
  104. // In this case, the frequency and spreading factor for downlink messages is adapted by this
  105. // gateway
  106. // NOTE: If your node has only one frequency enabled and one SF, you must set this to 1
  107. // in order to receive downlink messages
  108. // NOTE: In all other cases, value 0 works for most gateways with CAD enabled
  109. #define _STRICT_1CH 1
  110. // Allows configuration through WifiManager AP setup. Must be 0 or 1
  111. #define WIFIMANAGER 0
  112. // Define the name of the accesspoint if the gateway is in accesspoint mode (is
  113. // getting WiFi SSID and password using WiFiManager)
  114. #define AP_NAME "ESP8266-Gway-Things4U"
  115. #define AP_PASSWD "ttnAutoPw"
  116. // This section defines whether we use the gateway as a repeater
  117. // For his, we use another output channle as the channel (default==0) we are
  118. // receiving the messages on.
  119. #define _REPEATER 0
  120. // Will we use Mutex or not?
  121. // +SPI is input for SPI, SPO is output for SPI
  122. #define MUTEX 0
  123. // Define if OLED Display is connected to I2C bus. Note that defining an OLED display does not
  124. // impact performance very much, certainly if no OLED is connected. Wrong OLED will not show
  125. // sensible results on display
  126. // OLED==0; No OLED display connected
  127. // OLED==1; 0.9 Oled Screen based on SSD1306
  128. // OLED==2; 1"3 Oled screens for Wemos, 128x64 SH1106
  129. #define OLED 1
  130. // Define whether we want to manage the gateway over UDP (next to management
  131. // thru webinterface).
  132. // This will allow us to send messages over the UDP connection to manage the gateway
  133. // and its parameters. Sometimes the gateway is not accesible from remote,
  134. // in this case we would allow it to use the SERVER UDP connection to receive
  135. // messages as well.
  136. // NOTE: Be aware that these messages are NOT LoRa and NOT LoRa Gateway spec compliant.
  137. // However that should not interfere with regular gateway operation but instead offer
  138. // functions to set/reset certain parameters from remote.
  139. #define GATEWAYMGT 0
  140. // Do extensive loggin
  141. // Use the ESP8266 SPIFS filesystem to do extensive logging.
  142. // We must take care that the filesystem never(!) is full, and for that purpose we
  143. // rather have new records/line of statistics than very old.
  144. // Of course we must store enough records to make the filesystem work
  145. #define STAT_LOG 1
  146. // Name of he configfile in SPIFFs filesystem
  147. // In this file we store the configuration and other relevant info that should
  148. // survive a reboot of the gateway
  149. #define CONFIGFILE "/gwayConfig.txt"
  150. // Set the Server Settings (IMPORTANT)
  151. #define _LOCUDPPORT 1700 // UDP port of gateway! Often 1700 or 1701 is used for upstream comms
  152. // Timing
  153. #define _MSG_INTERVAL 15 // Reset timer in seconds
  154. #define _PULL_INTERVAL 55 // PULL_DATA messages to server to get downstream in milliseconds
  155. #define _STAT_INTERVAL 120 // Send a 'stat' message to server
  156. #define _NTP_INTERVAL 3600 // How often do we want time NTP synchronization
  157. #define _WWW_INTERVAL 60 // Number of seconds before we refresh the WWW page
  158. // MQTT definitions, these settings should be standard for TTN
  159. // and need not changing
  160. #define _TTNSERVER "router.eu.thethings.network"
  161. #define _TTNPORT 1700 // Standard port for TTN
  162. // If you have a second back-end server defined such as Semtech or loriot.io
  163. // your can define _THINGPORT and _THINGSERVER with your own value.
  164. // If not, make sure that you do not define these, which will save CPU time
  165. // Port is UDP port in this program
  166. //
  167. // Default for testing: Switched off
  168. #define _THINGSERVER "westenberg.org" // Server URL of the LoRa-udp.js handler
  169. //#define _THINGPORT 57084 // dash.westenberg.org:8057
  170. //#define _THINGSERVER "capgemini.thethings.industries" // Server URL of the LoRa-udp.js handler
  171. #define _THINGPORT 1700 // Port 1700 is old compatibility
  172. // This defines whether or not we would use the gateway as
  173. // as sort of backend system which decodes
  174. // 1: _LOCALSERVER is used
  175. // 0: Do not use _LOCALSERVER
  176. #define _LOCALSERVER 1 // See server definitions for decodes
  177. // Gateway Ident definitions
  178. #define _DESCRIPTION "ESP Gateway" // Name of the gateway
  179. #define _EMAIL "mw12554@hotmail.com" // Owner
  180. #define _PLATFORM "ESP8266"
  181. #define _LAT 52.237367
  182. #define _LON 5.978654
  183. #define _ALT 14 // Altitude
  184. // ntp
  185. // Please add daylight saving time to NTP_TIMEZONES when desired
  186. #define NTP_TIMESERVER "nl.pool.ntp.org" // Country and region specific
  187. #define NTP_TIMEZONES 2 // How far is our Timezone from UTC (excl daylight saving/summer time)
  188. #define SECS_IN_HOUR 3600
  189. #define NTP_INTR 0 // Do NTP processing with interrupts or in loop();
  190. // lora sensor code definitions
  191. // Defines whether the gateway will also report sensor/status value on MQTT
  192. // after all, a gateway can be a node to the system as well. Some sensors like GPS can be
  193. // sent to the backend as a parameter, some (like humidity for example) can only be sent
  194. // as a regular sensor value.
  195. // Set its LoRa address and key below in this file, See spec. para 4.3.2
  196. #define GATEWAYNODE 0
  197. #define _CHECK_MIC 0
  198. #if GATEWAYNODE==1
  199. #define _DEVADDR { 0x22, 0x22, 0x22, 0x22 }
  200. #define _APPSKEY { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
  201. #define _NWKSKEY { 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11 }
  202. #define _SENSOR_INTERVAL 300
  203. // For ESP32 based T_BEAM/TTGO boards these two are normally included
  204. // If included make value 1, else if not, make them 0
  205. #define _GPS 1
  206. #define _BATTERY 1
  207. #endif
  208. // Define the correct radio type that you are using
  209. #define CFG_sx1276_radio
  210. //#define CFG_sx1272_radio
  211. // Serial Port speed
  212. #define _BAUDRATE 115200 // Works for debug messages to serial momitor
  213. // We can put the gateway in such a mode that it will (only) recognize
  214. // nodes that are put in a list of trusted nodes
  215. // Values:
  216. // 0: Do not use names for trusted Nodes
  217. // 1: Use the nodes as a translation table for hex codes to names (in TLN)
  218. // 2: Same as 1, but is nodes NOT in the nodes list below they are NOT
  219. // forwarded or counted! (not yet fully implemented)
  220. #define _TRUSTED_NODES 1
  221. #define _TRUSTED_DECODE 1
  222. // Wifi definitions
  223. // WPA is an array with SSID and password records. Set WPA size to number of entries in array
  224. // When using the WiFiManager, we will overwrite the first entry with the
  225. // accesspoint we last connected to with WifiManager
  226. // NOTE: Structure needs at least one (empty) entry.
  227. // So WPASIZE must be >= 1
  228. struct wpas {
  229. char login[32]; // Maximum Buffer Size (and allocated memory)
  230. char passw[64];
  231. };
  232. // Please fill in at least ONE SSID and password from your own WiFI network
  233. // below. This is needed to get the gateway working
  234. // Note: DO NOT use the first and the last line of the stucture, these should be empty strings and
  235. // the first line in te struct is reserved for WifiManager.
  236. //
  237. wpas wpa[] = {
  238. { "" , "" }, // Reserved for WiFi Manager
  239. { "your SSID 1", "your PASS 1" },
  240. { "your SSID 2", "your PASS 2" },
  241. { "your SSID n", "your PASS n"}
  242. };
  243. // For asserting and testing the following defines are used.
  244. //
  245. #if !defined(CFG_noassert)
  246. #define ASSERT(cond) if(!(cond)) gway_failed(__FILE__, __LINE__)
  247. #else
  248. #define ASSERT(cond) /**/
  249. #endif