Skip to content

Instantly share code, notes, and snippets.

@rmeertens
Created November 18, 2016 19:25
Show Gist options
  • Save rmeertens/7f1749eb8e8fe13a545e7e50011f3b8c to your computer and use it in GitHub Desktop.
Save rmeertens/7f1749eb8e8fe13a545e7e50011f3b8c to your computer and use it in GitHub Desktop.
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <ESP8266HTTPClient.h>
// The WIFI SSID and password you connect to
const char* ssid = "XXXXX";
const char* password = "XXXX";
// We want to have a webserver at port 80,
// as this is the port you server websites on
ESP8266WebServer server(80);
// We create 3 states for the bed: on, off, and motion triggered
// later we will make a page where you can set the state
typedef enum {ALWAYS_ON, ALWAYS_OFF, MOTION_TRIGGERED} BED_STATE;
BED_STATE bed_state = MOTION_TRIGGERED;
// pin definitions.
// the GPIO pins can be found here:
// https://pradeepsinghblog.files.wordpress.com/2016/04/nodemcu_pins.png?w=616
int led_red = 5; // the pin that the LED is attached to
int led_green = 4; // the pin that the LED is attached to
int led_blue = 0; // the pin that the LED is attached to
int motion_sensor = 16; // the pin that the LED is attached to
// global (initial) values
int brightness_blue = 50; // how bright the LED is
int brightness_red = 50; // how bright the LED is
int brightness_green = 50; // how bright the LED is
int motion_sensor_state = 0;
/**
Text the user sees when navigating to your "website"
We give a small explanation about what they can do.
*/
void handleRoot() {
server.send(200, "text/plain", "hello from my intelligent bed. Call /setlight?blue=40&red=20&green=100 to set the light to a different color");
}
/**
Text user sees when navigating to a random page.
*/
void handleNotFound() {
server.send(404, "text/plain", "this page is not found, navigate to / to find some help");
}
/**
Gets the arguments in the URL and sets the lights to these arguments
*/
void userSetLight() {
brightness_blue = server.arg("blue").toInt();
brightness_red = server.arg("red").toInt();
brightness_green = server.arg("green").toInt();
server.send(200, "text/plain", "Done setting the lights");
}
/**
Changes the state of the bed based on the
state value the user passes
0 = ON
1 = OFF
2 = MOTION TRIGGERED
*/
void userSetState() {
int new_state = server.arg("state").toInt();
switch (new_state) {
case 0:
bed_state = ALWAYS_ON;
break;
case 1:
bed_state = ALWAYS_OFF;
break;
case 2:
default:
bed_state = MOTION_TRIGGERED;
break;
}
server.send(200, "text/plain", "Done setting the state");
}
void setup(void) {
// Set the pins to the transistors to output
pinMode(led_green, OUTPUT);
pinMode(led_blue, OUTPUT);
pinMode(led_red, OUTPUT);
// Start connecting to your local router
WiFi.begin(ssid, password);
// begin print function
Serial.begin(115200);
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// Print that we are connected and our IP.
// Note that writing down the IP helps us later when setting the lights
// and the IP is a variable in the app we will make in part 3.
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
if (MDNS.begin("bedserver")) {
Serial.println("MDNS responder started");
}
// define what the user sees when navigation to the
// ip adress/XX
server.on("/", handleRoot);
server.on("/setlight", userSetLight);
server.on("/setstate", userSetState);
server.onNotFound(handleNotFound);
// start the server!
server.begin();
Serial.println("HTTP server started");
}
/** Sets the frequency the pins connected to the transistors oscillate at **/
void set_lights(int new_blue, int new_green, int new_red) {
analogWrite(led_blue, new_blue);
analogWrite(led_green, new_green);
analogWrite(led_red, new_red);
}
void loop(void) {
server.handleClient();
// get if the motion sensor detected something
motion_sensor_state = digitalRead(motion_sensor);
// for each case: set the lights as dictated
switch (bed_state) {
case MOTION_TRIGGERED:
if (motion_sensor_state) {
set_lights(brightness_blue, brightness_red, brightness_green);
}
else {
set_lights(0, 0, 0);
}
break;
case ALWAYS_ON:
set_lights(brightness_blue, brightness_red, brightness_green);
break;
case ALWAYS_OFF:
set_lights(0, 0, 0);
break;
default:
set_lights(0, 0, 0);
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment