/* 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
//naložiti drivere za ESP-WROOM-32 https://developers.wia.io/release/things/esp-wroom-32
//uporabimo FireBeetle-ESP32 ploščico
//podrobnosti o ploščici: https://docs.espressif.com/projects/esp-idf/en/latest/esp32/hw-reference/esp32/get-started-devkitc.html
//datasheet: https://espressif.com/sites/default/files/documentation/esp32-wroom-32_datasheet_en.pdf
//dht sensor library: https://github.com/beegee-tokyo/DHTesp
//tutorial za ssd1306: https://randomnerdtutorials.com/esp32-ssd1306-oled-display-arduino-ide/
#include <WiFi.h>
#include <HTTPClient.h>
#include "DHTesp.h"
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
const int DHT_PIN = 15;
DHTesp dhtSensor;
Adafruit_SSD1306 display = Adafruit_SSD1306(128, 32, &Wire);
//pusti kot je, povezava je od wowki
//const char* ssid = "Xiaomi 11T Pro";
//const char* password = "bobosmrade";
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";
const char* serverName = "http://ec2-3-67-36-249.eu-central-1.compute.amazonaws.com/log-data?";
//v primeru dodatne varnosti se lahko generira API key
String apiKeyValue = "tPmAT5Ab3j7F9";
String sensorName = "Merilec18";
String sensorLocation = "Ljubljana";
//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
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // Address 0x3C for 128x32
// Clear the buffer.
display.clearDisplay();
display.display();
Serial.print("Connecting to WiFi");
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,0);
display.print("Connecting to SSID:\n");
display.print(ssid);
display.display(); // actually display all of the above
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println(" Connected!");
display.clearDisplay();
display.setCursor(0,0);
display.print("Connected!");
display.display(); // actually display all of the above
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;
display.clearDisplay();
display.setCursor(0,0);
display.print("Status: Sending\n");
display.print("Lux: " + String(lux, 1) +" L\n");
display.print("Tem: " + String(data.temperature, 2) +"C\n");
display.print("Hum: " + String(data.humidity, 1) +"%\n");
display.display(); // actually display all of the above
http.begin(client, serverName);
//ustvarimo željene headerje
http.addHeader("Content-Type", "image/gif");
http.addHeader("Connection", "Keep-Alive"); //brez tega ne gre, enkrat pošlje potem pa več ne
//pripravimo request
String httpRequestData = "id=" + sensorName + "&geo="+ sensorLocation + "&temp=" + String(data.temperature, 2)
+ "&hum=" + String(data.humidity, 1) + "&lux=" + String(lux, 1) + "";
Serial.print("httpRequestData: ");
Serial.println(httpRequestData);
String serverPath = serverName + httpRequestData;
// Your Domain name with URL path or IP address with path
http.begin(serverPath.c_str());
//pošljemo POST request
int httpResponseCode = http.GET();
if (httpResponseCode>0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
display.clearDisplay();
display.setCursor(0,0);
display.print("Succ: "+ String(httpResponseCode) +"\n");
display.print("Lux: " + String(lux, 1) +" L\n");
display.print("Tem: " + String(data.temperature, 2) +"C\n");
display.print("Hum: " + String(data.humidity, 1) +"%\n");
display.display(); // actually display all of the above
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
display.clearDisplay();
display.setCursor(0,0);
display.print("Err: "+ String(httpResponseCode) +"\n");
display.print("Lux: " + String(lux, 1) +" L\n");
display.print("Tem: " + String(data.temperature, 2) +"C\n");
display.print("Hum: " + String(data.humidity, 1) +"%\n");
display.display(); // actually display all of the above
}
http.end();
}
else {
Serial.println("WiFi Disconnected");
display.clearDisplay();
display.setCursor(0,0);
display.print("WiFi Disconnected!\n");
display.display(); // actually display all of the above
}
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 10 sekund
delay(10000);
}