Skip to content

Instantly share code, notes, and snippets.

@ivoras
Created July 29, 2018 19:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ivoras/153148612b45c2d07a4acfec596f4b53 to your computer and use it in GitHub Desktop.
Save ivoras/153148612b45c2d07a4acfec596f4b53 to your computer and use it in GitHub Desktop.
ESP8266 Dash button Arduino IDE sketch
// For use with this EasyEDA project: https://easyeda.com/account/project/setting/basic?project=c5413ffccb63491c831e6907e39ede2f
#include <EEPROM.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPClient.h>
#define PIN_POWER 2
#define PIN_LED 1
#define PIN_CONFIG 14
#define EEPROM_CONFIG_SIGNATURE 0x54
#define EEPROM_CONFIG_VERSION 1
struct DashConfig {
byte signature;
byte ver;
char ssid[50];
char password[50];
};
ESP8266WiFiMulti wifi;
ESP8266WebServer server(80);
bool config_mode = false;
bool led_state = false;
uint32_t led_time = 0;
int count;
struct DashConfig cfg;
bool read_eeprom_config() {
byte *b = (byte*)&cfg;
for (int i = 0; i < sizeof(cfg); i++)
*(b++) = EEPROM.read(i);
return cfg.signature == EEPROM_CONFIG_SIGNATURE && cfg.ver == EEPROM_CONFIG_VERSION;
}
void write_eeprom_config() {
byte *b = (byte*)&cfg;
for (int i = 0; i < sizeof(cfg); i++)
EEPROM.write(i, *(b++));
EEPROM.commit();
}
void led_on() {
digitalWrite(PIN_LED, LOW);
}
void led_off() {
digitalWrite(PIN_LED, HIGH);
}
String htmlHead(String title) {
String html;
html += "<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n<meta charset=\"utf-8\">\n<meta http-equiv=\"x-ua-compatible\" content=\"ie=edge\">\n<title>" + title + "</title>\n";
html += "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n";
html += "<style>body {background: white; margin-left: 5%; margin-right: 5%; font-family: Segoe UI, Droid Sans, Arial, sans-serif;}</style>\n";
html += "</head><body>\n";
return html;
}
String htmlFoot() {
return "</body></html>";
}
void handleRoot() {
String html = htmlHead("Akira_Dash device configuration");
html += "<h1>Akira_Dash device configuration</h1>\n";
html += "<form method=\"POST\" action=\"/save\">\n";
html += "<table align=\"center\">\n";
html += "<tr><td>Your WiFi SSID:</td><td><input name=\"ssid\" type=\"text\" value=\""; html += cfg.ssid; html += "\" size=\"20\"></td></tr>\n";
html += "<tr><td>Your WiFi password:</td><td><input name=\"password\" type=\"password\" size=\"20\"></td></tr>\n";
html += "<tr><td colspan=\"2\"><input type=\"submit\"></td></tr>\n";
html += "</table>\n";
html += "</form>\n";
html += htmlFoot();
server.send(200, "text/html", html);
}
void handleSave() {
String ssid = server.arg("ssid");
String password = server.arg("password");
String html = htmlHead("Akira_Dash configuration saved");
html += "<p>The configuration has been saved. You can start using the device now!</p>\n";
html += htmlFoot();
server.send(200, "text/html", html);
strcpy(cfg.ssid, ssid.c_str());
strcpy(cfg.password, password.c_str());
cfg.signature = EEPROM_CONFIG_SIGNATURE;
cfg.ver = EEPROM_CONFIG_VERSION;
write_eeprom_config();
delay(50);
digitalWrite(PIN_POWER, LOW);
// Bye bye.
}
void setup() {
// put your setup code here, to run once:
pinMode(PIN_POWER, OUTPUT);
digitalWrite(PIN_POWER, HIGH);
pinMode(PIN_LED, OUTPUT);
pinMode(PIN_CONFIG, INPUT_PULLUP);
led_on();
delay(5);
EEPROM.begin(512);
wifi.addAP("SignalGrad", "Crta3000");
config_mode = false;
return;
if (digitalRead(PIN_CONFIG) == LOW) {
config_mode = true;
} else {
config_mode = !read_eeprom_config();
}
if (!config_mode) {
wifi.addAP(cfg.ssid, cfg.password);
return;
}
cfg.password[0] = '\0';
if (cfg.signature != EEPROM_CONFIG_SIGNATURE || cfg.ver != EEPROM_CONFIG_VERSION)
cfg.ssid[0] = '\0';
char ap_ssid[100];
char ap_password[100];
sprintf(ap_ssid, "Akira_Dash_%06x", ESP.getChipId());
sprintf(ap_password, "%06x", ESP.getChipId());
WiFi.softAP(ap_ssid, ap_password);
server.on("/", handleRoot);
server.on("/save", handleSave);
server.begin();
}
void loop() {
if (config_mode) {
server.handleClient();
return;
}
if ((wifi.run() == WL_CONNECTED)) {
led_on();
HTTPClient http;
// USE_SERIAL.print("[HTTP] begin...\n");
// configure traged server and url
char url[100];
sprintf(url, "http://www.example.com/dash/?id=%d", ESP.getChipId());
http.begin(url); //HTTP
// start connection and send HTTP header
int httpCode = http.GET();
// httpCode will be negative on error
if(httpCode > 0) {
// HTTP header has been send and Server response header has been handled
// USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);
// file found at server
if(httpCode == HTTP_CODE_OK) {
String payload = http.getString();
// USE_SERIAL.println(payload);
}
} else {
// USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
led_off();
delay(200);
digitalWrite(PIN_POWER, LOW);
} else {
// Keep blinking if not connected
digitalWrite(PIN_LED, led_state ? LOW : HIGH);
if (millis() - led_time >= 500) {
led_state = !led_state;
led_time = millis();
if (++count >= 20) // Give up after 10 seconds
digitalWrite(2, LOW);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment