Last active
November 24, 2017 14:26
-
-
Save MarkusAmshove/69237448781b1b0dcd604fc35eb98bf4 to your computer and use it in GitHub Desktop.
Building a cheap WiFi power switch
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
#include <ESP8266WiFi.h> | |
#include <aREST.h> | |
// Configuration for each SonOFF | |
#define DEVICE_NAME "My Printer" | |
IPAddress ip(192, 168, 0, 100); | |
IPAddress gateway(192, 168, 0, 1); | |
IPAddress subnet(255, 255, 255, 0); | |
const char* ssid = "WLAN_SSID"; | |
const char* password = "WLAN_PASSWORD"; | |
#define LISTEN_PORT 80 | |
#define STATE_ON 1 | |
#define STATE_OFF 0 | |
#define GPIO_LED 13 | |
#define GPIO_RELAY 12 | |
#define GPIO_BUTTON 0 | |
// Global variables | |
aREST rest = aREST(); | |
WiFiServer server(LISTEN_PORT); | |
int currentPowerState; // remember our current state | |
// The function exposed to the API to turn the power on and off | |
int powerControl(String command); | |
void setup(void) | |
{ | |
Serial.begin(115200); | |
Serial.println("Setting up pins"); | |
pinMode(GPIO_LED, OUTPUT); | |
pinMode(GPIO_RELAY, OUTPUT); | |
pinMode(GPIO_BUTTON, INPUT); | |
// set the power relay initialy to off | |
digitalWrite(GPIO_RELAY, LOW); | |
currentPowerState = STATE_OFF; | |
// declare our function exposed to the API | |
rest.function("power", powerControl); | |
rest.set_id("1"); | |
rest.set_name(DEVICE_NAME); | |
// set up our static IP | |
WiFi.config(ip, gateway, subnet); | |
// Connect to WiFi | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.println("WiFi connected"); | |
// Start the WiFi server which listens to port 80 | |
server.begin(); | |
Serial.println("Server started"); | |
// Print our IP address | |
Serial.println(WiFi.localIP()); | |
// We're ready, so flash a bit! | |
digitalWrite(GPIO_LED, LOW); | |
delay(1); | |
digitalWrite(GPIO_LED, HIGH); | |
delay(1); | |
digitalWrite(GPIO_LED, LOW); | |
delay(1); | |
digitalWrite(GPIO_LED, HIGH); | |
} | |
// Another global variable to not toggle the state when button is hold down | |
bool needsHandle = true; | |
void loop() { | |
int newButtonState = digitalRead(GPIO_BUTTON); | |
// Button pressed | |
if (newButtonState == LOW && needsHandle) { | |
needsHandle = false; | |
Serial.println(newButtonState); | |
toggleState(); | |
} | |
// Button released | |
if (newButtonState == HIGH && !needsHandle) { | |
needsHandle = true; | |
} | |
WiFiClient client = server.available(); | |
if (!client) { | |
return; | |
} | |
while (!client.available()) { | |
delay(1); | |
} | |
rest.handle(client); | |
} | |
int powerControl(String command) { | |
if (command.length() == 0) { | |
return currentPowerState; | |
} | |
// Sanitize input, aREST doesn't remove the = | |
command.replace("=", ""); | |
int newState = command.toInt(); | |
if (newState == currentPowerState) { | |
return currentPowerState; | |
} | |
if (newState == STATE_ON) { | |
powerOn(); | |
} | |
if (newState == STATE_OFF) { | |
powerOff(); | |
} | |
return currentPowerState; | |
} | |
void powerOn() { | |
digitalWrite(GPIO_LED, LOW); | |
digitalWrite(GPIO_RELAY, HIGH); | |
currentPowerState = STATE_ON; | |
} | |
void powerOff() { | |
digitalWrite(GPIO_LED, HIGH); | |
digitalWrite(GPIO_RELAY, LOW); | |
currentPowerState = STATE_OFF; | |
} | |
void toggleState() { | |
if (currentPowerState == STATE_ON) { | |
powerOff(); | |
} | |
else if (currentPowerState == STATE_OFF) { | |
powerOn(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment