Skip to content

Instantly share code, notes, and snippets.

@tylergets
Created November 26, 2018 23:01
Show Gist options
  • Save tylergets/c6802bdf3f11a9debab8db18d90194bf to your computer and use it in GitHub Desktop.
Save tylergets/c6802bdf3f11a9debab8db18d90194bf to your computer and use it in GitHub Desktop.
Arduino code for wifi air freshner
gith#include <Button.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
int motorPin = 14;
int buttonPin = 4;
static unsigned char motorState = LOW;
static unsigned long motorStartTime = 0;
static unsigned long motorTime = 800;
const char* ssid = "ssid";
const char* password = "password";
ESP8266WebServer server(80);
Button button = Button(buttonPin);
String request;
void setup()
{
pinMode(motorPin, OUTPUT);
button.begin();
digitalWrite(motorPin, LOW);
Serial.begin(9600);
while (! Serial);
Serial.println("ESP8266 Air Freshner");
Serial.print("[WIFI] Connecting");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("[WIFI] Connected");
Serial.print("[WIFI] IP: ");
Serial.println(WiFi.localIP());
server.on("/", []() {
server.send(200, "text/plain", "hello from air freshner!");
});
server.on("/spray", []() {
spray();
server.send(200, "text/plain", "sprayed");
});
server.begin();
Serial.println("[HTTP] Started");
}
void spray() {
if(motorState == LOW)
{
digitalWrite(motorPin,HIGH);
motorState = HIGH;
motorStartTime = millis();
Serial.println("[SPRAY] On");
}
}
void checkSpray() {
if(motorState == HIGH)
{
if(millis()-motorStartTime > motorTime)
{
digitalWrite(motorPin,LOW);
motorState = LOW;
Serial.println("[SPRAY] Off");
}
}
}
void loop()
{
checkSpray();
server.handleClient();
if(button.pressed()) {
Serial.println("[BUTTON] Pressed");
spray();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment