Advertisement
michalmonday

Esp8266 - supremeDuck debugging

Dec 5th, 2018
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*   It should be uploaded to Esp8266 (not Arduino)
  2.  *   The code below is based on WiFiAccessPoint example (https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266WiFi/examples/WiFiAccessPoint/WiFiAccessPoint.ino)
  3.  *
  4.  * Copyright (c) 2015, Majenko Technologies
  5.  * All rights reserved.
  6.  *
  7.  * Redistribution and use in source and binary forms, with or without modification,
  8.  * are permitted provided that the following conditions are met:
  9.  *
  10.  * * Redistributions of source code must retain the above copyright notice, this
  11.  *   list of conditions and the following disclaimer.
  12.  *
  13.  * * Redistributions in binary form must reproduce the above copyright notice, this
  14.  *   list of conditions and the following disclaimer in the documentation and/or
  15.  *   other materials provided with the distribution.
  16.  *
  17.  * * Neither the name of Majenko Technologies nor the names of its
  18.  *   contributors may be used to endorse or promote products derived from
  19.  *   this software without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  23.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  24.  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  25.  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  26.  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  27.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  28.  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31.  */
  32.  
  33. /* Create a WiFi access point and provide a web server on it. */
  34.  
  35. #include <ESP8266WiFi.h>
  36. #include <WiFiClient.h>
  37. #include <ESP8266WebServer.h>
  38. #include <EEPROM.h>
  39.  
  40. /* Set these to your desired credentials. */
  41. #define SSID_LEN 32
  42. #define PASS_LEN 32
  43. char ssid[SSID_LEN] = "supremeDuck\0";
  44. char password[PASS_LEN] = "37373737\0";
  45.  
  46. #define MAX_RESPONSE 500
  47. char response[MAX_RESPONSE] = {0};
  48.  
  49. #define EEPROM_ADDRESS_AP_NAME_CHECK 0
  50. #define EEPROM_ADDRESS_AP_NAME 4
  51.  
  52. #define EEPROM_ADDRESS_AP_PASS_CHECK 36
  53. #define EEPROM_ADDRESS_AP_PASS 40
  54.  
  55.  
  56. void HandleData();
  57. void Read_AP_EEPROM_Creds();
  58.  
  59. ESP8266WebServer server(80);
  60.  
  61.  
  62.  
  63. void setup() {  
  64.   delay(1000);
  65.   Serial.begin(115200);
  66.   while(!Serial){}
  67.  
  68.   Serial.println("Setup function started");
  69.   Serial.print("Reading EEPROM credentials");
  70.  
  71.   Read_AP_EEPROM_Creds();             // read previously saved credentials (if by some chance they're incorrect then the last proper ones are used, that's how esp8266 seems to work)
  72.   /* You can remove the password parameter if you want the AP to be open. */
  73.  
  74.   Serial.println("Setting access point");
  75.   Serial.println(WiFi.softAP(ssid, password) ? "Success:)" : "Fail:(");
  76.  
  77.   Serial.println("Checking IP of the created access point");
  78.   IPAddress myIP = WiFi.softAPIP();
  79.   Serial.print("AP IP address: ");
  80.   Serial.println(myIP);
  81.  
  82.   Serial.println("Setting up routing");
  83.  
  84.   server.on("/", [](){
  85.     server.send(200, "text/plain", "Connected");
  86.   });
  87.  
  88.   server.on("/connection_status", [](){
  89.     server.send(200, "text/plain", "Connected");
  90.   });
  91.  
  92.   server.on("/ap_name_change", HTTP_POST, [](){    
  93.     if( ! server.hasArg("data") || server.arg("data") == NULL) { // If the POST request doesn't have username and password data
  94.       server.send(400, "text/plain", "400: Invalid Request");         // The request is invalid, so send HTTP status 400
  95.     }else{
  96.       EEPROM.begin(512);
  97.  
  98.       EEPROM.put(EEPROM_ADDRESS_AP_NAME_CHECK, (int)777);
  99.       //EEPROM.put(EEPROM_ADDRESS_AP_NAME_LEN, (int)server.arg("data").length());
  100.       char buff[32] = {0};
  101.       strcpy(buff, String(server.arg("data")).c_str());
  102.       EEPROM.put(EEPROM_ADDRESS_AP_NAME, buff);
  103.       //EEPROM.put(EEPROM_ADDRESS_AP_NAME, String(server.arg("data")).c_str());     // idk why but it does not write to eeprom properly...
  104.       EEPROM.commit();
  105.       EEPROM.end();
  106.       server.send(200, "text/plain", "OK");
  107.     }
  108.   });  
  109.  
  110.   server.on("/ap_pass_change", HTTP_POST, [](){
  111.     if( ! server.hasArg("data") || server.arg("data") == NULL) { // If the POST request doesn't have username and password data
  112.       server.send(400, "text/plain", "400: Invalid Request");         // The request is invalid, so send HTTP status 400
  113.     }else{
  114.       EEPROM.begin(512);
  115.       EEPROM.put(EEPROM_ADDRESS_AP_PASS_CHECK, (int)777);
  116.       char buff[32] = {0};
  117.       strcpy(buff, String(server.arg("data")).c_str());
  118.       EEPROM.put(EEPROM_ADDRESS_AP_PASS, buff);
  119.       EEPROM.commit();
  120.       EEPROM.end();
  121.       server.send(200, "text/plain", "OK");
  122.     }
  123.   });
  124.  
  125.   server.on("/ducky_script_ready_check", [](){
  126.     if(Serial.available() == 2){
  127.       char buff[3] = {0};
  128.       buff[0] = Serial.read();
  129.       buff[1] = Serial.read();
  130.       server.send(200, "text/plain", buff);
  131.     }else{
  132.       server.send(200);
  133.     }
  134.   });
  135.  
  136.   server.on("/data", HTTP_POST, HandleData);
  137.  
  138.   server.onNotFound([](){
  139.     server.send(404, "text/plain", "404: Not found");
  140.   });
  141.  
  142.   Serial.println("Starting server");
  143.  
  144.   server.begin();
  145.   Serial.println("HTTP server should be started");
  146. }
  147.  
  148. void loop() {
  149.   server.handleClient();
  150. }
  151.  
  152.  
  153.  
  154. void HandleData(){
  155.   /*
  156.   Serial.print("\nPOST_DATA:");
  157.   for(int i = 0; i < server.args(); i++){
  158.     Serial.print(server.argName(i) + " : " + server.arg(i));
  159.     Serial.println("");
  160.   }
  161.   Serial.print("HEADERS:");
  162.   for(int i = 0; i < server.headers(); i++){
  163.     Serial.print(server.headerName(i) + " : " + server.header(i));
  164.     Serial.println("");
  165.   }  
  166.   */
  167.  
  168.   if( ! server.hasArg("data") || server.arg("data") == NULL) { // If the POST request doesn't have username and password data
  169.     server.send(400, "text/plain", "400: Invalid Request");         // The request is invalid, so send HTTP status 400
  170.     //Serial.print("400: Invalid Request");
  171.     return;
  172.   }
  173.  
  174.   while(Serial.available() > 0){
  175.     char c = Serial.read(); // get rid of all unread data
  176.   }
  177.  
  178.   Serial.print(server.arg("data"));     // pass data from mobile app to arduino
  179.  
  180.   int i = 0;  
  181.   memset(response, 0, MAX_RESPONSE);
  182.  
  183.   if(server.arg("data") == "VER" || server.arg("data") == "Request_info"){
  184.     delay(500);       // wait for response to send it back (but only for these to avoid delaying mouse movement for example)
  185.   }
  186.  
  187.   while(Serial.available() > 0 && i < (MAX_RESPONSE-1)){
  188.     response[i++] = Serial.read();
  189.   }
  190.  
  191.   if(i > 0){
  192.     server.send(200, "text/plain", response);
  193.     //Serial.println("RECEIVED: " + String(response));
  194.   }else{
  195.     server.send(200);  
  196.   }
  197. }
  198.  
  199. void Read_AP_EEPROM_Creds(){
  200.   EEPROM.begin(512);  
  201.   int useCustomName=0;
  202.   EEPROM.get(EEPROM_ADDRESS_AP_NAME_CHECK, useCustomName);
  203.   Serial.println(String(useCustomName));
  204.   if(useCustomName == 777){
  205.     memset(ssid, 0, SSID_LEN);
  206.     EEPROM.get(EEPROM_ADDRESS_AP_NAME, ssid);
  207.   }
  208.   int useCustomPass=0;
  209.   EEPROM.get(EEPROM_ADDRESS_AP_PASS_CHECK, useCustomPass);
  210.   if(useCustomPass == 777){
  211.     memset(password, 0, PASS_LEN);
  212.     EEPROM.get(EEPROM_ADDRESS_AP_PASS, password);
  213.   }
  214.   EEPROM.end();
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement