0

Hello guys , I want to perform a mesh network between 3-4 modules using ESP8266 . Any one have idea how to do it?

  • @Stewart Mayer, your code above (AP & ST) is work! Thanks in advance! Greeting from Indonesia – Wahyu Exano Nov 17 '18 at 16:48
4

You can't, that's not how the esp8266 works.

The esp8266 is WiFi, so you will either need an access point or nominate one esp8266 module to act as an access point running in softap mode.

  • you can provide the sample – girish kolte Nov 27 '15 at 9:57
  • 6
    sorry @girishkolte but... I never used an esp8266, so I'm a newbie. But if Majenko says "you need to run the esp8266 in softap mode" I go on google (if you don't know it, it's a search engine) and search "esp8266 softap". The VERY FIRST result already has an example. And the second one too. And I didn't go through all the others, but I assume that a lot of them already have. So stop waiting for others to do your work and search on your own! It's also faster, because you will get results instantly instead of waiting for other people to search them for you – frarugi87 Nov 27 '15 at 10:04
3

Peer to Peer Example
Written by Stewart Meyer (engineer retired from the FAA).
This is first time that I have given an answer, hope you can read this:

Not only can the ESP8266 connect to a WiFi network and interact with the Internet, but it can also set up a network of its own, allowing other devices to connect directly to it. This example demonstrates how to turn the ESP8266 into an access point (AP) also called a server, and serve up data to any connected client also called a station.

Server (Access Point) First we will look at the server:

/* Server
*  We write the server first because we need its IP address
*/

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

const char* ssid = "Meyer";       // ssid of server (Access Point (AP))
const char* password = "";        // password of server (Access Point (AP))
WiFiServer server(80);            //Service Port

int ledPin = 2; // GPIO2 of Server ESP8266

void setup() 
{
    delay(1000);
    Serial.begin(115200);    // to use tools->serial monitor

    pinMode(ledPin, OUTPUT);   // set GPIO 2 as an output

    WiFi.mode(WIFI_AP_STA);  // Set WiFi to AP and station mode

    // Connect to the WiFi network
    Serial.println();
    Serial.print("Connecting to: "); Serial.println(ssid);
    WiFi.softAP(ssid, password);

    // Display the server address
    Serial.print("Connected, My address: ");
    Serial.print("http://");
    Serial.print(WiFi.softAPIP());
    Serial.println("/");

    // Tell the server to begin listening for incoming connections
    server.begin();
    Serial.println("Server listening for incoming connections");
}  

void loop() 
{
    // Check if a client has connected
    WiFiClient client = server.available();
    if (!client) // if not available, return
    {
        return;
    }

    // Wait until the client sends some data
    Serial.println();
    Serial.println("Client connected");
    while(!client.available())
    {
        delay(1);
    }

    // Read the request
    String request = client.readStringUntil('\r');  
    Serial.print("Request = "); Serial.println(request);

    if (request.indexOf("/ON/") != -1) 
    {
        digitalWrite(ledPin, LOW); // turn LED on
    } 
    if (request.indexOf("/OFF/") != -1)
    {
        digitalWrite(ledPin, HIGH); // turn LED off
    }

    client.flush();
    Serial.println("Client disconnected");
}

Client (Station) Next we will look at the client:

/*  Client
*   This client will turn the LED on or off every 5 seconds
*/
#include <ESP8266WiFi.h>
#include <WiFiClient.h> 

const char* ssid = "Meyer";   // ssid of access point (Server)
const char* password = "";    // password of access point (Server)
int port = 80;                // port number

byte ip[]= {192,168,4,1};

int LEDstatus = 0;

WiFiClient client;  // Declare client

void setup() 
{
    Serial.begin(115200);
    delay(10);

    WiFi.mode(WIFI_STA);  // set mode to station (client)

    // Connect to wiFi
    Serial.println(); Serial.println();
    Serial.println("CONNECTING TO WiFi");
    WiFi.begin(ssid, password);

    while (WiFi.status() != WL_CONNECTED) 
    {
        delay(500);
        Serial.print(".");  // print dots until connection
    }
    Serial.println();
    Serial.println("WiFi CONNECTED");
}

void loop() 
{
    // Connect client to server
    Serial.println("CONNECTING CLIENT TO SERVER");

    if (client.connect(ip, port)) 
    {
        Serial.println("CONNECTED");
    }
    else 
    {
        Serial.println("CONNECTION FAILED");
    }

    // Send message to control the LED   
    if (LEDstatus == 0)
    {
        client.println("/OFF/");
        Serial.println("LED OFF");
        LEDstatus = 1;
    }
    else
    {
        client.println("/ON/");
        Serial.println("LED ON");
        LEDstatus = 0;
    }

    client.println();

    client.flush();

    delay(1);
    Serial.println("CLIENT DISCONNECTED");
    Serial.println();
    delay(5000); // delay 5 seconds
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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