Skip to content

Instantly share code, notes, and snippets.

@hyukishi
Last active April 5, 2019 01:49
Show Gist options
  • Select an option

  • Save hyukishi/4f7ed44e602407712346e49724782a0d to your computer and use it in GitHub Desktop.

Select an option

Save hyukishi/4f7ed44e602407712346e49724782a0d to your computer and use it in GitHub Desktop.
The purpose of this code is to remove the need for yet another automation platform such as HomeAssistant or OpenHAB.
// Setup your sonoff //
// Flash your sonoff switch with tasmota //
// Associate the tasmotized sonoff with your WiFi network //
// Load the webpage for the sonoff and setup your mqtt topics //
// Go into module setup and set GPIO14 to Switch2 //
// Go into the console and type "SwitchMode2 1" (without quotes) //
// Lastly, in the console type "PulseTime 1" (without quotes) //
// Setup credentials in the code //
// Replace asterisks next to ssid & password with WiFi credentials //
// Replace asterisks next to update_username & update_password with creds //
#include <PubSubClient.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPUpdateServer.h>
const char* ssid = "******";
const char* password = "******";
const char* host = "CarPresence";
const char* update_path = "/";
const char* update_username = "******";
const char* update_password = "******";
IPAddress ip(192, 168, 86, 48);
IPAddress gateway(192, 168, 86, 160);
IPAddress subnet(255, 255, 255, 0);
char* topic = "cmnd/GarageDoor/POWER";
const char* willTopic = "cmnd/GarageDoor/POWER";
char* reedTopic = "cmnd/GarageDoor/POWER2";
char* server = "192.168.1.138";
byte willQoS = 1;
const char* willMessage = "1";
boolean willRetain = false;
boolean retained = true;
ESP8266WebServer httpServer(80);
ESP8266HTTPUpdateServer httpUpdater;
WiFiClient wifiClient;
PubSubClient client(wifiClient);
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("CarPresence")) {
Serial.println("connected");
// set callback and subscribe to topics
client.setCallback(callback);
client.subscribe("cmnd/GarageDoor/POWER");
client.subscribe("cmnd/GarageDoor/POWER2");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void callback(char* reedTopic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(reedTopic);
Serial.print("] ");
for (int i=0;i<length;i++) {
Serial.print((char)payload[i]);
}
Serial.println();
payload[length] = '\0';
String message = (char*)payload;
if(message == "ON"){
client.publish(topic, "Garage Door is CLOSED");
client.publish(topic, "1");
delay(10000);
while(message != "ON"){
break;
}
}
if(message == "OFF"){
client.publish(topic, "Garage Door is OPEN");
delay(10000);
while(message != "OFF"){
break;
}
}
}
String macToStr(const uint8_t* mac)
{
String result;
for (int i = 0; i < 6; ++i) {
result += String(mac[i], 16);
if (i < 5)
result += ':';
}
return result;
}
void setup() {
Serial.begin(115200);
delay(10);
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
httpUpdater.setup(&httpServer, update_path, update_username, update_password);
httpServer.begin();
Serial.println("HTTPUpdateServer ready!");
// Generate client name based on MAC address and last 8 bits of microsecond counter
String clientName = "CarPresence";
client.setServer(server, 1883);
client.setCallback(callback);
client.connect("CarPresence", willTopic, willQoS, willRetain, willMessage);
client.subscribe(topic);
client.subscribe(reedTopic, retained);
Serial.print("Connecting to ");
Serial.print(server);
Serial.print(" as ");
Serial.println(clientName);
if (client.connect((char*) clientName.c_str())) {
Serial.println("Connected to MQTT broker");
Serial.print("Topics are: ");
Serial.println(topic);
Serial.println(reedTopic);
if (client.publish(topic, "Hello from ESP8266")) {
Serial.println("Publish ok");
}
else {
Serial.println("Publish failed");
}
}
else {
Serial.println("MQTT connect failed");
Serial.println("Will reset and try again...");
reconnect();
}
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
httpServer.handleClient();
client.publish(topic, "Keeping Alive");
delay(2000);
while (WiFi.status() != WL_CONNECTED) {
delay(5000);
Serial.print(".");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment