Added source code and schematic.

This commit is contained in:
Evil.2000
2020-12-05 23:44:43 +01:00
parent 86325e071e
commit 324cc14c85
6 changed files with 1043 additions and 1 deletions
+169
View File
@@ -0,0 +1,169 @@
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
// Name and password of the WLAN access point.
#define SSID "YourWiFiSSIDhere" // Carmageddon
#define PASSWORD "OhAndYourWiFiPasswordHere;-)"
#define MQTT_BROKER "192.168.0.25"
#define MQTT_PORT 1883
// Relais status flag
uint8_t relais_status = 0;
// Variable for the MQTT Topic where the on/off command arrvies
char mqtt_topic[64];
// Variable for the MQTT topic where the current relais state is published to
char mqtt_topic_state[64];
WiFiClient espClient;
PubSubClient mqtt(espClient);
void Serialprint(const char *input...) {
va_list args;
va_start(args, input);
for (const char *i = input; *i != 0; ++i) {
if (*i != '%') {
Serial.print(*i);
continue;
}
switch (*(++i)) {
case '%':
Serial.print('%');
break;
case 's':
Serial.print(va_arg(args, char*));
break;
case 'd':
Serial.print(va_arg(args, int), DEC);
break;
case 'b':
Serial.print(va_arg(args, int), BIN);
break;
case 'o':
Serial.print(va_arg(args, int), OCT);
break;
case 'x':
Serial.print(va_arg(args, int), HEX);
break;
case 'f':
Serial.print(va_arg(args, double), 2);
break;
}
}
va_end(args);
}
void publish_relais_state() {
char payload[7];
if(relais_status==0) {
sprintf(payload,"%s","off");
} else {
sprintf(payload,"%s","on");
}
mqtt.publish(mqtt_topic_state,(byte*) payload,strlen(payload),true);
//mqtt.publish(mqtt_topic,payload,NULL,true);
}
void onWiFiEvent(WiFiEvent_t event) {
switch (event) {
case WIFI_EVENT_STAMODE_CONNECTED:
Serial.println("WiFi connection established.");
break;
case WIFI_EVENT_STAMODE_DISCONNECTED:
Serial.println("WiFi disconnected.");
break;
case WIFI_EVENT_STAMODE_GOT_IP:
Serialprint("WiFi IP recieved: %s\r\n", WiFi.localIP().toString().c_str());
break;
case WIFI_EVENT_STAMODE_DHCP_TIMEOUT:
Serial.println("WiFi DHCP timeout.");
break;
default:
break;
}
}
/**
* This callback is registered an run when data for a subscribed topic arrives.
* In this case, the payload can only be "on" and "off" which switches the relais
* to the corresponding state.
* (Maybe this can be tuned a bit: directly use payload by casting it from byte to char.)
*/
void mqtt_data_arrived(char *topic, byte *payload, unsigned int length) {
char buf[32];
snprintf(buf, length + 1, "%s", payload);
Serial.println(buf);
if (strcmp(buf, "on") == 0) {
// LOW (0V) level for "ON", because schematic inverts the signal
digitalWrite(2,LOW);
relais_status=1;
publish_relais_state();
} else if (strcmp(buf, "off") == 0) {
// HIGH (3,3V) level for "OFF", because schematic inverts the signal
digitalWrite(2,HIGH);
relais_status=0;
publish_relais_state();
}
}
/**
* Connect to the MQTT broker if not already connected.
* When connecting, register the last will topic, QoS, retained flag and last will also.
* This is done to tell any subscriber that the relay is off when it diconnects from the broker.
*/
void mqtt_connect() {
if (!mqtt.connected()) {
//Serialprint("MQTT connecting to: %s\r\n",MQTT_BROKER);
if (!mqtt.connect(WiFi.hostname().c_str(), mqtt_topic_state, 0, true, "off")) {
// connect using Hostname as Client-ID, last will, will QoS and will retianed flag
// Leave the function if the connection could not be established (broker down).
return;
}
}
mqtt.subscribe(mqtt_topic);
}
void setup() {
pinMode(2,OUTPUT);
digitalWrite(2,HIGH);
// put your setup code here, to run once:
Serial.begin(9600);
WiFi.onEvent(onWiFiEvent, WIFI_EVENT_STAMODE_CONNECTED);
WiFi.onEvent(onWiFiEvent, WIFI_EVENT_STAMODE_DISCONNECTED);
WiFi.onEvent(onWiFiEvent, WIFI_EVENT_STAMODE_GOT_IP);
WiFi.onEvent(onWiFiEvent, WIFI_EVENT_STAMODE_DHCP_TIMEOUT);
// Connect to Wifi
WiFi.mode(WIFI_STA);
WiFi.begin(SSID, PASSWORD);
if (WiFi.waitForConnectResult(60000) < 0) {
ESP.reset();
}
mqtt.setServer(MQTT_BROKER, MQTT_PORT);
// Set the topic for recieving the commands and the topic for publishing the
// relay state (on or off) to the hostname of the ESP (ESP-1A2B3C).
sprintf(mqtt_topic, "/%s/relay", WiFi.hostname().c_str());
sprintf(mqtt_topic_state, "/%s/relay/state", WiFi.hostname().c_str());
// Register the MQTT callback function which recieves the data from the topic
mqtt.setCallback(mqtt_data_arrived);
// Now, connect to the MQTT broker.
mqtt_connect();
publish_relais_state();
}
void loop() {
if (!mqtt.loop()) {
// loop() returns false if connection was closed
// try to reconnect
mqtt_connect();
}
}