Make a server connected to port#301 of the router

arduino4fun
Site Admin
Posts: 37
Joined: Mon Jun 06, 2016 1:16 pm

Make a server connected to port#301 of the router

Postby arduino4fun » Wed Jun 15, 2016 1:53 pm

I find this article from Instructables very interesting, it makes a server connected to port #301 of the router (originally the ESP8266 in the article) and turns a led on or off anywhere from the internet thru a browser but it runs fine with the WeMos Board; the link of the original article is in the sketch

here is the sketch :

Code: Select all

//
 // code from : http://www.instructables.com/…/Control-ESP8266-Over-the-In…/
 //

‪#‎include‬ <ESP8266WiFi.h>

const char* ssid = "Domi @ home"; //"Your_WiFi_SSID"; // SSID of local network
 const char* password = "9FY28FYGROL1"; //"Your_WiFi_Password"; // Password on network

const char* host = "192.168.0.227"; //it will tell you the IP once it starts up
 //just write it here afterwards and upload
 int ledPin = D13;

WiFiServer server( 301 ); //just pick any port number you like

void setup()
 {
 Serial.begin(9600);
 delay(10);
 Serial.println(WiFi.localIP());
 // prepare GPIO2
 pinMode(ledPin, OUTPUT);
 digitalWrite(D3, LOW);

// Connect to WiFi network
 Serial.println();
 Serial.println();
 Serial.print("Connecting to ");
 Serial.println(ssid);

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
 while (!client.available())
 {
 delay(1);
 }

// Read the first line of the request
 String req = client.readStringUntil('\r');
 client.flush();

// Match the request
 if (req.indexOf("") != -10) //checks if you're on the main page
 {
 if (req.indexOf("/OFF") != -1) //checks if you clicked OFF
 {
 digitalWrite(ledPin, LOW);
 Serial.println("You clicked OFF");
 }
 if (req.indexOf("/ON") != -1) //checks if you clicked ON
 {
 digitalWrite(ledPin, HIGH);
 Serial.println("You clicked ON");
 }
 }

else
 {
 Serial.println("invalid request");
 client.stop();
 return;
 }

// Prepare the response
 String s = "HTTP/1.1 200 OK\r\n";
 s += "Content-Type: text/html\r\n\r\n";
 s += "<!DOCTYPE HTML>\r\n<html>\r\n";
 s += "<br><input type=\"button\" name=\"bl\" value=\"Turn LED ON \" onclick=\"location.href='/ON'\">";
 s += "<br><br><br>";
 s += "<br><input type=\"button\" name=\"bl\" value=\"Turn LED OFF\" onclick=\"location.href='/OFF'\">";
 s += "</html>\n";

client.flush();

// Send the response to the client
 client.print(s);
 delay(1);
 }



Image
Image

Return to “WeMos D1 R2 WiFi ESP8266 Development Board”

Who is online

Users browsing this forum: No registered users and 1 guest