Saturday, 8 October 2016

LED Controlled by the Internet

LED Controlled by the Internet


Happy holidays! Just got done with school for this semester, so I finally have some free time to get back to my hobbies. Speaking of hobbies, I bought an Espressif ESP8266 Wi-Fi module a few days ago and I finally got it working today. Its surprisingly easy to use, and it was hard to pass up purchasing one since its only $5.
Just to test it out, I decided to make an make a project that can toggle an LED with the internet. The ESP2866 communicates with the microcontroller via UART and has an entire TCP/IP stack built into it. So it required me to know nothing about network protocols to be able to use :) I ended up writing a library to simplify sending GET and POST requests, but the basic underlying ESP2866 commands that I used are these.
 // Join access point 
AT+CWJAP="SSID","Password"

// Connect to server
AT+CIPSTART="TCP","Domain Name",80

// Send message
AT+CIPSEND=Length > Message
So I created webpage that stores the state of an LED in a database. This state can be requested and updated depending on the parameters of the GET request.
 <?php 

// Connect to database
$database = new mysqli("localhost", "Username", "Password", "Database");

if($database->connect_errno)
exit();

$database->set_charset("utf8");

// Create automation table
if(!$database->query("SELECT 1 FROM `Automation`")) {
$database->query("CREATE TABLE `Automation`(`ID` INT(10) NOT NULL AUTO_INCREMENT PRIMARY KEY, `Object` VARCHAR(16) NOT NULL UNIQUE KEY, `State` VARCHAR(255) NOT NULL) ENGINE=MyISAM DEFAULT CHARSET=utf8") or exit();
$database->query("OPTIMIZE TABLE `Automation`");

// Add Light entry to table
$database->query("INSERT INTO `Automation` VALUES(NULL, "Light", "Off")") or exit();
}

// Check if purpose is status
if($_GET["Purpose"] == "Status") {

// Get lights state
$result = $database->query("SELECT `State` FROM `Automation` WHERE `Object` = "Light"") or exit();
$object = $result->fetch_assoc();

// Return lights state
echo $object["State"];
}

// Otherwise check if purpose is set and state is set
else if($_GET["Purpose"] == "Set" && isset($_GET["State"])) {

// Update lights state
$database->query("UPDATE `Automation` SET `State` = "" . $database->real_escape_string(htmlentities($_GET["State"])) . "" WHERE `Object` = "Light"") or exit();

// Return if successful
echo $database->affected_rows ? "Successful" : "Failed";
}

?>
Now to we can manipulate the LED with the following GET requests.
 // Turn LED on 
Purpose=Set&State=On

// Turn LED off
Purpose=Set&State=Off

// Get state of LED
Purpose=Status
I ended up making it so that a pressing a button will trigger the microcontroller to send the appropriate GET request to toggle that state of the LED in the database, and the microcontroller constantly requests the state of the LED from the server and updates the actual LED accordingly. Since it uses normal GET request, the LED can be controlled directly from a web browser. Heres a video of it in action :)


In the process of making this project I accidentally broke my HP DV6-6B22HE laptop :( I use a UART-to-USB cable when developing for microcontrollers since it provides an easy means of debugging, and I was curious to see if the 500mA provided by my laptops USB port could drive both the UART-to-USB cable and my project. I somehow forgot to disconnect the 12V power supply that I was already using from the circuit, and this resulted in me putting 12V across the USB port on my laptop... It lasted about 3 seconds before dying... RIP :(

As a side note, I wanted to do a little bit more research on RSA bios before releasing my findings since the two modded RSA bios that I released didnt work. I guess the solution I found wasnt generalized enough for all bios. I have a lot more time to work on this now, so it shouldnt be too much longer :)

Available link for download