Jest gotowy wsad do ESP z mostem UART przez WIFI. W Win można zainstalować manager wirtualnych portów COM, darmowy Tibbo. Działa to na tańszym ESP8266, może być np. fajny moduł WEMOS MINI.
Natomiast taką samą funkcjonalność uzyskałem z przykładem Telnet, przykład WIFITelnetToSerial wszystko co przyleci z UART wysyłasz na Telnet. Tak samo z TCP, czy UDP. Zobacz jak tam wyglądają te funkcje, nie kojarzę żadnego readUntlil \n, jak to ma być przeźroczyste to cokolwiek przyleci przesyłasz dalej, nie widzę sensu tego analizować.
W ESP32 też jest ten przykład telet.
Z UDP robiłem coś takiego, na bazie tego:
Kod:
/*
WiFiEsp example: WiFi UDP Send and Receive String
This sketch wait an UDP packet on localPort using a WiFi shield.
When a packet is received an 'ACK' packet is sent to the client on port remotePort.
For more details see: http://yaab-arduino.blogspot.com/p/wifiesp-example-client.html
*/
#include <ESP8266WiFi.h>
#include <WiFiUDP.h>
const char* ssid = "nnn";
const char* password = "nnn";
int status = WL_IDLE_STATUS; // the Wifi radio's status
unsigned int localPort = 2390; // local port to listen on
char packetBuffer[255]; // buffer to hold incoming packet
char ReplyBuffer[] = "ACK"; // a string to send back
WiFiUDP Udp;
void setup() {
// initialize serial for debugging
Serial.begin(115200);
// initialize ESP module
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected to wifi");
printWifiStatus();
Serial.println("\nStarting connection to server...");
// if you get a connection, report back via serial:
Udp.begin(localPort);
Serial.print("Listening on port ");
Serial.println(localPort);
}
void loop() {
// if there's data available, read a 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, 255);
if (len > 0) {
packetBuffer[len] = 0;
}
Serial.println("Contents:");
Serial.println(packetBuffer);
// send a reply, to the IP address and port that sent us the packet we received
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
Udp.write(ReplyBuffer);
Udp.endPacket();
}
}
void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
Odwróciłem to tak, by wysyłało mi do Putty co przyleci na UART, faktycznie składałem to w linie, ale na bazie przykładu z serialEvent:
Kod:
#include <ESP8266WiFi.h>
#include <WiFiUDP.h>
#define buffsize 32
#define reczneIP 1
// wifi connection variables
const char* ssid = "nnn";
const char* password = "nnn";
boolean wifiConnected = false;
char serial_input[buffsize]; // a String to hold incoming data
bool stringComplete = false; // whether the string is complete
//byte index = 0;
// UDP variables
unsigned int localPort = 8888;
WiFiUDP UDP;
boolean udpConnected = false;
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
char ReplyBuffer[] = "acknowledged"; // a string to send back
#if (reczneIP)
IPAddress ip(192, 168, 1, 37);
IPAddress gateway(192, 168, 1, 3);
IPAddress subnet(255, 255, 255, 0);
IPAddress dns(192, 168, 1, 3);
#endif
void setup() {
// Initialise Serial connection
Serial.begin(115200);
// Initialise wifi connection
wifiConnected = connectWifi();
// only proceed if wifi connection successful
if(wifiConnected){
udpConnected = connectUDP();
if (udpConnected){
// initialise pins
//pinMode(5,OUTPUT);
}
}
}
void loop() {
serialEvent();//w esp nie ma serialevent, ktos cos wie?
// check if the WiFi and UDP connections were successful
if(wifiConnected){
if(udpConnected){
// if there’s data available, read a packet
int packetSize = UDP.parsePacket();
if(packetSize)
{
//Serial.println("");
//Serial.print("Received packet of size ");
//Serial.println(packetSize);
//Serial.print("From ");
//IPAddress remote = UDP.remoteIP();
//for (int i =0; i < 4; i++)
//{
//Serial.print(remote[i], DEC);
//if (i < 3)
//{
//Serial.print(".");
//}
//}
//Serial.print(", port ");
//Serial.println(UDP.remotePort());
// read the packet into packetBufffer
UDP.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);
//Serial.println("Contents:");
//int value = packetBuffer[0]*256 + packetBuffer[1];
//Serial.println(value);
Serial.println(packetBuffer);
// send a reply, to the IP address and port that sent us the packet we received
delay(50);
serialEvent();//w esp nie ma serialevent, ktos cos wie?
if(stringComplete)
{
UDP.beginPacket(UDP.remoteIP(), UDP.remotePort());
char *odpowiedz=serial_input;
UDP.write(odpowiedz);
stringComplete=false;
UDP.endPacket();
Serial.println("Wyslano i wyzerowano");
} else
{
UDP.beginPacket(UDP.remoteIP(), UDP.remotePort());
UDP.write(ReplyBuffer);
Serial.println("czy on to napierdala czaly czas?");
UDP.endPacket();
}
// turn LED on or off depending on value recieved
//digitalWrite(5,value);
}
delay(1);
}
//if(stringComplete)
//{
// UDP.beginPacket(UDP.remoteIP(), UDP.remotePort());
// char *odpowiedz=serial_input;
// UDP.write(odpowiedz);
// stringComplete=false;
// UDP.endPacket();
// Serial.println("Wyslano i wyzerowano");
//}
}
}
// connect to UDP – returns true if successful or false if not
boolean connectUDP(){
boolean state = false;
Serial.println("");
Serial.println("Connecting to UDP");
if(UDP.begin(localPort) == 1){
Serial.println("Connection successful");
state = true;
}
else{
Serial.println("Connection failed");
}
return state;
}
// connect to wifi – returns true if successful or false if not
boolean connectWifi(){
boolean state = true;
int i = 0;
#if (reczneIP)
WiFi.config(ip, dns, gateway, subnet); //only when IP static
#endif
WiFi.begin(ssid, password);
Serial.println("");
Serial.println("Connecting to WiFi");
// Wait for connection
Serial.print("Connecting");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
if (i > 10){
state = false;
break;
}
i++;
}
if (state){
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
else {
Serial.println("");
Serial.println("Connection failed.");
}
return state;
}
void serialEvent() {
while (Serial.available()>0) {
char aChar = Serial.read();
static byte index=0;
if(aChar == '\n')
{
// wykryto znak konca linii \n, koniec odbioru
serial_input[index] = 0;
index = 0;
stringComplete = true;
}
else
{
serial_input[index] = aChar;
if(index<(buffsize-1)) index++; //jesli napis bedzie dluzszy niz bufor to nadmiar leci w kosz
serial_input[index] = '\0'; // Na koncu napisu wstawiamy 0
}
}
}
Do ESP32 trzeba to sobie przepisać, jak w przykładzie z Telnet. Nie interesowało mnie co przyleci z UDP, bo tylko monitorowałem co robi ESP, ale jest to dalej, tylko zakomentowane.
Prawdę mówiąc to próbowałem skompilować dla ESP32 i wyszła lipa, rzuca się o składnie i konwersję zmiennych, wrzuciłem w Google pytanie o gotowca i też wysypuje błędy, mam najnowszy core, trochę w nim pomieszali i wiele rzeczy nie jest już tak proste jak kiedyś, a tu jest sprzed 5 lat:
https://github.com/ajaybnl/UDP-SERIAL-FOR-ESP32-ESP8266- więc pewnie z core 1.x by działał.