tonight I am experiencing a problem with transmitting UDP datagrams. I tried to configure a network as following:
[CLIENT 1 ] --------> [ ACCESS POINT ] <------------ [CLIENT 2]
Basically the "Access Point" has to communicate to "Client 1" and "Client 2" has to communicate with "Client 1" too, in practice this does not happen. I can send UDP packets from the Access Point to the Client 1, but no packets sent from Clients 2 is received by Client 1.
The "Access Point" is an ESP8266 configured as SoftAP, the code is the following:
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <stdlib.h>
#include <Wire.h>
extern "C" {
#include<user_interface.h>
}
#define MPU6050_ADDRESS 0X68
#define MPU6050_ACCEL_CONFIG 0x1C // R/W
#define MPU6050_PWR_MGMT_1 0x6B // R/W
#define DEBUG 0
//////////////////////
// WiFi Definitions //
//////////////////////
const char WiFiAPPsw[] = "password";
const char WiFiAPName[] = "UDP_master";
WiFiServer server(80);
boolean wifiConnected = false;
/////////////////////
// UDP Definitions //
/////////////////////
WiFiUDP UDP;
const int UDP_port = 8888;
IPAddress ip(192, 168, 4, 2);
boolean udpConnected = false;
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
char ReplyBuffer[] = "acknowledged"; // a string to send back
/////////////////////////
// MPU6050 Definitions //
/////////////////////////
int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;
char charBuffer[50];
unsigned int packetCounter;
void initHardware(void);
boolean connectUDP(void);
boolean connectWifi(void);
String printResults (void);
void setup()
{
initHardware();
wifiConnected = connectWifi();
if(wifiConnected)
udpConnected = connectUDP();
//setupWiFi();
server.begin();
}
void loop()
{
Wire.beginTransmission(MPU6050_ADDRESS);
Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
Wire.endTransmission(false);
Wire.requestFrom(MPU6050_ADDRESS,14,true); // request a total of 14 registers
AcX=Wire.read()<<8|Wire.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L) acc/2^15*4*1000 così l'unità di misura è il milli_g
AcY=Wire.read()<<8|Wire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
AcZ=Wire.read()<<8|Wire.read(); // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
Tmp=Wire.read()<<8|Wire.read(); // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
GyX=Wire.read()<<8|Wire.read(); // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
GyY=Wire.read()<<8|Wire.read(); // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
GyZ=Wire.read()<<8|Wire.read(); // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
AcX = (float) AcX / 16.384;
AcY = (float) AcY / 16.384;
AcZ = (float) AcZ / 16.384;
String buffer = printResults();
buffer.toCharArray(charBuffer,sizeof(charBuffer));
Serial.println(charBuffer);
UDP.beginPacket(ip, UDP_port);
UDP.write(charBuffer);
UDP.endPacket();
if (DEBUG)
{
Serial.print("AcX = "); Serial.print(AcX);
Serial.print(" | AcY = "); Serial.print(AcY);
Serial.print(" | AcZ = "); Serial.print(AcZ);
Serial.print(" | Tmp = "); Serial.print(Tmp/340.00+36.53); //equation for temperature in degrees C from datasheet
Serial.print(" | GyX = "); Serial.print(GyX);
Serial.print(" | GyY = "); Serial.print(GyY);
Serial.print(" | GyZ = "); Serial.println(GyZ);
}
delay(1000);
Serial.println("-------------Connected Clients List-----------------------");
Serial.print(wifi_softap_get_station_num()); Serial.println(" clients.");
struct station_info *station_list = wifi_softap_get_station_info();
while (station_list != NULL) {
char station_mac[18] = {0}; sprintf(station_mac, "%02X:%02X:%02X:%02X:%02X:%02X", MAC2STR(station_list->bssid));
String station_ip = IPAddress((&station_list->ip)->addr).toString();
Serial.print(station_mac); Serial.print(" "); Serial.println(station_ip);
station_list = STAILQ_NEXT(station_list, next);
}
wifi_softap_free_station_info();
Serial.println();
packetCounter++;
if (packetCounter > 999)
packetCounter = 0;
}
void initHardware()
{
Serial.begin(115200);
Wire.begin();
Wire.beginTransmission(MPU6050_ADDRESS);
Wire.write(MPU6050_PWR_MGMT_1); // PWR_MGMT_1 register
Wire.write(0); // set to zero (wakes up the MPU-6050)
Wire.endTransmission(true);
Wire.beginTransmission(MPU6050_ADDRESS);
Wire.write(MPU6050_ACCEL_CONFIG); // PWR_MGMT_1 register
Wire.write(0); // set to zero (wakes up the MPU-6050) // Configuro a +/- 2g
Wire.endTransmission(true);
}
String printResults (void)
{
String buffer;
buffer = String("L"+String(packetCounter));
buffer = String(buffer+"Ax"+String(AcX));
buffer = String(buffer+"Ay"+String(AcY));
buffer = String(buffer+"Az"+String(AcZ));
buffer = String(buffer+"Gx"+String(GyX));
buffer = String(buffer+"Gy"+String(GyY));
buffer = String(buffer+"Gz"+String(GyZ));
buffer = String(buffer+"T"+String(Tmp/340.00+36.53));
return buffer;
}
boolean connectUDP()
{
boolean state = false;
if (DEBUG)
{
Serial.println("");
Serial.println("Connecting to UDP");
}
if(UDP.begin(UDP_port) == 1)
{
if (DEBUG)
Serial.println("UDP Connection successful");
state = true;
}
else
{
if (DEBUG)
Serial.println("UDP Connection failed");
}
return state;
}
boolean connectWifi()
{
boolean state = true;
int i = 0;
WiFi.mode(WIFI_AP);
WiFi.softAP(WiFiAPName, WiFiAPPsw);
if (DEBUG)
{
Serial.println("");
Serial.println("WiFi Network Created");
}
return state;
}
Client 2 and Client 1 are my laptop (with netcat) and my smartphone ( with an UDP sender/receiver app) respectively, but I also tried with another ESP8266 configured as station having the same negative result.
Have you got any idea of what is not letting me communicating correctly?
Thank you in advance !