Skip to content

Instantly share code, notes, and snippets.

@matfra
Created December 21, 2020 20:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matfra/f2ba06ba29c26f86d49128378c09807b to your computer and use it in GitHub Desktop.
Save matfra/f2ba06ba29c26f86d49128378c09807b to your computer and use it in GitHub Desktop.
ESP8266 MQTT keypad
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <ArduinoOTA.h>
#include <PubSubClient.h>
const char* ssid = "yourwifiname";//put your wifi ssid here
const char* password = "yourwifipass";//put your wifi password here.
const char* mqtt_topic = "keypad";
const char* mqtt_server = "mqttserverhostname.example.com";
const char* HOSTNAME = "keypad";
const int pins[9] = {D0,D1,D2,A0,10,D5,D6,D7,D8};
int keepAlive = 0;
WiFiClient espClient;
PubSubClient client(espClient);
unsigned long lastMsg = 0;
#define MSG_BUFFER_SIZE (50)
char msg[MSG_BUFFER_SIZE];
int value = 0;
void setup_wifi() {
delay(10);
WiFi.mode(WIFI_STA);
// Advertise the hostname to the DHCP server
WiFi.hostname(HOSTNAME);
WiFi.begin(ssid, password);
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("Connection Failed! Rebooting...");
delay(5000);
ESP.restart();
}
//Setup OTA
ArduinoOTA.setHostname(HOSTNAME);
// No authentication by default
// ArduinoOTA.setPassword((const char *)"123");
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();
// Print local IP infos
Serial.println("Ready");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void setup() {
// Setup Serial
Serial.begin(115200);
Serial.println("Booting");
// Setup WiFi
setup_wifi();
// Initilize MQTT specific things
client.setServer(mqtt_server, 1883);
for (int i = 0; i <= 8; i++) {
Serial.print("Now enabling pin ");
Serial.println(pins[i]);
pinMode(pins[i],INPUT);
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Create a random client ID
String clientId = HOSTNAME;
clientId += "-";
clientId += String(random(0xffff), HEX);
// Attempt to connect
if (client.connect(clientId.c_str())) {
Serial.println("connected");
// Once connected, publish an announcement...
client.subscribe(mqtt_topic);
} 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() {
if (!client.connected()) {
reconnect();
}
client.loop();
for (int i = 0; i <= 8; i++) {
int readout;
if (pins[i] == A0) {
if (analogRead(pins[i]) > 512) {
readout = 1;
} else {
readout = 0;
}
} else {
readout = digitalRead(pins[i]);
}
if ( readout == 1) {
char *message = "0";
message[0] += i;
Serial.println(message);
client.publish(mqtt_topic, message);
}
}
delay(200);
keepAlive++;
if ( keepAlive == 20 ) {
client.publish(mqtt_topic, mqtt_topic);
Serial.println("Sending keepalive");
keepAlive = 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment