some more testing

This commit is contained in:
Hans P. Reiser 2019-04-04 23:46:37 +02:00
parent 34f9971143
commit d02d687670
4 changed files with 286 additions and 29 deletions

View File

@ -1,7 +1,11 @@
#include <WiFi.h>
#include <ESPAsyncWebServer.h>
#include <SPIFFS.h>
#include <U8x8lib.h>
#include <Sonde.h>
#include <Scanner.h>
#include <WiFi.h>
#include <RS41.h>
#include <SX1278FSK.h>
@ -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; j<i; j++) { Serial.print(networks[j].id); Serial.print(": "); Serial.println(networks[j].pw); }
}
const char *fetchWifiPw(const char *id) {
for(int i=0; i<nNetworks; i++) {
Serial.print("Comparing '");
Serial.print(id);
Serial.print("' and '");
Serial.print(networks[i].id.c_str());
Serial.println("'");
if(strcmp(id,networks[i].id.c_str())==0) return networks[i].pw.c_str();
}
return NULL;
}
#if 0
void wifiloop(void *arg){
lastdu=millis();
while(true) {
@ -107,6 +202,34 @@ void wifiloop(void *arg){
}
}
}
#endif
enum KeyPress { KP_NONE, KP_SHORT, KP_DOUBLE, KP_MID, KP_LONG };
struct Button {
const uint8_t PIN;
uint32_t numberKeyPresses;
KeyPress pressed;
unsigned long press;
};
Button button1 = {0, 0, KP_NONE, 0};
void IRAM_ATTR buttonISR() {
if(digitalRead(0)==0) { // Button down
button1.press = millis();
} else { //Button up
unsigned int elapsed = millis()-button1.press;
if(elapsed>1500) { 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);

16
RX_FSK/data/index.html Normal file
View File

@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<title>ESP32 Web Server</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" href="data:,">
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<h1>ESP32 Web Server</h1>
<p>GPIO state: <strong> %STATE%</strong></p>
<p><a href="/on"><button class="button">ON</button></a></p>
<p><a href="/off"><button class="button button2">OFF</button></a></p>
</body>
</html>

4
RX_FSK/data/networks.txt Normal file
View File

@ -0,0 +1,4 @@
DinoGast
Schokolade
AndroidDD
dl9rdzhr

28
RX_FSK/data/style.css Normal file
View File

@ -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;
}