diff --git a/RX_FSK/RX_FSK.ino b/RX_FSK/RX_FSK.ino index 7352a4c..2e79283 100644 --- a/RX_FSK/RX_FSK.ino +++ b/RX_FSK/RX_FSK.ino @@ -1,7 +1,11 @@ +#include +#include +#include + #include #include +#include -#include #include #include @@ -24,12 +28,14 @@ U8X8_SSD1306_128X64_NONAME_SW_I2C u8x8(/* clock=*/ OLED_SCL, /* data=*/ OLED_SDA int e; char my_packet[100]; -const char* ssid = "DinoGast"; -const char* password = "Schokolade"; +//const char* ssid = "DinoGast"; +//const char* password = "Schokolade"; +const char *ssid="AndroidDD"; +const char *password="dl9rdzhr"; -WiFiServer server(80); +AsyncWebServer server(80); -pthread_t wifithread; +//pthread_t wifithread; int conn = 0; @@ -37,6 +43,95 @@ String currentLine; WiFiClient client; unsigned long lastdu; + +// Set LED GPIO +const int ledPin = 2; +// Stores LED state +String ledState; + + +// Replaces placeholder with LED state value +String processor(const String& var){ + Serial.println(var); + if(var == "STATE"){ + if(digitalRead(ledPin)){ + ledState = "ON"; + } + else{ + ledState = "OFF"; + } + Serial.print(ledState); + return ledState; + } + return String(); +} + +void SetupAsyncServer() { +// Route for root / web page + server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){ + request->send(200, "text/plain", "Hello, world"); + }); + + server.on("/index.html", HTTP_GET, [](AsyncWebServerRequest *request){ + request->send(SPIFFS, "/index.html", String(), false, processor); + }); + + // Route to load style.css file + server.on("/style.css", HTTP_GET, [](AsyncWebServerRequest *request){ + request->send(SPIFFS, "/style.css", "text/css"); + }); + + // Route to set GPIO to HIGH + server.on("/on", HTTP_GET, [](AsyncWebServerRequest *request){ + digitalWrite(ledPin, HIGH); + request->send(SPIFFS, "/index.html", String(), false, processor); + }); + + // Route to set GPIO to LOW + server.on("/off", HTTP_GET, [](AsyncWebServerRequest *request){ + digitalWrite(ledPin, LOW); + request->send(SPIFFS, "/index.html", String(), false, processor); + }); + + // Start server + server.begin(); +} + +int nNetworks; +struct { String id; String pw; } networks[20]; + +void setupWifiList() { + File file = SPIFFS.open("/networks.txt", "r"); + if(!file){ + Serial.println("There was an error opening the file '/networks.txt' for reading"); + return; + } + int i=0; + while(file.available()) { + String line = file.readStringUntil('\n'); + if(!file.available()) break; + networks[i].id = line; + networks[i].pw = file.readStringUntil('\n'); + i++; + } + nNetworks = i; + Serial.print(i); Serial.println(" networks in networks.txt\n"); + for(int j=0; j1500) { if(elapsed<4000) { button1.pressed=KP_MID; } else { button1.pressed=KP_LONG; } } + else { button1.pressed=KP_SHORT; } + button1.numberKeyPresses += 1; + } +} + +int getKeyPress() { + KeyPress p = button1.pressed; + button1.pressed = KP_NONE; + return p; +} void setup() { @@ -115,23 +238,15 @@ void setup() u8x8.begin(); - pinMode(LORA_LED, OUTPUT); - WiFi.begin(ssid, password); - while(WiFi.status() != WL_CONNECTED) { - delay(500); - Serial.print("."); - } - - Serial.println(""); - Serial.println("WiFi connected"); - Serial.println("IP address: "); - Serial.println(WiFi.localIP()); - - server.begin(); - - xTaskCreatePinnedToCore(wifiloop, "WifiServer", 10240, NULL, 10, NULL, 0); + // Initialize SPIFFS + if(!SPIFFS.begin(true)){ + Serial.println("An Error has occurred while mounting SPIFFS"); + return; + } + + setupWifiList(); rs41.setup(); @@ -154,24 +269,118 @@ void setup() Serial.println(); - Serial.println("Setup finished"); - // int returnValue = pthread_create(&wifithread, NULL, wifiloop, (void *)0); + Serial.println("Setup finished"); + // int returnValue = pthread_create(&wifithread, NULL, wifiloop, (void *)0); - // if (returnValue) { - // Serial.println("An error has occurred"); - // } - // xTaskCreate(mainloop, "MainServer", 10240, NULL, 10, NULL); + // if (returnValue) { + // Serial.println("An error has occurred"); + // } + // xTaskCreate(mainloop, "MainServer", 10240, NULL, 10, NULL); + // Handle button press + attachInterrupt(0, buttonISR, CHANGE); } +enum MainState { ST_DECODER, ST_SCANNER, ST_SPECTRUM, ST_WIFISCAN }; + +static MainState mainState = ST_SPECTRUM; + +void loopDecoder() { + +} + +void loopScanner() { + +} + +void loopSpectrum() { + switch(getKeyPress()) { + case KP_SHORT: /* move selection of peak, TODO */ break; + case KP_MID: /* restart, TODO */ break; + case KP_LONG: mainState = ST_WIFISCAN; return; + case KP_DOUBLE: /* ignore */ break; + default: break; + } + scanner.scan(); + scanner.plotResult(); +} + +String translateEncryptionType(wifi_auth_mode_t encryptionType) { + switch (encryptionType) { + case (WIFI_AUTH_OPEN): + return "Open"; + case (WIFI_AUTH_WEP): + return "WEP"; + case (WIFI_AUTH_WPA_PSK): + return "WPA_PSK"; + case (WIFI_AUTH_WPA2_PSK): + return "WPA2_PSK"; + case (WIFI_AUTH_WPA_WPA2_PSK): + return "WPA_WPA2_PSK"; + case (WIFI_AUTH_WPA2_ENTERPRISE): + return "WPA2_ENTERPRISE"; + } +} + +void loopWifiScan() { + WiFi.mode(WIFI_STA); + const char *id, *pw; + int n = WiFi.scanNetworks(); + for (int i = 0; i < n; i++) { + Serial.print("Network name: "); + Serial.println(WiFi.SSID(i)); + Serial.print("Signal strength: "); + Serial.println(WiFi.RSSI(i)); + Serial.print("MAC address: "); + Serial.println(WiFi.BSSIDstr(i)); + Serial.print("Encryption type: "); + String encryptionTypeDescription = translateEncryptionType(WiFi.encryptionType(i)); + Serial.println(encryptionTypeDescription); + Serial.println("-----------------------"); + id=WiFi.SSID(i).c_str(); + pw=fetchWifiPw(id); + if(pw) break; + } + if(!pw) { id="test"; pw="test"; } + Serial.print("Connecting to: "); Serial.println(id); + WiFi.begin(id, pw); + while(WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + + Serial.println(""); + Serial.println("WiFi connected"); + Serial.println("IP address: "); + Serial.println(WiFi.localIP()); + + SetupAsyncServer(); + delay(5000); + mainState=ST_SPECTRUM; +} + + void loop() { Serial.println("Running main loop"); - + switch(mainState) { + case ST_DECODER: loopDecoder(); break; + case ST_SCANNER: loopScanner(); break; + case ST_SPECTRUM: loopSpectrum(); break; + case ST_WIFISCAN: loopWifiScan(); break; + } +#if 0 + if (button1.pressed) { + Serial.print( ((const char *[]){"SHORT PRESS -","LONG PRESS -","VERYLONG PRESS -"})[button1.pressed-1]); + Serial.printf("Button 1 has been pressed %u times\n", button1.numberKeyPresses); + button1.pressed = false; + } //wifiloop(NULL); //e = dfm.receiveFrame(); - e = rs41.receiveFrame(); - #if 0 + //e = rs41.receiveFrame(); + scanner.scan(); + scanner.plotResult(); + delay(1000); int rssi = sx1278.getRSSI(); Serial.print(" RSSI: "); Serial.print(rssi); diff --git a/RX_FSK/data/index.html b/RX_FSK/data/index.html new file mode 100644 index 0000000..952beba --- /dev/null +++ b/RX_FSK/data/index.html @@ -0,0 +1,16 @@ + + + + ESP32 Web Server + + + + + +

ESP32 Web Server

+

GPIO state: %STATE%

+

+

+ + + diff --git a/RX_FSK/data/networks.txt b/RX_FSK/data/networks.txt new file mode 100644 index 0000000..a0b956a --- /dev/null +++ b/RX_FSK/data/networks.txt @@ -0,0 +1,4 @@ +DinoGast +Schokolade +AndroidDD +dl9rdzhr diff --git a/RX_FSK/data/style.css b/RX_FSK/data/style.css new file mode 100644 index 0000000..50f94f8 --- /dev/null +++ b/RX_FSK/data/style.css @@ -0,0 +1,28 @@ +html { + font-family: Helvetica; + display: inline-block; + margin: 0px auto; + text-align: center; +} +h1{ + color: #0F3376; + padding: 2vh; +} +p{ + font-size: 1.5rem; +} +.button { + display: inline-block; + background-color: #008CBA; + border: none; + border-radius: 4px; + color: white; + padding: 16px 40px; + text-decoration: none; + font-size: 30px; + margin: 2px; + cursor: pointer; +} +.button2 { + background-color: #f44336; +}