summaryrefslogtreecommitdiffstats
path: root/fiz/naloga/vodnaraketa/src/main.cpp
blob: 91280ddd6ba4e372ce31ff3cbf7076859c014f88 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
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();
}