Thank you Krub
This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.
Show posts Menu
// 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("=======================");
}
#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("=======================");
}
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");
}