Skip to content

Instantly share code, notes, and snippets.

@aaronbieber
Created March 20, 2021 19:31
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 aaronbieber/58073db57bd61a970dcf51e970c9eba4 to your computer and use it in GitHub Desktop.
Save aaronbieber/58073db57bd61a970dcf51e970c9eba4 to your computer and use it in GitHub Desktop.
#include <Servo.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
const int servo_pin = D6;
const char* ssid = "<your WiFi SSID>";
const char* password = "<your WiFi password>";
const char* mqtt_server = "<your MQTT server name or IP>";
const char* mqtt_client_name = "hue_bar_base_1";
const char* mqtt_topic_pub = "lightbar/1/stat";
const char* mqtt_topic_sub = "lightbar/1/cmd";
int pos = 90;
Servo myservo;
WiFiClient espClient;
PubSubClient client(espClient);
void moveLight(int distance) {
Serial.println("Instructed to move " + String(distance));
int incPos;
int newPos = pos + distance;
moveLightTo(newPos);
}
void moveLightTo(int newPos) {
int incPos;
if (newPos < 0) {
newPos = 0;
}
if (newPos > 180) {
newPos = 180;
}
Serial.println("Currently at " + String(pos) + ", will move to " + String(newPos));
if (newPos > pos) {
Serial.println("Incrementing toward " + String(newPos));
for (incPos = pos; incPos <= newPos; incPos += 1) {
myservo.write(incPos);
delay(15);
}
pos = newPos;
return;
}
if (newPos < pos) {
Serial.println("Decrementing toward " + String(newPos));
for (incPos = pos; incPos >= newPos; incPos -= 1) {
myservo.write(incPos);
delay(15);
}
pos = newPos;
return;
}
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
char command[length+1];
command[length] = '\0';
for (int i = 0; i < length; i++) {
command[i] = (char)payload[i];
}
Serial.println(command);
if(strcmp(command, "left") == 0) {
Serial.println("Moving left");
client.publish(mqtt_topic_pub, "Moving left");
moveLight(10);
}
if(strcmp(command, "right") == 0) {
Serial.println("Moving right");
client.publish(mqtt_topic_pub, "Moving right");
moveLight(-10);
}
int newPos;
if(strcmp(command, "0") == 0) { // handle a legit 0 specially, see below
Serial.println("Moving to 0.");
client.publish(mqtt_topic_pub, "Moving to 0");
moveLightTo(0);
} else {
newPos = atoi(command);
if (newPos != 0) { // aoti() returns 0 on conversion error (pff)
Serial.println("Moving to " + newPos);
client.publish(mqtt_topic_pub, "Moving to " + newPos);
moveLightTo(newPos);
}
}
}
void mqttConnect() {
while (!client.connected()) {
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
Serial.print("Connecting to ");
Serial.print(mqtt_server);
Serial.print(" as ");
Serial.print(mqtt_client_name);
Serial.print("... ");
if (client.connect(mqtt_client_name)) {
Serial.println("connected");
char connStr[128];
sprintf(connStr, "%s connected from %s", mqtt_client_name, WiFi.localIP().toString().c_str());
client.publish(mqtt_topic_pub, connStr);
client.subscribe(mqtt_topic_sub);
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void wifiConnect() {
if (WiFi.status() != WL_CONNECTED) {
delay(10);
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 setup() {
Serial.begin(115200);
myservo.attach(servo_pin);
myservo.write(pos);
}
void loop() {
wifiConnect();
mqttConnect();
client.loop();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment