1
0
mirror of https://github.com/Adam-Ant/MQTT-nRF24-Bridge synced 2024-12-20 07:34:35 +00:00

Add data saving on form submission

This commit is contained in:
Adam Dodman 2018-01-29 16:06:28 +00:00
parent 1a1001bca3
commit dbb8e587f2
2 changed files with 54 additions and 7 deletions

View File

@ -26,7 +26,7 @@
<body> <body>
<h1><br>ESP8266 Server - Getting input from a client</h1> <h1><br>ESP8266 Server - Getting input from a client</h1>
<form action='/submit' method='POST'> <form action='/post' method='POST'>
<table id='dynamicInput'> <table id='dynamicInput'>
<tr> <tr>
<th>MQTT Address</th> <th>MQTT Address</th>

View File

@ -19,9 +19,55 @@ RF24Network network(radio);
RF24Mesh mesh(radio,network); RF24Mesh mesh(radio,network);
ESP8266WebServer http(80); ESP8266WebServer http(80);
String webpage = ""; String webpage = "";
bool saveData() {
if (http.args() > 0) { // We have some arguments
bool foundData = false;
int argPos;
// Verify the json, and load it into spiffs
for ( uint8_t i = 0; i < http.args(); i++ ) {
if (http.argName(i) == "mqtt0") { //TODO - Replace with actual post data field name
argPos = i; //Log its place to load later
foundData = true;
}
}
if (!foundData) {
Serial.println("POST argument not found");
return false;
}
DynamicJsonBuffer jsonBuff;
JsonObject& json = jsonBuff.parseObject(http.arg(argPos));
if (!json.success()) {
// Bad json. Dont write to file
Serial.println("Bad JSON from web!");
return false;
}
File routingFile = SPIFFS.open("/routes.json", "w");
if (!routingFile) {
Serial.println("Failed to open route file file for writing");
return false;
}
json.printTo(routingFile);
routingFile.close();
json.prettyPrintTo(Serial);
// Redirect back to the home page using a get request
http.sendHeader("Location", "/", true);
http.send(303, "text/plain", "");
}
else {
Serial.println("No Arguments sent");
http.send(200, "text/html", "No data"); //TODO - better status code, or handle this outside of this function?
}
}
void setup() { void setup() {
Serial.begin(115200); Serial.begin(115200);
@ -33,6 +79,11 @@ void setup() {
// Connect to the mesh // Connect to the mesh
mesh.begin(); mesh.begin();
if (!SPIFFS.begin()) {
Serial.println("Failed to mount file system");
return;
}
WiFi.begin(ssid, password); WiFi.begin(ssid, password);
// Wait for connection // Wait for connection
@ -57,13 +108,9 @@ void setup() {
}); });
http.serveStatic("/", SPIFFS, "/index.html"); http.serveStatic("/", SPIFFS, "/index.html");
http.on("/post", saveData);
http.begin(); http.begin();
if (!SPIFFS.begin()) {
Serial.println("Failed to mount file system");
return;
}
//TODO: Add SPiffs web site. JQuery to jsonify and post. Post JSON data, parse and validate, and store. Call load function to load it into arrays. MQTT message routing. Connected clients on wesbite, js to update //TODO: Add SPiffs web site. JQuery to jsonify and post. Post JSON data, parse and validate, and store. Call load function to load it into arrays. MQTT message routing. Connected clients on wesbite, js to update