Digistump Forums

General Discussion => Other Arduino Talk => Topic started by: inaho1688 on March 29, 2019, 01:30:10 am

Title: esp8266 wifi Peer to Peer.
Post by: inaho1688 on March 29, 2019, 01:30:10 am
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:
สล็อตฝากไม่มีขั้นต่ำ (https://www.ufa365s.com/%E0%B8%AA%E0%B8%A5%E0%B9%87%E0%B8%AD%E0%B8%95%E0%B8%9D%E0%B8%B2%E0%B8%81%E0%B9%84%E0%B8%A1%E0%B9%88%E0%B8%A1%E0%B8%B5%E0%B8%82%E0%B8%B1%E0%B9%89%E0%B8%99%E0%B8%95%E0%B9%88%E0%B8%B3/)
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
}


referer https://arduino.stackexchange.com/questions/18176/peer-to-peer-communication