mirror of
https://github.com/0x1d/esp8266-laser.git
synced 2025-12-15 18:38:20 +01:00
started change to REST API
This commit is contained in:
13
README.md
13
README.md
@@ -4,12 +4,15 @@ Wifi Host for the Arduino-Laser-Spirograph
|
|||||||
HTTP GET endpoints:
|
HTTP GET endpoints:
|
||||||
/motor/{motorNr}/{value}
|
/motor/{motorNr}/{value}
|
||||||
/laser/{value}
|
/laser/{value}
|
||||||
/ssid/{ssidname}
|
|
||||||
/pwd/{password}
|
|
||||||
/apmode/1 -> SofAP (default), 0 -> client
|
|
||||||
/saveconf -> write wificonfig to file
|
/saveconf -> write wificonfig to file
|
||||||
/resetwifi -> reconnect wifi with the new settings
|
/resetwifi -> reconnect wifi with the new settings
|
||||||
/readwifi -> returns wifi settings
|
/wificonfig -> GET returns wifi settings
|
||||||
|
POST sets new wifi settings
|
||||||
|
apMode=0: will try to connect to SSID first, 1: will directly start the AP
|
||||||
|
SSID=ssid
|
||||||
|
password=password
|
||||||
|
save=true will save config to spiffs
|
||||||
|
apply=true will reset wifi and try to connect with new param (buggy atm)
|
||||||
/readvalues -> return laser and motor values (all 0 at startup, ram only)
|
/readvalues -> return laser and motor values (all 0 at startup, ram only)
|
||||||
|
|
||||||
# Install
|
# Install
|
||||||
@@ -25,7 +28,7 @@ HTTP GET endpoints:
|
|||||||
https://github.com/bblanchon/ArduinoJson/
|
https://github.com/bblanchon/ArduinoJson/
|
||||||
|
|
||||||
- follow this instruction:
|
- follow this instruction:
|
||||||
set wifi parameters in data/config.json before upload
|
set wifi parameters in data/wifi.json before upload
|
||||||
http://esp8266.github.io/Arduino/versions/2.0.0/doc/filesystem.html#uploading-files-to-file-system
|
http://esp8266.github.io/Arduino/versions/2.0.0/doc/filesystem.html#uploading-files-to-file-system
|
||||||
|
|
||||||
You may also need to:
|
You may also need to:
|
||||||
|
|||||||
@@ -11,36 +11,47 @@
|
|||||||
#include <SoftwareSerial.h>
|
#include <SoftwareSerial.h>
|
||||||
#include <ArduinoJson.h>
|
#include <ArduinoJson.h>
|
||||||
|
|
||||||
SoftwareSerial laserSerial(14, 12, false, 256);
|
/*
|
||||||
|
some hard coded config, change if needed
|
||||||
|
*/
|
||||||
|
|
||||||
String ssid = "";
|
// Set how long the esp should try to connect to a network before starting it's own AP
|
||||||
String password = "";
|
int wifiTimeout = 15000; //ms
|
||||||
|
|
||||||
int apMode = 1; // 0: client, 1: AP (default) // will be overwriten
|
// Hardcoded Soft AP network parameters
|
||||||
int apModeRuntime = 0;
|
|
||||||
int wifiTimeout = 10000; //ms
|
|
||||||
|
|
||||||
|
|
||||||
// [0] laser[1] m1 [2] m2 [3] m3
|
|
||||||
byte lmValues[4] = {0, 0, 0, 0};
|
|
||||||
|
|
||||||
|
|
||||||
/* Soft AP network parameters */
|
|
||||||
char *ssidAP = "laserAp";
|
char *ssidAP = "laserAp";
|
||||||
IPAddress apIP(192, 168, 4, 1); // note: update metaRefreshStr string if ip change!
|
IPAddress apIP(192, 168, 4, 1);
|
||||||
IPAddress netMsk(255, 255, 255, 0);
|
IPAddress netMsk(255, 255, 255, 0);
|
||||||
|
|
||||||
|
// this is sent to server if invalid request
|
||||||
|
const char *metaRefreshStr = "<head><meta http-equiv=\"refresh\" content=\"3; url=/index.html\" /></head><body><p>redirecting...</p></body>";
|
||||||
|
|
||||||
/* hostname for mDNS. Should work at least on windows. Try http://esp8266.local */
|
/* hostname for mDNS. Should work at least on windows. Try http://esp8266.local */
|
||||||
const char *myHostname = "esplaser";
|
const char *myHostname = "esplaser";
|
||||||
|
|
||||||
const char *metaRefreshStr = "<head><meta http-equiv=\"refresh\" content=\"3; url=/index.html\" /></head><body><p>redirecting...</p></body>";
|
// espsoftwareserial
|
||||||
|
// SoftwareSerial(int receivePin, int transmitPin, bool inverse_logic = false, unsigned int buffSize = 64);
|
||||||
|
SoftwareSerial laserSerial(14, 12, false, 256);
|
||||||
|
|
||||||
|
/*
|
||||||
|
End of hard coded config
|
||||||
|
*/
|
||||||
|
|
||||||
|
String ssid = "";
|
||||||
|
String password = "";
|
||||||
|
|
||||||
|
int apMode = 1; // default value, will be overwriten with config from file
|
||||||
|
int _apMode = 0; // runtime apMode
|
||||||
|
|
||||||
|
// [0] laser[1] m1 [2] m2 [3] m3
|
||||||
|
byte lmValues[4] = {0, 0, 0, 0};
|
||||||
|
|
||||||
unsigned long previousMillis = 0;
|
unsigned long previousMillis = 0;
|
||||||
|
|
||||||
// Web server
|
// Web server
|
||||||
ESP8266WebServer server(80);
|
ESP8266WebServer server(80);
|
||||||
|
|
||||||
|
|
||||||
//format bytes
|
//format bytes
|
||||||
String formatBytes(size_t bytes) {
|
String formatBytes(size_t bytes) {
|
||||||
if (bytes < 1024) {
|
if (bytes < 1024) {
|
||||||
@@ -182,43 +193,70 @@ void handleReadValues()
|
|||||||
json = String();
|
json = String();
|
||||||
}
|
}
|
||||||
|
|
||||||
void handleReadWifi()
|
void hReadWifi()
|
||||||
{
|
{
|
||||||
String json = "{ \"handledReadWifi\":";
|
// Serial.println("hReadWifi");
|
||||||
json += "{\"apMode\":\"" + (String)apMode + "\",";
|
String responseBuffer = String();
|
||||||
json += "\"ssid\":\"" + (String)ssid + "\",";
|
StaticJsonBuffer<200> jsonBuffer;
|
||||||
json += "\"password\":\"***\"}}"; //(String)password
|
JsonObject& root = jsonBuffer.createObject();
|
||||||
server.send(200, "text/json", json);
|
JsonObject& data = root.createNestedObject("wificonfig");
|
||||||
json = String();
|
data["apMode"] = apMode;
|
||||||
|
data["SSID"] = ssid;
|
||||||
|
data["Password"] = "***"; //password;
|
||||||
|
//data["save"] = "false";
|
||||||
|
//data["apply"] = "false";
|
||||||
|
root.prettyPrintTo(responseBuffer);
|
||||||
|
//root.prettyPrintTo(Serial);
|
||||||
|
server.send(200, "text/json", responseBuffer);
|
||||||
|
jsonBuffer.clear();
|
||||||
|
responseBuffer = String();
|
||||||
}
|
}
|
||||||
|
|
||||||
void handleSsid(String uri)
|
void hWriteWifi()
|
||||||
{
|
{
|
||||||
ssid = uri.substring(6);
|
// Serial.println("hWriteWifi");
|
||||||
|
String responseBuffer = String();
|
||||||
String json = "{ \"handledSsid\":";
|
boolean save = false;
|
||||||
json += "{\"ssid\":\"" + (String)ssid + "\"}}";
|
boolean apply = false;
|
||||||
server.send(200, "text/json", json);
|
StaticJsonBuffer<200> jsonBuffer;
|
||||||
json = String();
|
JsonObject& root = jsonBuffer.createObject();
|
||||||
|
JsonObject& data = root.createNestedObject("wificonfig");
|
||||||
|
if (server.hasArg("apMode") ) {
|
||||||
|
apMode = server.arg("apMode").toInt();
|
||||||
|
data["apMode"] = apMode;
|
||||||
|
}
|
||||||
|
if (server.hasArg("SSID") ) {
|
||||||
|
ssid = server.arg("SSID");
|
||||||
|
data["SSID"] = ssid;
|
||||||
|
}
|
||||||
|
if (server.hasArg("password") ) {
|
||||||
|
password = server.arg("password");
|
||||||
|
data["Password"] = "***"; //password;
|
||||||
|
}
|
||||||
|
if (server.arg("save") == "true")
|
||||||
|
{
|
||||||
|
save = true;
|
||||||
|
data["save"] = "true";
|
||||||
|
}
|
||||||
|
if (server.arg("apply") == "true")
|
||||||
|
{
|
||||||
|
apply = true;
|
||||||
|
data["apply"] = "true";
|
||||||
|
}
|
||||||
|
root.prettyPrintTo(responseBuffer);
|
||||||
|
//root.prettyPrintTo(Serial);
|
||||||
|
server.send(200, "text/json", responseBuffer);
|
||||||
|
if (save) {
|
||||||
|
saveConfigFile();
|
||||||
|
}
|
||||||
|
if (apply) {
|
||||||
|
setupWifi();
|
||||||
|
}
|
||||||
|
jsonBuffer.clear();
|
||||||
|
responseBuffer = String();
|
||||||
}
|
}
|
||||||
|
|
||||||
void handlePwd(String uri)
|
|
||||||
{
|
|
||||||
password = uri.substring(5);
|
|
||||||
String json = "{ \"handledPwd\":";
|
|
||||||
json += "{\"password\":\"" + (String)password + "\"}}";
|
|
||||||
server.send(200, "text/json", json);
|
|
||||||
json = String();
|
|
||||||
}
|
|
||||||
void handleApmode(String uri)
|
|
||||||
{
|
|
||||||
apMode = uri.substring(8, 9).toInt();
|
|
||||||
apModeRuntime = apMode;
|
|
||||||
String json = "{ \"handledApmode\":";
|
|
||||||
json += "{\"apMode\":\"" + (String)apMode + "\"}}";
|
|
||||||
server.send(200, "text/json", json);
|
|
||||||
json = String();
|
|
||||||
}
|
|
||||||
|
|
||||||
bool saveConfigFile() {
|
bool saveConfigFile() {
|
||||||
StaticJsonBuffer<200> jsonBuffer;
|
StaticJsonBuffer<200> jsonBuffer;
|
||||||
@@ -227,7 +265,7 @@ bool saveConfigFile() {
|
|||||||
json["SSID"] = ssid;
|
json["SSID"] = ssid;
|
||||||
json["Password"] = password;
|
json["Password"] = password;
|
||||||
|
|
||||||
File configFile = SPIFFS.open("/config.json", "w");
|
File configFile = SPIFFS.open("/wifi.json", "w");
|
||||||
if (!configFile) {
|
if (!configFile) {
|
||||||
Serial.println("Failed to open config file for writing");
|
Serial.println("Failed to open config file for writing");
|
||||||
return false;
|
return false;
|
||||||
@@ -249,8 +287,8 @@ void handleSaveConf() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
boolean readConfigFile() {
|
boolean readConfigFile() {
|
||||||
Serial.println("Reading config.json");
|
Serial.println("Reading wifi.json");
|
||||||
File configFile = SPIFFS.open("/config.json", "r");
|
File configFile = SPIFFS.open("/wifi.json", "r");
|
||||||
if (!configFile) {
|
if (!configFile) {
|
||||||
Serial.println("Failed to open config file");
|
Serial.println("Failed to open config file");
|
||||||
return false;
|
return false;
|
||||||
@@ -273,7 +311,7 @@ boolean readConfigFile() {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
apMode = json["apMode"];
|
apMode = json["apMode"];
|
||||||
apModeRuntime = apMode;
|
apMode = apMode;
|
||||||
const char* tSsid = json["SSID"];
|
const char* tSsid = json["SSID"];
|
||||||
const char* tPwd = json["Password"];
|
const char* tPwd = json["Password"];
|
||||||
ssid = tSsid;
|
ssid = tSsid;
|
||||||
@@ -287,8 +325,8 @@ boolean readConfigFile() {
|
|||||||
|
|
||||||
boolean setupWifi() {
|
boolean setupWifi() {
|
||||||
Serial.print("setupWifi apMode=");
|
Serial.print("setupWifi apMode=");
|
||||||
Serial.println(apModeRuntime);
|
Serial.println(apMode);
|
||||||
if (apModeRuntime == 0)
|
if (_apMode == 0)
|
||||||
{
|
{
|
||||||
Serial.print("Connecting to ");
|
Serial.print("Connecting to ");
|
||||||
Serial.println(ssid);
|
Serial.println(ssid);
|
||||||
@@ -345,14 +383,14 @@ void setup(void) {
|
|||||||
if (!readConfigFile())
|
if (!readConfigFile())
|
||||||
{
|
{
|
||||||
Serial.println("reading wifi config failed, start SoftAP");
|
Serial.println("reading wifi config failed, start SoftAP");
|
||||||
apModeRuntime = 1;
|
_apMode = 1;
|
||||||
}
|
}
|
||||||
delay(100);
|
delay(100);
|
||||||
|
|
||||||
if (!setupWifi())
|
if (!setupWifi())
|
||||||
{
|
{
|
||||||
Serial.println("connect failed, start SoftAP");
|
Serial.println("connect failed, start SoftAP");
|
||||||
apModeRuntime = 1; // fallback to AP mode
|
_apMode = 1; // fallback to AP mode
|
||||||
setupWifi();
|
setupWifi();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -371,7 +409,13 @@ void setup(void) {
|
|||||||
server.on("/listFiles", HTTP_GET, handleFileList);
|
server.on("/listFiles", HTTP_GET, handleFileList);
|
||||||
server.on("/saveconf", HTTP_GET, handleSaveConf);
|
server.on("/saveconf", HTTP_GET, handleSaveConf);
|
||||||
server.on("/resetwifi", HTTP_GET, setupWifi);
|
server.on("/resetwifi", HTTP_GET, setupWifi);
|
||||||
server.on("/readwifi", HTTP_GET, handleReadWifi);
|
|
||||||
|
server.on("/wificonfig", HTTP_GET, hReadWifi);
|
||||||
|
server.on("/wificonfig", HTTP_POST, hWriteWifi);
|
||||||
|
// server.on("/wificonfig", HTTP_POST, []() {
|
||||||
|
// hWriteWifi();
|
||||||
|
// });
|
||||||
|
|
||||||
server.on("/readvalues", HTTP_GET, handleReadValues);
|
server.on("/readvalues", HTTP_GET, handleReadValues);
|
||||||
server.on("/heap", HTTP_GET, []() {
|
server.on("/heap", HTTP_GET, []() {
|
||||||
String json = "{";
|
String json = "{";
|
||||||
@@ -399,58 +443,20 @@ void handleNotFound() {
|
|||||||
handleLaser(uri);
|
handleLaser(uri);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (uri.substring(0, 6) == "/ssid/")
|
if (uri.substring(0, 12) == "/wifi.json")
|
||||||
{
|
|
||||||
handleSsid(uri);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (uri.substring(0, 5) == "/pwd/")
|
|
||||||
{
|
|
||||||
handlePwd(uri);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (uri.substring(0, 8) == "/apmode/")
|
|
||||||
{
|
|
||||||
handleApmode(uri);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (uri.substring(0, 12) == "/config.json")
|
|
||||||
{
|
{
|
||||||
server.send(403, "text/plain", "Forbidden");
|
server.send(403, "text/plain", "Forbidden");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!handleFileRead(server.uri()))
|
if (!handleFileRead(server.uri()))
|
||||||
{
|
{
|
||||||
server.send(302, "text/html", metaRefreshStr);
|
server.send(404, "text/html", metaRefreshStr);
|
||||||
// server.send(302, "text/plain", "blub");
|
// server.send(302, "text/plain", "blub");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop(void) {
|
void loop(void) {
|
||||||
server.handleClient();
|
server.handleClient();
|
||||||
|
|
||||||
|
|
||||||
int i = 0;
|
|
||||||
char commandbuffer[100];
|
|
||||||
String sBuffer;
|
|
||||||
|
|
||||||
if (laserSerial.available()) {
|
|
||||||
delay(100);
|
|
||||||
|
|
||||||
while ( laserSerial.available() && i < 99) {
|
|
||||||
commandbuffer[i] = laserSerial.read();
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
commandbuffer[i++] = '\0';
|
|
||||||
}
|
|
||||||
|
|
||||||
if (i > 0)
|
|
||||||
{
|
|
||||||
delay(200);
|
|
||||||
Serial.print("received from laser: ");
|
|
||||||
Serial.println((char*)commandbuffer);
|
|
||||||
// Serial.println(()sBuffer);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user