Hi David.
thanks for your assistants. i noticed that the relay and the led are sharing a single IO port. an that the numbers are noted near the leds. (there was a small typo in your list)
const int relayPin1 = 12; // Active high
const int relayPin2 = 5; // Active high
const int relayPin3 = 4; // Active high
const int relayPin4 = 15; // Active high
const int ledPin = 13; // Active low
#define button1Pin 0
#define button2Pin 9
#define button3Pin 10
#define button4Pin 14
currently the buttons are not working as they should be. (the are random high / low) but that is not a big issue for me. (i place a regular switch before this to have a manual “off” anyway)
For those who are intrested in the code.
For sure i am not a programmer so the layout is horrible and there should be a lot of better ways of doing this. but it is working fine for me.
the thing i miss the most in mqtt switches is the feedback. so on the bottom there is a status reporting (so when i am away from home i know for sure if the switch is on of off.
/*
* Simple example MQTT client to run on the Itead Studio Sonoff with OTA
* update support. Subscribes to a topic and watches for a message of
* either "0" (turn off LED and relay) or "1" (turn on LED and relay)
*
* For more information see:
* http://www.superhouse.tv/17-home-automation-control-with-sonoff-arduino-openhab-and-mqtt/
*/
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <ESP8266HTTPUpdateServer.h>
#include <WiFiUdp.h>
#include <PubSubClient.h>
#include <dummy.h>
/* current code version */
const char* Ver = "2.3";
/* WiFi Settings */
const char* host = "Vld-Sonoff14";
const char* ssid = "IOT";
const char* password = "****some random PSK*****";
ESP8266WebServer httpServer(80);
ESP8266HTTPUpdateServer httpUpdater;
/* Sonoff Outputs */
const int relayPin1 = 12; // Active high
const int relayPin2 = 5; // Active high
const int relayPin3 = 4; // Active high
const int relayPin4 = 15; // Active high
const int ledPin = 13; // Active low
#define button1Pin 0
#define button2Pin 9
#define button3Pin 10
#define button4Pin 14
boolean button1State = false;
boolean button2State = false;
boolean button3State = false;
boolean button4State = false;
boolean oldbutton1State = false;
boolean oldButton2State = false;
boolean oldButton3State = false;
boolean oldButton4State = false;
int oldstate1 = LOW; // the previous reading from the input pin
int oldstate2 = LOW; // the previous reading from the input pin
int oldstate3 = LOW; // the previous reading from the input pin
int oldstate4 = LOW; // the previous reading from the input pin
int count = 0;
int reboot = 0;/* Settings */
#define CLIENT_ID "Sonoff14" // Client ID to send to the broker
#define CLIENT_USER "sonof"
#define CLIENT_PSWD "**************"
IPAddress broker(172,16,20,60); // Address of the MQTT broker
/**
* MQTT callback to process messages
*/
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i=0;i<length;i++) {
Serial.print((char)payload[i]);
}
Serial.println();
String top = topic;
if (top == "/vld/vld-sonoff14/relay1") {
// Examine only the first character of the message
if(payload[0] == 49) // Message "1" in ASCII (turn outputs ON)
{
digitalWrite(relayPin1, HIGH);
} else if(payload[0] == 48) // Message "0" in ASCII (turn outputs OFF)
{
digitalWrite(relayPin1, LOW);
} else {
Serial.println("Unknown value");
}
}
if (top == "/vld/vld-sonoff14/relay2") {
// Examine only the first character of the message
if(payload[0] == 49) // Message "1" in ASCII (turn outputs ON)
{
digitalWrite(relayPin2, HIGH);
} else if(payload[0] == 48) // Message "0" in ASCII (turn outputs OFF)
{
digitalWrite(relayPin2, LOW);
} else {
Serial.println("Unknown value");
}
}
if (top == "/vld/vld-sonoff14/relay3") {
// Examine only the first character of the message
if(payload[0] == 49) // Message "1" in ASCII (turn outputs ON)
{
digitalWrite(relayPin3, HIGH);
} else if(payload[0] == 48) // Message "0" in ASCII (turn outputs OFF)
{
digitalWrite(relayPin3, LOW);
} else {
Serial.println("Unknown value");
}
}
if (top == "/vld/vld-sonoff14/relay4") {
// Examine only the first character of the message
if(payload[0] == 49) // Message "1" in ASCII (turn outputs ON)
{
digitalWrite(relayPin4, HIGH);
} else if(payload[0] == 48) // Message "0" in ASCII (turn outputs OFF)
{
digitalWrite(relayPin4, LOW);
} else {
Serial.println("Unknown value");
}
}
if (top == "/vld/vld-sonoff14/led") {
// Examine only the first character of the message
if(payload[0] == 49) // Message "1" in ASCII (turn led ON)
{
digitalWrite(ledPin, LOW); // LED is active-low, so this turns it on
} else if(payload[0] == 48) // Message "0" in ASCII (turn led OFF)
{
digitalWrite(ledPin, HIGH); // LED is active-low, so this turns it off
} else {
Serial.println("Unknown value");
}
}
}
WiFiClient wificlient;
PubSubClient client(wificlient);
/**
* Attempt connection to MQTT broker and subscribe to command topic
*/
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect(CLIENT_ID, CLIENT_USER, CLIENT_PSWD)) {
Serial.println("connected");
if (reboot == 0){
client.publish("/vld/state/vld-sonoff14/status", "started");
} else {
client.publish("/vld/state/vld-sonoff14/status", "reconnected");
}
reboot++;
client.publish("/vld/state/vld-sonoff14/version", Ver, true);
client.subscribe("/vld/vld-sonoff14/#");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
/**
* Setup
*/
void setup() {
Serial.begin(115200);
Serial.println("Booting");
WiFi.mode(WIFI_STA);
WiFi.hostname(host);
WiFi.begin(ssid, password);
Serial.println("WiFi begun");
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("Connection Failed! Rebooting...");
delay(5000);
ESP.restart();
}
Serial.println("Proceeding");
MDNS.begin(host);
httpUpdater.setup(&httpServer);
httpServer.begin();
// Serial.printf("HTTPUpdateServer ready! Open http://%s.local/update in your browser\n", host);
MDNS.addService("http", "tcp", 80);
/* Set up the outputs. LED is active-low */
pinMode(ledPin, OUTPUT);
pinMode(relayPin1, OUTPUT);
pinMode(relayPin2, OUTPUT);
pinMode(relayPin3, OUTPUT);
pinMode(relayPin4, OUTPUT);
digitalWrite(ledPin, HIGH);
digitalWrite(relayPin1, LOW);
digitalWrite(relayPin2, LOW);
digitalWrite(relayPin3, LOW);
digitalWrite(relayPin4, LOW);
/* Prepare MQTT client */
client.setServer(broker, 8883);
client.setCallback(callback);
}
/**
* Main
*/
void loop() {
if (WiFi.status() != WL_CONNECTED)
{
Serial.print("Connecting to ");
Serial.print(ssid);
Serial.println("...");
WiFi.begin(ssid, password);
if (WiFi.waitForConnectResult() != WL_CONNECTED)
return;
Serial.println("WiFi connected");
}
if (WiFi.status() == WL_CONNECTED) {
if (!client.connected()) {
reconnect();
}
}
if (client.connected())
{
httpServer.handleClient();
client.loop();
/* buttons are not working correctly on the 4ch model
*
*
button1State = digitalRead(button1Pin);
if (button1State == LOW){
count = 0;
while(digitalRead(button1Pin)==LOW){
delay(10);
count++;
if (count > 500){ //button held
digitalWrite(ledPin, LOW); // LED is active-low, so this turns it on
delay(500);
digitalWrite(ledPin, HIGH); // LED is active-low, so this turns it on
delay(500);
digitalWrite(ledPin, LOW); // LED is active-low, so this turns it on
delay(500);
digitalWrite(ledPin, HIGH); // LED is active-low, so this turns it on
delay(500);
digitalWrite(ledPin, LOW); // LED is active-low, so this turns it on
client.publish("/vld/state/vld-sonoff14/status", "Reset");
delay(2000);
digitalWrite(ledPin, HIGH); // LED is active-low, so this turns it on
ESP.reset();
}
if (count > 100){ //button held
digitalWrite(ledPin, LOW); // LED is active-low, so this turns it on
client.publish("/vld/vld-sonoff14/button", "2"); //button held
}
}
if (( count > 2) && ( count < 100)){
if ( digitalRead(relayPin1) == LOW ) {
digitalWrite(relayPin1, HIGH);
} else {
digitalWrite(relayPin1, LOW);
}
client.publish("/vld/vld-sonoff14/button", "1"); //button short
}
}
}
button2State = digitalRead(button2Pin);
if (button2State == LOW){
if ( digitalRead(relayPin2) == LOW ) {
digitalWrite(relayPin2, HIGH);
} else {
digitalWrite(relayPin2, LOW);
}
client.publish("/vld/vld-sonoff14/button2", "1"); //button short
}
button3State = digitalRead(button3Pin);
if (button3State == LOW){
if ( digitalRead(relayPin3) == LOW ) {
digitalWrite(relayPin3, HIGH);
} else {
digitalWrite(relayPin3, LOW);
}
client.publish("/vld/vld-sonoff14/button3", "1"); //button short
}
button4State = digitalRead(button4Pin);
if (button4State == LOW){
if ( digitalRead(relayPin4) == LOW ) {
digitalWrite(relayPin4, HIGH);
} else {
digitalWrite(relayPin4, LOW);
}
client.publish("/vld/vld-sonoff14/button4", "1"); //button short
*/
}
if ( oldstate1 != digitalRead(relayPin1)) {
oldstate1 = digitalRead(relayPin1);
if ( digitalRead(relayPin1) == LOW ) {
client.publish("/vld/vld-sonoff14S/relay1", "0", true);
} else {
client.publish("/vld/vld-sonoff14S/relay1", "1", true);
}
}
if ( oldstate2 != digitalRead(relayPin2)) {
oldstate2 = digitalRead(relayPin2);
if ( digitalRead(relayPin2) == LOW ) {
client.publish("/vld/vld-sonoff14S/relay2", "0", true);
} else {
client.publish("/vld/vld-sonoff14S/relay2", "1", true);
}
}
if ( oldstate3 != digitalRead(relayPin3)) {
oldstate3 = digitalRead(relayPin3);
if ( digitalRead(relayPin3) == LOW ) {
client.publish("/vld/vld-sonoff14S/relay3", "0", true);
} else {
client.publish("/vld/vld-sonoff14S/relay3", "1", true);
}
}
if ( oldstate4 != digitalRead(relayPin4)) {
oldstate4 = digitalRead(relayPin4);
if ( digitalRead(relayPin4) == LOW ) {
client.publish("/vld/vld-sonoff14S/relay4", "0", true);
} else {
client.publish("/vld/vld-sonoff14S/relay4", "1", true);
}
}
}