Skip to content

Instantly share code, notes, and snippets.

@andycb
Last active January 31, 2020 22:30
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 andycb/a868708b17d2b72771662087c0b3ca37 to your computer and use it in GitHub Desktop.
Save andycb/a868708b17d2b72771662087c0b3ca37 to your computer and use it in GitHub Desktop.
Arduino Sketch Used In My Smart Heating
#include "config.h"
#include <Stepper.h>
#include "AdafruitIO_WiFi.h"
#define IO_USERNAME ""
#define IO_KEY ""
#define WIFI_SSID ""
#define WIFI_PASS ""
#define TOTAL_STEPS 200 // The total number of setps the motor has
#define TOGGLE_STEPS 29 // The number of steps to move each time the heating is toggled
AdafruitIO_WiFi io(IO_USERNAME, IO_KEY, WIFI_SSID, WIFI_PASS);
Stepper stepper(TOTAL_STEPS, 0, 15, 12, 14);
AdafruitIO_Feed* heatingFeed = io.feed("heating");
// Tracks the current on/off state of the heating
bool currentState = false;
// Indicates that the system has booted and recived data from the feed for the first time
bool hasBooted = false;
void setup()
{
// Set the outputs for the lights
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
// Create a serial connection for debugging
Serial.begin(115200);
while(! Serial);
stepper.setSpeed(21);
io.connect();
// Attach handler for when new data is added to the feed
heatingFeed->onMessage(handleNewData);
// Wait for the network to connect
while(io.mqttStatus() < AIO_CONNECTED)
{
Serial.print(".");
delay(500);
}
// Request the most recent value from the feed
heatingFeed->get();
}
void loop()
{
io.run();
}
void handleNewData(AdafruitIO_Data* data)
{
Serial.print("New Data: ");
Serial.println(data->toBool());
bool isOn = data->toBool();
if (isOn)
{
digitalWrite(4, 1);
digitalWrite(5, 0);
}
else
{
digitalWrite(4, 0);
digitalWrite(5, 1);
}
// The first time the device is booted, get the current value from the feed
// but do not move the motor. This is used to sync our status with the feed
if (hasBooted && isOn != currentState)
{
// Disable software watchdog
ESP.wdtDisable();
// Move motor
stepper.step(TOGGLE_STEPS);
// Re-enable watchdog
ESP.wdtEnable(1000);
// Power off the motor
digitalWrite(0, 0);
digitalWrite(15, 0);
digitalWrite(12, 0);
digitalWrite(14, 0);
}
currentState = isOn;
hasBooted = true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment