Skip to content

Instantly share code, notes, and snippets.

@emorydunn
Created December 2, 2018 00:45
Show Gist options
  • Save emorydunn/51ca944db5efd395f00ef4770368f233 to your computer and use it in GitHub Desktop.
Save emorydunn/51ca944db5efd395f00ef4770368f233 to your computer and use it in GitHub Desktop.
Use a rotary phone dial connected to an ESP8266 to send MQTT messages.
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#define wifi_ssid "## SSID ##"
#define wifi_password "## PASSWORD ##"
#define mqtt_server "## SERVER IP ##"
//#define mqtt_user ""
//#define mqtt_password ""
#define rotaryTopic "## MQTT TOPIC ##"
// Comunication
WiFiClient espClient;
PubSubClient client(espClient);
// constants won't change. They're used here to set pin numbers:
const int controlPin = 14; // Control, white, D5
const int rotaryPin = 12; // Rotary, blue, D6
const int ledPin = 2; // the number of the LED pin, D4
// variables will change:
int rotaryState = 0;
int lastRotaryState = 0;
int controlState = 0;
int rotaryCount = 0;
void setup() {
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
Serial.println("MQTT Rotary Phone");
// put your setup code here, to run once:
pinMode(controlPin, INPUT_PULLUP);
pinMode(rotaryPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
}
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(wifi_ssid);
WiFi.begin(wifi_ssid, wifi_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 reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
// If you do not want to use a username and password, change next line to
// if (client.connect("ESP8266Client")) {
if (client.connect("ESP8266Client")) {
Serial.println("connected");
} 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() {
// put your main code here, to run repeatedly:
controlState = digitalRead(controlPin);
rotaryState = digitalRead(rotaryPin);
if (!client.connected()) {
reconnect();
}
client.loop();
// Serial.println("rotary button changed");
if (controlState == LOW) {
// Control pin is open, begin counting
// Serial.println("Control open");
if (rotaryState == LOW && rotaryState != lastRotaryState) {
rotaryCount ++;
// Serial.println("Rotary open. Count");Serial.println(rotaryCount);
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
// Delay a little bit to avoid bouncing
delay(1);
} else {
if (rotaryCount != 0) {
Serial.println("Control closed. Count"); Serial.println(rotaryCount);
bool success = client.publish(rotaryTopic, String(rotaryCount).c_str());
Serial.println("Sent message to MQTT: ");
Serial.println(success);
rotaryCount = 0;
}
}
lastRotaryState = rotaryState;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment