Skip to content

Commit a3d7303

Browse files
authored
New SSL implementation
Added support to secured connexion to mqtt server thanks to WiFiClientSecure class. Please see comments in code. You can look for WiFiClientSecure, MY_GATEWAY_ESP8266_SECURE, MY_SSL_CERT, MY_SSL_FINGERPRINT and MY_SSL_CERT_CLIENT in the code below to see what has changed. No new method, no new class to be used by my_sensors. The following constants have to be defined from the gateway code: MY_GATEWAY_ESP8266_SECURE in place of MY_GATEWAY_ESP8266 to go to secure connections. MY_SSL_CERT_AUTHx Up to three root Certificates Authorities could be defined to validate the mqtt server' certificate. The most secure. Let's Encrypt requires at least two to validate all the certificates signed by them. MY_SSL_FINGERPRINT Alternatively, the mqtt server' certificate finger print could be used. Less secure and less convenient as you'll have to update the fingerprint each time the mqtt server' certificate is updated If neither MY_SSL_CERT_AUTH1 nor MY_SSL_FINGERPRINT are defined, insecure connexion will be established. The mqtt server' certificate will not be validated. MY_SSL_CERT_CLIENT The mqtt server may require client certificate for MY_SSL_KEY_CLIENT authentication.
1 parent 5816399 commit a3d7303

File tree

1 file changed

+226
-0
lines changed

1 file changed

+226
-0
lines changed
Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
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+
*
24+
* DESCRIPTION
25+
* The ESP8266 MQTT gateway sends radio network (or locally attached sensors) data to your MQTT broker.
26+
* The node also listens to MY_MQTT_TOPIC_PREFIX and sends out those messages to the radio network
27+
*
28+
* LED purposes:
29+
* - To use the feature, uncomment any of the MY_DEFAULT_xx_LED_PINs in your sketch
30+
* - RX (green) - blink fast on radio message received. In inclusion mode will blink fast only on presentation received
31+
* - TX (yellow) - blink fast on radio message transmitted. In inclusion mode will blink slowly
32+
* - ERR (red) - fast blink on error during transmission error or receive crc error
33+
*
34+
* See https://www.mysensors.org/build/connect_radio for wiring instructions.
35+
*
36+
* If you are using a "barebone" ESP8266, see
37+
* https://www.mysensors.org/build/esp8266_gateway#wiring-for-barebone-esp8266
38+
*
39+
* Inclusion mode button:
40+
* - Connect GPIO5 (=D1) via switch to GND ('inclusion switch')
41+
*
42+
* Hardware SHA204 signing is currently not supported!
43+
*
44+
* Make sure to fill in your ssid and WiFi password below for ssid & pass.
45+
*
46+
********************************
47+
*
48+
* SSL support by Eric Grammatico. You should have an updated version of MyGatewayTransportMQTTClient.cpp.
49+
* Please see: https://forum.mysensors.org/topic/11941/esp8266-mqtt-gateway-ssl-connection
50+
*
51+
* The following constants have to be defined from the gateway code:
52+
* MY_GATEWAY_ESP8266_SECURE in place of MY_GATEWAY_ESP8266 to go to secure connexions.
53+
* MY_SSL_CERT_AUTHx Up to three root Certificates Authorities could be defined
54+
* to validate the mqtt server' certificate. The most secure.
55+
* MY_SSL_FINGERPRINT Alternatively, the mqtt server' certificate finger print
56+
* could be used. Less secure and less convenient as you'll
57+
* have to update the fingerprint each time the mqtt server'
58+
* certificate is updated
59+
* If neither MY_SSL_CERT_AUTH1 nor MY_SSL_FINGERPRINT are
60+
* defined, insecure connexion will be established. The mqtt
61+
* server' certificate will not be validated.
62+
* MY_SSL_CERT_CLIENT The mqtt server may require client certificate for
63+
* MY_SSL_KEY_CLIENT authentication.
64+
*
65+
* The certs.h file holds the mqtt server' fingerprint and root Certificate Authorities and
66+
* client certificate and key. This a sample how to populate MY_SSL_CERT_AUTHx, MY_SSL_FINGERPRINT,
67+
* MY_SSL_CERT_CLIENT and MY_SSL_KEY_CLIENT.
68+
*/
69+
70+
// Imports certificates and client key
71+
#include "certs.h"
72+
73+
/**********************************
74+
* MySensors node configuration
75+
*/
76+
77+
// General settings
78+
#define SKETCH_NAME "MySensorsMQTTGW_Secure"
79+
#define SKETCH_VERSION "0.5"
80+
#define MY_DEBUG
81+
#define MY_NODE_ID 1
82+
83+
// Use a bit lower baudrate for serial prints on ESP8266 than default in MyConfig.h
84+
#define MY_BAUD_RATE 9600
85+
86+
// Enables and select radio type (if attached)
87+
//#define MY_RADIO_RF24
88+
//#define MY_RF24_PA_LEVEL RF24_PA_LOW
89+
90+
//#define MY_RADIO_RFM69
91+
//#define MY_RADIO_RFM95
92+
93+
/**************
94+
* Secured connexion with ESP8266
95+
*/
96+
#define MY_GATEWAY_ESP8266_SECURE
97+
//** Set WIFI SSID and password
98+
#define MY_WIFI_SSID "ssid"
99+
#define MY_WIFI_PASSWORD "password"
100+
//** Set the hostname for the WiFi Client. This is the hostname
101+
// passed to the DHCP server if not static.
102+
#define MY_HOSTNAME "esp8266-gw"
103+
//** Certificate Authorities. One or two should be enough
104+
#define MY_SSL_CERT_AUTH1 cert_isrgrootx1_Authority
105+
#define MY_SSL_CERT_AUTH2 cert_isrgrootx2_Authority
106+
//#define MY_SSL_CERT_AUTH3 cert_letsEncryptR3_Authority
107+
//** Server certificate validation with its fingerprint
108+
// less secure and less convenient than with Certificate
109+
// Authorities as server certificates are updated often.
110+
// Will not be used if MY_SSL_CERT_AUTH1 defined.
111+
#define MY_SSL_FINGERPRINT mqtt_fingerprint
112+
//** The mqtt server may require client certificate for
113+
// authentication.
114+
#define MY_SSL_CERT_CLIENT cert_client
115+
#define MY_SSL_KEY_CLIENT key_client
116+
117+
118+
/**************
119+
* MQTT_CLIENT configuration
120+
*/
121+
#define MY_GATEWAY_MQTT_CLIENT
122+
//** MQTT broker if using URL instead of ip address.
123+
// should correspond to the CN field in the mqtt server'
124+
// certificate.
125+
#define MY_CONTROLLER_URL_ADDRESS mqtt_host
126+
//** The MQTT broker port to open
127+
#define MY_PORT mqtt_port
128+
//** Enable these if your MQTT broker requires username/password
129+
//#define MY_MQTT_USER "<mqtt-user>"
130+
//#define MY_MQTT_PASSWORD "<mqtt-passwd>"
131+
//** Set MQTT client id
132+
//#define MY_MQTT_CLIENT_ID "<mqtt-userID>"
133+
//** Set this node's subscribe and publish topic prefix
134+
#define MY_MQTT_PUBLISH_TOPIC_PREFIX "esp8266-gw/out"
135+
#define MY_MQTT_SUBSCRIBE_TOPIC_PREFIX "esp8266-gw/in"
136+
137+
138+
/***********************************
139+
* NodeManager configuration
140+
*/
141+
142+
#define NODEMANAGER_DEBUG ON
143+
#define NODEMANAGER_INTERRUPTS OFF
144+
#define NODEMANAGER_SLEEP OFF
145+
#define NODEMANAGER_RECEIVE ON
146+
#define NODEMANAGER_DEBUG_VERBOSE OFF
147+
#define NODEMANAGER_POWER_MANAGER OFF
148+
#define NODEMANAGER_CONDITIONAL_REPORT OFF
149+
#define NODEMANAGER_EEPROM OFF
150+
#define NODEMANAGER_TIME OFF
151+
#define NODEMANAGER_RTC OFF
152+
#define NODEMANAGER_SD OFF
153+
#define NODEMANAGER_HOOKING OFF
154+
#define NODEMANAGER_OTA_CONFIGURATION OFF
155+
#define NODEMANAGER_SERIAL_INPUT OFF
156+
157+
158+
// import NodeManager library (a nodeManager object will be then made available)
159+
#include <MySensors_NodeManager.h>
160+
161+
/***********************************
162+
* Add your sensors
163+
*/
164+
#include <sensors/SensorThermistor.h>
165+
SensorThermistor thermistor(A0);
166+
167+
168+
169+
170+
// before
171+
void before() {
172+
173+
174+
175+
/***********************************
176+
* Configure your sensors
177+
*/
178+
179+
// report measures of every attached sensors every 10 minutes
180+
nodeManager.setReportIntervalMinutes(10);
181+
182+
// set an offset to -1 to a thermistor sensor
183+
//thermistor.setOffset(-1);
184+
185+
// call NodeManager before routine
186+
nodeManager.before();
187+
}
188+
189+
void presentation()
190+
{
191+
// call NodeManager presentation routine
192+
nodeManager.presentation();
193+
}
194+
195+
void setup()
196+
{
197+
198+
// SSL is cycles consuming for the ESP8266
199+
system_update_cpu_freq(160);
200+
201+
// call NodeManager setup routine
202+
nodeManager.setup();
203+
}
204+
205+
206+
void loop()
207+
{
208+
// call NodeManager loop routine
209+
nodeManager.loop();
210+
}
211+
212+
#if NODEMANAGER_RECEIVE == ON
213+
// receive
214+
void receive(const MyMessage &message) {
215+
// call NodeManager receive routine
216+
nodeManager.receive(message);
217+
}
218+
#endif
219+
220+
#if NODEMANAGER_TIME == ON
221+
// receiveTime
222+
void receiveTime(unsigned long ts) {
223+
// call NodeManager receiveTime routine
224+
nodeManager.receiveTime(ts);
225+
}
226+
#endif

0 commit comments

Comments
 (0)