diff --git a/FDRS_Gateway2000/README.md b/FDRS_Gateway2000/README.md index 9d8dea2..f6a93c3 100644 --- a/FDRS_Gateway2000/README.md +++ b/FDRS_Gateway2000/README.md @@ -1,26 +1,47 @@ # Gateway 2.000 +The FDRS Gateway listens for packets over ESP-NOW, UART, LoRa, and/or MQTT, then retransmits the packets over these interfaces using rules defined in the "Actions" section of the configuration file. -This is the FDRS Multiprotocol Gateway sketch. The device listens for packets over ESP-NOW, UART, LoRa, and/or MQTT, then retransmits the packets over these interfaces using rules defined in the "Actions" section of the configuration file. +## Actions +Actions define how the gateway reacts to a packet received via each data source. An action may consist of one or multiple commands separated by (and terminated with) semicolons. -The most commonly used configuration tells the device to take any ESP-NOW packet it receives and output the data over the serial port (UART): -``` -#define UNIT_MAC 0x00 -#define ESPNOWG_ACT sendSerial(); -``` -The companion for this device, connected via serial, takes any data it receives from the serial port and sends it via MQTT: -``` -#define USE_WIFI -#define SERIAL_ACT sendMQTT(); -``` -Splitting the gateway into two devices allows you to use ESP-NOW and WiFi simultaneously without channel conflicts. You can also connect the first device to a computer with a USB-UART adapter and get the data that way, eliminating WiFi altogether. +The following commands re-send data instantaneously: ```sendESPNOW(MAC)```, ```sendSerial()```, and ```sendMQTT()```. -If you have sensors that are out of range of your first gateway, you can use a gateway as a repeater. First set the UNIT_MAC to 0x01, then send general ESP-NOW traffic to the address of the first gateway: +The following commands send data to buffers to be released at an interval: ```bufferLoRa(interface)```, ```bufferESPNOW(interface)```, ```bufferSerial()```, and ```bufferMQTT()```. + +In the following example, the gateway is set to take any ESP-NOW packet it receives and send it first over the serial port, then re-transmit via ESP-NOW it to another gateway with the address 0x01: ``` -#define UNIT_MAC 0x01 -#define ESPNOWG_ACT sendESPNOW(0x00); +#define ESPNOWG_ACT sendSerial(); sendESPNOW(0x01); ``` -### LoRa -You can also use LoRa to expand the distances between hops. While ESP-NOW is quick enough to handle a lot of traffic in real-time, LoRa is much slower. For this reason, you must send LoRa data to a buffer and transmit it at standard intervals. + +## Options +### ```#define UNIT_MAC (0xNN)``` +The UNIT_MAC is the ESP-NOW and LoRa address of the gateway. This is the address that nodes or other gateways will use to pass data to this device. +### ```#define DEBUG``` +This definition enables debug messages to be sent over the serial port. If disabled, the USB serial port is still used to echo data being sent via the sendSerial() command. +### ```#define RXD2 (pin)``` and ```TXD2 (pin)``` +These are the pins for inter-device serial communication. The single ESP8266 serial interface is not configurable, and thus these options only apply to ESP32 boards. +### ```#define USE_LORA``` +Enables LoRa. Make sure that you set the LoRa module configuration parameters in the lines below. + +BAND and SF (spreading factor) can also be configured in 'fdrs_globals.h' if enabled. +### ```#define USE_WIFI``` +Enables WiFi. Used only on the MQTT gateway. + +SSID, password, and MQTT credentials are also configurable in 'fdrs_globals.h'. +### ```#define USE_LED``` +This option initializes FastLED! I haven't developed this very much, perhaps you have ideas? + +## Peers +### Routing +In addition to reacting to packets from general (unknown) ESP-NOW and LoRa devices, the gateway can also listen for traffic from a specific peer's device address (MAC) and react differently than it would to general traffic. This can be used to 'propel' packets upstream or downstream and allows the user to define different paths for data originating from either direction. The user can define up to two peer addresses each for the ESP-NOW and LoRa interfaces (ESPNOW1 & ESPNOW2 and LORA1 & LORA2). +### Buffers +Each peer also has a send buffer associated with it. Buffers are enabled by uncommenting their corresponding DELAY macro (ex: ```#define LORAG_DELAY 1000```). When enabled, the gateway will automatically send the buffer contents at the interval specified. + +While ESP-NOW is quick enough to handle a lot of traffic in real-time, LoRa is much slower. For this reason, you must send LoRa data to a buffer. Since buffers are mandatory, a LoRa repeater always needs to be configured using a LoRa peer. + +Buffers can hold a maximum of 256 DataReadings. + + @@ -31,4 +52,3 @@ You can also use LoRa to expand the distances between hops. While ESP-NOW is qui ![Basic LoRa](/FDRS_Gateway2000/Basic_LoRa_Setup.png) ![Advanced LoRa](/FDRS_Gateway2000/Advanced_Setup_LoRa.png) - diff --git a/README.md b/README.md index 64e5e53..fbdb99f 100644 --- a/README.md +++ b/README.md @@ -3,28 +3,21 @@ The Farm Data Relay System is an easy way to collect data from remote sensors without relying on WiFi. It is based around the ESP-NOW protocol, which is readily available on ESP32 and ESP8266 microcontroller boards. The system can be used to collect and transmit sensor data in situations where it would be too difficult or energy-consuming to provide full WiFi coverage. -Using an assigned MAC address scheme allows for the whole system to be configured by setting just a handful of values in code. Every wireless gateway is assigned a single-byte identifier, known as the UNIT_MAC. This, along with a set, 5-byte prefix is assigned to the MAC address of the ESP's radio at boot. - -Gateways can be configured to send an ESP-NOW transmission to the serial port using JSON, another ESP-NOW gateway, or broadcast it via LoRa PHY. They can also be configured to transmit incoming serial packets over MQTT, ESP-NOW, or LoRa PHY. +Using an assigned MAC address scheme allows for the whole system to be configured by setting just a handful of values in code. Every wireless gateway is assigned a single-byte identifier, known as the UNIT_MAC. This, along with a set, 5-byte prefix is assigned to the MAC address of the ESP at boot. ## Getting Started -To use FDRS with Node-Red and MQTT you'll need two ESP devices (gateways) that are connected via UART, plus additional ESP or LoRa devices with sensors connected. -### Sensors -The [Sensor2000](https://github.com/timmbogner/Farm-Data-Relay-System/tree/main/FDRS_Sensor2000) sketch is an example of how a sensor is configured in FDRS. The configuration parameters and functional code are located in the 'fdrs_sensor.h' file, so the user is free to use the main sketch for individual sensor code. +### [Sensors](https://github.com/timmbogner/Farm-Data-Relay-System/tree/main/FDRS_Sensor2000) +Sensors gather data and send it to a gateway via ESP-NOW or LoRa. -### Gateways -Gateways are programmed using the instructions [found with the Gateway2000 sketch](https://github.com/timmbogner/Farm-Data-Relay-System/tree/main/FDRS_Gateway2000). +### [Gateways](https://github.com/timmbogner/Farm-Data-Relay-System/tree/main/FDRS_Gateway2000) +Gateways are programmed to receive data over a variety of protocols and pass it on to other devices using the same or different protocols. ### Front-end The Node-RED front-end can be set up with these nodes to format and send the data to InfluxDB: ``` [{"id":"66d36c0f.cedf94","type":"influxdb out","z":"d7346a99.716ef8","influxdb":"905dd357.34717","name":"","measurement":"DataReading","precision":"","retentionPolicy":"","database":"database","precisionV18FluxV20":"ms","retentionPolicyV18Flux":"","org":"the_organization","bucket":"bkt","x":760,"y":240,"wires":[]},{"id":"93e9822a.3ad59","type":"mqtt in","z":"d7346a99.716ef8","name":"","topic":"esp/fdrs","qos":"2","datatype":"auto","broker":"c513f7e9.760658","nl":false,"rap":true,"rh":0,"x":170,"y":220,"wires":[["d377f9e0.faef98"]]},{"id":"d377f9e0.faef98","type":"json","z":"d7346a99.716ef8","name":"","property":"payload","action":"obj","pretty":false,"x":290,"y":220,"wires":[["ca383562.4014e8"]]},{"id":"ca383562.4014e8","type":"split","z":"d7346a99.716ef8","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":410,"y":220,"wires":[["6eaba8dd.429e38"]]},{"id":"6eaba8dd.429e38","type":"function","z":"d7346a99.716ef8","name":"Fields","func":"msg.payload = [{\n data: msg.payload.data\n},{\n id: msg.payload.id,\n type: msg.payload.type\n}]\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":530,"y":220,"wires":[["296d0f4b.37a46","66d36c0f.cedf94"]]},{"id":"296d0f4b.37a46","type":"debug","z":"d7346a99.716ef8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":670,"y":200,"wires":[]},{"id":"905dd357.34717","type":"influxdb","hostname":"127.0.0.1","port":"8086","protocol":"http","database":"database","name":"","usetls":false,"tls":"d50d0c9f.31e858","influxdbVersion":"2.0","url":"http://localhost:8086","rejectUnauthorized":true},{"id":"c513f7e9.760658","type":"mqtt-broker","name":"","broker":"localhost","port":"1883","clientid":"","usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":""},{"id":"d50d0c9f.31e858","type":"tls-config","name":"","cert":"","key":"","ca":"","certname":"","keyname":"","caname":"","servername":"","verifyservercert":false}] ``` -The following nodes do the same as above, but I added the ability to add an alias to each sensor ID via the dashboard using globals. It could use improvement, but might be helpful to some: - ``` -[{"id":"66d36c0f.cedf94","type":"influxdb out","z":"d7346a99.716ef8","influxdb":"905dd357.34717","name":"","measurement":"DataReading","precision":"","retentionPolicy":"","database":"database","precisionV18FluxV20":"ms","retentionPolicyV18Flux":"","org":"the_organization","bucket":"bkt","x":1160,"y":300,"wires":[]},{"id":"93e9822a.3ad59","type":"mqtt in","z":"d7346a99.716ef8","name":"","topic":"esp/fdrs","qos":"2","datatype":"auto","broker":"c513f7e9.760658","nl":false,"rap":true,"rh":0,"x":270,"y":300,"wires":[["d377f9e0.faef98"]]},{"id":"d377f9e0.faef98","type":"json","z":"d7346a99.716ef8","name":"","property":"payload","action":"obj","pretty":false,"x":390,"y":300,"wires":[["ca383562.4014e8"]]},{"id":"ca383562.4014e8","type":"split","z":"d7346a99.716ef8","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":530,"y":300,"wires":[["f9edcceb.225e8"]]},{"id":"6eaba8dd.429e38","type":"function","z":"d7346a99.716ef8","name":"Fields","func":"msg.payload = [{\n data: msg.payload.data\n},{\n id: msg.payload.id,\n type: msg.payload.type,\n alias: msg.payload.alias,\n model: msg.payload.model,\n indoor: msg.payload.indoor,\n outdoor: msg.payload.outdoor\n}]\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":810,"y":300,"wires":[["66d36c0f.cedf94","296d0f4b.37a46"]]},{"id":"296d0f4b.37a46","type":"debug","z":"d7346a99.716ef8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1030,"y":240,"wires":[]},{"id":"edec396f.607608","type":"ui_form","z":"d7346a99.716ef8","name":"","label":"Enter ID and Alias","group":"361e2c11.6c24e4","order":0,"width":0,"height":0,"options":[{"label":"Device ID","value":"id","type":"number","required":true,"rows":null},{"label":"Desired Alias","value":"alias","type":"text","required":true,"rows":null},{"label":"Sensor Type","value":"model","type":"text","required":false,"rows":null},{"label":"Indoor","value":"indoor","type":"checkbox","required":false,"rows":null},{"label":"Outdoor","value":"outdoor","type":"checkbox","required":false,"rows":null}],"formValue":{"id":"","alias":"","model":"","indoor":false,"outdoor":false},"payload":"","submit":"submit","cancel":"cancel","topic":"topic","topicType":"msg","splitLayout":"","x":310,"y":380,"wires":[["24844b21.4e0f24"]]},{"id":"391507dd.567968","type":"debug","z":"d7346a99.716ef8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":810,"y":240,"wires":[]},{"id":"f9edcceb.225e8","type":"function","z":"d7346a99.716ef8","name":"","func":"var atts = global.get(\"attribute_list[\" + msg.payload.id + \"]\" );\nif(atts !== undefined){\nmsg.payload.alias = atts.alias;\nmsg.payload.model = atts.model;\nmsg.payload.indoor = atts.indoor;\nmsg.payload.outdoor = atts.outdoor;\n}\n\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":660,"y":300,"wires":[["391507dd.567968","6eaba8dd.429e38"]]},{"id":"c27b4f12.71867","type":"debug","z":"d7346a99.716ef8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":670,"y":380,"wires":[]},{"id":"24844b21.4e0f24","type":"change","z":"d7346a99.716ef8","name":"","rules":[{"t":"set","p":"attribute_list[msg.payload.id].alias","pt":"global","to":"payload.alias","tot":"msg"},{"t":"set","p":"attribute_list[msg.payload.id].model","pt":"global","to":"payload.model","tot":"msg"},{"t":"set","p":"attribute_list[msg.payload.id].indoor","pt":"global","to":"payload.indoor","tot":"msg"},{"t":"set","p":"attribute_list[msg.payload.id].outdoor","pt":"global","to":"payload.outdoor","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":520,"y":380,"wires":[["c27b4f12.71867"]]},{"id":"905dd357.34717","type":"influxdb","hostname":"127.0.0.1","port":"8086","protocol":"http","database":"database","name":"","usetls":false,"tls":"d50d0c9f.31e858","influxdbVersion":"2.0","url":"http://localhost:8086","rejectUnauthorized":true},{"id":"c513f7e9.760658","type":"mqtt-broker","name":"","broker":"localhost","port":"1883","clientid":"","usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":""},{"id":"361e2c11.6c24e4","type":"ui_group","name":"Configuration","tab":"8a89d1e.eb12c3","order":1,"disp":true,"width":"10","collapse":false},{"id":"d50d0c9f.31e858","type":"tls-config","name":"","cert":"","key":"","ca":"","certname":"","keyname":"","caname":"","servername":"","verifyservercert":false},{"id":"8a89d1e.eb12c3","type":"ui_tab","name":"Farm Data Relay System","icon":"dashboard","order":1,"disabled":false,"hidden":false}] - ``` ![Basic](/FDRS_Gateway2000/Basic_Setup.png) ![Advanced](/FDRS_Gateway2000/Advanced_Setup.png)