consolidate all repos to one for archive
This commit is contained in:
5
projektna_naloga/nrs/.gitignore
vendored
Normal file
5
projektna_naloga/nrs/.gitignore
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
.pio
|
||||
.vscode/.browse.c_cpp.db*
|
||||
.vscode/c_cpp_properties.json
|
||||
.vscode/launch.json
|
||||
.vscode/ipch
|
10
projektna_naloga/nrs/.vscode/extensions.json
vendored
Normal file
10
projektna_naloga/nrs/.vscode/extensions.json
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
{
|
||||
// See http://go.microsoft.com/fwlink/?LinkId=827846
|
||||
// for the documentation about the extensions.json format
|
||||
"recommendations": [
|
||||
"platformio.platformio-ide"
|
||||
],
|
||||
"unwantedRecommendations": [
|
||||
"ms-vscode.cpptools-extension-pack"
|
||||
]
|
||||
}
|
42
projektna_naloga/nrs/include/index.hpp
Normal file
42
projektna_naloga/nrs/include/index.hpp
Normal file
@@ -0,0 +1,42 @@
|
||||
const char index_html[] = R"rawliteral(
|
||||
<!DOCTYPE HTML>
|
||||
<html data-bs-theme="dark">
|
||||
|
||||
<head>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>Password</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
|
||||
<div class="container mt-5 text-center" style="width: 300px;">
|
||||
|
||||
<div class="mb-3">
|
||||
<label for="ssid" class="form-label">SSID</label>
|
||||
<input type="text" class="form-control" id="ssid">
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<label for="password" class="form-label">Password</label>
|
||||
<input type="text" class="form-control" id="password">
|
||||
</div>
|
||||
|
||||
<button class="btn btn-primary" onclick="updateSlider()">Submit</button>
|
||||
|
||||
</div>
|
||||
|
||||
<script>
|
||||
function updateSlider() {
|
||||
let button = document.getElementById("button");
|
||||
let ssid = document.getElementById("ssid");
|
||||
let password = document.getElementById("password");
|
||||
|
||||
var xhr = new XMLHttpRequest();
|
||||
xhr.open("GET", "/change?ssid=" + ssid.value + "&password=" + password.value, true);
|
||||
xhr.send();
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
)rawliteral";
|
40
projektna_naloga/nrs/include/index.html
Normal file
40
projektna_naloga/nrs/include/index.html
Normal file
@@ -0,0 +1,40 @@
|
||||
<!DOCTYPE HTML>
|
||||
<html data-bs-theme="dark">
|
||||
|
||||
<head>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>Password</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
|
||||
<div class="container mt-5 text-center" style="width: 300px;">
|
||||
|
||||
<div class="mb-3">
|
||||
<label for="ssid" class="form-label">SSID</label>
|
||||
<input type="text" class="form-control" id="ssid">
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<label for="password" class="form-label">Password</label>
|
||||
<input type="text" class="form-control" id="password">
|
||||
</div>
|
||||
|
||||
<button class="btn btn-primary" onclick="updateSlider()">Submit</button>
|
||||
|
||||
</div>
|
||||
|
||||
<script>
|
||||
function updateSlider() {
|
||||
let button = document.getElementById("button");
|
||||
let ssid = document.getElementById("ssid");
|
||||
let password = document.getElementById("password");
|
||||
|
||||
var xhr = new XMLHttpRequest();
|
||||
xhr.open("GET", "/change?ssid=" + ssid.value + "&password=" + password.value, true);
|
||||
xhr.send();
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
|
||||
</html>
|
18
projektna_naloga/nrs/platformio.ini
Normal file
18
projektna_naloga/nrs/platformio.ini
Normal file
@@ -0,0 +1,18 @@
|
||||
; 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:esp32dev]
|
||||
platform = espressif32
|
||||
board = esp32dev
|
||||
framework = arduino
|
||||
monitor_speed = 115200
|
||||
lib_deps =
|
||||
esphome/ESPAsyncWebServer-esphome @ ^3.1.0
|
||||
arduino-libraries/NTPClient @ ^3.2.1
|
234
projektna_naloga/nrs/src/main.cpp
Normal file
234
projektna_naloga/nrs/src/main.cpp
Normal file
@@ -0,0 +1,234 @@
|
||||
#include <Arduino.h>
|
||||
#include <WiFi.h>
|
||||
#include <ESPAsyncWebServer.h>
|
||||
#include <HTTPClient.h>
|
||||
#include <NTPClient.h>
|
||||
|
||||
#include "index.hpp"
|
||||
|
||||
#define Deb 0
|
||||
#if Deb
|
||||
#define serial_begin(x) Serial.begin(x)
|
||||
#define print(x) Serial.print(x)
|
||||
#define println(x) Serial.println(x)
|
||||
#else
|
||||
#define serial_begin(x)
|
||||
#define print(x)
|
||||
#define println(x)
|
||||
#endif
|
||||
|
||||
const int trigPin = 5;
|
||||
const int echoPin = 18;
|
||||
|
||||
String ssid = "";
|
||||
String password = "";
|
||||
|
||||
WiFiUDP ntpUDP;
|
||||
NTPClient timeClient(ntpUDP, "ntp1.arnes.si");
|
||||
|
||||
// define sound speed in cm/uS
|
||||
#define SOUND_SPEED 0.034
|
||||
|
||||
long duration = 0;
|
||||
int distanceCm = 0;
|
||||
uint16_t car_count = 0;
|
||||
|
||||
bool isSetup = false;
|
||||
|
||||
unsigned long previousMillisCount = 0;
|
||||
unsigned long previousMillisPost = 0;
|
||||
|
||||
bool send = true;
|
||||
|
||||
AsyncWebServer server(80);
|
||||
|
||||
String serverUrl = "http://164.8.31.61:3000/api/hourlyData";
|
||||
|
||||
const char *format = "{\"location_id\": 999,\"date\": \"%d/%d/%d 1:0:0\",\"car_count\": %d,\"hour\": %d,\"day\": %d,\"month\": %d, \"year\": %d}";
|
||||
|
||||
struct tm getTime()
|
||||
{
|
||||
timeClient.update();
|
||||
|
||||
// Get the current time in UNIX epoch format
|
||||
unsigned long epochTime = timeClient.getEpochTime();
|
||||
|
||||
// Convert epoch time to a struct containing year, month, day, etc.
|
||||
struct tm timeinfo;
|
||||
gmtime_r((const time_t *)&epochTime, &timeinfo);
|
||||
|
||||
return timeinfo;
|
||||
}
|
||||
|
||||
void makePostRequest()
|
||||
{
|
||||
uint8_t temp[200];
|
||||
|
||||
struct tm timeInfo = getTime();
|
||||
|
||||
try
|
||||
{
|
||||
HTTPClient http;
|
||||
// Make the PUT request
|
||||
http.begin(serverUrl);
|
||||
|
||||
// Set the content type to JSON (adjust as needed)
|
||||
http.addHeader("Content-Type", "application/json");
|
||||
|
||||
// Your PUT payload data
|
||||
// String putData = "Your PUT payload data goes here";
|
||||
int len = sprintf((char *)temp, format, timeInfo.tm_mon + 1, timeInfo.tm_mday, timeInfo.tm_year + 1900, car_count, timeInfo.tm_hour, timeInfo.tm_mday, timeInfo.tm_mon + 1, timeInfo.tm_year + 1900);
|
||||
|
||||
// Get the HTTP response code
|
||||
int httpResponseCode = http.POST(temp, (size_t)len);
|
||||
|
||||
// If the request was successful
|
||||
if (httpResponseCode > 0)
|
||||
{
|
||||
print("HTTP Response code: ");
|
||||
println(httpResponseCode);
|
||||
|
||||
// Get the response payload
|
||||
String payload = http.getString();
|
||||
println("Response payload: " + payload);
|
||||
}
|
||||
else
|
||||
{
|
||||
print("HTTP Request failed. Error code: ");
|
||||
println(httpResponseCode);
|
||||
}
|
||||
http.end();
|
||||
}
|
||||
catch (std::exception &ex)
|
||||
{
|
||||
print("Error: ");
|
||||
println(ex.what());
|
||||
}
|
||||
}
|
||||
|
||||
void setupWifiStation()
|
||||
{
|
||||
println("Setting up Wifi Station");
|
||||
WiFi.begin(ssid, password);
|
||||
|
||||
while (WiFi.status() != WL_CONNECTED)
|
||||
{
|
||||
delay(500);
|
||||
print(".");
|
||||
}
|
||||
|
||||
println("");
|
||||
println("WiFi connected");
|
||||
println("IP address: ");
|
||||
println(WiFi.localIP());
|
||||
timeClient.begin();
|
||||
isSetup = true;
|
||||
}
|
||||
|
||||
void setupWifiAcessPoint()
|
||||
{
|
||||
println("Setting up Wifi Access Point");
|
||||
WiFi.softAP("ESP32-Access-Point", "123456789");
|
||||
IPAddress IP = WiFi.softAPIP();
|
||||
print("AP IP address: ");
|
||||
println(IP);
|
||||
|
||||
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request)
|
||||
{ request->send(200, "text/html", index_html); });
|
||||
|
||||
server.on("/change", HTTP_GET, [](AsyncWebServerRequest *request)
|
||||
{
|
||||
ssid = request->getParam("ssid")->value();
|
||||
password = request->getParam("password")->value();
|
||||
|
||||
println("SSID: " + ssid);
|
||||
println("Password: " + password);
|
||||
|
||||
request->send(200, "text/plain", "OK");
|
||||
server.end();
|
||||
WiFi.softAPdisconnect(true);
|
||||
|
||||
setupWifiStation(); });
|
||||
|
||||
server.begin();
|
||||
}
|
||||
|
||||
void setup()
|
||||
{
|
||||
|
||||
serial_begin(115200); // Starts the serial communication
|
||||
|
||||
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
|
||||
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
|
||||
|
||||
// setupWifiStation();
|
||||
setupWifiAcessPoint();
|
||||
}
|
||||
|
||||
void readDistance()
|
||||
{
|
||||
// Clears the trigPin
|
||||
digitalWrite(trigPin, LOW);
|
||||
delayMicroseconds(2);
|
||||
// Sets the trigPin on HIGH state for 10 micro seconds
|
||||
digitalWrite(trigPin, HIGH);
|
||||
delayMicroseconds(11);
|
||||
digitalWrite(trigPin, LOW);
|
||||
|
||||
// Reads the echoPin, returns the sound wave travel time in microseconds
|
||||
duration = pulseIn(echoPin, HIGH);
|
||||
|
||||
// Calculate the distance
|
||||
distanceCm = duration * SOUND_SPEED / 2;
|
||||
}
|
||||
|
||||
int mes[5] = {0};
|
||||
int point = 0;
|
||||
bool prev = false;
|
||||
bool curr = false;
|
||||
|
||||
void loop()
|
||||
{
|
||||
if (!isSetup)
|
||||
return;
|
||||
|
||||
unsigned long currentMillis = millis();
|
||||
|
||||
if (currentMillis - previousMillisCount > 25)
|
||||
{
|
||||
previousMillisCount = currentMillis;
|
||||
readDistance();
|
||||
|
||||
if (distanceCm > 40)
|
||||
distanceCm = 40;
|
||||
|
||||
mes[point] = distanceCm;
|
||||
point++;
|
||||
if (point > 4)
|
||||
point = 0;
|
||||
// https://dsp.stackexchange.com/questions/60277/is-the-typical-implementation-of-low-pass-filter-in-c-code-actually-not-a-typica
|
||||
int avg = (mes[0] + mes[1] + mes[2] + mes[3] + mes[4]) / 5;
|
||||
|
||||
if (avg < 10)
|
||||
curr = true;
|
||||
else
|
||||
curr = false;
|
||||
|
||||
if (curr && !prev)
|
||||
{
|
||||
car_count++;
|
||||
// println(car_count);
|
||||
}
|
||||
|
||||
prev = curr;
|
||||
|
||||
println(avg);
|
||||
}
|
||||
|
||||
if (currentMillis - previousMillisPost > 1000 * 60 * 60)
|
||||
{
|
||||
previousMillisPost = currentMillis;
|
||||
makePostRequest();
|
||||
car_count = 0;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user