0

It seems to me that humidity is getting high in one room in the basement in my house. To monitor the situation temporarily, I would like to build a very small wifi-mesh network to exchange these humidity data to a locally installed raspberry pie server. Most of the time the networking would be in idle state, every some 5 minutes sending the humidity sensor data over mqtt to the server.

What is the simplest code that sets up an esp8266 to connect to an existing wifi network and act as an access point and wifi extender a couple of yards further?

I tried this code:

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>

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

const char* newssid = "SSID_Extender";
const char* newpassword = "qaz12345";

void setup(void){
  Serial.begin(115200);
  Serial.println("");

  // set both access point and station
  WiFi.mode(WIFI_AP_STA);
  WiFi.softAP(newssid, newpassword);

  Serial.print(newssid);
  Serial.print(" server ip: ");
  Serial.println(WiFi.softAPIP());

  WiFi.begin(ssid, password);
  int wifi_it = 0;
  int wifi_status;
  while ((wifi_status = WiFi.status()) != WL_CONNECTED) {
    Serial.println("Trying to connect to wifi");

    wifi_it++;
    delay(500);
  }

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

  long rssi = WiFi.RSSI();
  Serial.print("Signal strength: ");
  Serial.print(rssi);
  Serial.println("dBm");
}

void loop(void){

}

It successfully registers the esp8266 to the wifi, sets up an AP, but if I connect to SID_Extender with my mobile phone, the internet traffic is not forwarded further.

There are projects that allows to configure multiple esps in user friendly manner, but I am wondering how to setup such an esp myself.

1
  • 1
    it's not a router, it's just something hooked up to two networks. I've never been able to get IO from both in such a situation (ap+sta), though i was trying to access an internal server, not internet. The only way i've been able to "bridge" is via a 2nd ESP and serial comm between them, but maybe there's something I don't know...
    – dandavis
    Oct 23, 2018 at 20:28

1 Answer 1

1

The esp8266 will not bridge the networks. You can perhaps bridge the communication from your other ep8266 on the TCP level with WiFiServer and WiFiClient. Send the sensor value to middle esp8266 running AP and it sends the value to Raspberry.

With MQTT you could create some MQTT proxy. Receive the data with WiFiServer and forward it with WiFiClient. without parsing or anything, only copying the chars between servers client and local client in booth directions

something like this (not tested):

void loop() {

  if (!serverClient || !serverClient.connected()) {
    serverClient = server.available();
  }
  if (!serverClient || !serverClient.connected()) {
    if (client.connected()) {
      client.stop();
    }
    return;
  }

  if (!client || !client.connected()) {
    if (!client.connect(raspiIP, MQTT_PORT)) {
      Serial.println("Can't connect to MQTT");
      delay(1000);
      return;
    }
  }

  while (serverClient.available() > 0) {
    client.write(serverClient.read());
  }
  while (client.available() > 0) {
    serverClient.write(client.read());
  }
}
9
  • youtube.com/watch?v=NH1rjk3Q_MU this is what most probably you mean. Do you maybe have the nodemcu C code for that?
    – arthur
    Oct 24, 2018 at 9:19
  • how do you send and receive the sensor value?
    – Juraj
    Oct 24, 2018 at 9:23
  • How do I sent the sensor value? Over MQTT. I have a TLS enabled broker on an internal server (raspberry) inside the "home network"
    – arthur
    Oct 24, 2018 at 9:31
  • then you could create some mqtt proxy. receive the mqtt message by WiFiServer and forward it with WiFiClient. without parsing or anything, just copying the chars between servers client and local client in booth directions
    – Juraj
    Oct 24, 2018 at 9:32
  • great idea. Do you think you have an example code for that ready maybe?
    – arthur
    Oct 24, 2018 at 9:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.