#define ARDUINOJSON_ENABLE_COMMENTS 1
#include <stdlib.h>
#include <Arduino.h>
#include <ArduinoJson.h>
#include "LittleFS.h"
#include <DNSServer.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <TimeLib.h>
#include <WiFiUdp.h>
#include <ESP8266httpUpdate.h>
#include <ESPAsyncWebServer.h>
#include <limits>
#undef __POLLEDTIMING_H__ // http://github.com/dplasa/FTPClientServer/pull/19
#include <FTPServer.h>
#define TEXT_ENC "text/plain"
#include "mpu9250.h"
const int str2pin_map[] = { D0, D1, D2, D3, D4, D5, D6, D7, D8, D9, D10, 0, 0, 0, 0, 0, 0, 0, 0, 0, A0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int str2pin (const char * s) {
if (s[0] >= '0' && s[0] <= '9')
return atoi(s);
return str2pin_map[atoi(s+1) + ((s[0] == 'A') ? 20 : 0)];
}
DynamicJsonDocument settings(1024);
void load_settings () {
if (LittleFS.exists("settings.json")) {
File s = LittleFS.open("settings.json", "r");
if (s)
deserializeJson(settings, s);
s.close();
}
}
void store_settings () {
File s = LittleFS.open("settings.json", "w");
serializeJson(settings, s);
s.close();
}
void notFound (AsyncWebServerRequest * request) {
request->send(404, "text/plain", "404");
}
FTPServer ftpServer(LittleFS);
DNSServer dnsServer;
AsyncWebServer httpServer(80);
bfs::Mpu9250 imu;
File measureFile;
int do_reload = 0;
int measure = 0;
void reload (bool w) {
Serial.println("reload");
if (!w) {
Serial.println("stopping services: ftp");
ftpServer.stop();
Serial.println("stopping services: http");
httpServer.reset();
httpServer.end();
Serial.println("stopping services: dns");
dnsServer.stop();
}
Serial.println("loading settings");
load_settings();
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH); // izklopi luč
Serial.println("access point");
const char * ap_pass = settings["ap_pass"].as<String>().c_str();
if (ap_pass && strlen(ap_pass) < 8)
ap_pass = NULL;
WiFi.mode(WIFI_AP);
WiFi.softAP(settings["ap_ssid"].as<String>().c_str(), ap_pass, settings["ap_ch"].as<int>(), settings["ap_hidden"].as<int>());
Serial.println("WiFi.softAPIP() = " + WiFi.softAPIP().toString());
Serial.println("station networking");
const char * sta_ssid = settings["sta_ssid"].as<String>().c_str();
if (sta_ssid && strlen(sta_ssid)) {
Serial.println("setting up wifi station mode");
IPAddress sta_ip(10, 69, 69, 82);
sta_ip.fromString(settings["sta_ip"].as<String>());
IPAddress sta_gw(10, 69, 69, 1);
sta_gw.fromString(settings["sta_gw"].as<String>());
IPAddress sta_nm(10, 69, 69, 255);
sta_nm.fromString(settings["sta_nm"].as<String>());
IPAddress sta_dns1(93, 103, 235, 126);
sta_dns1.fromString(settings["sta_dns1"].as<String>());
IPAddress sta_dns2(89, 212, 146, 168);
sta_dns2.fromString(settings["sta_dns2"].as<String>());
const char * sta_mac = settings["sta_mac"].as<String>().c_str();
if (sta_mac && strlen(sta_mac)) {
long long unsigned int mac;
uint8_t macar[6];
mac = strtoull(sta_mac, NULL, 16);
memcpy(macar, (char *) &mac, 6);
wifi_set_macaddr(STATION_IF, macar);
}
WiFi.disconnect(true);
WiFi.hostname(settings["sta_host"].as<String>());
const char * sta_pass = settings["sta_pass"].as<String>().c_str();
if (sta_pass && strlen(sta_pass) < 8)
sta_pass = NULL;
WiFi.begin(sta_ssid, sta_pass);
if (settings["sta_static"].as<int>())
if (!WiFi.config(sta_ip, sta_gw, sta_nm, sta_dns1, sta_dns2))
Serial.println("failed to wifi config");
Serial.println(String("wifi local ip: "+WiFi.localIP().toString()));
} else
WiFi.disconnect();
dnsServer.start(53, settings["host"], WiFi.softAPIP());
ftpServer.begin(settings["ftp_user"], settings["ftp_pass"]);
#define ADD_AUTH(s) s.setAuthentication(settings["http_user"], settings["http_pass"])
auto f = httpServer.serveStatic("/", LittleFS, "/").setDefaultFile("index.html");
ADD_AUTH(f);
auto s = httpServer.on("/s", [](AsyncWebServerRequest * r) {
r->send(201, TEXT_ENC, "OK\n");
ESP.restart();
});
ADD_AUTH(s);
auto l = httpServer.on("/l", [](AsyncWebServerRequest * r) {
r->send(201, TEXT_ENC, "OK\n");
do_reload = 1;
});
ADD_AUTH(l);
auto m = httpServer.on("/m", [](AsyncWebServerRequest * r) {
if (measure) {
r->send(400, TEXT_ENC, "meritev že poteka\n");
return;
}
measure = 1;
char filename[32];
snprintf(filename, 32, "meritev%lld.tsv", time(NULL));
Serial.println(String("opening measureFile ")+filename);
measureFile = LittleFS.open(filename, "w");
r->send(201, TEXT_ENC, String(filename) + "\n");
});
ADD_AUTH(m);
auto k = httpServer.on("/k", [](AsyncWebServerRequest * r) {
if (!measure) {
r->send(400, TEXT_ENC, "trenutno ni meritve\n");
return;
}
measure = 0;
measureFile.close();
digitalWrite(LED_BUILTIN, HIGH); // izklopi luč
r->send(201, TEXT_ENC, "OK\n");
});
ADD_AUTH(k);
auto c = httpServer.on("/c", [](AsyncWebServerRequest * r) {
r->send(404, TEXT_ENC, "404\n");
});
ADD_AUTH(c);
httpServer.begin();
Wire.begin();
Wire.setClock(400000);
imu.Config(&Wire, bfs::Mpu9250::I2C_ADDR_PRIM);
if (!imu.Begin()) {
Serial.println("mpu ni dosegljiv na naslovu 0x68");
}
if (!imu.ConfigSrd(19))
Serial.println("imu.ConfigSrd(19)");
Serial.println("reload: done");
}
void handleMeasure () {
if (!imu.Read())
Serial.println("!imu.Read()");
Serial.print(imu.new_imu_data());
Serial.print("\t");
Serial.print(imu.mag_x_ut());
Serial.print("\t");
Serial.print(imu.mag_y_ut());
Serial.print("\t");
Serial.print(imu.mag_z_ut());
Serial.print("\t");
Serial.print(imu.die_temp_c());
Serial.print("\n");
if (!measureFile.isFile())
Serial.println("!measureFile.isFile()");
else {
digitalWrite(LED_BUILTIN, LOW); // vklopi luč
measureFile.printf("%lu\t%d\t%f\t%f\t%f\t%f\n", millis(), imu.new_imu_data(), imu.mag_x_ut(), imu.mag_y_ut(), imu.mag_z_ut(), imu.die_temp_c());
// String(millis())+String("\t")+imu.new_imu_data()+String("\t")+imu.mag_x_ut()+String("\t")+imu.mag_y_ut()+String("\t")+imu.mag_z_ut()+String("\t")+imu.die_temp_c()+String("\n"));
}
return;
}
void setup () {
Serial.begin(MONITOR_SPEED);
Serial.println("živ sem");
LittleFS.begin();
reload(1);
}
void loop () {
if (measure)
handleMeasure();
ftpServer.handleFTP();
dnsServer.processNextRequest();
if (do_reload) {
reload(0);
do_reload = 0;
}
}