Esp8266 การควบคุม LED ด้วย Mode AP

Started by spider28, September 05, 2016, 11:03:59 PM

Previous topic - Next topic

spider28

เอาโจทย์มาจาก Facebook น่ะครับ

อยากให้esp8266สองตัวสื่อสารกันได้.โดยตัวแรกเป็น input แล้วส่งให้ตัวที่สองแสดงผลเป็น output
--->วิธีการแบบนี้เขาเรียกว่าอะไรครับ.เผื่อผมไปดู google ต่อ. ขอบคุณนะครับ.^^. (มือใหม่ครับ)

พอจะจับใจความได้ว่า
ให้ตัวนึงเป็น AP Mode
อีกตัวเป็น Static Mode


==================================================
ผมลองตีโจทย์แบ่งเป็น AP Server และ client

Esp8266 AP Server
-AP MODE
-LED

กำหนดให้ AP Server  เป็นโหมด AP สามารถรัน port 80 หรือทำเป็น Http server ควบคุม LED ด้วย API ได้
การทดสอบ http://IP-Address/?stage=1


Esp8266 Client
-Static mode
-API GET

ตัว Client ก็กำหนดให้ connec wifi เหมือนกับทั่วๆไป เป็นโหมด Static จากนั้นก็ให้มันไป get ค่าจาก server ครับ

การ get ข้อมูลบน Protocal TCP port 80 ไม่ใช้เป็นการดึงข้อมูลเพียงอย่างเดียว มันคือการ ส่งค่าขึ้นไปยังปลายทาง และรับค่าตอบกลับมา

=============================
Link Youtube

https://youtu.be/v3O11mppptc

https://youtu.be/DiWlVcmRoJQ
=============================

Code AP Server [Master]

//  http://IP_Address/?state=1

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

const int led = 2;

/* Set these to your desired credentials. */
const char *ssidAP = "ESPap";
const char *passwordAP = "esp@thai";

ESP8266WebServer server(80);
//***************************************//
///////////////////////////////////////////
//***************************************//
void setup()
{
  delay(1000);
  Serial.begin(115200);

  pinMode(led, OUTPUT);
  digitalWrite(led, LOW);
 
  ConfigAPmode();
  server.on("/", handleRoot);
  server.begin();
  Serial.println("HTTP server started");
}
//***************************************//
///////////////////////////////////////////
//***************************************//
void loop()
{
  server.handleClient();

}
//***************************************//
///////////////////////////////////////////
//***************************************//
void handleRoot() {
//  server.send(200, "text/html", "<h1>You are connected</h1>");

  int state = server.arg("state").toInt();
  digitalWrite(led, state);
 
  server.send(200, "text/plain", String("LED is now ") + ((state)?"on":"off"));
}
//***************************************//
void ConfigAPmode()
{
  Serial.println();
  Serial.println("Configuring access point...");
  /* You can remove the password parameter if you want the AP to be open. */
  WiFi.softAP(ssidAP, passwordAP);

  IPAddress myIP = WiFi.softAPIP();
  Serial.print("AP IP address: ");
  Serial.println(myIP);
  Serial.println("=======================");
}



Code Client

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

const char* ssid = "ESPap";
const char* password = "esp@thai";

ESP8266WebServer server(80);
//***************************************//
///////////////////////////////////////////
//***************************************//
void setup()
{
  delay(1000);
  Serial.begin(115200);

  SetMode();
  Conect_WiFi();
 
}
//***************************************//
///////////////////////////////////////////
//***************************************//
void loop()
{
  httpGET( "0" );
  delay(2000);
  httpGET( "1" );
  delay(2000);
}
//***************************************//
///////////////////////////////////////////
//***************************************//
void SetMode(void)
{
  // Set WiFi to station mode and disconnect from an AP if it was previously connected
  //WiFi.mode =>  WIFI_AP_STA OR  WIFI_STA
  WiFi.mode(WIFI_STA);
  WiFi.disconnect();
  delay(1000);
 
  Serial.println("Setup Mode done");
  Serial.println("=======================");
}

void Conect_WiFi(void)
{
  WiFi.begin(ssid, password);
  Serial.println("");

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
  Serial.println("");
  delay(1000);
 
  Serial.println("Esp8266 End Conect WiFi");
  Serial.println("=======================");
}



Funtion API get

void httpGET(String var) {
    static const char* host   = "192.168.4.1";

    // Use WiFiClient class to create TCP connections
    WiFiClient client;
    const int httpPort = 80;
    if (!client.connect(host, httpPort)) {
        Serial.println("connection failed");
        return;
    }

    // We now create a URI for the request
    String url = "/?state=";
    url += var;

//    Serial.print("Requesting URL: ");
//    Serial.println(url);

    // This will send the request to the server
    client.print(String("GET ") + url + " HTTP/1.1\r\n" +
                 "Host: " + host + "\r\n" +
                 "Connection: close\r\n\r\n");
}