ESP8266-12 ON/OFF LED
Posted:
Fri Nov 06, 2015 7:17 am
by Alin
Hi guys,
Can some help me with a piece of code/function or something ? I want to make a webpage, hosted by the ESP8266 and I want to have a button or 2 that controls an LED. I do not want to use any java or PHP !
I want to program the module in the ARDUINO IDE.
Are there any functions in the default libraries of the ESP8266 that sends data from server to MCU ?
Re: ESP8266-12 ON/OFF LED
Posted:
Mon Nov 09, 2015 1:13 am
by bob8435
Hi Alin
I finally got something working and maybe it will help you out. Please feel free to comment. I am new
at using this device and it's been pretty hard to find simple examples for some things.
Bob
//Taken from Sparkfun ESP8266 Thing Access Point demo modified for adafruit huzzah board
//best to review the sparkfun page for more background details.
//Also modified the web page to provide an on and off button to operate the single led on the huzzah
//unused code present for the val = -2 case
//Just got it working tonight, no claims to elegance or completeness. Posting because there seems to
//be some interest in this topic.
//things to be aware of: the web page is stored in a string variable, 255 characters max
//I have been using an ipad to test with. Everytime the esp8266 is rebooted, I have to go back
//on the ipad and select the access point again. Could be fixed by disabling the house wifi ap.
//Noted some weirdness changing the AP name and password. The first time I ran it the sparkfun
//ap name and password seemed to stick and I could not change them. I did eventually get them
//changed by doing one change at a time. It could have been the ipad remembering the pw also.
//Also saw the the esp8266 hang up when I was testing the range. I got out to about 30 feet and
//it hung. It could need a better power supply. I am just running it off the usb serial adaptor.
//The power draw may have increased because it was putting out more RF and the power supply folded
//a bit. Just a thought
#include <ESP8266WiFi.h>
//////////////////////
// WiFi Definitions //
//////////////////////
const char WiFiAPPSK[] = "1234567890"; //password
/////////////////////
// Pin Definitions //
/////////////////////
const int LED_PIN = 0; // changed to match adafruit huzzah
const int ANALOG_PIN = A0; // The only analog pin on the Thing
const int DIGITAL_PIN = 12; // Digital pin to be read
WiFiServer server(80);
void setup()
{
initHardware();
setupWiFi();
server.begin();
}
void loop()
{
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
// Read the first line of the request
String req = client.readStringUntil('\r');
Serial.println(req);
client.flush();
// Match the request
int val = -1; // We'll use 'val' to keep track of both the
// request type (read/set) and value if set.
if (req.indexOf("/led/0") != -1)
val = 0; // Will write LED low
else if (req.indexOf("/led/1") != -1)
val = 1; // Will write LED high
else if (req.indexOf("/read") != -1)
val = -2; // Will print pin reads
// Otherwise request will be invalid. We'll say as much in HTML
// Set LED according to the request
if (val >= 0)
digitalWrite(LED_PIN, val);
client.flush();
// Prepare the web page response. Start with the common header:
String s = "HTTP/1.1 200 OK\r\n";
s += "Content-Type: text/html\r\n\r\n";
s += "<!DOCTYPE HTML>\r\n<html>\r\n";
s += "<input type=\"button\" onclick=\"location.href='192.168.4.1/led/1';\" value=\" OFF \" />";
s += "<input type=\"button\" onclick=\"location.href='192.168.4.1/led/0';\" value=\" ON \" />";
// If we're setting the LED, print out a message saying we did
if (val >= 0)
{
s += " LED is now ";
//s += (val)?"on":"off";
s += (digitalRead(LED_PIN))?"off":"on";
//s += String(digitalRead(LED_PIN));
}
else if (val == -2)//this left over from the sparkfun demo, not currently used
{ // If we're reading pins, print out those values:
s += "Analog Pin = ";
s += String(analogRead(ANALOG_PIN));
s += "<br>"; // Go to the next line.
s += "Digital Pin 12 = ";
s += String(digitalRead(DIGITAL_PIN));
}
else
{
//s += "Invalid Request.<br> Try /led/1, /led/0, or /read.";
}
s += "</html>\n";
// Send the response to the client
client.print(s);
delay(1);
Serial.println("Client disconnected");
// The client will actually be disconnected
// when the function returns and 'client' object is destroyed
}
void setupWiFi()
{
WiFi.mode(WIFI_AP);
// Do a little work to get a unique-ish name. Append the
// last two bytes of the MAC (HEX'd) to "Thing-":
uint8_t mac[WL_MAC_ADDR_LENGTH];
WiFi.softAPmacAddress(mac);
String macID = String(mac[WL_MAC_ADDR_LENGTH - 2], HEX) +
String(mac[WL_MAC_ADDR_LENGTH - 1], HEX);
macID.toUpperCase();
String AP_NameString = "ESP8266 Thing " + macID;
char AP_NameChar[AP_NameString.length() + 1];
memset(AP_NameChar, 0, AP_NameString.length() + 1);//zero out AP_NameChar
for (int i=0; i<AP_NameString.length(); i++)
AP_NameChar[i] = AP_NameString.charAt(i);
WiFi.softAP(AP_NameChar, WiFiAPPSK);
}
void initHardware()
{
Serial.begin(115200);
pinMode(DIGITAL_PIN, INPUT_PULLUP);
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
}