• Witaj na Forum Arduino Polska! Zapraszamy do rejestracji!
  • Znajdziesz tutaj wiele informacji na temat hardware / software.
Witaj! Logowanie Rejestracja


Ocena wątku:
  • 0 głosów - średnia: 0
  • 1
  • 2
  • 3
  • 4
  • 5
ESP8266 - odbiór UART , wysyłka przez WiFi
#1
Witam

Oprogramowałem sobie moduł ESP8266, jako AP odbierający "stringi" przez WiFi UDP, w odpowiedzi na odebrane dane moduł odpowiada po WiFi do klienta od którego odebrał dane.

Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
   Udp.write(replyPacket);// odpowiedz z karty zdefinjowanan na gorze
   Udp.endPacket();

Chcę teraz podłączyć do modułu przez UART Arduino Uno, Arduino będzie wysyłało strinaga przez Uart do ESP 8266, a ESP8266 ma wysłać go dalej korzystając z WiFi. 
Jak zapisać do bufora przeznaczonego do wysłania przez WiFi w ESP8266 dane odebrane przez UART z modułu Arduino Uno?
 
Odpowiedź
#2
https://circuits4you.com/2018/01/01/esp-...unication/
Arduino zostało wymyślone po to, by robić dobrze jedną prostą rzecz – migać diodą. 
 
Odpowiedź
#3
Jak to jest kontynuacja wątku z elektrody to tam wrzuciłem kod:
Kod:
#include <ESP8266WiFi.h>
#include <WiFiUDP.h>

#define buffsize 32
#define reczneIP 1
// wifi connection variables
const char* ssid = "ccc";
const char* password = "ccc";
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, 0, 11);
 IPAddress gateway(192, 168, 0, 1);
 IPAddress subnet(255, 255, 255, 0);
 IPAddress dns(192, 168, 0, 1);
#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(5000);
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  
   }
}
}
Po odbiorze danych z UDP czeka 5000ms (to jest do zmiany) bo odpowiedź UART wpisywałem z palca, jeśli jest odpowiedź to ją przesyła, jeśli nie to tylko to potwierdzenie. Jeśli Arduino odpowiada w ciągu 5ms to od biedy można zostawić delay. Można jeszcze zerować stringa z UART, teraz jak coś przyleci przed komunikatem z UDP to od razu tym odpowie, niekoniecznie w związku z aktualnym zapytaniem z UDP.
Edit: Wykasowałem dubla tego tematu.
Miło być decenianym https://buycoffee.to/kaczakat
 
Odpowiedź
  


Skocz do:


Przeglądający: 1 gości