#include <EEPROM.h>
#include <ESP8266WiFi.h>
#include <ESP8266httpUpdate.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266WebServer.h>
String accessPointSsid = "Stingray-0000";
String accessPointPassword = "11111111";
String deviceName = "thisDevice";
String ssidEntered;
String passwordEntered;
String compiledFileHeader;
WiFiClient clientInfo;
IPAddress local_IP(192, 168, 4, 22);
IPAddress gateway(192, 168, 4, 9);
IPAddress subnet(255, 255, 255, 0);
ESP8266WebServer server(80);
void setup()
{ Serial.begin(9600);
otaSetup();
//ewiowpj();
}
void loop() {
otaLoop();
//vsaiopwij();
}
void otaSetup() {
Serial.println("in here");
setupAccessPoint();
setupServerHandles();
getUpdateHeaderFromEEPROM();
Serial.println("Got EEPROM HEADER");
getHotSpotCredentialsFromEEPROM();
}
unsigned long initialTime = millis();
void otaLoop() {
server.handleClient();
if (millis() - initialTime > 1000) {
checkForNewCompiledFileAndUpload();
initialTime = millis();
}
}
void checkForNewCompiledFileAndUpload() {
Serial.println("compiling");
char rfidWebAddress[] = "24.206.15.96";
String currentCompiledFileHeader = "";
Serial.println(WiFi.status());
if (clientInfo.connect(rfidWebAddress, 8080)) {
Serial.println("connected");
clientInfo.print("GET /" + deviceName + "/emptyFile.txt HTTP/1.1\n");
clientInfo.print("Host: 24.206.15.96\n");
clientInfo.print("Connection: close\n");
clientInfo.println();
delay(100);
int timeTaken = 0;
while (!clientInfo.available()) {
Serial.print("gettingData: ");
Serial.println(timeTaken);
timeTaken++;
}
int compiledFileSizeToRead = 300;
while (clientInfo.available() && compiledFileSizeToRead > 0) {
char dataAsChar = (char)clientInfo.read();
currentCompiledFileHeader += dataAsChar;
compiledFileSizeToRead--;
}
Serial.println(currentCompiledFileHeader);
if (compiledFileHeader != currentCompiledFileHeader && currentCompiledFileHeader != "") {
WiFiClient client;
compiledFileHeader = currentCompiledFileHeader;
saveUpdateHeaderToEEPROM();
delay(7000);
ESPhttpUpdate.update(client, "http://24.206.15.96:8080/" + deviceName + "/" + deviceName + ".ino.bin");
} else {
}
}
}
/**
Set which page to load from ESP8266
*/
void setupServerHandles() {
server.on("/", HTTP_GET, connectToHotspotGUI);
server.on("/connectToHotspot", HTTP_POST, connectToHotspot);
server.on("/hotspotConnectingStatus", HTTP_GET, hotspotConnectingStatus);
server.begin();
}
/**
*/
void connectToHotspotGUI() {
String connectToHotspotGUIStatus = "";
if (WiFi.status() == WL_CONNECTED) {
connectToHotspotGUIStatus = "Status: Connected to " + ssidEntered + "<hr>";
} else {
connectToHotspotGUIStatus = "Status: Disconnected<hr>";
}
server.send(200, "text/html", "<html><meta content='width=device-width, initial-scale=1.0, user-scalable=0' name='viewport' /><body ><div style=\"font:14px Calibri;text-align:center;background-color:rgba(200,200,200,0.7);-moz-border-radius:2vw;-webkit-border-radius:2vw;border-radius:2vw;position:fixed;left:50%;top:50%;transform:translate(-50%,-50%);padding:5vh 5vh;\">" + connectToHotspotGUIStatus + "<h2 style=\"padding:2vh;margin:2vh;margin-left:0px;font:14px Calibri\">Connect To A New Hotspot</h2><form action=\"/connectToHotspot\" method=\"POST\"> <input type=\"text\" name=\"ssid\" placeholder=\"SSID\" style=\"padding:2vh;margin:2vh;\"><br> <input type=\"password\" name=\"password\" placeholder=\"Password\" style=\"padding:2vh;margin:2vh;\"><br> <input type=\"submit\" value=\"Connect\" style=\"padding:2vh;margin:2vh;font:20px Calibri;-moz-border-radius:15px;-webkit-border-radius:15px;border-radius:15px;border-color:blue;\"></form> </div></body></html>");
}
int hotspotConnectionAttemptCounter = 0;
void hotspotConnectingStatus() {
if (WiFi.status() != WL_CONNECTED) {
hotspotConnectionAttemptCounter++;
if (hotspotConnectionAttemptCounter > 10) {
connectToHotspotGUI();
hotspotConnectionAttemptCounter = 0;
} else {
server.send(200, "text/html", "<html><meta content='width=device-width, initial-scale=1.0, user-scalable=0' name='viewport' /><body onload='javascript:checkStatus()'><div style=\"font:40px Calibri;text-align:center;background-color:rgba(200,200,200,0.7);-moz-border-radius:2vw;-webkit-border-radius:2vw;border-radius:2vw;position:fixed;left:50%;top:50%;transform:translate(-50%,-50%);padding:5vh 5vh;\"><h2 style=\"padding:2vh;margin:2vh;margin-left:0px;font:20px Calibri\" id='connectionStatus'>Connecting. </h2></div></body><script>function checkStatus(){setTimeout(function(){document.getElementById('connectionStatus').innerHTML=\"Connecting..\"},1000);setTimeout(function(){window.location.replace(\"/hotspotConnectingStatus\")},2000);}</script></html>");
}
} else {
//WiFi is connected
saveHotSpotCredentialsToEEPROM();
connectToHotspotGUI();
}
}
void saveHotSpotCredentialsToEEPROM() {
EEPROM.begin(100);
EEPROM.put(0, ssidEntered.length());
for (int i = 0; i < ssidEntered.length(); i++) {
EEPROM.put(i + 1, ssidEntered.charAt(i));
}
EEPROM.put(ssidEntered.length() + 1, passwordEntered.length());
for (int i = 0; i < passwordEntered.length(); i++) {
EEPROM.put(ssidEntered.length() + i + 2, passwordEntered.charAt(i));
}
EEPROM.commit();
}
void getHotSpotCredentialsFromEEPROM() {
ssidEntered = "";
passwordEntered = "";
char eePROMCharToRead;
EEPROM.begin(100);
byte eepromSsidLength = 0;
EEPROM.get(0, eepromSsidLength);
for (int i = 0; i < eepromSsidLength; i++) {
EEPROM.get(i + 1, eePROMCharToRead);
ssidEntered += eePROMCharToRead;
}
byte eepromPasswordLength = 0;
EEPROM.get(eepromSsidLength + 1, eepromPasswordLength);
for (int i = 0; i < eepromPasswordLength; i++) {
EEPROM.get(eepromSsidLength + 2 + i, eePROMCharToRead);
passwordEntered += eePROMCharToRead;
}
WiFi.begin(ssidEntered, passwordEntered);
delay(100);
}
void saveUpdateHeaderToEEPROM() {
EEPROM.begin(400);
for (int i = 0; i < 300; i++) {
EEPROM.put(i + 50, compiledFileHeader.charAt(i));
}
EEPROM.commit();
}
void getUpdateHeaderFromEEPROM() {
compiledFileHeader = "";
char eePROMCharToRead;
EEPROM.begin(400);
for (int i = 0; i < 300; i++) {
EEPROM.get(i + 50, eePROMCharToRead);
compiledFileHeader += eePROMCharToRead;
}
delay(100);
}
void connectToHotspot() {
ssidEntered = server.arg("ssid");
passwordEntered = server.arg("password");
WiFi.begin(ssidEntered, passwordEntered);
hotspotConnectingStatus();
}
/**
Set ESP8266 as accesspoint
*/
void setupAccessPoint() {
WiFi.softAPConfig(local_IP, gateway, subnet);
WiFi.softAP(accessPointSsid, accessPointPassword);
WiFi.softAPIP();
server.begin();
}