در این قسمت ارسال داده یا مقدار را از طریق URL به ESP8266 را یاد خواهیم گرفت. نحوه ی اتصال به اینترنت و پروگرام کردن ESP8266 مانند قبل می باشد.
برای بهتر فهمیدن این آموزش بهتر است که آموزش های قبلی را دنبال کنید.
در ادامه کد برنامه را قرار داده ام.
#include <ESP8266WiFi.h>
const char* ssid = "Suborg";
const char* password = "Nahbos5672730";
// Create an instance of the server
// specify the port to listen on as an argument
WiFiServer server(80);
void setup() {
Serial.begin(115200);
delay(10);
// prepare GPIO0
pinMode(0, OUTPUT);
digitalWrite(0, 0);
// Connect to WiFi network
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Start the server
server.begin();
Serial.println("Server started");
// Print the IP address
Serial.println(WiFi.localIP());
}
void loop() {
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
// Wait until the client sends some data
Serial.println("new client");
while (!client.available()) {
delay(1);
}
// Read the first line of the request
String req = client.readStringUntil('\r');
Serial.println(req);
client.flush();
// Match the request
String sVal = "";
int val;
if (req.indexOf("/gpio0/") != -1) {
int i=11;
while(req[i] != ' ')
{
sVal += req[i];
i++;
}
val = sVal.toInt();
Serial.println(val);
}
// Set GPIO0 according to the request
analogWrite(0, val);
client.flush();
// Prepare the response
String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nGPIO is now ";
s += (String)val;
s += "</html>\n";
// Send the response to the client
client.print(s);
delay(1);
Serial.println("Client disonnected");
// The client will actually be disconnected
// when the function returns and 'client' object is detroyed
}
طبق معمول من از ESP8266 سری یک استفاده کرده ام. توضیحات کامل در فیلم گفته شده. با کمی خلاقیت می توانید شاخ و برگ به این برنامه دهید و تنوع آن را زیاد کنید.
در ادامه فیلم را برای شما قرار می دهم:
دیدگاه ها :