Hello to all,
I made a wifi connection between 2 esp8266. The program works perfectly for sending one byte (on/off). The problem arises when i try to send more value's.
The code gives on the receiving esp8266 the right value's, one byte and the floating value as a string through the serial monitor I tried creating a struct, which didn't work. Esp-now doesn't work either.
thanks in advance
The code for sending
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include<DHT.h> // Including library for dht
// Set WiFi credentials
#define WIFI_SSID "TheOtherESP"
#define WIFI_PASS "flashmeifyoucan"
// UDP
WiFiUDP UDP;
IPAddress remote_IP(192, 168, 4, 1);
#define UDP_PORT 4210
#define dht_dpin 12 //digital pin, that DHT's data line is connected
#define DHTTYPE DHT22 //When using DHT11, put here DHT11 instead of DHT22
DHT dht(12, DHT22); //Not sure, what this line excatly does, but same aplies here as above
float temp;
float hum;
void setup() {
dht.begin();
// Setup IO
pinMode(2, INPUT);
// Setup serial port
Serial.begin(115200);
Serial.println();
// Begin WiFi
WiFi.begin(WIFI_SSID, WIFI_PASS);
WiFi.mode(WIFI_STA);
// Connecting to WiFi...
Serial.print("Connecting to ");
Serial.print(WIFI_SSID);
// Loop continuously while WiFi is not connected
while (WiFi.status() != WL_CONNECTED)
{
delay(100);
Serial.print(".");
}
// Connected to WiFi
Serial.println();
Serial.print("Connected! IP address: ");
Serial.println(WiFi.localIP());
// Begin UDP port
UDP.begin(UDP_PORT);
Serial.print("Opening UDP port ");
Serial.println(UDP_PORT);
}
void loop() {
delay(500);
temp = dht.readTemperature();
hum = dht.readHumidity();
// Read button
char buttonState = digitalRead(2);
// Send Packet
UDP.beginPacket(remote_IP, UDP_PORT);
UDP.write(buttonState);
if (!UDP.endPacket()) {
Serial.println("data has not been sent");
} else
Serial.println("data has been sent"); {
}
UDP.beginPacket(remote_IP, UDP_PORT);
String msg = String(hum);
UDP.write(msg.c_str());
if (!UDP.endPacket()) {
Serial.println("data has not been sent");
} else
Serial.println("data has been sent"); {
}
Serial.print("Temperature: "); //Print text "Temperature: " in to serial monitor
Serial.println(temp);
Serial.print("Humidity: "); //Print text "Humidity: " in to serial monitor
Serial.println(hum); //Print variable (temperature value) in to serial port. ln for line break
Serial.println(" ");
}
The receiver code
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
// Set AP credentials
#define AP_SSID "TheOtherESP"
#define AP_PASS "flashmeifyoucan"
// UDP
WiFiUDP UDP;
IPAddress local_IP(192, 168, 4, 1);
IPAddress gateway(192, 168, 4, 1);
IPAddress subnet(255, 255, 255, 0);
#define UDP_PORT 4210
float hum;
// UDP Buffer
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];
char ReplyBuffer[] = "acknowledged";
void setup() {
// Setup LED pin
pinMode(2, OUTPUT);
// Setup serial port
Serial.begin(115200);
Serial.println();
// Begin Access Point
Serial.println("Starting access point...");
WiFi.softAPConfig(local_IP, gateway, subnet);
WiFi.softAP(AP_SSID, AP_PASS);
Serial.println(WiFi.localIP());
// Begin listening to UDP port
UDP.begin(UDP_PORT);
Serial.print("Listening on UDP port ");
Serial.println(UDP_PORT);
}
void loop() {
// Receive packet
int packetSize = UDP.parsePacket();
if (packetSize) {
Serial.print("Received packet of size ");
Serial.println(packetSize);
Serial.print("From ");
IPAddress remoteIp = UDP.remoteIP();
Serial.print(remoteIp);
Serial.print(", port ");
Serial.println(UDP.remotePort());
// read the packet into packetBufffer
int len = UDP.read(packetBuffer, 10);
if (len > 0) {
packetBuffer[len] = 0;
}
if (packetBuffer[0]) {
digitalWrite(2, HIGH);
} else {
digitalWrite(2, LOW);
}
Serial.println("Contents:");
Serial.println(packetBuffer);
}
}