Chat freely about anything...

User avatar
By MolsonB
#44528 Hi ESP8266 community. This is my first project with the wireless device, please bear with me.

My project has an arduino that reads a bunch of sensors, sends to bluetooth (HC-05) via uart that an android device can read/controller. I want to step it up to allow multiple devices to read/controller the sensors. I started to play around making a 'main' Android device connect to bluetooth then open a hot-spot that other android devices will connect to as httpserver.

I started to read about the ESP8266 and thought that would be a better solution, making that the Acess Point for all devices to connect to. Get rid of my server / client android coding.

On a high level, just wondering how I would go about that. My arduino would send data via UART or I2C or SPI to ESP. Would the ESP keep on refreshing an http page every x seconds, that the android devices would keep on reading the http page? Or does the andriod request a page every x seconds?

Or do I setup a TCP port on the ESP that listens on. I'm just not sure if multi devices can connect to 1 port or if I'd have to open up 10 of them and each client keeps on trying until it finds a free port? (That's how I was doing my android Hotspot scenario)

Basically how do you send a bunch of values to a bunch of devices (and these devices can send control words back to control sensors).
User avatar
By MolsonB
#44663 Using Tardis Time Server by: M. Ray Burnette and others, I came up with....

Code: Select all#include <ESP8266WiFi.h>
#include <WiFiUDP.h>
#include <ESP8266WebServer.h>
#include <DNSServer.h>


/* Set these to your desired credentials. */
const char *ssid = "MyWifi";
const char *password = "++++++++";

const byte   DNS_PORT  =   53;                                  // Capture DNS requests on port 53
unsigned int localPort = 8888;                                  // any unused port on LAN
IPAddress    apIP(10, 10, 10, 1);                               // Private network address: local & gateway
IPAddress    broadcastIP(255, 255, 255, 255);

char         packetBuffer[UDP_TX_PACKET_MAX_SIZE];              // buffer to hold incoming packet,
char         ReplyBuffer[] = "acknowledged";                    // a 12 character string to send back
String       responseHTML = ""
                      "<!DOCTYPE html><html><head><title>MyPlane_Portal</title></head><body>"
                      "<h1>MyPlane WiFi</h1><p>You are connected to your Plane's BCM</p></body></html>";

DNSServer         dnsServer;                                    // Create the DNS object
WiFiUDP           UDP;                                          // UDP protocol on STA interface, localPort
ESP8266WebServer  webServer(80);                                // HTTP web server on common port 80



void setup()
{
  Serial.begin(19200);
 
  WiFi.mode(WIFI_AP);                                           // AcessPoint
  WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0));   // subnet FF FF FF 00
  WiFi.softAP(ssid, password);

  dnsServer.start(DNS_PORT, "*", apIP);                         // "*" creates a captive portal

  // replay to all requests with same HTML
  webServer.onNotFound([]() {webServer.send(200, "text/html", responseHTML);});
  webServer.begin();
 
  while (! UDP.begin(localPort) ) {                             // UDP protocol connected to localPort which is a variable
    yield();                                                    // Play nicely with RTOS (alias = delay(0))
  }                                                             // This will loop forever if UDP fails

}


void loop()
{
  dnsServer.processNextRequest();
  yield();
  webServer.handleClient();
  yield();

 
  //Passes data along
  if (Serial.available() > 0) {
   // the following delay is required because otherwise the arduino will read the first letter of the command but not the rest
   // In other words without the delay if you use AT+RST, for example, the Arduino will read the letter A send it, then read the rest and send it
   // but we want to send everything at the same time. - See more at: http://www.esp8266.com/viewtopic.php?f=8&t=4826#sthash.Rk0AYXrD.dpuf
    delay(50);
    UDP.beginPacketMulticast(broadcastIP, localPort, apIP); 
    while(Serial.available()) {         // read the command character by character
      UDP.write(Serial.read());         // read one character
    }
    UDP.endPacket();                    // clear UDP buffer
  }
 
 
  int packetSize = UDP.parsePacket();                         // if there’s data available, read a packet
  if (packetSize) {
    UDP.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);           // read the packet into packetBufffer
    for (uint16_t i=0; i < packetSize; i++) {
      Serial.write(packetBuffer[i]);
    }

  }
  yield();

}

User avatar
By MolsonB
#45401 I changed the last part processing the packet.
Nobody sees anything wrong? Seems to work so far.

Code: Select all  int packetSize = UDP.parsePacket();                         // if there’s data available, read a packet
  if (packetSize) {
    int len = UDP.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);           // read the packet into packetBufffer
    if (len > 0) {
      packetBuffer[len] = 0;
    }
    Serial.println(packetBuffer);
   
  }
User avatar
By MolsonB
#57017 I have 3 devices connected to the AP.

Samsung Tab 10.1 (2010) - 0.4% loss
Samsung Galaxy S5 (2014) - 10.6% loss
Samsung Tab A 7.0 (2016) - 18.5% loss

All running the same program yet the S5 and Tab 7.0 have very bad packet loss compared to the Tab 10.1

I have no idea why. Is the ESP8266 weak when it comes to being an access point?