/* ESP32 WiFi Scanning example */
//povzeto po: https://randomnerdtutorials.com/esp32-esp8266-mysql-database-php/
//kaj si začneti z lux podatki: https://docs.wokwi.com/parts/wokwi-photoresistor-sensor
#include <WiFi.h>
#include <HTTPClient.h>
#include "DHTesp.h"
const int DHT_PIN = 15;
DHTesp dhtSensor;
//pusti kot je, povezava je od wowki
const char* ssid = "Wokwi-GUEST";
const char* password = "";
//naslov spletnega mesta, kjer je API. Obvezna uporaba HTTP (ne HTTPS)!
const char* serverName = "http://digitalnidvojcek.000webhostapp.com/post-esp-data.php";
//v primeru dodatne varnosti se lahko generira API key
String apiKeyValue = "tPmAT5Ab3j7F9";
String sensorName = "DHT22+photoresistor";
String sensorLocation = "simulator";
//vrednosti za izračun svetlosti
const float GAMMA = 0.7;
const float RL10 = 50;
void setup() {
pinMode(34, ANALOG); //vhod 34 uporabljamo kot analognega
Serial.begin(9600); //za povezavo na internet uporabljamo baud 9600
Serial.print("Connecting to WiFi");
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println(" Connected!");
Serial.begin(115200); //za branje senzorjev uporabljamo baud 115200
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
}
void loop() {
TempAndHumidity data = dhtSensor.getTempAndHumidity();
//uporabljamo 10 bitno kodiranje
analogReadResolution(10);
int analogValue = analogRead(34);
float voltage = analogValue / 1024. * 5;
float resistance = 2000 * voltage / (1 - voltage / 5);
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
if(WiFi.status()== WL_CONNECTED){
WiFiClient client;
HTTPClient http;
http.begin(client, serverName);
//ustvarimo željene headerje
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
http.addHeader("Connection", "Keep-Alive"); //brez tega ne gre, enkrat pošlje potem pa več ne
//pripravimo request
String httpRequestData = "api_key=" + apiKeyValue + "&sensor=" + sensorName
+ "&location=" + sensorLocation + "&value1=" + String(data.temperature, 2)
+ "&value2=" + String(data.humidity, 1) + "&value3=" + String(lux, 1) + "";
Serial.print("httpRequestData: ");
Serial.println(httpRequestData);
//pošljemo POST request
int httpResponseCode = http.POST(httpRequestData);
if (httpResponseCode>0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
http.end();
}
else {
Serial.println("WiFi Disconnected");
}
Serial.println("analogValue: " + String(analogValue, 2));
Serial.println("voltage: " + String(voltage, 2) + "V");
Serial.println("resistance: " + String(resistance, 2) + " ohm");
Serial.println("lux: " + String(lux, 2) + " lux");
Serial.println("Temp: " + String(data.temperature, 2) + "°C");
Serial.println("Humidity: " + String(data.humidity, 1) + "%");
Serial.println("---");
//ponovimo vsakih 30 sekund
delay(10000);
}
/*#include "WiFi.h"
void setup() {
Serial.begin(115200);
Serial.println("Initializing WiFi...");
WiFi.mode(WIFI_STA);
Serial.println("Setup done!");
}
void loop() {
Serial.println("Scanning...");
// WiFi.scanNetworks will return the number of networks found
int n = WiFi.scanNetworks();
Serial.println("Scan done!");
if (n == 0) {
Serial.println("No networks found.");
} else {
Serial.println();
Serial.print(n);
Serial.println(" networks found");
for (int i = 0; i < n; ++i) {
// Print SSID and RSSI for each network found
Serial.print(i + 1);
Serial.print(": ");
Serial.print(WiFi.SSID(i));
Serial.print(" (");
Serial.print(WiFi.RSSI(i));
Serial.print(")");
Serial.println((WiFi.encryptionType(i) == WIFI_AUTH_OPEN) ? " " : "*");
delay(10);
}
}
Serial.println("");
// Wait a bit before scanning again
delay(5000);
}*/