Now to avoid major $$$ FCC co-location testing, I must insure that both transmitters are mutually dependent i.e. guaranteed not to transmit at the same time. I'm using WIFI_STA mode since broadcast beacons sent by ESP32 in AP mode add difficulty.
Here are conditions I anticipate:
Wi-Fi not connected --> TX2=OFF
Wi-Fi connected, ESP32 listening for incoming packet --> TX2=ON
Wi-Fi connected, ESP32 received packet --> TX2=OFF until ESP32 transmits "PACKET RESPONSE", then TX2 is back ON
Please confirm that the following code precludes simultaneous transmission and that I'm not missing something going on elsewhere in the ESP32 core.
Code: Select all
#include <WiFi.h>
#include <WiFiUdp.h>
#define MYPORT 37000
char buf[256];
int wiFiStatus;
char ssid[] = "MYSSID";
char pass[] = "MYPASS";
WiFiUDP udp;
IPAddress broadcast_ip(255, 255, 255, 255);
uint32_t timeOfLastWiFiRetry;
void TX2_ON() {}
void TX2_OFF() {}
void checkIncomingPacket() {
int packetSize = udp.parsePacket();
if (packetSize > 0) {
udp.read(buf, 256);
udp.beginPacket(broadcast_ip, MYPORT);
udp.write((uint8_t *) "PACKET RESPONSE", strlen("PACKET RESPONSE"));
TX2_OFF();
udp.endPacket();
Serial.println("PACKET RESPONSE SENT WITH TX2 OFF");
TX2_ON();
}
}
void setup() {
Serial.begin(115200);
WiFi.persistent(false);
WiFi.mode(WIFI_STA);
udp.begin(MYPORT);
}
void loop() {
if (wiFiStatus != WiFi.status()) {
wiFiStatus = WiFi.status();
if (wiFiStatus != WL_CONNECTED) {
TX2_OFF();
} else {
TX2_ON();
Serial.println("\nConnected to Wifi " + WiFi.SSID());
Serial.println("IP address of this ESP32: " + WiFi.localIP().toString());
}
}
if (WiFi.status() != WL_CONNECTED) {
if (timeOfLastWiFiRetry == 0 || (millis() - timeOfLastWiFiRetry) >= 3000 ) {
timeOfLastWiFiRetry = millis();
WiFi.begin(ssid, pass);
}
} else {
checkIncomingPacket();
}
}