So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By elcrni
#91070 hi all,

I am Alek, from Austria. Been Arduino user for a couple of years and recently started using ESP platforms, amazing stuff.
Now, i have a small project and i need a bit of help.

I have one ESP8266 (D1 Mini) that has one pin as INPUT where the signal from signal generator is captured, then that signal is transmitted to another ESP8266. So when signal generator outputs HIGH, that signal then generates HIGH output on receiving ESP. I have one signal per second, signal width varies from 100 to 200ms.

This all works but i have 2 problems i would like to solve:

1. Signal latency:
Output signal on receiving ESP is some 10-15ms delayed compared to signal generator output, that is the signal before it's sent via WiFi

2. Multiple servers:
My code now has one server and one client, that is one transmitter and one receiver. What i would like to have is one transmitter and multiple receivers, and if possible in a way that i just plug in new receivers without having to change transmitter code every time i add new receiver.

My code is bellow:

TRANSMITER

Code: Select all#include <ESP8266WiFi.h>

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

int sensorValue1;       
int btn1 = 4;
static int sensorValueLast ;

void setup() {
  pinMode(btn1, INPUT_PULLUP);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
  }
}

void loop() {
  if(digitalRead(btn1) == LOW) {
    sensorValue1 = 1;
  } else  {
    sensorValue1 = 0;
  }
 
  if ( sensorValueLast == sensorValue1 ) return ;  // leave
  sensorValueLast = sensorValue1 ;
  WiFiClient client;
  const char * host = "192.168.4.1";
  const int httpPort = 80;
  if (!client.connect(host, httpPort)) {
    return;
  }
  String url = "/data/?sensor_reading=sensor1_value";
  url.replace("sensor1_value", String(sensorValue1));
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "Connection: close\r\n\r\n");
}


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


const char *ssid      = "ssid";
const char *password  = "password";
int sensorValue1 = 0;
ESP8266WebServer server(80);

void handleSentVar() {
  if (server.hasArg("sensor_reading"))
  {
    sensorValue1 = server.arg("sensor_reading").toInt() ;
    if (sensorValue1 == 0)  {
      digitalWrite(2, HIGH);
    }
    if (sensorValue1 == 1)  {
      digitalWrite(2, LOW);
    }
  }
}

void setup() {
  WiFi.softAP(ssid, password);
  IPAddress myIP = WiFi.softAPIP();
  pinMode(2, OUTPUT);
  server.on("/data/", HTTP_GET, handleSentVar); // when the server receives a request with /data/ in the string then run the handleSentVar function
  server.begin();
}

void loop() {
  server.handleClient();
}



Many thanks in advance,
Alek

P.S. i was not sure if this was the right forum section for this thread and if not, my apologies.