Teil 2:
Mein Code
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ArduinoOTA.h>
#include <DFRobot_BME680.h>
ESP8266WiFiMulti wifiMulti; // Create an instance of the ESP8266WiFiMulti class, called 'wifiMulti'
/*
----------------- BME680 Einstellungen --------------
*/
const uint8_t bme_addr = 0x77; // BME680 I2C Adresse
DFRobot_BME680 bme(bme_addr);
int t; // Variable für Temperatur
int p; // Variable für Luftdruck
int a; // Variable für Höhe über NN
int h; // Variable für Luftfeuchtigkeit
int g; // Variable für Co2 Wert
#define id 1
/*
----------------- Delay Einstellungen --------------
*/
#define resetinterval 5 // In Minuten
#define displayinterval 1 // In Sekunden
unsigned long previousMillis = 0;
const long interval = 1000; //displayinterval * 1000;
unsigned long previousMillis2 = 0;
const long interval2 = resetinterval * 1000 * 60;
/*
----------------- UDP Einstellungen --------------
*/
WiFiUDP Udp;
unsigned int localUdpPort = 8888; // Lokaler UDP Port
char incomingPacket[255]; // Buffer für eingehende Pakete
char replyPacekt[] = "nodeMCU - Got message"; // Paket was als Antwort gesendet wird
const uint16_t UDP_REMOTE_PORT = 8888; // Entfernter UDP Port
const char UDP_REMOTE_HOST[] = "192.168.2.150"; // IP des Empfängers
const byte led = D8;
void setup()
{
Wire.begin(D3, D4);
Wire.setClock(100000);
Serial.begin(115200); // Start the Serial communication to send messages to the computer
delay(10);
Serial.println('\n');
while (!Serial);
if (bme.init()) {
Serial.println("init sucessful");
} else {
Serial.println("init faild");
for (;;);
}
wifiMulti.addAP("****", "****"); // add Wi-Fi networks you want to connect to
Serial.println("Connecting ...");
int i = 0;
while (wifiMulti.run() != WL_CONNECTED) { // Wait for the Wi-Fi to connect
delay(250);
Serial.print('.');
}
Serial.println('\n');
Serial.print("Connected to ");
Serial.println(WiFi.SSID()); // Tell us what network we're connected to
Serial.print("IP address:\t");
Serial.println(WiFi.localIP()); // Send the IP address of the ESP8266 to the computer
ArduinoOTA.setHostname("ESP8266");
ArduinoOTA.setPassword("esp8266");
ArduinoOTA.onStart([]() {
Serial.println("Start");
});
ArduinoOTA.onEnd([]() {
Serial.println("\nEnd");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("End Failed");
});
ArduinoOTA.begin();
Serial.println("OTA ready");
pinMode(led, OUTPUT);
digitalWrite(led, 1);
Udp.begin(localUdpPort);
}
unsigned long previousTime = millis();
void loop()
{
ArduinoOTA.handle();
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
BMEreadSensors();
// BMEprintValues();
// UDP();
}
unsigned long diff = millis() - previousTime;
if (diff > interval) {
digitalWrite(led, !digitalRead(led)); // Change the state of the LED
previousTime += diff;
}
}
// ----------------------------------------------------------- UDP -------------------------------------------------------------------
void UDP()
{
byte message[] = {(t), (h), (p), (id), (a - 300), (g)};
Udp.beginPacket(UDP_REMOTE_HOST, UDP_REMOTE_PORT);
Udp.write(message, sizeof(message));
Udp.endPacket();
Udp.beginPacket(UDP_REMOTE_HOST, UDP_REMOTE_PORT);
Udp.write(message, sizeof(message));
Udp.endPacket();
}
// ---------------------------------------------------- BMEreadSensors ----------------------------------------------------------------
void BMEreadSensors()
{
bme.startConvert();
t = bme.readTempture();
h = bme.readHumidity();
p = bme.readPressure();
a = bme.readAltitude();
g = bme.readGas();
}
// ---------------------------------------------------- BMEprintValues -------------------------------------------------------------------
/*
void BMEprintValues()
{
Serial.println();
Serial.print("Temperatur: ");
Serial.print(t);
Serial.write(0xb0);
Serial.println("C");
Serial.print("Luftdruck: ");
Serial.print(p / 100);
Serial.println(" hPa");
Serial.print("H");
Serial.print(char(246));
Serial.print("he: ");
Serial.print(a);
Serial.print(" Meter ");
Serial.print(char(252));
Serial.println("ber NN");
Serial.print("Luftfeuchtigkeit: ");
Serial.print(h);
Serial.println(" %");
Serial.print("Co2-Gehalt: ");
Serial.print(g);
Serial.println(" ppm");
}*/
Kommentiere ich also
void UDP()
{
byte message[] = {(t), (h), (p), (id), (a - 300), (g)};
Udp.beginPacket(UDP_REMOTE_HOST, UDP_REMOTE_PORT);
Udp.write(message, sizeof(message));
Udp.endPacket();
Udp.beginPacket(UDP_REMOTE_HOST, UDP_REMOTE_PORT);
Udp.write(message, sizeof(message));
Udp.endPacket();
}
aus, kann ich den Sketch jederzeit per Wlan hochladen.
Kommentiere ich es ein, passiert nach dem Upload wieder nichts.
Hat da jemand erfahrung mit?
Die Alternative zu dem Upload mit der IDE ist ja irgendwie nen Upload via Webserver? Sowas will ich allerdings nicht.
Kennt dort jemand eine Lösung für? Habe schon Videos vom Kerl mit dem schweizer Akzent geschaut, bin aber nicht schlauer.
Danke vorab schonmal (Soll ja Menschen geben die sich nach 2 Stunden beschweren man sagt hier nicht danke
)