Last active
November 28, 2020 02:54
-
-
Save jpwsutton/ed57c692fc1d2e94af61967f7882013e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Muji Cuckoo Clock MQTT Controller V1.0 | |
James Sutton - 2017 | |
jsutton.co.uk | |
To trigger the cuckoo animation, publish an mqtt message with the payload of '1' to the topic | |
described in the settings below. | |
Use board "WeMos D1 R2 & mini" | |
CPU 160MHz | |
4M (3M SPIFFS) | |
upload speed 115200 | |
*/ | |
#include <ESP8266WiFi.h> | |
#include <PubSubClient.h> | |
// remember to change MQTT_KEEPALIVE to 60 in the header file ~/Arduino/libraries/PubSubClient/src | |
///////////////////////////////////////////////////////////////////////////////////////////// | |
// Wireless Network Settings | |
const char* wifi_ssid = "SSID"; | |
const char* wifi_password = "PASSWORD"; | |
// MQTT Settings | |
#define CLIENTID "cuckooclock" | |
#define COMMAND_TOPIC "cuckoo" | |
#define BROKER "BROKER_URI" | |
#define PASSWORD "BROKER_PASS" | |
#define USER "BROKER_USER" | |
#define STATE_TOPIC "cuckoo/state" | |
#define WILL_QOS 1 | |
#define WILL_RETAIN 1 | |
#define WILL_MESSAGE "offline" | |
#define ONLINE_MSG "online" | |
///////////////////////////////////////////////////////////////////////////////////////////// | |
WiFiClient espClient; | |
PubSubClient client(espClient); | |
int motor_forwards = D1; | |
int motor_back = D2; | |
int limit_switch = D0; | |
int limitSwitchState = 0; | |
void setup() { | |
// Initialise pins as outputs | |
pinMode(motor_forwards, OUTPUT); | |
pinMode(motor_back, OUTPUT); | |
pinMode(limit_switch, INPUT); | |
pinMode(LED_BUILTIN, OUTPUT); | |
Serial.begin(9600); | |
setup_wifi(); | |
client.setServer(BROKER, 1883); | |
client.setCallback(callback); | |
} | |
void setup_wifi() { | |
// connecting to wifi | |
// We start by connecting to a WiFi network | |
Serial.println(); | |
Serial.print("Connecting to "); | |
Serial.println(wifi_ssid); | |
WiFi.mode(WIFI_STA); | |
WiFi.begin(wifi_ssid, wifi_password); | |
wait_for_wifi(); | |
} | |
void callback(char* topic, byte* payload, unsigned int length) { | |
char content[10]; | |
int count; | |
char count_str[3]; | |
strncpy(content, (char *)payload, length); | |
content[length] = '\0'; | |
uint32_t value = strtol(content, 0, 10); | |
// If the Value is 1, then tweet. | |
if (value == 1) { | |
tweet(); | |
} | |
} | |
void wait_for_wifi() | |
{ | |
Serial.println("waiting for Wifi"); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(1000); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.println("WiFi connected"); | |
} | |
void reconnect() { | |
boolean first = true; | |
// Loop until we're reconnected to the broker | |
while (!client.connected()) { | |
if (WiFi.status() != WL_CONNECTED) { | |
wait_for_wifi(); | |
first = true; | |
} | |
Serial.print("Attempting MQTT connection..."); | |
if (first) { | |
// now we're on wifi, show connecting to MQTT colour | |
first = false; | |
} | |
// Attempt to connect | |
if (client.connect(CLIENTID, USER, PASSWORD, STATE_TOPIC, WILL_QOS, WILL_RETAIN, WILL_MESSAGE)) { | |
Serial.println("connected"); | |
client.publish(STATE_TOPIC,ONLINE_MSG); | |
} | |
else { | |
Serial.print("failed, rc="); | |
Serial.print(client.state()); | |
Serial.println(" try again in 5 seconds"); | |
flashLed(3,250); | |
delay(5000); | |
} | |
} | |
// subscribe to the command topic | |
client.subscribe(COMMAND_TOPIC); | |
} | |
void loop() { | |
int reading; | |
if (!client.connected()) { | |
reconnect(); | |
} | |
client.loop(); | |
} | |
void tweet() { | |
Serial.println("Tweeting!"); | |
// Turn the LED on | |
digitalWrite(LED_BUILTIN, LOW); | |
// Start the motor going forwards | |
digitalWrite(motor_forwards, HIGH); | |
// Listen for cycle complete | |
delay(750); | |
limitSwitchState = digitalRead(limit_switch); | |
Serial.println(limitSwitchState); | |
while (limitSwitchState == HIGH) { | |
limitSwitchState = digitalRead(limit_switch); | |
delay(500); | |
} | |
// Stop the motor going forwards | |
digitalWrite(motor_forwards, LOW); | |
// Wait a second | |
delay(1000); | |
// Start the motor going back | |
digitalWrite(motor_back, HIGH); | |
delay(500); | |
// stop the motor going back | |
digitalWrite(motor_back, LOW); | |
// Turn the LED off | |
digitalWrite(LED_BUILTIN, HIGH); | |
} | |
void flashLed(int count, int del){ | |
for (int x = 0; x < count; x++){ | |
// Turn the LED on | |
digitalWrite(LED_BUILTIN, LOW); | |
delay(del); | |
// Turn the LED off | |
digitalWrite(LED_BUILTIN, HIGH); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment