MySensors Library & Examples  2.3.2-62-ge298769
GatewayESP8266SecureMQTTClient.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  *
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_MQTT_CA_CERTx Up to three root Certificates Authorities could be defined
54  * to validate the mqtt server' certificate. The most secure.
55  * MY_MQTT_CA_CERT is deprecated and MY_MQTT_CA_CERT1 should
56  * be used instead.
57  * MY_MQTT_FINGERPRINT Alternatively, the mqtt server' certificate finger print
58  * could be used. Less secure and less convenient as you'll
59  * have to update the fingerprint each time the mqtt server'
60  * certificate is updated
61  * If neither MY_MQTT_CA_CERT1 nor MY_MQTT_FINGERPRINT are
62  * defined, insecure connexion will be established. The mqtt
63  * server' certificate will not be validated.
64  * MY_MQTT_CLIENT_CERT The mqtt server may require client certificate for
65  * MY_MQTT_CLIENT_KEY authentication.
66  *
67  * The certs.h file holds the mqtt server' fingerprint and root Certificate Authorities and
68  * client certificate and key. This a sample how to populate MY_MQTT_CA_CERTx, MY_MQTT_FINGERPRINT,
69  * MY_MQTT_CLIENT_CERT and MY_MQTT_CLIENT_KEY.
70  */
71 
72 // Imports certificates and client key
73 #include "certs.h"
74 
75 /**********************************
76  * MySensors node configuration
77  */
78 
79 // General settings
80 #define SKETCH_NAME "MySensorsMQTTGW_Secure"
81 #define SKETCH_VERSION "0.6"
82 #define MY_DEBUG
83 #define MY_NODE_ID 1
84 
85 // Use a bit lower baudrate for serial prints on ESP8266 than default in MyConfig.h
86 #define MY_BAUD_RATE 9600
87 
88 // Enables and select radio type (if attached)
89 #define MY_RADIO_RF24
90 //#define MY_RF24_PA_LEVEL RF24_PA_LOW
91 
92 //#define MY_RADIO_RFM69
93 //#define MY_RADIO_RFM95
94 
95 /**************
96  * Secured connexion with ESP8266
97  */
98 #define MY_GATEWAY_ESP8266_SECURE
99 //** Set WIFI SSID and password
100 #define MY_WIFI_SSID "ssid"
101 #define MY_WIFI_PASSWORD "password"
102 //** Set the hostname for the WiFi Client. This is the hostname
103 // passed to the DHCP server if not static.
104 #define MY_HOSTNAME "esp8266-gw"
105 // Enable MY_IP_ADDRESS here if you want a static ip address (no DHCP)
106 //#define MY_IP_ADDRESS 192,168,178,87
107 
108 // If using static ip you can define Gateway and Subnet address as well
109 //#define MY_IP_GATEWAY_ADDRESS 192,168,178,1
110 //#define MY_IP_SUBNET_ADDRESS 255,255,255,0
111 
112 //** Certificate Authorities. One or two should be enough
113 #define MY_MQTT_CA_CERT1 cert_isrgrootx1_Authority
114 #define MY_MQTT_CA_CERT2 cert_isrgrootx2_Authority
115 //#define MY_MQTT_CA_CERT3 cert_letsEncryptR3_Authority
116 
117 //** Server certificate validation with its fingerprint
118 // less secure and less convenient than with Certificate
119 // Authorities as server certificates are updated often.
120 // Will not be used if MY_MQTT_CA_CERT1 defined.
121 #define MY_MQTT_FINGERPRINT mqtt_fingerprint
122 
123 //** The mqtt server may require client certificate for
124 // authentication.
125 #define MY_MQTT_CLIENT_CERT cert_client
126 #define MY_MQTT_CLIENT_KEY key_client
127 
128 
129 /**************
130  * MQTT_CLIENT configuration
131  */
132 #define MY_GATEWAY_MQTT_CLIENT
133 
134 //** MQTT broker if using URL instead of ip address.
135 // should correspond to the CN field in the mqtt server'
136 // certificate.
137 #define MY_CONTROLLER_URL_ADDRESS mqtt_host
138 
139 //** The MQTT broker port to open
140 #define MY_PORT mqtt_port
141 
142 //** Enable these if your MQTT broker requires username/password
143 //#define MY_MQTT_USER "<mqtt-user>"
144 //#define MY_MQTT_PASSWORD "<mqtt-passwd>"
145 //** Set MQTT client id
146 //#define MY_MQTT_CLIENT_ID "<mqtt-userID>"
147 
148 //** Set this node's subscribe and publish topic prefix
149 #define MY_MQTT_PUBLISH_TOPIC_PREFIX "esp8266-gw/out"
150 #define MY_MQTT_SUBSCRIBE_TOPIC_PREFIX "esp8266-gw/in"
151 
152 
153 // Enable inclusion mode
154 //#define MY_INCLUSION_MODE_FEATURE
155 // Enable Inclusion mode button on gateway
156 //#define MY_INCLUSION_BUTTON_FEATURE
157 // Set inclusion mode duration (in seconds)
158 //#define MY_INCLUSION_MODE_DURATION 60
159 // Digital pin used for inclusion mode button
160 //#define MY_INCLUSION_MODE_BUTTON_PIN D1
161 
162 // Set blinking period
163 //#define MY_DEFAULT_LED_BLINK_PERIOD 300
164 
165 // Flash leds on rx/tx/err
166 //#define MY_DEFAULT_ERR_LED_PIN 16 // Error led pin
167 //#define MY_DEFAULT_RX_LED_PIN 16 // Receive led pin
168 //#define MY_DEFAULT_TX_LED_PIN 16 // the PCB, on board LED
169 
170 #include <MySensors.h>
171 
172 void setup()
173 {
174 
175  // In order to speed up certs and keys verifications
176  system_update_cpu_freq(160);
177 
178  // Setup locally attached sensors
179 }
180 
182 {
183  // Present locally attached sensors here
184 }
185 
186 void loop()
187 {
188  // Send locally attached sensors data here
189 }
190 
loop
void loop()
Main loop.
Definition: GatewayESP8266SecureMQTTClient.ino:186
presentation
void presentation()
Node presentation.
Definition: GatewayESP8266SecureMQTTClient.ino:181
setup
void setup()
Called after node initialises but before main loop.
Definition: GatewayESP8266SecureMQTTClient.ino:172
MySensors.h
API declaration for MySensors.