How to get data from multiple ESP32?

bujna94
Posts: 1
Joined: Tue Sep 25, 2018 12:01 pm

How to get data from multiple ESP32?

Postby bujna94 » Tue Sep 25, 2018 12:09 pm

Hi, I have multiple ESP32 dev boards, what I'm using like a magnetic detector. With one ESP32 it's not a big problem (code below = combination of web server & hall sensor example).

Question is, how can I send this infos from multiple ESP32 boards into one website?
(Image below)

Thanks.


Code: Select all

#include <WiFi.h>
int hall_reading = 0;
boolean LED_state; // true means ON; false means OFF

const char* ssid     = "ssid";
const char* password = "password";

WiFiServer server(80);

void setup()
{
    Serial.begin(115200);
    pinMode(5, OUTPUT);      // set the LED pin mode
    digitalWrite(5, HIGH);
  LED_state = true;

    delay(10);

    // We start by connecting to a WiFi network

    Serial.println();
    Serial.println();
    Serial.print("Connecting to ");
    Serial.println(ssid);

    WiFi.begin(ssid, password);

    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }

    Serial.println("");
    Serial.println("WiFi connected.");
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());
    
    server.begin();

}

int value = 0;

void loop(){
 WiFiClient client = server.available();   // listen for incoming clients

  if (client) {                             // if you get a client,
    Serial.println("New Client.");           // print a message out the serial port
    String currentLine = "";                // make a String to hold incoming data from the client
    while (client.connected()) {            // loop while the client's connected
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        Serial.write(c);                    // print it out the serial monitor
        if (c == '\n') {                    // if the byte is a newline character

          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println();

            // the content of the HTTP response follows the header:
            

            // The HTTP response ends with another blank line:
            client.println();
            // break out of the while loop:
            break;
          } else {    // if you got a newline, then clear currentLine:
            currentLine = "";
          }
        } else if (c != '\r') {  // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }

        // Check to see if the client request was "GET /H" or "GET /L":
        if (currentLine.endsWith("GET /H")) {
          digitalWrite(5, HIGH);               // GET /H turns the LED on
        }
        if (currentLine.endsWith("GET /L")) {
          digitalWrite(5, LOW);                // GET /L turns the LED off
        }
      }
    }

    hall_reading = hallRead();
  Serial.print("Hallr reading: ");
  Serial.println(hall_reading);
 
  //when no magnet
  if(hall_reading >= -10 && hall_reading <= 50){
    if(LED_state){
      digitalWrite(5, HIGH);
      LED_state = false;
       client.print("NOTHING FALSE");
    }else {
      digitalWrite(5, HIGH);
      LED_state = true;
      client.print("NOTHING TRUE");
    }    
  //when reading is greater than 50, turn on LED
  }else if(hall_reading > 50){
    digitalWrite(5, LOW);
    LED_state = true;
    client.print("TOP");
  //when reading is less than -10, turn off LED
  }else if(hall_reading < -10){
    digitalWrite(5, LOW);
    LED_state = false;
    client.print("BOTTOM");
  }
client.print("<A HREF=\"javascript:history.go(0)\"><br>Click to refresh the page</A>");
delay(500);

  
    // close the connection:
    client.stop();
    Serial.println("Client Disconnected.");
  }
}
Attachments
esp32.png
esp32.png (259.43 KiB) Viewed 3089 times

ESP_Sprite
Posts: 9764
Joined: Thu Nov 26, 2015 4:08 am

Re: How to get data from multiple ESP32?

Postby ESP_Sprite » Wed Sep 26, 2018 1:34 am

Sure! You'd need some protocol to send the data from the ESP32 to the webserver (they could, e.g. do a http request with the ID and detection status encoded in the URL), then the webserver needs to accumulate these responses and display them.

Who is online

Users browsing this forum: Google [Bot], Majestic-12 [Bot] and 77 guests