mirror of
https://github.com/meshtastic/firmware.git
synced 2025-02-05 04:10:07 +00:00
212 lines
6.2 KiB
C++
212 lines
6.2 KiB
C++
|
#include "meshwifi.h"
|
||
|
#include <WiFi.h>
|
||
|
#include "configuration.h"
|
||
|
#include "main.h"
|
||
|
#include "NodeDB.h"
|
||
|
#include <WebServer.h>
|
||
|
|
||
|
WebServer webserver(80);
|
||
|
|
||
|
String something = "";
|
||
|
String sender = "";
|
||
|
|
||
|
void initWebServer() {
|
||
|
webserver.onNotFound(handleNotFound);
|
||
|
webserver.on("/", handleJSONChatHistory);
|
||
|
webserver.on("/json/chat/history", handleJSONChatHistory);
|
||
|
//webserver.on("/", []() {
|
||
|
//webserver.send(200, "text/plain", "everything is awesome!");
|
||
|
//});
|
||
|
webserver.begin();
|
||
|
|
||
|
}
|
||
|
|
||
|
void initWifi()
|
||
|
{
|
||
|
strcpy(radioConfig.preferences.wifi_ssid, WiFi_SSID_NAME);
|
||
|
strcpy(radioConfig.preferences.wifi_password, WiFi_SSID_PASSWORD);
|
||
|
if (radioConfig.has_preferences) {
|
||
|
const char *wifiName = radioConfig.preferences.wifi_ssid;
|
||
|
|
||
|
if (*wifiName) {
|
||
|
const char *wifiPsw = radioConfig.preferences.wifi_password;
|
||
|
if (radioConfig.preferences.wifi_ap_mode) {
|
||
|
DEBUG_MSG("STARTING WIFI AP: ssid=%s, ok=%d\n", wifiName, WiFi.softAP(wifiName, wifiPsw));
|
||
|
} else {
|
||
|
WiFi.mode(WIFI_MODE_STA);
|
||
|
WiFi.onEvent(WiFiEvent);
|
||
|
|
||
|
DEBUG_MSG("JOINING WIFI: ssid=%s\n", wifiName);
|
||
|
if (WiFi.begin(wifiName, wifiPsw) == WL_CONNECTED) {
|
||
|
DEBUG_MSG("MY IP ADDRESS: %s\n", WiFi.localIP().toString().c_str());
|
||
|
} else {
|
||
|
DEBUG_MSG("Started Joining WIFI\n");
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
} else
|
||
|
DEBUG_MSG("Not using WIFI\n");
|
||
|
}
|
||
|
|
||
|
void WiFiEvent(WiFiEvent_t event)
|
||
|
{
|
||
|
DEBUG_MSG("************ [WiFi-event] event: %d ************\n", event);
|
||
|
|
||
|
switch (event) {
|
||
|
case SYSTEM_EVENT_WIFI_READY:
|
||
|
DEBUG_MSG("WiFi interface ready");
|
||
|
break;
|
||
|
case SYSTEM_EVENT_SCAN_DONE:
|
||
|
DEBUG_MSG("Completed scan for access points");
|
||
|
break;
|
||
|
case SYSTEM_EVENT_STA_START:
|
||
|
DEBUG_MSG("WiFi client started");
|
||
|
break;
|
||
|
case SYSTEM_EVENT_STA_STOP:
|
||
|
DEBUG_MSG("WiFi clients stopped");
|
||
|
break;
|
||
|
case SYSTEM_EVENT_STA_CONNECTED:
|
||
|
DEBUG_MSG("Connected to access point");
|
||
|
break;
|
||
|
case SYSTEM_EVENT_STA_DISCONNECTED:
|
||
|
DEBUG_MSG("Disconnected from WiFi access point");
|
||
|
|
||
|
// Reconnect WiFi
|
||
|
reconnectWiFi();
|
||
|
break;
|
||
|
case SYSTEM_EVENT_STA_AUTHMODE_CHANGE:
|
||
|
DEBUG_MSG("Authentication mode of access point has changed");
|
||
|
break;
|
||
|
case SYSTEM_EVENT_STA_GOT_IP:
|
||
|
DEBUG_MSG("Obtained IP address: ");
|
||
|
Serial.println(WiFi.localIP());
|
||
|
|
||
|
// Start web server
|
||
|
initWebServer();
|
||
|
break;
|
||
|
case SYSTEM_EVENT_STA_LOST_IP:
|
||
|
DEBUG_MSG("Lost IP address and IP address is reset to 0");
|
||
|
break;
|
||
|
case SYSTEM_EVENT_STA_WPS_ER_SUCCESS:
|
||
|
DEBUG_MSG("WiFi Protected Setup (WPS): succeeded in enrollee mode");
|
||
|
break;
|
||
|
case SYSTEM_EVENT_STA_WPS_ER_FAILED:
|
||
|
DEBUG_MSG("WiFi Protected Setup (WPS): failed in enrollee mode");
|
||
|
break;
|
||
|
case SYSTEM_EVENT_STA_WPS_ER_TIMEOUT:
|
||
|
DEBUG_MSG("WiFi Protected Setup (WPS): timeout in enrollee mode");
|
||
|
break;
|
||
|
case SYSTEM_EVENT_STA_WPS_ER_PIN:
|
||
|
DEBUG_MSG("WiFi Protected Setup (WPS): pin code in enrollee mode");
|
||
|
break;
|
||
|
case SYSTEM_EVENT_AP_START:
|
||
|
DEBUG_MSG("WiFi access point started");
|
||
|
break;
|
||
|
case SYSTEM_EVENT_AP_STOP:
|
||
|
DEBUG_MSG("WiFi access point stopped");
|
||
|
break;
|
||
|
case SYSTEM_EVENT_AP_STACONNECTED:
|
||
|
DEBUG_MSG("Client connected");
|
||
|
break;
|
||
|
case SYSTEM_EVENT_AP_STADISCONNECTED:
|
||
|
DEBUG_MSG("Client disconnected");
|
||
|
break;
|
||
|
case SYSTEM_EVENT_AP_STAIPASSIGNED:
|
||
|
DEBUG_MSG("Assigned IP address to client");
|
||
|
break;
|
||
|
case SYSTEM_EVENT_AP_PROBEREQRECVED:
|
||
|
DEBUG_MSG("Received probe request");
|
||
|
break;
|
||
|
case SYSTEM_EVENT_GOT_IP6:
|
||
|
DEBUG_MSG("IPv6 is preferred");
|
||
|
break;
|
||
|
case SYSTEM_EVENT_ETH_START:
|
||
|
DEBUG_MSG("Ethernet started");
|
||
|
break;
|
||
|
case SYSTEM_EVENT_ETH_STOP:
|
||
|
DEBUG_MSG("Ethernet stopped");
|
||
|
break;
|
||
|
case SYSTEM_EVENT_ETH_CONNECTED:
|
||
|
DEBUG_MSG("Ethernet connected");
|
||
|
break;
|
||
|
case SYSTEM_EVENT_ETH_DISCONNECTED:
|
||
|
DEBUG_MSG("Ethernet disconnected");
|
||
|
break;
|
||
|
case SYSTEM_EVENT_ETH_GOT_IP:
|
||
|
DEBUG_MSG("Obtained IP address");
|
||
|
break;
|
||
|
default: break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void handleJSONChatHistory() {
|
||
|
|
||
|
String out = "";
|
||
|
out += "{\n";
|
||
|
out += " \"data\" : {\n";
|
||
|
out += " \"chat\" : ";
|
||
|
out += "[";
|
||
|
out += "\"" + sender + "\"";
|
||
|
out += ",";
|
||
|
out += "\"" + something + "\"";
|
||
|
out += "]\n";
|
||
|
|
||
|
|
||
|
|
||
|
out += "\n";
|
||
|
out += " }\n";
|
||
|
out += "}\n";
|
||
|
|
||
|
webserver.send ( 200, "application/json", out );
|
||
|
return;
|
||
|
|
||
|
|
||
|
}
|
||
|
|
||
|
void handleWebResponse() {
|
||
|
webserver.handleClient();
|
||
|
}
|
||
|
|
||
|
|
||
|
void handleNotFound() {
|
||
|
String message = "File Not Found\n\n";
|
||
|
message += "URI: ";
|
||
|
message += webserver.uri();
|
||
|
message += "\nMethod: ";
|
||
|
message += (webserver.method() == HTTP_GET) ? "GET" : "POST";
|
||
|
message += "\nArguments: ";
|
||
|
message += webserver.args();
|
||
|
message += "\n";
|
||
|
|
||
|
for (uint8_t i = 0; i < webserver.args(); i++) {
|
||
|
message += " " + webserver.argName(i) + ": " + webserver.arg(i) + "\n";
|
||
|
}
|
||
|
|
||
|
webserver.send(404, "text/plain", message);
|
||
|
/*
|
||
|
*/
|
||
|
}
|
||
|
|
||
|
void reconnectWiFi() {
|
||
|
if ( WiFi.status() != WL_CONNECTED ) {
|
||
|
DEBUG_MSG("... Reconnecting to WiFi access point");
|
||
|
WiFi.begin( );
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
void notifyWebUI() {
|
||
|
DEBUG_MSG("************ Got a message! ************\n");
|
||
|
MeshPacket &mp = devicestate.rx_text_message;
|
||
|
NodeInfo *node = nodeDB.getNode(mp.from);
|
||
|
sender = (node && node->has_user) ? node->user.long_name : "???";
|
||
|
|
||
|
static char tempBuf[256]; // mesh.options says this is MeshPacket.encrypted max_size
|
||
|
assert(mp.decoded.which_payload == SubPacket_data_tag);
|
||
|
snprintf(tempBuf, sizeof(tempBuf), "%s", mp.decoded.data.payload.bytes);
|
||
|
|
||
|
|
||
|
something = tempBuf;
|
||
|
|
||
|
}
|