suria sarathAzharvkAsha Stebi
Published © GPL3+

Mood Light with Python

An IoT-controlled mood light system using Python.

IntermediateShowcase (no instructions)3 hours3,414
Mood Light with Python

Things used in this project

Hardware components

ESP8266 ESP-12E
Espressif ESP8266 ESP-12E
×1
RGB Diffused Common Anode
RGB Diffused Common Anode
×1

Software apps and online services

Arduino IDE
Arduino IDE
Python IDLE

Story

Read more

Schematics

mood_light connection diagram

simply open with fritzing s/w

image of connection diagram

image of schematic diagram

Code

Arduino code for Mood Light

Arduino
simply open with arduino and upload to ESP8266 12 e
/*
 *  This sketch demonstrates how to set up a simple HTTP-like server.
 *  The server will set a GPIO pin depending on the request
 *    http://server_ip/gpio/0 will set the GPIO2 low,
 *    http://server_ip/gpio/1 will set the GPIO2 high
 *  server_ip is the IP address of the ESP8266 module, will be 
 *  printed to Serial when the module is connected.
 */

#include <ESP8266WiFi.h>

const char* ssid = "acew";
const char* password = "micromax";
String red,green,blue;
// Create an instance of the server
// specify the port to listen on as an argument
WiFiServer server(80);

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

  // prepare GPIO2
  pinMode(2, OUTPUT);
    pinMode(5, OUTPUT);
  pinMode(14, OUTPUT);
  pinMode(12, OUTPUT);
  digitalWrite(2, 0);
  
  // Connect to WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  
  WiFi.begin(ssid, password);
  
  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.println(WiFi.localIP());
}

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 req = client.readStringUntil('\r');
  Serial.println(req);
  red  = req.substring(5,8);
  green = req.substring(8,11);
  blue = req.substring(11,14);
  //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@222

  analogWrite(5, 1023-(4*red.toInt()));
  analogWrite(14, 1023-(4*green.toInt()));
  analogWrite(12, 1023-(4*blue.toInt()));
  
  //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  client.flush();
  
  // Match the request
  int val;
  if (req.indexOf("/gpio/0") != -1)
    val = 0;
  else if (req.indexOf("/gpio/1") != -1)
    val = 1;
 

  // Set GPIO2 according to the request
  digitalWrite(2, val);
  
  client.flush();

  // Prepare the response
  String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nGPIO is now ";
  s += (val)?"high":"low";
  s += "</html>\n";

  // Send the response to the client
  client.print(s);
  delay(1);
  //Serial.println("Client disonnected");

  // The client will actually be disconnected 
  // when the function returns and 'client' object is detroyed
}

Python code for mode light

Python
python script and open file with python IDLE then run
from tkinter import *
import urllib.request
from tkinter.colorchooser import *
colour='red'

def con(data):
    data = int(data)
    if len(str(data))==3:
        return str(data)
    
    elif len(str(data))==2:
        data ="0"+str(data)
        return data

    elif len(str(data))==1:
        data  = "00"+str(data)
        return data
    

def getColor():
    color = askcolor() 
    colour=color[1]
    print(int(color[0][1]))
    print(colour)
    k.configure(bg=colour)
    try:
        o='http://192.168.43.184/'+con(color[0][0])+con(color[0][1])+con(color[0][2])
        print(o)
        urllib.request.urlopen(o).read()
    except Exception as e:
        print(e)
    
    
    
k = Button(text='Select Color', command=getColor,bg=colour,height=100, width=100)
k.pack()
mainloop()

Credits

suria sarath
8 projects • 26 followers
Embedded software programmer at Lanware Solutions
Contact
Azharvk
2 projects • 2 followers
Contact
Asha Stebi
0 projects • 1 follower
Contact

Comments

Please log in or sign up to comment.