Jeremy Cook
Published

Smartphone Controlled Jack-O-Lantern

Control lights on this pumpkin's eyes and mouth using a smartphone or other connected device.

IntermediateFull instructions provided4 hours1,181
Smartphone Controlled Jack-O-Lantern

Things used in this project

Hardware components

Wemos D1 Mini
Espressif Wemos D1 Mini
×1
LED (generic)
LED (generic)
×1

Story

Read more

Code

Wemos-Pumpkin-demo.ino

Arduino
//By Jeremy S. Cook - controls a few LEDs meant for a Pumpkin
//
//based on relay demo by Innovative Tom: 
//https://github.com/Innovativetom/InnovativeTomDemo-Wemos-Relay/blob/master/InnovativeTomDemo.txt
//
//That code in turn was resued from the example on the ESP8266 Learning Webpage below: 
//http://www.esp8266learning.com/wemos-webserver-example.php

//CODE START 

#include <ESP8266WiFi.h>

// Below you will need to use your own WIFI information.

const char* ssid = "XXXYYY"; //WIFI Name, WeMo will only connect to a 2.4GHz network.
const char* password = "ABC123"; //WIFI Password

//defining the pins and setting up the "server"3

int Light1 = D1; // System uses pin 1 for first LED
int Light2 = D2; // System uses pin 2 for second LED

WiFiServer server(80);
IPAddress ip(10, 0, 0, 99); // where xx is the desired IP Address
IPAddress gateway(10, 0, 0, 1); // set gateway to match your network
IPAddress subnet(255, 255, 255, 0); // set subnet mask to match your network


// void setup is where we initialize variables, pin modes, start using libraries, etc. 
//The setup function will only run once, after each powerup or reset of the wemos board.

void setup() {
  Serial.begin(9600);
  delay(10);
 

  pinMode(Light1, OUTPUT);
  pinMode(Light2, OUTPUT);
  pinMode(BUILTIN_LED, OUTPUT);
  
  digitalWrite(Light1, LOW);
  digitalWrite(Light2, LOW);
  digitalWrite(BUILTIN_LED, HIGH); //note that BUILTIN_LED is output low
 
  Serial.print(F("Setting static ip to : "));
  Serial.println(ip);
  
  // Connect to WiFi network
  
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.config(ip, gateway, subnet); 
  WiFi.begin(ssid, password);
  //Trying to connect it will display dots
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
 
  // Start the server
  server.begin();
  Serial.println("Server started");
 
  // Print the IP address
  Serial.print("Use this URL : ");
  Serial.print("http://");
  Serial.print(WiFi.localIP());
  Serial.println("/"); 
}

//void loop is where you put all your code. it is a funtion that returns nothing and will repeat over and over again

void loop() {
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
 
  // Wait until the client sends some data
  Serial.println("new client");
  while(!client.available()){
    delay(1);
  }
 
  // Read the first line of the request
  String request = client.readStringUntil('\r');
  Serial.println(request);
  client.flush();
 
  //Match the request, checking to see what the currect state is
  int value = LOW;
  
  if (request.indexOf("/RTEYE=ON") != -1) {
    digitalWrite(Light1, HIGH);
  } 
  if (request.indexOf("/RTEYE=OFF") != -1){
    digitalWrite(Light1, LOW);
  }
  if (request.indexOf("/LTEYE=ON") != -1) {
    digitalWrite(Light2, HIGH);
  }
  if (request.indexOf("/LTEYE=OFF") != -1){
    digitalWrite(Light2, LOW);
  }
  if (request.indexOf("/MOUTH=ON") != -1) {
    digitalWrite(BUILTIN_LED, LOW);
  }
  if (request.indexOf("/MOUTH=OFF") != -1){
    digitalWrite(BUILTIN_LED, HIGH);
  }
  
  //Build the html page
  
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println(""); //  do not forget this one
  client.println("<!DOCTYPE HTML>");
  client.println("<html>");
 
  client.println("<a href=\"/RTEYE=ON\"><button><font size=7>Click here to Turn ON RT EYE<br><br></button></a> <br><br>");
  client.println("<a href=\"/RTEYE=OFF\"><button><font size=7>Click here to turn OFF RT EYE.<br><br></button></a><br><br>");
  client.println("<a href=\"/LTEYE=ON\"><button><font size=7>Click here to Turn ON LT EYE.<br><br></button></a><br><br>");
  client.println("<a href=\"/LTEYE=OFF\"><button><font size=7>Click here to Turn OFF LT EYE.<br><br></button></a><br><br>");
  client.println("<a href=\"/MOUTH=ON\"><button><font size=7>Click here to Turn ON MOUTH.<br><br></button></a><br><br>");
  client.println("<a href=\"/MOUTH=OFF\"><button><font size=7>Click here to Turn OFF MOUTH.<br><br></button></a><br><br>");
  client.println("</html>");
 
  delay(1);
  Serial.println("Client disconnected");
  Serial.println("");
 
}//END

//Original code by 

Credits

Jeremy Cook

Jeremy Cook

64 projects • 884 followers
Engineer, maker of random contraptions, love learning about tech. Write for various publications, including Hackster!

Comments