#define ARDUINOJSON_ENABLE_COMMENTS 1 #include #include #include #include "LittleFS.h" #include #include #include #include #include #include #include #include #include DynamicJsonDocument settings(2048); #include FTPServer ftpServer(LittleFS); DNSServer dnsServer; AsyncWebServer httpServer(80); HX711 scale; time_t measure = 0; File measureFile; void reload (bool w = 0) { /* stop services unless they were never started, indicate this by w in for example setup() */ if (!w) { ftpServer.stop(); httpServer.reset(); dnsServer.stop(); } /* reload settings */ load_settings(); /* bring up AP networking */ IPAddress ap_ip(10, 82, 66, 1); ap_ip.fromString(settings["ap_ip"].as()); IPAddress ap_gateway(10, 82, 66, 1); ap_gateway.fromString(settings["ap_gw"].as()); IPAddress ap_subnet(255, 255, 255, 0); ap_subnet.fromString(settings["ap_nm"].as()); WiFi.mode(WIFI_AP); WiFi.softAPConfig(ap_ip, ap_gateway, ap_subnet); const char * ap_pass = settings["ap_pass"].as().c_str(); if (ap_pass && strlen(ap_pass) < 8) ap_pass = NULL; WiFi.softAP(settings["ap_ssid"].as().c_str(), ap_pass, settings["cp_ch"].as(), settings["ap_hidden"].as()); Serial.println("WiFi.softAPIP() = " + WiFi.softAPIP().toString()); /* bring up STA networking - if that's desired */ const char * sta_ssid = settings["sta_ssid"].as().c_str(); if (sta_ssid && strlen(sta_ssid) > 0) { IPAddress sta_ip(10, 69, 69, 82); sta_ip.fromString(settings["sta_ip"].as()); IPAddress sta_gw(10, 69, 69, 1); sta_ip.fromString(settings["sta_gw"].as()); IPAddress sta_nm(255, 255, 255, 0); sta_ip.fromString(settings["sta_nm"].as()); IPAddress sta_dns1(93, 103, 235, 126); sta_dns1.fromString(settings["sta_dns1"].as()); IPAddress sta_dns2(193, 2, 1, 66); sta_dns2.fromString(settings["sta_dns2"].as()); WiFi.disconnect(true); WiFi.hostname(settings["sta_host"].as()); const char * sta_pass = settings["sta_pass"].as().c_str(); if (strlen(sta_pass) < 8) sta_pass = NULL; WiFi.begin(sta_ssid, sta_pass); if (settings["sta_static"].as()) if (!WiFi.config(sta_ip, sta_gw, sta_nm, sta_dns1, sta_dns2)) Serial.println("failed to wifi config"); } /* start services */ dnsServer.start(53, settings["host"], WiFi.softAPIP()); ftpServer.begin(settings["ftp_user"], settings["ftp_pass"]); /* web server */ #define ADD_AUTH(h) if (strlen(settings["http_user"].as().c_str()) || strlen(settings["http_pass"].as().c_str())) h.setAuthentication(settings["http_user"], settings["http_pass"]) auto h = httpServer.serveStatic("/", LittleFS, "/") .setDefaultFile("index.html") .setAuthentication(settings["http_user"], settings["http_pass"]); ADD_AUTH(h); h = httpServer.on("/rst", [](AsyncWebServerRequest * r) { r->send(201, "text/plain", "OK"); ESP.restart(); }); ADD_AUTH(h); h = httpServer.on("/rld", [](AsyncWebServerRequest * r) { r->send(201, "text/plain", "OK"); reload(); }); h = httpServer.on("/m", [](AsyncWebServerRequest * r) { if (!r->hasParam("n")) r->send(400, "text/plain", "Manjka parameter n - število sekund meritve"); else if (!measure) { measure = time(NULL)+atoi(r->getParam("n")->value().c_str()); r->send(201, "text/plain", "OK"); } else { r->send(400, "text/plain", "Meritev že poteka!"); } }); ADD_AUTH(h); /* specifični ukazi za meritve: začetek loadcella, kalibracija, tara */ scale.begin(str2pin(settings["scale_dout"].as().c_str()), str2pin(settings["scale_sck"].as().c_str())); h = httpServer.on("/c", [](AsyncWebServerRequest * r) { if (!r->hasParam("t")) { r->send(400, "text/plain", "Manjka parameter t - teža na tehtnici"); } if (scale.wait_ready_timeout(1000)) { scale.set_scale(); scale.tare(); scale.set_scale(scale.get_units(10)/atof(r->getParam("t")->value().c_str())); r->send(200, "text/plain", "OK"); } else { r->send(500, "text/plain", "Tehtnica ni najdena"); } }); ADD_AUTH(h); h = httpServer.on("/t", [](AsyncWebServerRequest * r) { if (scale.wait_ready_timeout(1000)) { scale.tare(); r->send(400, "text/plain", "OK"); } else { r->send(500, "text/plain", "Tehtnica ni najdena."); } }); ADD_AUTH(h); } void handleMeasure() { if (measure != 0) { if (measure < time(NULL)) { if (measureFile.isFile()) { char filename[32]; snprintf(filename, 32, "meritev%ldl.csv", time(NULL)); measureFile = LittleFS.open(filename, "w"); scale.power_up(); } if (scale.wait_ready_timeout(1000)) { long reading = scale.get_units(10); measureFile.print(millis()+""+"\n"); } else { Serial.println("HX711 ni najden."); } } else { measure = 0; measureFile.close(); scale.power_down(); } } } void setup () { Serial.begin(9600); Serial.println("Živ sem!"); LittleFS.begin(); reload(1); } void loop () { handleMeasure(); ftpServer.handleFTP(); dnsServer.processNextRequest(); }