Hallo Zusammen,
ich habe eine Verbindung zwischen einem ESP8266 und einem ESP32 hergestellt,
leider scheint der ESP32 zu schnell für den ESP8266 und überflutet daher den ESP8266, da dieser nicht schnell genug den Buffer leeren kann.
Ich tätige zwar mit Client.flush() nach jedem Client.read() den Buffer (82er), dennoch bin ich gezwungen ein Delay zwischen den Loops (32er) ein zu fügen, damit der 82er noch funktioniert.
Empfänger (ESP8266)
#include <Arduino.h>
#include <ESP8266WiFi.h>
/* Your WiFi Soft Access Point settings */
/* SERVER */
#define LEFTDIRECTION D1 // ESP8266 pin 5 or D1 is connected to MDDS30 pin IN1.
#define RIGHTDIRECTION D2 // ESP8266 pin 0 or D2 is connected to MDDS30 pin IN2.
#define LEFTPWM D3 // ESP8266 pin 4 or D3 is connected to MDDS30 pin AN1.
#define RIGHTPWM D0 // ESP8266 pin 2 or D4 is connected to MDDS30 pin AN2.
const int joyThreshold = 60;
const int joyMidValue = 1880;
const int joyMidValueLow = joyMidValue - joyThreshold;
const int joyMidValueHigh = joyMidValue + joyThreshold;
const int joyValueMax = 4095;
const int joyValueMin = 0;
const int motorSpeed = 0;
const int motorSpeedMin = 100;
const int motorSpeedMax = 1024;
int speedLeft = 0;
int speedRight = 0;
int speedFwdI = 0;
int speedTurnI = 0;
const char* ssid = "TOREPLACE"; //this will be the network name
const char* password = "TOREPLACE"; //this will be the network password
const uint16_t port = 1337;
WiFiServer server(port);
uint8_t msg[4];
uint16_t sensorValueX = 0;
uint16_t sensorValueY = 0;
int speedTurn(uint16_t joyXValue) {
if(joyXValue > joyMidValueHigh) //right
{
return map(joyXValue, joyMidValueHigh, joyValueMax, motorSpeedMin, motorSpeedMax);
}
else if(joyXValue < joyMidValueLow) //left
{
return map(joyXValue, joyMidValueLow, joyValueMin, -motorSpeedMin, -motorSpeedMax);
}
else
{
return 0;
}
}
int speedFwd(uint16_t joyYValue) {
if(joyYValue > joyMidValueHigh)//forward
{
return map(joyYValue, joyMidValueHigh, joyValueMax, motorSpeedMin, motorSpeedMax);
}
else if(joyYValue < joyMidValueLow) //backward
{
return map(joyYValue, joyMidValueLow, joyValueMin, -motorSpeedMin, -motorSpeedMax);
}
else
{
return 0;
}
}
void bewegeWohnwagen(int speedLinks, int speedRechts) {
if (speedLinks > 0){
digitalWrite(LEFTDIRECTION, HIGH);
}
else {
digitalWrite(LEFTDIRECTION, LOW);
}
if (speedRechts > 0){
digitalWrite(RIGHTDIRECTION, HIGH);
}
else {
digitalWrite(RIGHTDIRECTION, LOW);
}
analogWrite(LEFTPWM,abs(speedLinks));
analogWrite(RIGHTPWM,abs(speedRechts));
}
void setup() {
// pins vorbereiten
pinMode(LEFTDIRECTION, OUTPUT);
pinMode(RIGHTDIRECTION, OUTPUT);
pinMode(LEFTPWM, OUTPUT);
pinMode(RIGHTPWM, OUTPUT);
Serial.begin(9600);
Serial.print("Configuring WiFi access point...");
//Adressen festlegen
IPAddress ip(192, 168, 1, 1);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress dns(192, 168, 1, 1);
WiFi.softAPConfig(ip, gateway, subnet);
WiFi.mode(WIFI_AP);
boolean result = WiFi.softAP(ssid, password);
if(result==true) {
server.begin();
} else {
Serial.println("couldn't open SoftAp");
}
}
void loop() {
// Check if a client has connected
WiFiClient client = WiFiClient();
client = server.available();
if(!client) {
return;
}
// ToDo: Overflow sicher machen
// Wait until the client sends some data
unsigned long initialTime = millis();
unsigned long timeout = initialTime + 3000;
while (!client.available() && millis() < timeout) {
Serial.println("wait for data");
delay(100);
}
if (millis() > timeout) {
Serial.println("timeout");
client.flush();
return;
}
while(client.connected()){
if(!client.available()) continue;
int got = client.read(msg, 4);
client.flush();
if(got = 4) {
sensorValueX = ((uint16_t)msg[1] << 8) | msg[0];
sensorValueY = ((uint16_t)msg[3] << 8) | msg[2];
Serial.print("SensorX:");
Serial.print(sensorValueX);
Serial.print(" SensorY:");
Serial.print(sensorValueY);
speedFwdI = speedFwd(sensorValueY);
speedTurnI = speedTurn(sensorValueX);
speedLeft = speedFwdI + speedTurnI;
speedRight = speedFwdI - speedTurnI;
bewegeWohnwagen(speedLeft, speedRight);
Serial.print(" SpeedLeft:");
Serial.print(speedLeft);
Serial.print(" SpeedRight:");
Serial.print(speedRight);
Serial.print(" SpeedFwd:");
Serial.print(speedFwdI);
Serial.print(" SpeedTurn:");
Serial.println(speedTurnI);
}
}
if(!client.available())
{
client.stop();
Serial.println("Client disconnected");
}
}
Sender (ESP32)
#include <Arduino.h>
#include <WiFi.h>
#define JOYX A5 // ESP32 pin 5 or D1 is connected to MDDS30 pin IN1.
#define JOYY A4 // ESP32 pin 0 or D2 is connected to MDDS30 pin IN2.
const char* ssid = "TOREPLACE"; //replace this with your WiFi network name
const char* password = "TOREPLACE"; //replace this with your WiFi network password
const uint16_t port = 1337;
const char *host = "192.168.1.1";
uint16_t sensorValueX = 0;
uint16_t sensorValueY = 0;
uint8_t sensorValues[4];
WiFiClient client;
void connectToNetwork() {
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Establishing connection to WiFi..");
//WiFi.begin(ssid, password);
}
Serial.println("Connected to network");
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
connectToNetwork();
IPAddress ip(192, 168, 1, 3);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress dns(192, 168, 1, 1);
WiFi.config(ip, dns, gateway, subnet);
Serial.println(WiFi.localIP());
}
void loop() {
if (!WiFi.isConnected()) connectToNetwork();
if(!client.connected()) {
Serial.println("not connected try to connect to server");
int state = client.connect(host,port);
if (!state){
Serial.println("Connection to host failed");
delay(1000);
return;
}
}
while(client.connected()){
delay(200);
// put your main code here, to run repeatedly:
sensorValueX = analogRead(JOYX);
sensorValues[0] = sensorValueX & 0xff;
sensorValues[1] = (sensorValueX >> 8);
sensorValueY = analogRead(JOYY);
sensorValues[2] = sensorValueY & 0xff;
sensorValues[3] = (sensorValueY >> 8);
int size = sizeof(sensorValues);
client.write(sensorValues, sizeof(size));
Serial.print("SensorX:");
Serial.print(sensorValueX);
Serial.print(" Sensory:");
Serial.println(sensorValueY);
client.flush();
}
}
Empfänger.cpp (4.37 KB)
Sender.cpp (2 KB)