It's messy.... very messy.
Here's the code:
/*
Basic ESP8266 MQTT example
This sketch demonstrates the capabilities of the pubsub library in combination
with the ESP8266 board/library.
It connects to an MQTT server then:
- publishes "hello world" to the topic "outTopic" every two seconds
- subscribes to the topic "inTopic", printing out any messages
it receives. NB - it assumes the received payloads are strings not binary
- If the first character of the topic "inTopic" is an 1, switch ON the ESP Led,
else switch it off
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.
To install the ESP8266 board, (using Arduino 1.6.4+):
- Add the following 3rd party board manager under "File -> Preferences -> Additional Boards Manager URLs":
http://arduino.esp8266.com/stable/package_esp8266com_index.json
- Open the "Tools -> Board -> Board Manager" and click install for the ESP8266"
- Select your ESP8266 in "Tools -> Board"
*/
#include <RCSwitch.h>
RCSwitch mySwitch = RCSwitch();
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <Adafruit_NeoPixel.h>
int LED = 0;
int Transmitter = 2;
#define PIN 4
#define NUMPIXELS 1
#define ROW_SELECT 5
#define COLA 16
#define COLB 14
#define COLC 12
#define COLD 13
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
// Update these with values suitable for your network.
const char* ssid = "yourssid";
const char* password = "yourpw";
const char* mqtt_server = "ID of mqtt server";
const char* clientID = "remote433";
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
bool redAlert = false;
bool redAlert_toggle = true;
unsigned long redAlert_ms = 1000;
unsigned long time_earlier;
void setup() {
pixels.begin();
pinMode(ROW_SELECT, OUTPUT);
pinMode(COLA, INPUT_PULLDOWN_16);
pinMode(COLB, INPUT);
pinMode(COLC, INPUT);
pinMode(COLD, INPUT);
time_earlier = millis();
pinMode(LED, OUTPUT); // Initialize the pin as an output
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
pixels.setPixelColor(0, pixels.Color(0, 0, 255));
pixels.show();
mySwitch.enableTransmit(Transmitter); // Using Pin #10
// Hostname defaults to esp8266-[ChipID]
ArduinoOTA.setHostname(clientID);
// No authentication by default
ArduinoOTA.setPassword((const char *)"OTApasswd");
ArduinoOTA.onStart([]() {
Serial.println("Start");
});
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();
}
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)
{
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++)
{
Serial.print((char)payload[i]);
}
Serial.println();
if (strcmp(topic, "inTopic") == 0)
{
if ((char)payload[0] == 'A')
{
if ((char)payload[1] == '1')
{
mySwitch.switchOn("11000", "10000");
}
else
{
mySwitch.switchOff("11000", "10000");
}
}
if ((char)payload[0] == 'B')
{
if ((char)payload[1] == '1')
{
mySwitch.switchOn("11000", "01000");
}
else
{
mySwitch.switchOff("11000", "01000");
}
}
if ((char)payload[0] == 'C')
{
if ((char)payload[1] == '1')
{
mySwitch.switchOn("11000", "00100");
}
else
{
mySwitch.switchOff("11000", "00100");
}
}
if ((char)payload[0] == 'D')
{
if ((char)payload[1] == '1')
{
mySwitch.switchOn("11000", "00010");
}
else
{
mySwitch.switchOff("11000", "00010");
}
}
if ((char)payload[0] == 'E')
{
if ((char)payload[1] == '1')
{
mySwitch.switchOn("11000", "00001");
}
else
{
mySwitch.switchOff("11000", "00001");
}
}
}
if (strcmp(topic, "redAlert") == 0)
{
if ((char)payload[0] == '1')
{
redAlert = true;
}
else
{
redAlert = false;
}
delay(100);
}
}
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", "hello world");
// ... and resubscribe
client.subscribe("inTopic");
client.subscribe("redAlert");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void loop()
{
ArduinoOTA.handle();
if (!client.connected())
{
digitalWrite(LED, HIGH);
reconnect();
}
digitalWrite(LED, LOW);
client.loop();
checkRedAlert();
digitalWrite(ROW_SELECT, HIGH);
if (digitalRead(COLA) == HIGH)
{
digitalWrite(LED, HIGH);
mySwitch.switchOn("11000", "10000");
Serial.println("A1");
delay(250);
digitalWrite(LED, LOW);
}
if (digitalRead(COLB) == HIGH)
{
digitalWrite(LED, HIGH);
mySwitch.switchOn("11000", "01000");
Serial.println("B1");
delay(250);
digitalWrite(LED, LOW);
}
if (digitalRead(COLC) == HIGH)
{
digitalWrite(LED, HIGH);
mySwitch.switchOn("11000", "00100");
Serial.println("C1");
delay(250);
digitalWrite(LED, LOW);
}
if (digitalRead(COLD) == HIGH)
{
digitalWrite(LED, HIGH);
mySwitch.switchOn("11000", "00010");
Serial.println("D1");
delay(250);
digitalWrite(LED, LOW);
}
digitalWrite(ROW_SELECT, LOW);
if (digitalRead(COLA) == HIGH)
{
digitalWrite(LED, HIGH);
Serial.println("A0");
mySwitch.switchOff("11000", "10000");
delay(250);
digitalWrite(LED, LOW);
}
if (digitalRead(COLB) == HIGH)
{
digitalWrite(LED, HIGH);
Serial.println("B0");
mySwitch.switchOff("11000", "01000");
delay(250);
digitalWrite(LED, LOW);
}
if (digitalRead(COLC))
{
digitalWrite(LED, HIGH);
Serial.println("C0");
mySwitch.switchOff("11000", "00100");
delay(250);
digitalWrite(LED, LOW);
}
if (digitalRead(COLD))
{
digitalWrite(LED, HIGH);
Serial.println("D0");
mySwitch.switchOff("11000", "00010");
delay(250);
digitalWrite(LED, LOW);
}
}
void checkRedAlert()
{
if (redAlert)
{
unsigned long time_now = millis() - time_earlier;
if (time_now >= redAlert_ms)
{
if (redAlert_toggle)
{
pixels.setPixelColor(0, pixels.Color(255, 0, 0));
}
else
{
pixels.setPixelColor(0, pixels.Color(0, 0, 0));
}
pixels.show();
redAlert_toggle = !redAlert_toggle;
time_earlier = millis();
}
}
else
{
redAlert_toggle = true;
pixels.setPixelColor(0, pixels.Color(0, 0, 0));
pixels.show();
}
}