/*
Copyright (c) 2015, Majenko Technologies
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
* * Neither the name of Majenko Technologies nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*//* Create a WiFi access point and provide a web server on it. */#include<ESP8266WiFi.h>#include<WiFiClient.h>#include<ESP8266WebServer.h>#include<ESP_EEPROM.h>// EEPROM structure//MAX values are both +1 in size to make room for zero terminator#define SSID_MAX 33#define PW_MAX 64structMyEEPROMStruct {char ssid[SSID_MAX];
char password[PW_MAX];
/* User timer settings: { hours, minutes } */
byte sleep_time[2];
byte doze_time[2];
byte wake_time[2];
byte day_time[2];
/* Timezone offset in minutes from UTC */signedint tzoffset;
} eepromVar1, eepromVar2;
#ifndef APSSID#define APSSID "ESPap"#define APPSK "thereisnospoon"#endif/* Set these to your desired credentials. */constchar *ssid = APSSID;
constchar *password = APPSK;
ESP8266WebServer server(80);
String getPage(){
String page = "<!DOCTYPE html>";
page += "<html>""<head>""<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">""</head>""<body style=\"width:480px; margin: 0 auto;\">""""<h1>Improved Okay-to-Wake Clock</h1>""<h2>Update now:</h2>""<form action=\"/settime\" method=\"POST\">"" <label for=\"time\">Set current time:</label>"" <input type=\"time\" id=\"time\" name=\"time\">"" <input type=\"submit\"><br />""</form><br /><br />""""<h2>Settings:</h2>""<form action=\"/\">"" <label for=\"ssid\">SSID: </label>"" <input type=\"text\" id=\"fname\" name=\"ssid\" value=\"";
page += eepromVar1.ssid;
page += "\"><br /><br />"" <label for=\"pass\">Password: </label>"" <input type=\"password\" id=\"pass\" name=\"pass\" value=\"";
page += eepromVar1.password;
page += "\"><br /><br />"" <input type=\"submit\">""</form><br /><br />""<h2>Toggle Onboard LED</h2>""<form action=\"/LED\" method=\"POST\"><input type=\"submit\" value=\"Toggle LED\"></form>""""</html>""</body>";
return page;
}
/* Just a little test message. Go to http://192.168.4.1 in a web browser
connected to this access point to see it.
*/voidhandleRoot(){
if ( server.hasArg("ssid") ) {
handlePrefs();
}
server.send(200, "text/html", getPage());
}
voidhandlePrefs(){
digitalWrite(D0,!digitalRead(D0)); // Change the state of the LED
eepromVar1.ssid[0] = '\0';
strncat(eepromVar1.ssid, server.arg("ssid").c_str(), SSID_MAX-1);
eepromVar1.password[0] = '\0';
strncat(eepromVar1.password, server.arg("pass").c_str(), SSID_MAX-1);
EEPROM.put(0, eepromVar1);
boolean ok = EEPROM.commit();
Serial.println((ok) ? "Commit OK" : "Commit failed");
Serial.println();
eepromInfo();
server.sendHeader("Location","/"); // Add a header to respond with a new location for the browser to go to the home page again
server.send(303); // Send it back to the browser with an HTTP status 303 (See Other) to redirect
}
voidhandleLED(){ // If a POST request is made to URI /LED
digitalWrite(D0,!digitalRead(D0)); // Change the state of the LED
server.sendHeader("Location","/"); // Add a header to respond with a new location for the browser to go to the home page again
server.send(303); // Send it back to the browser with an HTTP status 303 (See Other) to redirect
}
voideepromInfo(){
if(EEPROM.percentUsed()>=0) {
EEPROM.get(0, eepromVar1);
Serial.println("EEPROM has data from a previous run.");
Serial.print(EEPROM.percentUsed());
Serial.println("% of ESP flash space currently used");
} else {
Serial.println("EEPROM size changed - EEPROM data zeroed - commit() to make permanent");
}
}
voidsetup(){
delay(1000);
pinMode(D0, OUTPUT);
Serial.begin(115200);
Serial.println();
Serial.println("Servicing EEPROM emulation...");
/* Set default values */
eepromVar1.ssid[0] = '\0';
strncat(eepromVar1.ssid, "ESPap", SSID_MAX-1);
eepromVar1.password[0] = '\0';
strncat(eepromVar1.password, "thereisnospoon", PW_MAX-1);
eepromVar1.sleep_time[0] = 18;
eepromVar1.sleep_time[1] = 45;
eepromVar1.doze_time[0] = 5;
eepromVar1.doze_time[1] = 30;
eepromVar1.wake_time[0] = 6;
eepromVar1.wake_time[1] = 30;
eepromVar1.day_time[0] = 7;
eepromVar1.day_time[1] = 30;
eepromVar1.tzoffset = -300;
EEPROM.begin(sizeof(MyEEPROMStruct));
eepromInfo();
Serial.println();
Serial.print("Configuring access point...");
/* You can remove the password parameter if you want the AP to be open. */
WiFi.softAP(eepromVar1.ssid, eepromVar1.password);
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
server.on("/", handleRoot);
server.on("/LED", HTTP_POST, handleLED); // Call the 'handleLED' function when a POST request is made to URI "/LED"
server.begin();
Serial.println("HTTP server started");
}
voidloop(){
server.handleClient();
}
Ever since first doing this hack I've had a real problem with keeping the thing powered. USB power seems to die if you look at the thing wrong.
At first I thought I was browning-out the incoming power but adding a capacitor didn't fix it. I resoldered the power connections, and even clipped off the pin connectors to make direct solder joints. No dice. If you shake it around the power blinks on and off.
I think I finally tracked it down to a shoddy power cable that came with the clock. It's USB on one side and barrel jack on the other. I don't have the same size barrel connector on hand so I just unscrewed the port inside the clock and soldered a new USB cable in its place. So far so good.