343 lines
10 KiB
C++
343 lines
10 KiB
C++
|
|
/*
|
|
* Getting Started example sketch for nRF24L01+ radios
|
|
* This is a very basic example of how to send data from one node to another
|
|
* Updated: Dec 2014 by TMRh20
|
|
*/
|
|
|
|
//MQTT Gateway is a NodeMCU 1.0 (ESP-12E Module) - ESP8266
|
|
#include <ESP8266WiFi.h>
|
|
#include <ESP8266mDNS.h>
|
|
#include <NTPClient.h>
|
|
#include <WiFiUdp.h>
|
|
#include <ArduinoOTA.h>
|
|
#include <PubSubClient.h>
|
|
#include <Ticker.h>
|
|
|
|
const uint32_t wdtTimeout = 8388607; //maximum for the ESP8266
|
|
uint32_t LastWdgFeeding;
|
|
|
|
Ticker timer;
|
|
|
|
#ifndef STASSID
|
|
#define STASSID "EasyBox-368239"
|
|
#define STAPSK "inginf95"
|
|
#endif
|
|
|
|
#include <SPI.h>
|
|
#include "RF24.h"
|
|
|
|
/****************** User Config ***************************/
|
|
/*** Set this radio as radio number 0 or 1 ***/
|
|
bool radioNumber = 0;
|
|
int len = 0;
|
|
|
|
const char* ssid = STASSID;
|
|
const char* password = STAPSK;
|
|
const char* MQTT_BROKER = "montana2020";
|
|
|
|
const char* MQTT_STATUS = "stat/Badezimmer/MQTTGateway/Garage/status";
|
|
const char* MQTT_DEBUG = "tele/Badezimmer/MQTTGateway/Garage/debug";
|
|
|
|
const char* MQTT_HEARTBEAT = "stat/Flur/MA/lastRcv";
|
|
|
|
#define MAXSIZE 100
|
|
#define CHANNEL 108
|
|
|
|
char gotmsg[MAXSIZE];
|
|
char logString[MAXSIZE];
|
|
|
|
char TorOffen[] = "OPENED";
|
|
char TorGeschlossen[] = "CLOSED";
|
|
char TorInBewegung[] = "IN TRANSIT";
|
|
|
|
char TorAuf[] = "Oeffne Garagentor";
|
|
char TorZu[] = "Schliesse Garagentor";
|
|
char TorStatus[] = "Schicke Status";
|
|
|
|
WiFiClient espClient;
|
|
PubSubClient client(espClient);
|
|
|
|
char msg[MAXSIZE];
|
|
int value = 0;
|
|
|
|
/* Hardware configuration: Set up nRF24L01 radio on SPI bus plus pins 7 & 8 */
|
|
RF24 radio(D4,D8);
|
|
/**********************************************************/
|
|
|
|
byte addresses[][6] = {"1Node","2Node"};
|
|
|
|
void ICACHE_RAM_ATTR resetModule() {
|
|
/*Serial.print("Current time: ");
|
|
Serial.println(millis());
|
|
Serial.print("Last fed: ");
|
|
Serial.println(LastWdgFeeding);*/
|
|
|
|
//restart if the program hangs for more than 10min...
|
|
if (millis() - LastWdgFeeding > 600000) {
|
|
Serial.print("Restarting due to watchdog!");
|
|
client.publish(MQTT_DEBUG, "Restarting due to watchdog!", true);
|
|
client.disconnect();
|
|
ESP.restart();
|
|
}
|
|
timer1_write(wdtTimeout);
|
|
}
|
|
|
|
void setup() {
|
|
Serial.begin(115200);
|
|
Serial.println(F("Booting"));
|
|
|
|
WiFi.mode(WIFI_STA);
|
|
WiFi.begin(ssid, password);
|
|
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
|
|
Serial.println("Connection Failed! Rebooting...");
|
|
delay(5000);
|
|
ESP.restart();
|
|
}
|
|
|
|
// Port defaults to 8266
|
|
// ArduinoOTA.setPort(8266);
|
|
|
|
// Hostname defaults to esp8266-[ChipID]
|
|
ArduinoOTA.setHostname("MQTT-Gateway");
|
|
|
|
// No authentication by default
|
|
ArduinoOTA.setPassword("inginf");
|
|
|
|
// Password can be set with it's md5 value as well
|
|
// MD5(admin) = 21232f297a57a5a743894a0e4a801fc3
|
|
// ArduinoOTA.setPasswordHash("21232f297a57a5a743894a0e4a801fc3");
|
|
|
|
ArduinoOTA.onStart([]() {
|
|
String type;
|
|
if (ArduinoOTA.getCommand() == U_FLASH) {
|
|
type = "sketch";
|
|
} else { // U_SPIFFS
|
|
type = "filesystem";
|
|
}
|
|
|
|
// NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
|
|
Serial.println("Start updating " + type);
|
|
});
|
|
ArduinoOTA.onEnd([]() {
|
|
Serial.println("\nEnd");
|
|
});
|
|
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
|
|
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
|
|
});
|
|
ArduinoOTA.onError([](ota_error_t error) {
|
|
Serial.printf("Error[%u]: ", error);
|
|
if (error == OTA_AUTH_ERROR) {
|
|
Serial.println("Auth Failed");
|
|
} else if (error == OTA_BEGIN_ERROR) {
|
|
Serial.println("Begin Failed");
|
|
} else if (error == OTA_CONNECT_ERROR) {
|
|
Serial.println("Connect Failed");
|
|
} else if (error == OTA_RECEIVE_ERROR) {
|
|
Serial.println("Receive Failed");
|
|
} else if (error == OTA_END_ERROR) {
|
|
Serial.println("End Failed");
|
|
}
|
|
});
|
|
ArduinoOTA.begin();
|
|
Serial.println("Ready");
|
|
Serial.print("IP address: ");
|
|
Serial.println(WiFi.localIP());
|
|
|
|
client.setServer(MQTT_BROKER, 1884);
|
|
client.setCallback(Callback);
|
|
client.publish(MQTT_DEBUG, "Startup!", true);
|
|
|
|
radio.begin();
|
|
|
|
// Set the PA Level low to prevent power supply related issues since this is a
|
|
// getting_started sketch, and the likelihood of close proximity of the devices. RF24_PA_MAX is default.
|
|
radio.setPALevel(RF24_PA_MAX);
|
|
radio.setDataRate(RF24_250KBPS);
|
|
radio.setAutoAck(false);
|
|
radio.setRetries(15, 15);
|
|
//radio.setChannel(CHANNEL);
|
|
|
|
// Open a writing and reading pipe on each radio, with opposite addresses
|
|
if(radioNumber){
|
|
radio.openWritingPipe(addresses[1]);
|
|
radio.openReadingPipe(1,addresses[0]);
|
|
}else{
|
|
radio.openWritingPipe(addresses[0]);
|
|
radio.openReadingPipe(1,addresses[1]);
|
|
}
|
|
|
|
// Start the radio listening for data
|
|
radio.startListening();
|
|
|
|
timer1_attachInterrupt(resetModule);
|
|
timer1_enable(TIM_DIV256, TIM_EDGE, TIM_SINGLE);
|
|
timer1_write(wdtTimeout);
|
|
}
|
|
|
|
|
|
void reconnect() {
|
|
while (!client.connected()) {
|
|
Serial.print("Reconnecting...");
|
|
if (!client.connect("MQTT-Gateway")) {
|
|
Serial.print("failed, rc=");
|
|
Serial.print(client.state());
|
|
Serial.println(" retrying in 5 seconds");
|
|
delay(5000);
|
|
} else {
|
|
Serial.println("connected");
|
|
client.subscribe("cmnd/Badezimmer/MQTTGateway/Garage/target");
|
|
|
|
//Heartbeat meassge every 7 min...
|
|
client.subscribe(MQTT_HEARTBEAT);
|
|
|
|
client.publish(MQTT_DEBUG, "Subscribed to mqtt messages", true);
|
|
LastWdgFeeding = millis();
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
void Callback(char* topic, byte* payload, unsigned int length) {
|
|
bool doTransmit;
|
|
bool doTransmitStatus;
|
|
int retries = 0;
|
|
|
|
doTransmit = false;
|
|
|
|
Serial.print("Message arrived [");
|
|
|
|
Serial.print(topic);
|
|
Serial.print("] ");
|
|
for (int i=0;i<length;i++) {
|
|
gotmsg[i]=(char)payload[i];
|
|
}
|
|
|
|
//needs to be checked here as a client.publish overwrites the topic string...
|
|
if (strncmp(topic,MQTT_HEARTBEAT,strlen(MQTT_HEARTBEAT)) == 0){
|
|
doTransmitStatus = true;
|
|
} else {
|
|
doTransmitStatus = false;
|
|
}
|
|
|
|
gotmsg[length]=0;
|
|
Serial.println(gotmsg);
|
|
|
|
|
|
while (retries<3)
|
|
{
|
|
retries++;
|
|
|
|
unsigned long start_time = micros();
|
|
if (strcmp(gotmsg,"OPEN") == 0) {
|
|
client.publish(MQTT_DEBUG, "Request garage door opened.", true);
|
|
radio.stopListening(); // First, stop listening so we can talk.
|
|
Serial.print(F("Now sending: "));
|
|
Serial.println(TorAuf);
|
|
if (!radio.write( &TorAuf, strlen(TorAuf) )){
|
|
Serial.println(F("failed"));
|
|
}
|
|
doTransmit = true;
|
|
radio.startListening(); // Now, continue listening
|
|
} else if (strcmp(gotmsg,"CLOSE") == 0){
|
|
client.publish(MQTT_DEBUG, "Request garage door closed.", true);
|
|
radio.stopListening(); // First, stop listening so we can talk.
|
|
Serial.print(F("Now sending: "));
|
|
Serial.println(TorZu);
|
|
if (!radio.write( &TorZu, strlen(TorZu) )){
|
|
Serial.println(F("failed"));
|
|
}
|
|
doTransmit = true;
|
|
radio.startListening(); // Now, continue listening
|
|
} else if (doTransmitStatus){
|
|
client.publish(MQTT_DEBUG, "Request garage door status.", true);
|
|
radio.stopListening(); // First, stop listening so we can talk.
|
|
Serial.print(F("Now sending: "));
|
|
Serial.println(TorStatus);
|
|
if (!radio.write( &TorStatus, strlen(TorStatus) )){
|
|
Serial.println(F("failed"));
|
|
}
|
|
doTransmit = true;
|
|
radio.startListening(); // Now, continue listening
|
|
}
|
|
|
|
|
|
|
|
if (doTransmit){
|
|
// Set up a timeout period, get the current microseconds
|
|
unsigned long started_waiting_at = micros();
|
|
boolean timeout = false; // Set up a variable to indicate if a response was received or not
|
|
|
|
while ( ! radio.available() ){ // While nothing is received
|
|
if (micros() - started_waiting_at > 200000 ){ // If waited longer than 200ms, indicate timeout and exit while loop
|
|
timeout = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if ( timeout ){ // Describe the results
|
|
Serial.println(F("Failed, response timed out."));
|
|
client.publish(MQTT_DEBUG, "Failed, response timed out.", true);
|
|
} else {
|
|
retries = 3; //exit retry loop...
|
|
|
|
// Grab the response, compare, and send to debugging spew
|
|
len = radio.getDynamicPayloadSize();
|
|
radio.read( &gotmsg, len );
|
|
unsigned long end_time = micros();
|
|
// Spew it
|
|
sprintf(logString,"Sent: %i, Got response: %s, Round-trip delay: %i microseconds",start_time,gotmsg,end_time-start_time);
|
|
Serial.println(logString);
|
|
client.publish(MQTT_DEBUG, logString, true);
|
|
}
|
|
}
|
|
|
|
if (retries<3){
|
|
delay(2500); //wait for 2.5ms between tries...
|
|
}
|
|
} //End of 3 retries...
|
|
//feed the watchdog...
|
|
LastWdgFeeding = millis();
|
|
}
|
|
|
|
|
|
// MAIN CONTROL LOOP //
|
|
void loop() {
|
|
ArduinoOTA.handle();
|
|
|
|
if (!client.loop()) {
|
|
char msg[16];
|
|
itoa(client.state(), msg, 10);
|
|
//client.publish(MQTT_DEBUG, msg, true);
|
|
reconnect();
|
|
}
|
|
|
|
if ( true ) //always keep on listening!
|
|
{
|
|
unsigned long got_time;
|
|
|
|
if( radio.available()){
|
|
// Variable for the received timestamp
|
|
while (radio.available()) { // While there is data ready
|
|
len = radio.getDynamicPayloadSize();
|
|
radio.read( &gotmsg, len ); // Get the payload
|
|
}
|
|
|
|
radio.stopListening(); // First, stop listening so we can talk
|
|
radio.write( &gotmsg, strlen(gotmsg) ); // Send the final one back.
|
|
//delay(500);
|
|
radio.startListening();
|
|
|
|
// Now, resume listening so we catch the next packets.
|
|
Serial.print(F("Sent response ACK: "));
|
|
Serial.println(gotmsg);
|
|
|
|
Serial.print("Publish status MQTT message: ");
|
|
Serial.println(gotmsg);
|
|
client.publish(MQTT_STATUS, gotmsg, true);
|
|
}
|
|
}
|
|
} // Loop
|