MySensors Library & Examples  2.3.2
GatewayESP8266OTA.ino
1 /*
2  * The MySensors Arduino library handles the wireless radio link and protocol
3  * between your home built sensors/actuators and HA controller of choice.
4  * The sensors forms a self healing radio network with optional repeaters. Each
5  * repeater and gateway builds a routing tables in EEPROM which keeps track of the
6  * network topology allowing messages to be routed to nodes.
7  *
8  * Created by Henrik Ekblad <[email protected]>
9  * Copyright (C) 2013-2019 Sensnology AB
10  * Full contributor list: https://github.com/mysensors/MySensors/graphs/contributors
11  *
12  * Documentation: http://www.mysensors.org
13  * Support Forum: http://forum.mysensors.org
14  *
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License
17  * version 2 as published by the Free Software Foundation.
18  *
19  *******************************
20  *
21  * REVISION HISTORY
22  * Version 1.0 - Henrik Ekblad
23  * Contribution by tekka,
24  * Contribution by a-lurker and Anticimex,
25  * Contribution by Norbert Truchsess <[email protected]>
26  * Contribution by Ivo Pullens (ESP8266 support)
27  *
28  * DESCRIPTION
29  * The EthernetGateway sends data received from sensors to the WiFi link.
30  * The gateway also accepts input on ethernet interface, which is then sent out to the radio network.
31  *
32  * VERA CONFIGURATION:
33  * Enter "ip-number:port" in the ip-field of the Arduino GW device. This will temporarily override any serial configuration for the Vera plugin.
34  * E.g. If you want to use the default values in this sketch enter: 192.168.178.66:5003
35  *
36  * LED purposes:
37  * - To use the feature, uncomment WITH_LEDS_BLINKING in MyConfig.h
38  * - RX (green) - blink fast on radio message received. In inclusion mode will blink fast only on presentation received
39  * - TX (yellow) - blink fast on radio message transmitted. In inclusion mode will blink slowly
40  * - ERR (red) - fast blink on error during transmission error or receive crc error
41  *
42  * See https://www.mysensors.org/build/connect_radio for wiring instructions.
43  *
44  * If you are using a "barebone" ESP8266, see
45  * https://www.mysensors.org/build/esp8266_gateway#wiring-for-barebone-esp8266
46  *
47  * Inclusion mode button:
48  * - Connect GPIO5 via switch to GND ('inclusion switch')
49  *
50  * Hardware SHA204 signing is currently not supported!
51  *
52  * Make sure to fill in your ssid and WiFi password below for ssid & pass.
53  */
54 
55 // Enable debug prints to serial monitor
56 #define MY_DEBUG
57 
58 // Use a bit lower baudrate for serial prints on ESP8266 than default in MyConfig.h
59 #define MY_BAUD_RATE 9600
60 
61 // Enables and select radio type (if attached)
62 #define MY_RADIO_RF24
63 //#define MY_RADIO_RFM69
64 //#define MY_RADIO_RFM95
65 
66 #define MY_GATEWAY_ESP8266
67 
68 #define MY_WIFI_SSID "MySSID"
69 #define MY_WIFI_PASSWORD "MyVerySecretPassword"
70 
71 // Set the hostname for the WiFi Client. This is the hostname
72 // passed to the DHCP server if not static.
73 #define MY_HOSTNAME "ESP8266_GW"
74 
75 // Enable UDP communication
76 //#define MY_USE_UDP // If using UDP you need to set MY_CONTROLLER_IP_ADDRESS below
77 
78 // Enable MY_IP_ADDRESS here if you want a static ip address (no DHCP)
79 //#define MY_IP_ADDRESS 192,168,178,87
80 
81 // If using static ip you can define Gateway and Subnet address as well
82 //#define MY_IP_GATEWAY_ADDRESS 192,168,178,1
83 //#define MY_IP_SUBNET_ADDRESS 255,255,255,0
84 
85 // The port to keep open on node server mode
86 #define MY_PORT 5003
87 
88 // How many clients should be able to connect to this gateway (default 1)
89 #define MY_GATEWAY_MAX_CLIENTS 2
90 
91 // Controller ip address. Enables client mode (default is "server" mode).
92 // Also enable this if MY_USE_UDP is used and you want sensor data sent somewhere.
93 //#define MY_CONTROLLER_IP_ADDRESS 192, 168, 178, 68
94 
95 // Enable inclusion mode
96 #define MY_INCLUSION_MODE_FEATURE
97 
98 // Enable Inclusion mode button on gateway
99 //#define MY_INCLUSION_BUTTON_FEATURE
100 // Set inclusion mode duration (in seconds)
101 #define MY_INCLUSION_MODE_DURATION 60
102 // Digital pin used for inclusion mode button
103 #define MY_INCLUSION_MODE_BUTTON_PIN 3
104 
105 // Set blinking period
106 // #define MY_DEFAULT_LED_BLINK_PERIOD 300
107 
108 // Flash leds on rx/tx/err
109 // Led pins used if blinking feature is enabled above
110 #define MY_DEFAULT_ERR_LED_PIN 16 // Error led pin
111 #define MY_DEFAULT_RX_LED_PIN 16 // Receive led pin
112 #define MY_DEFAULT_TX_LED_PIN 16 // the PCB, on board LED
113 
114 #include <ArduinoOTA.h>
115 #include <MySensors.h>
116 
117 void setup()
118 {
119  // Setup locally attached sensors
120  ArduinoOTA.onStart([]() {
121  Serial.println("ArduinoOTA start");
122  });
123  ArduinoOTA.onEnd([]() {
124  Serial.println("\nArduinoOTA end");
125  });
126  ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
127  Serial.printf("OTA Progress: %u%%\r", (progress / (total / 100)));
128  });
129  ArduinoOTA.onError([](ota_error_t error) {
130  Serial.printf("Error[%u]: ", error);
131  if (error == OTA_AUTH_ERROR) {
132  Serial.println("Auth Failed");
133  } else if (error == OTA_BEGIN_ERROR) {
134  Serial.println("Begin Failed");
135  } else if (error == OTA_CONNECT_ERROR) {
136  Serial.println("Connect Failed");
137  } else if (error == OTA_RECEIVE_ERROR) {
138  Serial.println("Receive Failed");
139  } else if (error == OTA_END_ERROR) {
140  Serial.println("End Failed");
141  }
142  });
143  ArduinoOTA.begin();
144 }
145 
147 {
148  // Present locally attached sensors here
149 }
150 
151 void loop()
152 {
153  // Send locally attached sensors data here
154  ArduinoOTA.handle();
155 }
156 
API declaration for MySensors.
void setup()
Called after node initialises but before main loop.
void presentation()
Node presentation.
void loop()
Main loop.