Projekt Simple Höhenmesser
Arduino > Pojects
/// ESP32 WiFi Libraries
#include "WiFiUdp.h"
#include <ESP8266WiFi.h>
// Output Time Status Line
#include <time.h>
#include <NTPClient.h>
// Font For Output
// Include Emon Library
#include <dht.h>
#include <DHT_U.h>
#include <FirebaseESP8266.h>
#include "WiFi_config.inc"
WiFiUDP Udp;
uint16_t localUdpPort = 1234; // local port to listen on
DHT_Unified dht(DHTPIN, DHTTYPE);
FirebaseData firebaseData;
FirebaseJsonArray ArrayHumidity;
FirebaseJsonArray ArrayTemperature;
FirebaseJsonArray ArrayTimeStamp;
/*********************************************************************************
* @param Init
*********************************************************************************/
void InitDHTSensors();
/*********************************************************************************
* @param prototypes
*********************************************************************************/
void LoopSetKlimaValue();
void InitOTA();
///-------------
void InitTimer0();
void InitTimer1();
void IRAM_ATTR ontimer0();
void IRAM_ATTR ontimer1();
void connectToWiFi(const char * ssid, const char * pwd);
void PrintText(uint16_t X, uint16_t Y, const char* Text, uint16_t Color, uint16_t EraseColor);
tm ConvertEpochTime2Date(time_t Value);
void SendUDPPackets(String Value );
void printFiles();
void printExample2();
void printReceivedMessage(const uint8_t mac[6], const uint8_t* buf, size_t count, void* cbarg);
void counter_loop() {
uint32_t counter = 0;
while(true) {
Serial.print("counter_loop: ");
Serial.println(counter);
counter++;
}
}
void setup()
{
Serial.begin(115200);
// Initialize SPIFFS
connectToWiFi(sBufferSSID, sBufferPASS);
// Give it 500 tries to connect
uint32_t Versuch = 0;
while ((bWiFiConnected == false) & (Versuch++ < 1000))
{
Serial.printf(". %d", Versuch);
delay(100);
bWiFiConnected = WiFi.isConnected();
}
InitDHTSensors();
pinMode(LED_BUILTIN, OUTPUT);
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
Firebase.reconnectWiFi(true);
// InitTimer0(); No working Correct
InitTimer1();
Udp.begin(UDP_PORT_Send_Receive);
ArrayHumidity.clear();
ArrayTemperature.clear();
ArrayTimeStamp.clear();
}
void loop()
{
if (bWiFiConnected)
{
int packetSize = Udp.parsePacket();
if (packetSize)
{
Serial.printf("Received %d bytes from %s, port %d\n", packetSize, Udp.remoteIP().toString().c_str(), Udp.remotePort());
int len = Udp.read(Buffer, 255);
if (len > 0)
{
String sIP = Udp.remoteIP().toString();
Serial.printf("IP is %s", Udp.remoteIP().toString().c_str());
if (sIP == "192.168.1.49")
bKlimaTrigger = true;
Buffer[len] = '\0';
Serial.printf("UDP packet contents: %s\n", Buffer);
}}
if (bKlimaTrigger)
{
Serial.print("Klima Trigger !") ;
LoopSetKlimaValue();
bKlimaTrigger = false;
}
if (bSecondTrigger)
{
bSecondTrigger = false;
}
if (bGotNTPData)
{
//PrintText(10,50,"Got NTP Data",ILI9341_LIGHTSALMON, ILI9341_DARKGREEN);
bGotNTPData = false;
}
}
delay(100);
}
/*********************************************************************************
* @brief InitDHTSensors()
* @param none
* @return none
*********************************************************************************/
void InitDHTSensors()
{
dht.begin();
sensor_t sensor;
dht.temperature().getSensor(&sensor);
Serial.println("------------------------------------");
Serial.println("Temperature");
Serial.print ("Sensor: "); Serial.println(sensor.name);
Serial.print ("Driver Ver: "); Serial.println(sensor.version);
Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id);
Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println(" *C");
Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println(" *C");
Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println(" *C");
Serial.println("------------------------------------");
// Print humidity sensor details.
dht.humidity().getSensor(&sensor);
Serial.println("------------------------------------");
Serial.println("Humidity");
Serial.print ("Sensor: "); Serial.println(sensor.name);
Serial.print ("Driver Ver: "); Serial.println(sensor.version);
Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id);
Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println("%");
Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println("%");
Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println("%");
Serial.println("------------------------------------");
}
/*********************************************************************************
* @brief LoopGetKlimaValue()
* @param none
* @return Kima Values
*********************************************************************************/
rKlimaServer LoopGetKlimaValue()
{
sensors_event_t event;
dht.temperature().getEvent(&event);
if (isnan(event.temperature)) {
Serial.println("Error reading temperature!");
} else
vKlimaServer.Temperature = event.temperature;
dht.humidity().getEvent(&event);
if (isnan(event.relative_humidity)) {
Serial.println("Error reading Humidity!");
} else
vKlimaServer.Humidity= event.relative_humidity;
return (vKlimaServer);
}
/*********************************************************************************
* @brief LoopGetKlimaValue()
* @param none
* @return none
*********************************************************************************/
void LoopSetKlimaValue()
{
vKlimaServer = LoopGetKlimaValue();
Serial.print("LoopSetKlimaValue");
// dtostrf(vKlimaServer.Humidity,3,2,sPuffer);
sprintf(Buffer,"->%f3.2<-->%f3.2<- \0", vKlimaServer.Temperature, vKlimaServer.Humidity );
sData = Buffer;
SendUDPPackets(sData );
}
/*********************************************************************************
* @brief InitTimer1()
* @param none
* @return none
*********************************************************************************/
void InitTimer1()
{
// Inalize Timer
timer1_isr_init();
timer1_attachInterrupt(ontimer1);
timer1_enable(TIM_DIV256, TIM_EDGE, TIM_LOOP);
timer1_write(1000000);
interrupts();
}
/*********************************************************************************
* @brief InitTimer0()
* @param none
* @return none
*********************************************************************************/
void InitTimer0()
{
noInterrupts();
timer0_isr_init();
timer0_attachInterrupt(ontimer0);
uint32_t next=ESP.getCycleCount()+1000000000;
timer0_write(next);
}
/*********************************************************************************
* @brief void IRAM_ATTR ontimer1()
* @param none
* @return none
*********************************************************************************/
void IRAM_ATTR ontimer1()
{
if (TriggerKlimaCounter++ > 10) // every 20 Minutes Get NTP Datas
{
//bKlimaTrigger = true;
TriggerKlimaCounter = 0;
}
}
/*********************************************************************************
* @brief void IRAM_ATTR ontimer0()
* @param none
* @return none
*********************************************************************************/
void IRAM_ATTR ontimer0()
{
Serial.print("Timer 0");
}
//// void connectToWiFi(const char * ssid, const char * pwd)
/// connect to WiFi via Callback
void connectToWiFi(const char * ssid, const char * pwd)
{
Serial.printf(Buffer, "Connecting to WiFi network : %s", ssid);
// delete old config
WiFi.disconnect(true);
//register event handler
//Initiate connection
WiFi.begin(ssid, pwd);
WiFi.hostname("ESP32_Klima_Server");
}
void SendUDPPackets(String Value )
{
Udp.beginPacket(UDP_SERVER_IP, UDP_PORT_Send);
Udp.print(Value);
Udp.endPacket();
Serial.print("Send UDP") ;
}