diff options
author | sijanec <anton@sijanec.eu> | 2021-05-25 07:07:16 +0200 |
---|---|---|
committer | sijanec <anton@sijanec.eu> | 2021-05-25 07:07:16 +0200 |
commit | 71a466c66b3476d5aa58c3fc6de2d77d10d625d3 (patch) | |
tree | 2a608e45350a2dc7e266963eb3ae201feb94f211 /fiz | |
parent | https://zapiski.xn--ijanec-9jb.eu/p/psi# (diff) | |
download | sola-gimb-2-71a466c66b3476d5aa58c3fc6de2d77d10d625d3.tar sola-gimb-2-71a466c66b3476d5aa58c3fc6de2d77d10d625d3.tar.gz sola-gimb-2-71a466c66b3476d5aa58c3fc6de2d77d10d625d3.tar.bz2 sola-gimb-2-71a466c66b3476d5aa58c3fc6de2d77d10d625d3.tar.lz sola-gimb-2-71a466c66b3476d5aa58c3fc6de2d77d10d625d3.tar.xz sola-gimb-2-71a466c66b3476d5aa58c3fc6de2d77d10d625d3.tar.zst sola-gimb-2-71a466c66b3476d5aa58c3fc6de2d77d10d625d3.zip |
Diffstat (limited to 'fiz')
-rw-r--r-- | fiz/naloga/vodnaraketa/.gitignore | 1 | ||||
-rw-r--r-- | fiz/naloga/vodnaraketa/Makefile | 23 | ||||
-rw-r--r-- | fiz/naloga/vodnaraketa/README.txt | 5 | ||||
-rw-r--r-- | fiz/naloga/vodnaraketa/data/index.html | 19 | ||||
-rw-r--r-- | fiz/naloga/vodnaraketa/data/settings.json | 40 | ||||
-rw-r--r-- | fiz/naloga/vodnaraketa/include/README | 39 | ||||
-rw-r--r-- | fiz/naloga/vodnaraketa/lib/README | 46 | ||||
-rw-r--r-- | fiz/naloga/vodnaraketa/platformio.ini | 25 | ||||
-rw-r--r-- | fiz/naloga/vodnaraketa/src/func.c | 31 | ||||
-rw-r--r-- | fiz/naloga/vodnaraketa/src/main.cpp | 171 | ||||
-rw-r--r-- | fiz/naloga/vodnaraketa/test/README | 11 |
11 files changed, 411 insertions, 0 deletions
diff --git a/fiz/naloga/vodnaraketa/.gitignore b/fiz/naloga/vodnaraketa/.gitignore new file mode 100644 index 0000000..03f4a3c --- /dev/null +++ b/fiz/naloga/vodnaraketa/.gitignore @@ -0,0 +1 @@ +.pio diff --git a/fiz/naloga/vodnaraketa/Makefile b/fiz/naloga/vodnaraketa/Makefile new file mode 100644 index 0000000..921476a --- /dev/null +++ b/fiz/naloga/vodnaraketa/Makefile @@ -0,0 +1,23 @@ +default: + @echo "*** vodna raketa *** 🚀" + @echo "recepti:" + @echo " - p: naloži knjižnice" + @echo " - c: prevede v strojno kodo" + @echo " - u: nalozi na mikrokontroler" + @echo " - n: pobriše generirane direktorije in datoteke iz tega direktorija" + @echo "imeti morate python3-pip" + @echo "to je vse, hvala" + +c: + pio run + +u: + pio run --target uploadfs + pio run --target upload + +p: + pip install -U platformio + pio run + +n: + pio run --target clean diff --git a/fiz/naloga/vodnaraketa/README.txt b/fiz/naloga/vodnaraketa/README.txt new file mode 100644 index 0000000..1927334 --- /dev/null +++ b/fiz/naloga/vodnaraketa/README.txt @@ -0,0 +1,5 @@ +*** vodna raketa *** 🚀 + +programje za mojo projektno nalogo pri fiziki v drugem letniku + +več informacij v predstavitvi sami, ki jo, če je že narejena, najdete v repozitoriju sijanec/sola-gimb-2 diff --git a/fiz/naloga/vodnaraketa/data/index.html b/fiz/naloga/vodnaraketa/data/index.html new file mode 100644 index 0000000..2e4fd3c --- /dev/null +++ b/fiz/naloga/vodnaraketa/data/index.html @@ -0,0 +1,19 @@ +<!DOCTYPE html> +<html lang=sl> + <head> + <meta charset=UTF-8 /> + <title> + spletni vmesnik vodne rakete + </title> + <link rel=stylesheet href=//sijanec.eu/assets/css/styles.css /> + </head> + <body> + <h1> + spletni vmesnik računalnika projetne naloge pri fiziki v drugem letniku + </h1> + <hr> + <p> + © Anton Luka Šijanec 2021 - <a href=//git.sijanec.eu/sijanec/sola-gimb-2/src/branch/master/fiz/naloga/vodnaraketa>vodnaraketa</a> + </p> + </body> +</html> diff --git a/fiz/naloga/vodnaraketa/data/settings.json b/fiz/naloga/vodnaraketa/data/settings.json new file mode 100644 index 0000000..4ef4b59 --- /dev/null +++ b/fiz/naloga/vodnaraketa/data/settings.json @@ -0,0 +1,40 @@ +{ + /* wifi access point configuration */ + "ap_ssid": "vodnaraketa", + "ap_pass": "", + "ap_ch": 1, + "ap_hidden": 0, + "ap_ip": "10.82.66.1", + "ap_gw": "10.82.66.1", + "ap_nm": "255.255.255.0", + "ap_dns1": "", + "ap_dns2": "", + "ap_host": "vodnaraketa", + + /* wifi station (client) configuration, leave ip settings empty to use DHCP obtained */ + "sta_ssid": "", + "sta_pass": "", + "sta_ip": "", + "sta_dns": "", + "sta_gw": "", + "sta_nm": "", + "sta_dns1": "", + "sta_dns2": "", + "sta_host": "vodnaraketa", + "sta_static": 0, + + /* pin configuration */ + "scale_dout": "D6", + "scale_sck": "D5", + + /* username and password for FTP and HTTP authentication */ + "ftp_user": "vodnaraketa", + "ftp_pass": "", + "http_user": "", + "http_pass": "", + + /* general info */ + "host": "vodnaraketa", + + "json parser": "don't be angry" +} diff --git a/fiz/naloga/vodnaraketa/include/README b/fiz/naloga/vodnaraketa/include/README new file mode 100644 index 0000000..194dcd4 --- /dev/null +++ b/fiz/naloga/vodnaraketa/include/README @@ -0,0 +1,39 @@ + +This directory is intended for project header files. + +A header file is a file containing C declarations and macro definitions +to be shared between several project source files. You request the use of a +header file in your project source file (C, C++, etc) located in `src` folder +by including it, with the C preprocessing directive `#include'. + +```src/main.c + +#include "header.h" + +int main (void) +{ + ... +} +``` + +Including a header file produces the same results as copying the header file +into each source file that needs it. Such copying would be time-consuming +and error-prone. With a header file, the related declarations appear +in only one place. If they need to be changed, they can be changed in one +place, and programs that include the header file will automatically use the +new version when next recompiled. The header file eliminates the labor of +finding and changing all the copies as well as the risk that a failure to +find one copy will result in inconsistencies within a program. + +In C, the usual convention is to give header files names that end with `.h'. +It is most portable to use only letters, digits, dashes, and underscores in +header file names, and at most one dot. + +Read more about using header files in official GCC documentation: + +* Include Syntax +* Include Operation +* Once-Only Headers +* Computed Includes + +https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html diff --git a/fiz/naloga/vodnaraketa/lib/README b/fiz/naloga/vodnaraketa/lib/README new file mode 100644 index 0000000..6debab1 --- /dev/null +++ b/fiz/naloga/vodnaraketa/lib/README @@ -0,0 +1,46 @@ + +This directory is intended for project specific (private) libraries. +PlatformIO will compile them to static libraries and link into executable file. + +The source code of each library should be placed in a an own separate directory +("lib/your_library_name/[here are source files]"). + +For example, see a structure of the following two libraries `Foo` and `Bar`: + +|--lib +| | +| |--Bar +| | |--docs +| | |--examples +| | |--src +| | |- Bar.c +| | |- Bar.h +| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html +| | +| |--Foo +| | |- Foo.c +| | |- Foo.h +| | +| |- README --> THIS FILE +| +|- platformio.ini +|--src + |- main.c + +and a contents of `src/main.c`: +``` +#include <Foo.h> +#include <Bar.h> + +int main (void) +{ + ... +} + +``` + +PlatformIO Library Dependency Finder will find automatically dependent +libraries scanning project source files. + +More information about PlatformIO Library Dependency Finder +- https://docs.platformio.org/page/librarymanager/ldf.html diff --git a/fiz/naloga/vodnaraketa/platformio.ini b/fiz/naloga/vodnaraketa/platformio.ini new file mode 100644 index 0000000..5977f35 --- /dev/null +++ b/fiz/naloga/vodnaraketa/platformio.ini @@ -0,0 +1,25 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html +[env:myboard] +platform = espressif8266 +board = nodemcuv2 +monitor_speed = 9600 +upload_speed = 921600 +build_flags = -DDEBUG_ESP_CORE -DDEBUG_ESP_WIFI -DDEBUG_ESP_PORT=Serial -DIP_FORWARD=1 +framework = arduino +board_build.filesystem = littlefs +board_build.ldscript = eagle.flash.4m2m.ld +src_filter = -<*> +<main.cpp> +lib_deps = + Time + https://github.com/dplasa/FTPClientServer + bogde/HX711 + DNSServer + ESP Async WebServer diff --git a/fiz/naloga/vodnaraketa/src/func.c b/fiz/naloga/vodnaraketa/src/func.c new file mode 100644 index 0000000..07bfd6e --- /dev/null +++ b/fiz/naloga/vodnaraketa/src/func.c @@ -0,0 +1,31 @@ +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)]; +} + +/* reads settings into json object */ +void load_settings () { + if (LittleFS.exists("settings.json")) { + File s = LittleFS.open("settings.json", "r"); + if (s) + deserializeJson(settings, s); + s.close(); + } +} + +/* stores settings from json object to file */ +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"); +} 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(); +} diff --git a/fiz/naloga/vodnaraketa/test/README b/fiz/naloga/vodnaraketa/test/README new file mode 100644 index 0000000..b94d089 --- /dev/null +++ b/fiz/naloga/vodnaraketa/test/README @@ -0,0 +1,11 @@ + +This directory is intended for PlatformIO Unit Testing and project tests. + +Unit Testing is a software testing method by which individual units of +source code, sets of one or more MCU program modules together with associated +control data, usage procedures, and operating procedures, are tested to +determine whether they are fit for use. Unit testing finds problems early +in the development cycle. + +More information about PlatformIO Unit Testing: +- https://docs.platformio.org/page/plus/unit-testing.html |