Advertisement
firtel

Untitled

Feb 18th, 2022
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 14.65 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <WiFiClientSecure.h>
  3. #include "AudioFileSourceSPIFFS.h"
  4. #include "AudioFileSourceID3.h"
  5. #include "AudioGeneratorMP3.h"
  6. #include "AudioOutputI2SNoDAC.h"
  7. #include <WiFiClient.h>
  8. #include <ESP8266WebServer.h>
  9. #include <ESP8266mDNS.h>
  10. //#include <FS.h>
  11.  
  12. AudioGeneratorMP3 *mp3;
  13. AudioFileSourceSPIFFS *file;
  14. AudioOutputI2SNoDAC *out;
  15. AudioFileSourceID3 *id3;
  16.  
  17. ESP8266WebServer server(80);
  18.  
  19. #define debug
  20.  
  21. File fsUploadFile;
  22.  
  23. //https://www.mischianti.org/online-converter-file-to-cpp-gzip-byte-array-3/
  24. //File: index.html.gz, Size: 620
  25. #define index_html_gz_len 620
  26. const uint8_t index_html_gz[] PROGMEM = {  
  27. 0x1F, 0x8B, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x7D, 0x54, 0xC1, 0x6E, 0x13, 0x31,
  28. 0x10, 0xBD, 0x47, 0xCA, 0x3F, 0x58, 0xBE, 0x64, 0xA3, 0x36, 0xBB, 0xE9, 0x0D, 0xA9, 0xBB, 0x2B,
  29. 0x95, 0x92, 0x52, 0xAA, 0x52, 0xAA, 0x06, 0x04, 0x12, 0xE2, 0xE0, 0xAC, 0x27, 0x59, 0x23, 0xAF,
  30. 0x6D, 0xD6, 0xB3, 0x49, 0x2A, 0xD4, 0x43, 0x7B, 0xE5, 0x80, 0xC4, 0x01, 0xC4, 0x8D, 0x13, 0x5F,
  31. 0xC0, 0x99, 0x9F, 0xA9, 0x2A, 0xF8, 0x0C, 0x6C, 0x6F, 0x52, 0x12, 0x0E, 0x9C, 0x62, 0xBF, 0x99,
  32. 0x79, 0x33, 0x7E, 0xF3, 0x36, 0x69, 0x89, 0x95, 0xCC, 0xBB, 0x9D, 0xB4, 0x04, 0xC6, 0xFD, 0x6F,
  33. 0x05, 0xC8, 0x88, 0x62, 0x15, 0x90, 0x8C, 0xD0, 0xB9, 0x80, 0x85, 0xD1, 0x35, 0x52, 0x52, 0x68,
  34. 0x85, 0xA0, 0xD0, 0x83, 0x0B, 0xC1, 0xB1, 0x74, 0x07, 0x0E, 0x73, 0x51, 0xC0, 0x20, 0x5C, 0x77,
  35. 0x89, 0x50, 0x02, 0x05, 0x93, 0x03, 0x5B, 0x30, 0xE9, 0x6B, 0xF7, 0xE2, 0xE1, 0x2E, 0xA9, 0xD8,
  36. 0x52, 0x54, 0x4D, 0xB5, 0x0D, 0x36, 0x16, 0xEA, 0x80, 0xB0, 0x89, 0x84, 0x6C, 0x48, 0xEF, 0xBB,
  37. 0x16, 0x25, 0xAB, 0x2D, 0x60, 0x46, 0x1B, 0x9C, 0x0E, 0x1E, 0xD0, 0xC4, 0x07, 0x50, 0xA0, 0x84,
  38. 0x7C, 0xDC, 0xCC, 0x04, 0x19, 0x2F, 0x04, 0x16, 0x65, 0x9A, 0xB4, 0x90, 0x8B, 0x25, 0xEB, 0x99,
  39. 0x27, 0x9A, 0x5F, 0x86, 0x37, 0xEC, 0x6D, 0x67, 0xBA, 0xBB, 0x43, 0xCF, 0xF3, 0x47, 0xCC, 0xB1,
  40. 0xBF, 0x30, 0x52, 0x33, 0x9E, 0x4E, 0x6A, 0x8F, 0x4D, 0x75, 0x5D, 0x11, 0xD7, 0xB4, 0xD4, 0x3C,
  41. 0xA3, 0x46, 0x5B, 0xF7, 0x42, 0x50, 0x05, 0x5E, 0x1A, 0xC8, 0x68, 0xD5, 0x48, 0x14, 0x86, 0xD5,
  42. 0x98, 0xF8, 0xAC, 0x01, 0x77, 0xC5, 0x94, 0xB0, 0x02, 0x85, 0x56, 0x19, 0x4D, 0x9A, 0x40, 0x13,
  43. 0x17, 0x33, 0x11, 0x06, 0x17, 0xCA, 0x34, 0x48, 0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x0E, 0xC4, 0x4F, 0x1E, 0x04, 0x00, 0x00
  66. };
  67.  
  68. boolean wifiConnect() {
  69.   #ifdef debug
  70.   Serial.println("");
  71.   //Serial.print("Connecting to " + String(ssid));
  72.   #endif
  73.  
  74.   //WiFi接続開始
  75.   WiFi.mode(WIFI_STA);
  76.   WiFi.begin();
  77.  
  78.   //接続状態になるまで待つ
  79.   unsigned int cnt = 0;
  80.   while (WiFi.status() != WL_CONNECTED) {
  81.     cnt++;
  82.     delay(500);
  83.     if(cnt > 20){
  84.       return false;
  85.     }
  86.     #ifdef debug
  87.     Serial.print(".");
  88.     #endif
  89.   }
  90.  
  91.   //接続に成功。IPアドレスを表示
  92.   #ifdef debug
  93.   Serial.println(cnt);
  94.   Serial.print("Connected! IP address: ");
  95.   Serial.println(WiFi.localIP());
  96.   #endif
  97.   return true;
  98. }
  99.  
  100.  
  101. //Line通知
  102. void sendLineNotify() {
  103.   const char* host = "notify-api.line.me";
  104.   const char* token = "TOKEN WO IRETENE";
  105.   const char* message = "%f0%9f%8d%9a"; //ごはんの絵文字をURLエンコードしたもの
  106.   BearSSL::WiFiClientSecure client;
  107.   client.setInsecure();
  108.   //WiFiClientSecure client;
  109.   #ifdef debug
  110.   Serial.println("Try");
  111.   #endif
  112.   //LineのAPIサーバに接続
  113.   if (!client.connect(host, 443)) {
  114.     #ifdef debug
  115.     Serial.println("Connection failed");
  116.     #endif
  117.     return;
  118.   }
  119.   Serial.println("Connected");
  120.   //リクエストを送信
  121.   String query = String("message=") + String(message);
  122.   String request = String("") +
  123.                "POST /api/notify HTTP/1.1\r\n" +
  124.                "Host: " + host + "\r\n" +
  125.                "Authorization: Bearer " + token + "\r\n" +
  126.                "Content-Length: " + String(query.length()) +  "\r\n" +
  127.                "Content-Type: application/x-www-form-urlencoded\r\n\r\n" +
  128.                 query + "\r\n";
  129.   client.print(request);
  130.  
  131.   //受信終了まで待つ
  132.   while (client.connected()) {
  133.     String line = client.readStringUntil('\n');
  134.     if (line == "\r") {
  135.       break;
  136.     }
  137.   }
  138.  
  139.   String line = client.readStringUntil('\n');
  140.   Serial.println(line);
  141. }
  142.  
  143.  
  144.  
  145.  
  146.  
  147. bool nearby = false;
  148. bool lastStatus;
  149. unsigned int stateCount = 0;
  150.  
  151.  
  152. void MDCallback(void *cbData, const char *type, bool isUnicode, const char *string)
  153. {
  154.   (void)cbData;
  155.   Serial.printf("ID3 callback for: %s = '", type);
  156.  
  157.   if (isUnicode) {
  158.     string += 2;
  159.   }
  160.  
  161.   while (*string) {
  162.     char a = *(string++);
  163.     if (isUnicode) {
  164.       string++;
  165.     }
  166.     Serial.printf("%c", a);
  167.   }
  168.   Serial.printf("'\n");
  169.   Serial.flush();
  170. }
  171.  
  172.  
  173. void setup() {
  174.   //初期設定
  175.   WiFi.mode(WIFI_OFF);
  176.   WiFi.begin();
  177.   Serial.begin(115200);
  178.   SPIFFS.begin();
  179.   Serial.printf("Sample MP3 playback begins...\n");
  180.   audioLogger = &Serial;
  181.   unsigned int ran = random(0, 9);
  182.   String filename = "/" + String(ran) +".mp3";
  183.   char charBuf[7];
  184.   filename.toCharArray(charBuf, 7);
  185.   Serial.println(charBuf);
  186.   file = new AudioFileSourceSPIFFS(charBuf);
  187.   id3 = new AudioFileSourceID3(file);
  188.   id3->RegisterMetadataCB(MDCallback, (void*)"ID3TAG");
  189.   out = new AudioOutputI2SNoDAC();
  190.   mp3 = new AudioGeneratorMP3();
  191.   mp3->begin(id3, out);
  192.  
  193.   while(1){
  194.     if (mp3->isRunning()) {
  195.       if (!mp3->loop()) mp3->stop();
  196.     } else {
  197.       Serial.printf("MP3 done\n");
  198.       if(wifiConnect() == true){
  199.         sendLineNotify();
  200.       }else{
  201.         #ifdef debug
  202.         Serial.println("");
  203.         Serial.println("No SSID available");
  204.         Serial.println("Change mode to AP mode");
  205.         #endif
  206.         digitalWrite(16, LOW);//シャットダウン
  207.         WiFi.mode(WIFI_AP);
  208.         WiFi.softAP("Sugi-Switch");
  209.        
  210.       }
  211.       break;
  212.     }
  213.   }
  214.  
  215.   pinMode(16, OUTPUT);
  216.   digitalWrite(16, LOW);//シャットダウン
  217.   delay(10000);
  218.   digitalWrite(16, HIGH);
  219.  
  220.  if (MDNS.begin("sugi")) {
  221.     Serial.println("MDNS responder started");
  222.   }
  223.   server.on("/", [](){
  224.   server.sendHeader(F("Content-Encoding"), F("gzip"));
  225.   server.send(200, "text/html", (const char*)index_html_gz, index_html_gz_len);
  226.   });
  227.   server.on("/setup", wifimgr_top);
  228.   server.on("/wifiinput", HTTP_GET, wifiinput);
  229.   server.on("/wifiset", HTTP_GET, wifiset);
  230.   server.on("/reboot", reboot);
  231.   server.on("/doreboot", doreboot);
  232.   server.on("/list", HTTP_GET, handleFileList);
  233.   server.on("/upload.cgi", HTTP_POST, [](){
  234.     server.send(200, "text/plain", "{\"success\":1}");
  235.   }, handleFileUpload);
  236.  
  237.   //server.onNotFound(handleNotFound);
  238.  
  239.   server.begin();
  240.   #ifdef debug
  241.   Serial.println("HTTP server started");
  242.   #endif
  243. }
  244.  
  245.  
  246. void loop() {
  247.   server.handleClient();
  248.   MDNS.update();
  249. }
  250.  
  251. void handleFileUpload()
  252. {
  253.   HTTPUpload& upload = server.upload();
  254.   if(upload.status == UPLOAD_FILE_START)
  255.   {
  256.     String filename = upload.filename;
  257.     if(!filename.startsWith("/"))
  258.       filename = "/"+filename;
  259.     Serial.print("handleFileUpload Name: "); Serial.println(filename);
  260.     fsUploadFile = SPIFFS.open(filename, "w");
  261.   } else if(upload.status == UPLOAD_FILE_WRITE)
  262.   {
  263.     if(fsUploadFile)
  264.       fsUploadFile.write(upload.buf, upload.currentSize);
  265.   } else if(upload.status == UPLOAD_FILE_END)
  266.   {
  267.     if(fsUploadFile)
  268.       fsUploadFile.close();
  269.     Serial.print("handleFileUpload Size: "); Serial.println(upload.totalSize);
  270.   }
  271. }
  272.  
  273. void handleFileList()
  274. {
  275.   String path = "/";
  276.   // Assuming there are no subdirectories
  277.   Dir dir = SPIFFS.openDir(path);
  278.   String output = "[{\"filename\":\"";
  279.   while(dir.next())
  280.   {
  281.     File entry = dir.openFile("r");
  282.     // Separate by comma if there are multiple files
  283.     if(output != "[{\"name\":\"")
  284.       output += "\"},{\"name\":\"";
  285.     output += String(entry.name()).substring(1);
  286.     //output += ",\"id\":";
  287.     //output += String(millis());
  288.     entry.close();
  289.   }
  290.   output += "\"}]";
  291.   server.send(200, "text/plain", output);
  292. }
  293.  
  294. // scan SSID
  295. #define SSIDLIMIT 30
  296. String ssid_rssi_str[SSIDLIMIT];
  297. String ssid_str[SSIDLIMIT];
  298.  
  299. String ssid;
  300. String passwd;
  301.  
  302.  
  303.  
  304. //*******************************************
  305.  
  306.  
  307.  
  308.  
  309. String maskpasswd(String passwd){
  310.   String maskpasswd = "";
  311.  
  312.   for (int i=0; i<passwd.length(); i++) maskpasswd = maskpasswd + "*";
  313.   if (passwd.length() == 0) maskpasswd = "(null)";
  314.  
  315.   return maskpasswd;
  316. }
  317.  
  318. void wifimgr_top() {
  319.  
  320.   String html = Headder_str();
  321.   html += "<a href='/wifiinput'>WIFI setup</a>";
  322.   html += "<hr><h3>Current Settings</h3>";
  323.   html += "SSID: " + ssid + "<br>";
  324.   html += "passwd: " + maskpasswd(passwd) + "<br>";
  325.   html += "<hr><p><center><a href='/reboot'>Reboot</a></center>";
  326.   html += "</body></html>";
  327.   server.send(200, "text/html", html);
  328. }
  329.  
  330. String Headder_str() {
  331.   String html = "";
  332.   html += "<!DOCTYPE html><html><head>";
  333.   html += "<meta name='viewport' content='width=device-width, initial-scale=1.3'>";
  334.   html += "<meta http-equiv='Pragma' content='no-cache'>";
  335.   html += "<meta http-equiv='Cache-Control' content='no-cache'></head>";
  336.   html += "<meta http-equiv='Expires' content='0'>";
  337.   html += "<style>";
  338.   html += "a:link, a:visited { background-color: #009900; color: white; padding: 5px 15px;";
  339.   html += "text-align: center; text-decoration: none;  display: inline-block;}";
  340.   html += "a:hover, a:active { background-color: green;}";
  341.   html += "bo32{ background-color: #EEEEEE;}";
  342.   html += "input[type=button], input[type=submit], input[type=reset] {";
  343.   html += "background-color: #000099;  border: none;  color: white;  padding: 5px 20px;";
  344.   html += "text-decoration: none;  margin: 4px 2px;";
  345.   html += "</style>";
  346.   html += "<body>";
  347.   html += "<h2>Sugi Switch Setup</h2>";
  348.   return html;
  349. }
  350.  
  351.  
  352.  
  353. void wifiinput() {
  354.   String html = Headder_str();
  355.   html += "<a href='/'>TOP</a> ";
  356.   html += "<hr><p>";
  357.   html += "<h3>WiFi Selector</h3>";
  358.   html += WIFI_Form_str();
  359.   html += "<br><hr><p><center><a href='/'>Cancel</a></center>";
  360.   html += "</body></html>";
  361.   server.send(200, "text/html", html);
  362. }
  363.  
  364. //*******************************************
  365. String WIFI_Form_str(){
  366.  
  367.   Serial.println("wifi scan start");
  368.  
  369.   // WiFi.scanNetworks will return the number of networks found
  370.   uint8_t ssid_num = WiFi.scanNetworks();
  371.   Serial.println("scan done\r\n");
  372.  
  373.   if (ssid_num == 0) {
  374.     Serial.println("no networks found");
  375.   } else {
  376.     Serial.printf("%d networks found\r\n\r\n", ssid_num);
  377.     if (ssid_num > SSIDLIMIT) ssid_num = SSIDLIMIT;
  378.     for (int i = 0; i < ssid_num; ++i) {
  379.       ssid_str[i] = WiFi.SSID(i);
  380.       String wifi_auth_open = ((WiFi.encryptionType(i) == 7)?" ":"*");
  381.       ssid_rssi_str[i] = ssid_str[i] + " (" + WiFi.RSSI(i) + "dBm)" + wifi_auth_open;
  382.       ssid_rssi_str[i] = ssid_str[i] + wifi_auth_open;
  383.       Serial.printf("%d: %s\r\n", i, ssid_rssi_str[i].c_str());
  384.       delay(10);
  385.     }
  386.   }
  387.  
  388.   String str = "";
  389.   str += "<form action='/wifiset' method='get'>";
  390.   str += "<select name='ssid' id ='ssid'>";
  391.   for(int i=0; i<ssid_num; i++){
  392.     str += "<option value=" + ssid_str[i] + ">" + ssid_rssi_str[i] + "</option>";
  393.   }
  394.   str += "<option value=" + ssid + ">" + ssid + "(current)</option>";
  395. //  if (ssid != defaultSSID){
  396. //    str += "<option value=" + defaultSSID + ">" + defaultSSID + "(default)</option>";
  397. //  }
  398.   str += "</select><br>\r\n";
  399.   str += "Password<br><input type='password' name='passwd' value='" + passwd + "'>";
  400.   str += "<br><input type='submit' value='set'>";
  401.   str += "</form><br>";
  402.   str += "<script>document.getElementById('ssid').value = '"+ ssid +"';</script>";
  403.   return str;
  404. }
  405.  
  406.  
  407. void wifiset(){
  408.  
  409.   ssid = server.arg("ssid");
  410.   passwd = server.arg("passwd");
  411.   ssid.trim();
  412.   passwd.trim();
  413.  
  414.   Serial.println(ssid);
  415.  
  416.   //WriteConfigFile();
  417.   WiFi.begin(ssid.c_str(), passwd.c_str());
  418.    
  419.   // 「/」に転送
  420.   server.sendHeader("Location", String("http://cam.local"), true);
  421.   server.send(302, "text/plain", "");
  422.   ESP.restart();
  423. }
  424.  
  425. void reboot() {
  426.   String html = Headder_str();
  427.   html += "<hr><p>";
  428.   html += "<h3>reboot confirmation</h3><p>";
  429.   html += "Are you sure to reboot?<p>";
  430.   html += "<center><a href='/doreboot'>YES</a> <a href='/'>no</a></center>";
  431.   html += "<p><hr>";
  432.   html += "</body></html>";
  433.   server.send(200, "text/html", html);
  434. }
  435.  
  436. void doreboot() {
  437.   String html = Headder_str();
  438.   html += "<hr><p>";
  439.   html += "<h3>rebooting</h3><p>";
  440.   html += "The setting WiFi connection will be disconnected...<p>";
  441.   html += "<hr>";
  442.   html += "</body></html>";
  443.   server.send(200, "text/html", html);
  444.  
  445.   // reboot esp32
  446.   Serial.println("reboot esp32 now.");
  447.   //digitalWrite(LED_BUILTIN, LOW);
  448.    
  449.   delay(2000); // hold 2 sec
  450.   ESP.restart(); // restart ESP32
  451. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement