https://github.com/esp8266/Arduino/issues/3481
Kommt mir sehr bekannt vor! Ich sende auch nur ein einziges Paket!
Hier der Code der funktioniert:
Code:
#include <ESP8266WiFi.h> //ESP8266 Core WiFi Library (you most likely already have this in your sketch)
#include <WiFiUdp.h>
#include <DHT.h>
#define DHTPIN 0
#define DHTTYPE DHT22
//Init DHT-Sensor
DHT dht(DHTPIN, DHTTYPE);
//set IPs
IPAddress IPudp (192, 168, 10, 68);
IPAddress IPlocal (192, 168, 10, 201);
IPAddress IPdns (192, 168, 10, 1);
IPAddress IPgateway (192, 168, 10, 1);
IPAddress IPsubnetmask (255, 255, 255, 0);
//*******DeviceID-config************************
char UnitID[] = "id_1";
//******************************************
unsigned int receiveUdpPort = 5006;
unsigned int sendUdpPort = 5005;
char ssid[] = ("xxx");
char passphrase[] = ("xxx");
char charTemp[10];
char charHum[10];
int sendingStat;
WiFiUDP udp;
void setup() {
Serial.begin(115200);
delay(1000);
Serial.println("\n\nSerial connection started");
WiFi.mode (WIFI_STA);
//WiFi.config (IPlocal, IPdns, IPgateway, IPsubnetmask);
WiFi.begin(ssid, passphrase);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
Serial.print(WiFi.status());
}
WiFi.printDiag(Serial);
Serial.print(WiFi.status());
pinMode(12, OUTPUT);
Serial.printf("Now listening at IP %s, UDP port %d\n\n", WiFi.localIP().toString().c_str(), receiveUdpPort);
delay(5000);
}
void loop() {
digitalWrite(12, HIGH);
delay(3500);
float hum = dht.readHumidity();
float temp = dht.readTemperature();
digitalWrite(12, LOW);
dtostrf(temp,5,2,charTemp);
dtostrf(hum,5,2,charHum);
delay(1000);
if (isnan(hum) || isnan(temp)) {
Serial.println("Sensorfehler!");
udp.beginPacket(IPudp,sendUdpPort);
udp.write(UnitID);
udp.write(" Sensorfehler\n");
udp.endPacket();
}
else {
long rssi = WiFi.RSSI();
Serial.println("Sending Sensordata");
if (!udp.begin(receiveUdpPort)) {
Serial.println("UDP Socketerror");
}
Serial.printf("Now sending UDP on port %d \n", sendUdpPort);
udp.beginPacket(IPudp,sendUdpPort);
udp.write(UnitID);
udp.write(" ");
udp.write(charTemp);
udp.write(" ");
udp.write(charHum);
udp.write(" ");
udp.write(rssi);
sendingStat == udp.endPacket();
yield();
if(sendingStat = 1) {
Serial.println("UDP Packet successful send");
}
else {
Serial.println("UDP Packet not send");
}
Serial.println("Wait 0,5s..");
delay(500);
Serial.print(" UDP Destination IP: :");
Serial.println(IPudp);
Serial.print(" UDP Port: ");
Serial.println(sendUdpPort);
Serial.print(" Device ID: ");
Serial.println(UnitID);
Serial.print(" Temperatur: ");
Serial.println(charTemp);
Serial.print(" Relative Luftfeuchte: ");
Serial.println(charHum);
Serial.print(" Daempfung: ");
Serial.println(rssi);
//udp.stop();
//WiFi.disconnect();
//WiFi.mode(WIFI_OFF);
Serial.println("wait 5,5s");
delay(5500);
//Serial.println("ESP goes to deepsleep 60s");
//ESP.deepSleep(60e6);
delay(100);
}
}
Und dieser funktioniert nicht zuverlässig. Die Pakete kommen zwischen ca. 10s - 500s an (Unterschied nur in den letzten, nicht mehr auskommentierten Zeilen):
Code:
#include <ESP8266WiFi.h> //ESP8266 Core WiFi Library (you most likely already have this in your sketch)
#include <WiFiUdp.h>
#include <DHT.h>
#define DHTPIN 0
#define DHTTYPE DHT22
//Init DHT-Sensor
DHT dht(DHTPIN, DHTTYPE);
//set IPs
IPAddress IPudp (192, 168, 10, 68);
IPAddress IPlocal (192, 168, 10, 201);
IPAddress IPdns (192, 168, 10, 1);
IPAddress IPgateway (192, 168, 10, 1);
IPAddress IPsubnetmask (255, 255, 255, 0);
//*******DeviceID-config************************
char UnitID[] = "id_1";
//******************************************
unsigned int receiveUdpPort = 5006;
unsigned int sendUdpPort = 5005;
char ssid[] = ("XXX");
char passphrase[] = ("XXX");
char charTemp[10];
char charHum[10];
int sendingStat;
WiFiUDP udp;
void setup() {
Serial.begin(115200);
delay(1000);
Serial.println("\n\nSerial connection started");
WiFi.mode (WIFI_STA);
//WiFi.config (IPlocal, IPdns, IPgateway, IPsubnetmask);
WiFi.begin(ssid, passphrase);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
Serial.print(WiFi.status());
}
WiFi.printDiag(Serial);
Serial.print(WiFi.status());
pinMode(12, OUTPUT);
Serial.printf("Now listening at IP %s, UDP port %d\n\n", WiFi.localIP().toString().c_str(), receiveUdpPort);
delay(5000);
}
void loop() {
digitalWrite(12, HIGH);
delay(3500);
float hum = dht.readHumidity();
float temp = dht.readTemperature();
digitalWrite(12, LOW);
dtostrf(temp,5,2,charTemp);
dtostrf(hum,5,2,charHum);
delay(1000);
if (isnan(hum) || isnan(temp)) {
Serial.println("Sensorfehler!");
udp.beginPacket(IPudp,sendUdpPort);
udp.write(UnitID);
udp.write(" Sensorfehler\n");
udp.endPacket();
}
else {
long rssi = WiFi.RSSI();
Serial.println("Sending Sensordata");
if (!udp.begin(receiveUdpPort)) {
Serial.println("UDP Socketerror");
}
Serial.printf("Now sending UDP on port %d \n", sendUdpPort);
udp.beginPacket(IPudp,sendUdpPort);
udp.write(UnitID);
udp.write(" ");
udp.write(charTemp);
udp.write(" ");
udp.write(charHum);
udp.write(" ");
udp.write(rssi);
sendingStat == udp.endPacket();
yield();
if(sendingStat = 1) {
Serial.println("UDP Packet successful send");
}
else {
Serial.println("UDP Packet not send");
}
Serial.println("Wait 0,5s..");
delay(500);
Serial.print(" UDP Destination IP: :");
Serial.println(IPudp);
Serial.print(" UDP Port: ");
Serial.println(sendUdpPort);
Serial.print(" Device ID: ");
Serial.println(UnitID);
Serial.print(" Temperatur: ");
Serial.println(charTemp);
Serial.print(" Relative Luftfeuchte: ");
Serial.println(charHum);
Serial.print(" Daempfung: ");
Serial.println(rssi);
udp.stop();
WiFi.disconnect();
WiFi.mode(WIFI_OFF);
Serial.println("wait 5,5s");
delay(5500);
Serial.println("ESP goes to deepsleep 60s");
ESP.deepSleep(60e6);
delay(100);
}
}
Lesezeichen