Adafruit Feather Huzzah UDP connection to multiple clients
Posted: Tue Jul 05, 2016 7:13 pm
Managed to get AP & STA connected using slightly modified IDE sketches below:
Server
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
WiFiUDP port;
char packetBuffer[255];
unsigned int localPort = 9999;
const char *ssid = "ESP8266AP";
const char *password = "yourPassword";
void setup()
{
Serial.begin(115200);
Serial.println("\n\nMyAdafruitHeatherAP_5-7-16_2"); //prints filename
WiFi.softAP(ssid, password);
WiFi.mode(WIFI_AP); // to force connection as only AP
port.begin(localPort);
Serial.print("[Connecting] ");
Serial.println(WiFi.localIP()); //0.0.0.0
Serial.println(WiFi.softAPIP()); //192.168.4.1
}
void loop()
{
int packetSize = port.parsePacket();
Serial.print("Received (IP/Size/Data): ");
Serial.print(port.remoteIP());Serial.print(" / ");
Serial.print(packetSize);Serial.print(" / ");
if (packetSize)
{
int len = port.read(packetBuffer, 255);
if (len > 0) packetBuffer[len-1] = 0;
Serial.print(packetBuffer);
port.beginPacket(port.remoteIP(),port.remotePort());
port.write("Your UDP packet was received OK\r\n");
port.endPacket();
}
Serial.println("");
delay(1000);
Serial.print("[Connected] ");
Serial.println (WiFi.localIP());
Serial.println(WiFi.softAPIP());
Serial.println("");
}
........................................................................................................................................................................
client
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
WiFiUDP port;
char packetBuffer[255];
unsigned int localPort = 9999;
void setup() {
Serial.begin(115200);
Serial.println("MyAdafruitHeatherSTA_5-7-16_2"); //prints filename
WiFi.begin("ESP8266AP", "yourPassword");
port.begin(localPort);
Serial.print("[Connecting]: ");
Serial.println(WiFi.localIP());
}
void loop()
{
//Received
int packetSize = port.parsePacket();
Serial.print("Received(Size/data): ");
Serial.print(packetSize);Serial.print(" / ");
if (packetSize)
{
int len = port.read(packetBuffer, 255);
if (len > 0) packetBuffer[len-1] = 0;
Serial.print(packetBuffer);
}
Serial.println("");
//Send
port.beginPacket("192.168.4.1",9999);
port.write("Send millis: ");
char buf[20];
unsigned long testID = millis();
sprintf(buf, "%lu", testID);
Serial.print("Sending: ");Serial.println(buf);
port.write(buf);
port.write("\r\n");
port.endPacket();
delay(500);
Serial.print("[Connected] ");
Serial.println(WiFi.localIP());
Serial.println("");
}
I now want to add 3 more clients to send their temperature data to the server but don't know how to begin; any suggestions?
ps
I don't need a web connection just connect to local WLAN
Server
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
WiFiUDP port;
char packetBuffer[255];
unsigned int localPort = 9999;
const char *ssid = "ESP8266AP";
const char *password = "yourPassword";
void setup()
{
Serial.begin(115200);
Serial.println("\n\nMyAdafruitHeatherAP_5-7-16_2"); //prints filename
WiFi.softAP(ssid, password);
WiFi.mode(WIFI_AP); // to force connection as only AP
port.begin(localPort);
Serial.print("[Connecting] ");
Serial.println(WiFi.localIP()); //0.0.0.0
Serial.println(WiFi.softAPIP()); //192.168.4.1
}
void loop()
{
int packetSize = port.parsePacket();
Serial.print("Received (IP/Size/Data): ");
Serial.print(port.remoteIP());Serial.print(" / ");
Serial.print(packetSize);Serial.print(" / ");
if (packetSize)
{
int len = port.read(packetBuffer, 255);
if (len > 0) packetBuffer[len-1] = 0;
Serial.print(packetBuffer);
port.beginPacket(port.remoteIP(),port.remotePort());
port.write("Your UDP packet was received OK\r\n");
port.endPacket();
}
Serial.println("");
delay(1000);
Serial.print("[Connected] ");
Serial.println (WiFi.localIP());
Serial.println(WiFi.softAPIP());
Serial.println("");
}
........................................................................................................................................................................
client
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
WiFiUDP port;
char packetBuffer[255];
unsigned int localPort = 9999;
void setup() {
Serial.begin(115200);
Serial.println("MyAdafruitHeatherSTA_5-7-16_2"); //prints filename
WiFi.begin("ESP8266AP", "yourPassword");
port.begin(localPort);
Serial.print("[Connecting]: ");
Serial.println(WiFi.localIP());
}
void loop()
{
//Received
int packetSize = port.parsePacket();
Serial.print("Received(Size/data): ");
Serial.print(packetSize);Serial.print(" / ");
if (packetSize)
{
int len = port.read(packetBuffer, 255);
if (len > 0) packetBuffer[len-1] = 0;
Serial.print(packetBuffer);
}
Serial.println("");
//Send
port.beginPacket("192.168.4.1",9999);
port.write("Send millis: ");
char buf[20];
unsigned long testID = millis();
sprintf(buf, "%lu", testID);
Serial.print("Sending: ");Serial.println(buf);
port.write(buf);
port.write("\r\n");
port.endPacket();
delay(500);
Serial.print("[Connected] ");
Serial.println(WiFi.localIP());
Serial.println("");
}
I now want to add 3 more clients to send their temperature data to the server but don't know how to begin; any suggestions?
ps
I don't need a web connection just connect to local WLAN