summaryrefslogtreecommitdiffstats
path: root/fiz/naloga/merjenje/src/main.cpp
blob: afe19de2f34a96318a5f4f6e3d7c3c0160bebdb1 (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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#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;
	}
}