Skip to content

Instantly share code, notes, and snippets.

@terryspitz
Created February 9, 2020 16:44
Show Gist options
  • Save terryspitz/19991109d831c9473b2ec3f6942f5985 to your computer and use it in GitHub Desktop.
Save terryspitz/19991109d831c9473b2ec3f6942f5985 to your computer and use it in GitHub Desktop.
ESP8266 with motor shield for controlling blinds via Blynk or Google Assistant via IFTTT
#include "WEMOS_Motor.h"
#include <ESP8266WiFi.h>
#include <DNSServer.h> //Local DNS Server used for redirecting all requests to the configuration portal
#include <ESP8266WebServer.h> //Local WebServer used to serve the configuration portal
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager allow phone to configure device wifi connection
//#include <ESP8266HTTPClient.h> //doesn't support https easily, so use WiFiClientSecure instead
#include <WiFiClientSecure.h>
#include <EEPROM.h>
#include <BlynkSimpleEsp8266.h>
#include <Blynk/BlynkParam.h>
// Auth Token for Blynk App.
char blynkAuth[] = "jWe4uyUQK72fg3XLZZVVBoT3KYdlat4x";
//Motor shiled I2C Address: 0x30 PWM frequency: 1000Hz(1kHz)
Motor M1(0x30, _MOTOR_A, 1000);
int openSecs = 0;
void setup() {
Serial.begin(115200);
while (!Serial) {}
WiFiManager wifiManager;
wifiManager.setTimeout(180); //in seconds
//wifiManager.resetSettings(); //for testing
//fetches configured ssid and password and tries to connect
if (!wifiManager.autoConnect("Blinds")) {
Serial.println("Failed WiFi connect, Restarting...");
delay(3000);
//reset and try again
ESP.reset();
delay(5000);
}
Serial.println("WiFi Connected: " + WiFi.SSID());
Serial.println("Connect to Blynk...");
Blynk.config(blynkAuth);
if (!Blynk.connect())
{
Serial.println("Failed Blynk connect,Restarting...");
//reset and try again
ESP.reset();
delay(5000);
}
Serial.println("Connected to Blynk");
EEPROM.begin(4);
EEPROM.get(0, openSecs);
Serial.println("Set openSecs to: " + String(openSecs));
}
unsigned long motorStart = 0;
unsigned long motorStop = 0;
uint8_t openPowerPercent = 80;
uint8_t closePowerPercent = 60;
//STOP
BLYNK_WRITE(V0)
{
M1.setmotor(_STOP);
}
//Open, plus hold to program time
BLYNK_WRITE(V1)
{
int value = param.asInt();
Serial.println("Write V1: " + String(value));
if (value == 1) {
motorStart = millis();
M1.setmotor(_CCW, openPowerPercent);
}
else if (value == 0) {
motorStop = motorStart + openSecs*1000;
Serial.println("open for " + String(openSecs*1000));
if (millis() - motorStart > 5000) {
openSecs = (millis() - motorStart)/1000;
Serial.println("Set openSecs to: " + String(openSecs));
//record time to open
EEPROM.put(0, openSecs);
EEPROM.commit(); //see http://esp8266.github.io/Arduino/versions/2.0.0/doc/libraries.html
}
}
}
// Half Open
BLYNK_WRITE(V2)
{
int value = param.asInt();
Serial.println("Write V2: " + String(value));
if (value == 1) {
M1.setmotor(_CCW, openPowerPercent);
motorStop = millis() + openSecs*1000/2;
Serial.println("half open for " + String(openSecs*1000/2));
}
}
// Half Close
BLYNK_WRITE(V3)
{
int value = param.asInt();
Serial.println("Write V3: " + String(value));
if (value == 1) {
M1.setmotor(_CW, closePowerPercent);
motorStop = millis() + openSecs*1000/2;
Serial.println("close for " + String(openSecs*1000/2));
}
}
// Close
BLYNK_WRITE(V4)
{
int value = param.asInt();
Serial.println("Write V4: " + String(value));
if (value == 1) {
M1.setmotor(_CW, closePowerPercent);
motorStop = millis() + openSecs*1000;
Serial.println("close for " + String(openSecs*1000));
}
}
void loop()
{
Blynk.run();
if (motorStop>0 && millis() > motorStop) {
Serial.println("Stop");
M1.setmotor(_STOP);
M1.setmotor(_STANDBY);
motorStop = 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment