From be93772bd4ab8b9af5fa155565d732e5e7cff51e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anton=20Luka=20=C5=A0ijanec?= Date: Mon, 30 May 2022 22:05:30 +0200 Subject: =?UTF-8?q?nedelujo=C4=8D=20MPU=20lib,=20izbiram=20drugega?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fiz/naloga/merjenje/src/main.cpp | 171 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 171 insertions(+) create mode 100644 fiz/naloga/merjenje/src/main.cpp (limited to 'fiz/naloga/merjenje/src/main.cpp') diff --git a/fiz/naloga/merjenje/src/main.cpp b/fiz/naloga/merjenje/src/main.cpp new file mode 100644 index 0000000..a5209da --- /dev/null +++ b/fiz/naloga/merjenje/src/main.cpp @@ -0,0 +1,171 @@ +#define ARDUINOJSON_ENABLE_COMMENTS 1 +#include +#include +#include +#include "LittleFS.h" +#include +#include +#include +#include +#include +#include +#include +#undef __POLLEDTIMING_H__ // http://github.com/dplasa/FTPClientServer/pull/19 +#include +#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); +MPU9250 mpu; +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().c_str(); + if (ap_pass && strlen(ap_pass) < 8) + ap_pass = NULL; + WiFi.mode(WIFI_AP); + WiFi.softAP(settings["ap_ssid"].as().c_str(), ap_pass, settings["ap_ch"].as(), settings["ap_hidden"].as()); + Serial.println("WiFi.softAPIP() = " + WiFi.softAPIP().toString()); + Serial.println("station networking"); + const char * sta_ssid = settings["sta_ssid"].as().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()); + IPAddress sta_gw(10, 69, 69, 1); + sta_gw.fromString(settings["sta_gw"].as()); + IPAddress sta_nm(10, 69, 69, 255); + sta_nm.fromString(settings["sta_nm"].as()); + IPAddress sta_dns1(93, 103, 235, 126); + sta_dns1.fromString(settings["sta_dns1"].as()); + IPAddress sta_dns2(89, 212, 146, 168); + sta_dns2.fromString(settings["sta_dns2"].as()); + const char * sta_mac = settings["sta_mac"].as().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()); + const char * sta_pass = settings["sta_pass"].as().c_str(); + if (sta_pass && 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"); + 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; + r->send(201, TEXT_ENC, "OK\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; + r->send(201, TEXT_ENC, "OK\n"); + }); + ADD_AUTH(k); + auto c = httpServer.on("/c", [](AsyncWebServerRequest * r) { + r->send(201, TEXT_ENC, "glej sporočila na UART za navodila\n"); + mpu.calibrateMag(); + }); + ADD_AUTH(c); + httpServer.begin(); + Wire.begin(); + int naslov = settings["mpu_address"].as(); + if (!mpu.setup(naslov)) { + Serial.println("mpu ni dosegljiv na naslovu " + naslov); + } + mpu.verbose(true); + Serial.println("reload: done"); +} +void handleMeasure () { + if (!mpu.update()) { + Serial.println("!mpu.update()"); + } + Serial.printf("getMag: X: %f\tY: %f\tZ: %f\n", mpu.getMagX(), mpu.getMagY(), mpu.getMagZ()); + 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; + } +} -- cgit v1.2.3