Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <WiFiClientSecure.h>
- #include "AudioFileSourceSPIFFS.h"
- #include "AudioFileSourceID3.h"
- #include "AudioGeneratorMP3.h"
- #include "AudioOutputI2SNoDAC.h"
- #include <WiFiClient.h>
- #include <ESP8266WebServer.h>
- #include <ESP8266mDNS.h>
- //#include <FS.h>
- AudioGeneratorMP3 *mp3;
- AudioFileSourceSPIFFS *file;
- AudioOutputI2SNoDAC *out;
- AudioFileSourceID3 *id3;
- ESP8266WebServer server(80);
- #define debug
- File fsUploadFile;
- //https://www.mischianti.org/online-converter-file-to-cpp-gzip-byte-array-3/
- //File: index.html.gz, Size: 620
- #define index_html_gz_len 620
- const uint8_t index_html_gz[] PROGMEM = {
- 0x1F, 0x8B, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x7D, 0x54, 0xC1, 0x6E, 0x13, 0x31,
- 0x10, 0xBD, 0x47, 0xCA, 0x3F, 0x58, 0xBE, 0x64, 0xA3, 0x36, 0xBB, 0xE9, 0x0D, 0xA9, 0xBB, 0x2B,
- 0x95, 0x92, 0x52, 0xAA, 0x52, 0xAA, 0x06, 0x04, 0x12, 0xE2, 0xE0, 0xAC, 0x27, 0x59, 0x23, 0xAF,
- 0x6D, 0xD6, 0xB3, 0x49, 0x2A, 0xD4, 0x43, 0x7B, 0xE5, 0x80, 0xC4, 0x01, 0xC4, 0x8D, 0x13, 0x5F,
- 0xC0, 0x99, 0x9F, 0xA9, 0x2A, 0xF8, 0x0C, 0x6C, 0x6F, 0x52, 0x12, 0x0E, 0x9C, 0x62, 0xBF, 0x99,
- 0x79, 0x33, 0x7E, 0xF3, 0x36, 0x69, 0x89, 0x95, 0xCC, 0xBB, 0x9D, 0xB4, 0x04, 0xC6, 0xFD, 0x6F,
- 0x05, 0xC8, 0x88, 0x62, 0x15, 0x90, 0x8C, 0xD0, 0xB9, 0x80, 0x85, 0xD1, 0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 0x51, 0x9F, 0x64, 0x39, 0x79, 0xDF, 0xED, 0x90, 0xAD, 0x2E, 0x2E, 0x72,
- 0x32, 0x7E, 0x76, 0x16, 0x1B, 0x6F, 0xAC, 0xC8, 0x57, 0xAC, 0xA3, 0x9E, 0x83, 0x78, 0x57, 0x5B,
- 0x2D, 0x21, 0x96, 0x7A, 0x16, 0x6D, 0xD6, 0xB5, 0x51, 0xB7, 0xA0, 0x68, 0xCE, 0x6A, 0x22, 0xB2,
- 0xE1, 0xBE, 0x48, 0x37, 0xE3, 0xB1, 0x04, 0x35, 0xC3, 0x72, 0x5F, 0xEC, 0xEC, 0xF4, 0x43, 0x53,
- 0x42, 0xFC, 0x5B, 0xB4, 0xD9, 0x54, 0xA4, 0xA8, 0x81, 0x21, 0xAC, 0x44, 0x89, 0x7A, 0xDA, 0x78,
- 0x07, 0xF5, 0x5A, 0x6A, 0xE2, 0x72, 0xE3, 0x39, 0x93, 0x8D, 0xD7, 0x70, 0x93, 0xF9, 0xB5, 0x78,
- 0x13, 0xFB, 0xBD, 0xFF, 0xCD, 0xF2, 0xAB, 0x3A, 0xBC, 0xFF, 0xFA, 0xFE, 0x93, 0xBB, 0xBD, 0x9B,
- 0x98, 0x19, 0x27, 0x1C, 0x3F, 0x2C, 0x85, 0xE4, 0x91, 0x36, 0x6D, 0xDB, 0xAB, 0x6E, 0xE7, 0xAA,
- 0x55, 0xCE, 0xBA, 0x58, 0xD4, 0x0A, 0xE9, 0x6C, 0x14, 0x4C, 0x10, 0x1C, 0xB5, 0xFE, 0x94, 0x92,
- 0xF0, 0xBF, 0xF0, 0x07, 0xE4, 0x0E, 0xC4, 0x4F, 0x1E, 0x04, 0x00, 0x00
- };
- boolean wifiConnect() {
- #ifdef debug
- Serial.println("");
- //Serial.print("Connecting to " + String(ssid));
- #endif
- //WiFi接続開始
- WiFi.mode(WIFI_STA);
- WiFi.begin();
- //接続状態になるまで待つ
- unsigned int cnt = 0;
- while (WiFi.status() != WL_CONNECTED) {
- cnt++;
- delay(500);
- if(cnt > 20){
- return false;
- }
- #ifdef debug
- Serial.print(".");
- #endif
- }
- //接続に成功。IPアドレスを表示
- #ifdef debug
- Serial.println(cnt);
- Serial.print("Connected! IP address: ");
- Serial.println(WiFi.localIP());
- #endif
- return true;
- }
- //Line通知
- void sendLineNotify() {
- const char* host = "notify-api.line.me";
- const char* token = "TOKEN WO IRETENE";
- const char* message = "%f0%9f%8d%9a"; //ごはんの絵文字をURLエンコードしたもの
- BearSSL::WiFiClientSecure client;
- client.setInsecure();
- //WiFiClientSecure client;
- #ifdef debug
- Serial.println("Try");
- #endif
- //LineのAPIサーバに接続
- if (!client.connect(host, 443)) {
- #ifdef debug
- Serial.println("Connection failed");
- #endif
- return;
- }
- Serial.println("Connected");
- //リクエストを送信
- String query = String("message=") + String(message);
- String request = String("") +
- "POST /api/notify HTTP/1.1\r\n" +
- "Host: " + host + "\r\n" +
- "Authorization: Bearer " + token + "\r\n" +
- "Content-Length: " + String(query.length()) + "\r\n" +
- "Content-Type: application/x-www-form-urlencoded\r\n\r\n" +
- query + "\r\n";
- client.print(request);
- //受信終了まで待つ
- while (client.connected()) {
- String line = client.readStringUntil('\n');
- if (line == "\r") {
- break;
- }
- }
- String line = client.readStringUntil('\n');
- Serial.println(line);
- }
- bool nearby = false;
- bool lastStatus;
- unsigned int stateCount = 0;
- void MDCallback(void *cbData, const char *type, bool isUnicode, const char *string)
- {
- (void)cbData;
- Serial.printf("ID3 callback for: %s = '", type);
- if (isUnicode) {
- string += 2;
- }
- while (*string) {
- char a = *(string++);
- if (isUnicode) {
- string++;
- }
- Serial.printf("%c", a);
- }
- Serial.printf("'\n");
- Serial.flush();
- }
- void setup() {
- //初期設定
- WiFi.mode(WIFI_OFF);
- WiFi.begin();
- Serial.begin(115200);
- SPIFFS.begin();
- Serial.printf("Sample MP3 playback begins...\n");
- audioLogger = &Serial;
- unsigned int ran = random(0, 9);
- String filename = "/" + String(ran) +".mp3";
- char charBuf[7];
- filename.toCharArray(charBuf, 7);
- Serial.println(charBuf);
- file = new AudioFileSourceSPIFFS(charBuf);
- id3 = new AudioFileSourceID3(file);
- id3->RegisterMetadataCB(MDCallback, (void*)"ID3TAG");
- out = new AudioOutputI2SNoDAC();
- mp3 = new AudioGeneratorMP3();
- mp3->begin(id3, out);
- while(1){
- if (mp3->isRunning()) {
- if (!mp3->loop()) mp3->stop();
- } else {
- Serial.printf("MP3 done\n");
- if(wifiConnect() == true){
- sendLineNotify();
- }else{
- #ifdef debug
- Serial.println("");
- Serial.println("No SSID available");
- Serial.println("Change mode to AP mode");
- #endif
- digitalWrite(16, LOW);//シャットダウン
- WiFi.mode(WIFI_AP);
- WiFi.softAP("Sugi-Switch");
- }
- break;
- }
- }
- pinMode(16, OUTPUT);
- digitalWrite(16, LOW);//シャットダウン
- delay(10000);
- digitalWrite(16, HIGH);
- if (MDNS.begin("sugi")) {
- Serial.println("MDNS responder started");
- }
- server.on("/", [](){
- server.sendHeader(F("Content-Encoding"), F("gzip"));
- server.send(200, "text/html", (const char*)index_html_gz, index_html_gz_len);
- });
- server.on("/setup", wifimgr_top);
- server.on("/wifiinput", HTTP_GET, wifiinput);
- server.on("/wifiset", HTTP_GET, wifiset);
- server.on("/reboot", reboot);
- server.on("/doreboot", doreboot);
- server.on("/list", HTTP_GET, handleFileList);
- server.on("/upload.cgi", HTTP_POST, [](){
- server.send(200, "text/plain", "{\"success\":1}");
- }, handleFileUpload);
- //server.onNotFound(handleNotFound);
- server.begin();
- #ifdef debug
- Serial.println("HTTP server started");
- #endif
- }
- void loop() {
- server.handleClient();
- MDNS.update();
- }
- void handleFileUpload()
- {
- HTTPUpload& upload = server.upload();
- if(upload.status == UPLOAD_FILE_START)
- {
- String filename = upload.filename;
- if(!filename.startsWith("/"))
- filename = "/"+filename;
- Serial.print("handleFileUpload Name: "); Serial.println(filename);
- fsUploadFile = SPIFFS.open(filename, "w");
- } else if(upload.status == UPLOAD_FILE_WRITE)
- {
- if(fsUploadFile)
- fsUploadFile.write(upload.buf, upload.currentSize);
- } else if(upload.status == UPLOAD_FILE_END)
- {
- if(fsUploadFile)
- fsUploadFile.close();
- Serial.print("handleFileUpload Size: "); Serial.println(upload.totalSize);
- }
- }
- void handleFileList()
- {
- String path = "/";
- // Assuming there are no subdirectories
- Dir dir = SPIFFS.openDir(path);
- String output = "[{\"filename\":\"";
- while(dir.next())
- {
- File entry = dir.openFile("r");
- // Separate by comma if there are multiple files
- if(output != "[{\"name\":\"")
- output += "\"},{\"name\":\"";
- output += String(entry.name()).substring(1);
- //output += ",\"id\":";
- //output += String(millis());
- entry.close();
- }
- output += "\"}]";
- server.send(200, "text/plain", output);
- }
- // scan SSID
- #define SSIDLIMIT 30
- String ssid_rssi_str[SSIDLIMIT];
- String ssid_str[SSIDLIMIT];
- String ssid;
- String passwd;
- //*******************************************
- String maskpasswd(String passwd){
- String maskpasswd = "";
- for (int i=0; i<passwd.length(); i++) maskpasswd = maskpasswd + "*";
- if (passwd.length() == 0) maskpasswd = "(null)";
- return maskpasswd;
- }
- void wifimgr_top() {
- String html = Headder_str();
- html += "<a href='/wifiinput'>WIFI setup</a>";
- html += "<hr><h3>Current Settings</h3>";
- html += "SSID: " + ssid + "<br>";
- html += "passwd: " + maskpasswd(passwd) + "<br>";
- html += "<hr><p><center><a href='/reboot'>Reboot</a></center>";
- html += "</body></html>";
- server.send(200, "text/html", html);
- }
- String Headder_str() {
- String html = "";
- html += "<!DOCTYPE html><html><head>";
- html += "<meta name='viewport' content='width=device-width, initial-scale=1.3'>";
- html += "<meta http-equiv='Pragma' content='no-cache'>";
- html += "<meta http-equiv='Cache-Control' content='no-cache'></head>";
- html += "<meta http-equiv='Expires' content='0'>";
- html += "<style>";
- html += "a:link, a:visited { background-color: #009900; color: white; padding: 5px 15px;";
- html += "text-align: center; text-decoration: none; display: inline-block;}";
- html += "a:hover, a:active { background-color: green;}";
- html += "bo32{ background-color: #EEEEEE;}";
- html += "input[type=button], input[type=submit], input[type=reset] {";
- html += "background-color: #000099; border: none; color: white; padding: 5px 20px;";
- html += "text-decoration: none; margin: 4px 2px;";
- html += "</style>";
- html += "<body>";
- html += "<h2>Sugi Switch Setup</h2>";
- return html;
- }
- void wifiinput() {
- String html = Headder_str();
- html += "<a href='/'>TOP</a> ";
- html += "<hr><p>";
- html += "<h3>WiFi Selector</h3>";
- html += WIFI_Form_str();
- html += "<br><hr><p><center><a href='/'>Cancel</a></center>";
- html += "</body></html>";
- server.send(200, "text/html", html);
- }
- //*******************************************
- String WIFI_Form_str(){
- Serial.println("wifi scan start");
- // WiFi.scanNetworks will return the number of networks found
- uint8_t ssid_num = WiFi.scanNetworks();
- Serial.println("scan done\r\n");
- if (ssid_num == 0) {
- Serial.println("no networks found");
- } else {
- Serial.printf("%d networks found\r\n\r\n", ssid_num);
- if (ssid_num > SSIDLIMIT) ssid_num = SSIDLIMIT;
- for (int i = 0; i < ssid_num; ++i) {
- ssid_str[i] = WiFi.SSID(i);
- String wifi_auth_open = ((WiFi.encryptionType(i) == 7)?" ":"*");
- ssid_rssi_str[i] = ssid_str[i] + " (" + WiFi.RSSI(i) + "dBm)" + wifi_auth_open;
- ssid_rssi_str[i] = ssid_str[i] + wifi_auth_open;
- Serial.printf("%d: %s\r\n", i, ssid_rssi_str[i].c_str());
- delay(10);
- }
- }
- String str = "";
- str += "<form action='/wifiset' method='get'>";
- str += "<select name='ssid' id ='ssid'>";
- for(int i=0; i<ssid_num; i++){
- str += "<option value=" + ssid_str[i] + ">" + ssid_rssi_str[i] + "</option>";
- }
- str += "<option value=" + ssid + ">" + ssid + "(current)</option>";
- // if (ssid != defaultSSID){
- // str += "<option value=" + defaultSSID + ">" + defaultSSID + "(default)</option>";
- // }
- str += "</select><br>\r\n";
- str += "Password<br><input type='password' name='passwd' value='" + passwd + "'>";
- str += "<br><input type='submit' value='set'>";
- str += "</form><br>";
- str += "<script>document.getElementById('ssid').value = '"+ ssid +"';</script>";
- return str;
- }
- void wifiset(){
- ssid = server.arg("ssid");
- passwd = server.arg("passwd");
- ssid.trim();
- passwd.trim();
- Serial.println(ssid);
- //WriteConfigFile();
- WiFi.begin(ssid.c_str(), passwd.c_str());
- // 「/」に転送
- server.sendHeader("Location", String("http://cam.local"), true);
- server.send(302, "text/plain", "");
- ESP.restart();
- }
- void reboot() {
- String html = Headder_str();
- html += "<hr><p>";
- html += "<h3>reboot confirmation</h3><p>";
- html += "Are you sure to reboot?<p>";
- html += "<center><a href='/doreboot'>YES</a> <a href='/'>no</a></center>";
- html += "<p><hr>";
- html += "</body></html>";
- server.send(200, "text/html", html);
- }
- void doreboot() {
- String html = Headder_str();
- html += "<hr><p>";
- html += "<h3>rebooting</h3><p>";
- html += "The setting WiFi connection will be disconnected...<p>";
- html += "<hr>";
- html += "</body></html>";
- server.send(200, "text/html", html);
- // reboot esp32
- Serial.println("reboot esp32 now.");
- //digitalWrite(LED_BUILTIN, LOW);
- delay(2000); // hold 2 sec
- ESP.restart(); // restart ESP32
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement