Relay with button
This code, which can be uploaded through the Arduino IDE, turns a relay[GPIO12](or led for that matter) on when a 1 is received trough MQTT on the inTopic (currently Sonoff1in), off when a 0 is received and switches when a 2 is received. When the button[GPIO0] is pressed it switches the current state and publishes the new state trough MQTT on the outTopic(currently Sonoff1out). The current state is always stored in EEPROM and restored on reboot.
/*
It connects to an MQTT server then:
- on 0 switches off relay
- on 1 switches on relay
- on 2 switches the state of the relay
- sends 0 on off relay
- sends 1 on on relay
It will reconnect to the server if the connection is lost using a blocking
reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to
achieve the same result without blocking the main loop.
The current state is stored in EEPROM and restored on bootup
*/
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <Bounce2.h>
#include <EEPROM.h>
const char* ssid = "YourSSID";
const char* password = "YourPASS";
const char* mqtt_server = "YourMQTTBroker'sIP";
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
const char* outTopic = "Sonoff1out";
const char* inTopic = "Sonoff1in";
int relay_pin = 12;
int button_pin = 0;
bool relayState = LOW;
// Instantiate a Bounce object :
Bounce debouncer = Bounce();
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
extButton();
for(int i = 0; i<500; i++){
extButton();
delay(1);
}
Serial.print(".");
}
digitalWrite(13, LOW);
delay(500);
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
delay(500);
digitalWrite(13, HIGH);
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
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();
// Switch on the LED if an 1 was received as first character
if ((char)payload[0] == '0') {
digitalWrite(relay_pin, LOW); // Turn the LED on (Note that LOW is the voltage level
Serial.println("relay_pin -> LOW");
relayState = LOW;
EEPROM.write(0, relayState); // Write state to EEPROM
EEPROM.commit();
} else if ((char)payload[0] == '1') {
digitalWrite(relay_pin, HIGH); // Turn the LED off by making the voltage HIGH
Serial.println("relay_pin -> HIGH");
relayState = HIGH;
EEPROM.write(0, relayState); // Write state to EEPROM
EEPROM.commit();
} else if ((char)payload[0] == '2') {
relayState = !relayState;
digitalWrite(relay_pin, relayState); // Turn the LED off by making the voltage HIGH
Serial.print("relay_pin -> switched to ");
Serial.println(relayState);
EEPROM.write(0, relayState); // Write state to EEPROM
EEPROM.commit();
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("ESP8266Client")) {
Serial.println("connected");
// Once connected, publish an announcement...
client.publish(outTopic, "Sonoff1 booted");
// ... and resubscribe
client.subscribe(inTopic);
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
for(int i = 0; i<5000; i++){
extButton();
delay(1);
}
}
}
}
void extButton() {
debouncer.update();
// Call code if Bounce fell (transition from HIGH to LOW) :
if ( debouncer.fell() ) {
Serial.println("Debouncer fell");
// Toggle relay state :
relayState = !relayState;
digitalWrite(relay_pin,relayState);
EEPROM.write(0, relayState); // Write state to EEPROM
if (relayState == 1){
client.publish(outTopic, "1");
}
else if (relayState == 0){
client.publish(outTopic, "0");
}
}
}
void setup() {
EEPROM.begin(512); // Begin eeprom to store on/off state
pinMode(relay_pin, OUTPUT); // Initialize the relay pin as an output
pinMode(button_pin, INPUT); // Initialize the relay pin as an output
pinMode(13, OUTPUT);
relayState = EEPROM.read(0);
digitalWrite(relay_pin,relayState);
debouncer.attach(button_pin); // Use the bounce2 library to debounce the built in button
debouncer.interval(50); // Input must be low for 50 ms
digitalWrite(13, LOW); // Blink to indicate setup
delay(500);
digitalWrite(13, HIGH);
delay(500);
Serial.begin(115200);
setup_wifi(); // Connect to wifi
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
extButton();
}
DHT22
Description
/*
MQTT DHT22, when "temperature, c" is sent it returns the temperature in celcius
when "humidity" is sent it returns the humidity as measured by the DHT22
the signal pin is connected to a pull-ip resistor and GPIO 2
*/
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <DHT.h>
#define DHTTYPE DHT22
#define DHTPIN 2
// Update these with values suitable for your network.
const char* ssid = "RS108";
const char* password = "martiomarti";
const char* mqtt_server = "192.168.178.135";
const char* clientID = "NodeMCUDevKit";
const char* outTopic = "NodeMCUout";
const char* inTopic = "NodeMCUin";
// Initialize DHT sensor
// NOTE: For working with a faster than ATmega328p 16 MHz Arduino chip, like an ESP8266,
// you need to increase the threshold for cycle counts considered a 1 or 0.
// You can do this by passing a 3rd parameter for this threshold. It's a bit
// of fiddling to find the right value, but in general the faster the CPU the
// higher the value. The default for a 16mhz AVR is a value of 6. For an
// Arduino Due that runs at 84mhz a value of 30 works.
// This is for the ESP8266 processor on ESP-01
DHT dht(DHTPIN, DHTTYPE, 11); // 11 works fine for ESP8266
float humidity, temp_c; // Values read from sensor
// Generally, you should use "unsigned long" for variables that hold time
unsigned long previousMillis = 0; // will store last temp was read
const long interval = 2000; // interval at which to read sensor
WiFiClient espClient;
PubSubClient client(espClient);
char msg[50];
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
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());
}
void callback(char* topic, byte* payload, unsigned int length) {
// Conver the incoming byte array to a string
payload[length] = '\0'; // Null terminator used to terminate the char array
String message = (char*)payload;
Serial.print("Message arrived on topic: [");
Serial.print(topic);
Serial.print("], ");
Serial.println(message);
if(message == "temperature, c"){
gettemperature();
Serial.print("Sending temperature:");
Serial.println(temp_c);
dtostrf(temp_c , 2, 2, msg);
client.publish(outTopic, msg);
} else if (message == "humidity"){
gettemperature();
Serial.print("Sending humidity:");
Serial.println(humidity);
dtostrf(humidity , 2, 2, msg);
client.publish(outTopic, msg);
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect(clientID)) {
Serial.println("connected");
// Once connected, publish an announcement...
client.publish(outTopic, clientID);
// ... and resubscribe
client.subscribe(inTopic);
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup() {
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
dht.begin(); // initialize temperature sensor
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
}
void gettemperature() {
// Wait at least 2 seconds seconds between measurements.
// if the difference between the current time and last time you read
// the sensor is bigger than the interval you set, read the sensor
// Works better than delay for things happening elsewhere also
unsigned long currentMillis = millis();
if(currentMillis - previousMillis >= interval) {
// save the last time you read the sensor
previousMillis = currentMillis;
// Reading temperature for humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (it's a very slow sensor)
humidity = dht.readHumidity(); // Read humidity (percent)
temp_c = dht.readTemperature(); // Read temperature as Celcius
// Check if any reads failed and exit early (to try again).
if (isnan(humidity) || isnan(temp_c)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
}
}
All of the above examples are used in combination with NodeRED and Mosquitto, see the linked thread for more info on that