summaryrefslogtreecommitdiffstats
path: root/fiz/naloga/vodnaraketa/src/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'fiz/naloga/vodnaraketa/src/main.cpp')
-rw-r--r--fiz/naloga/vodnaraketa/src/main.cpp171
1 files changed, 171 insertions, 0 deletions
diff --git a/fiz/naloga/vodnaraketa/src/main.cpp b/fiz/naloga/vodnaraketa/src/main.cpp
new file mode 100644
index 0000000..91280dd
--- /dev/null
+++ b/fiz/naloga/vodnaraketa/src/main.cpp
@@ -0,0 +1,171 @@
+#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 <HX711.h>
+#include <FTPServer.h>
+
+DynamicJsonDocument settings(2048);
+
+#include <func.c>
+
+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<String>());
+ IPAddress ap_gateway(10, 82, 66, 1);
+ ap_gateway.fromString(settings["ap_gw"].as<String>());
+ IPAddress ap_subnet(255, 255, 255, 0);
+ ap_subnet.fromString(settings["ap_nm"].as<String>());
+ WiFi.mode(WIFI_AP);
+ WiFi.softAPConfig(ap_ip, ap_gateway, ap_subnet);
+ const char * ap_pass = settings["ap_pass"].as<String>().c_str();
+ if (ap_pass && strlen(ap_pass) < 8)
+ ap_pass = NULL;
+ WiFi.softAP(settings["ap_ssid"].as<String>().c_str(), ap_pass, settings["cp_ch"].as<int>(), settings["ap_hidden"].as<int>());
+ Serial.println("WiFi.softAPIP() = " + WiFi.softAPIP().toString());
+
+
+ /* bring up STA networking - if that's desired */
+ const char * sta_ssid = settings["sta_ssid"].as<String>().c_str();
+ if (sta_ssid && strlen(sta_ssid) > 0) {
+ IPAddress sta_ip(10, 69, 69, 82);
+ sta_ip.fromString(settings["sta_ip"].as<String>());
+ IPAddress sta_gw(10, 69, 69, 1);
+ sta_ip.fromString(settings["sta_gw"].as<String>());
+ IPAddress sta_nm(255, 255, 255, 0);
+ sta_ip.fromString(settings["sta_nm"].as<String>());
+ IPAddress sta_dns1(93, 103, 235, 126);
+ sta_dns1.fromString(settings["sta_dns1"].as<String>());
+ IPAddress sta_dns2(193, 2, 1, 66);
+ sta_dns2.fromString(settings["sta_dns2"].as<String>());
+ WiFi.disconnect(true);
+ WiFi.hostname(settings["sta_host"].as<String>());
+ const char * sta_pass = settings["sta_pass"].as<String>().c_str();
+ if (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");
+ }
+
+ /* 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<String>().c_str()) || strlen(settings["http_pass"].as<String>().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<String>().c_str()), str2pin(settings["scale_sck"].as<String>().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();
+}