Advertisement
Ruddog

InovationTomsWebserver/updated by Mark Aldrich

Jan 22nd, 2018
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.86 KB | None | 0 0
  1. //This example will use a static IP to control the switching of a relay. Over LAN using a web browser.
  2. //A lot of this code have been resued from the example on the ESP8266 Learning Webpage below.
  3. //http://www.esp8266learning.com/wemos-webserver-example.php
  4.  
  5. //CODE START
  6. //1
  7. #include <ESP8266WiFi.h>
  8.  
  9. // Below you will need to use your own WIFI informaiton.
  10. //2
  11. const char* ssid = ""; //WIFI Name, WeMo will only connect to a 2.4GHz network.
  12. const char* password = ""; //WIFI Password
  13.  
  14. //defining the pin and setting up the "server"
  15. //3
  16. int relayPin = D1; // The Shield uses pin 1 for the relay-
  17. int value = LOW;
  18. WiFiServer server(80);
  19. IPAddress ip(192, 168, 1, 5); // where xx is the desired IP Address
  20. IPAddress gateway(192, 168, 1, 1); // set gateway to match your network
  21. IPAddress subnet(255, 255, 255, 0); // set subnet mask to match your network
  22.  
  23.  
  24. // void setup is where we initialize variables, pin modes, start using libraries, etc.
  25. //The setup function will only run once, after each powerup or reset of the wemos board.
  26. //4
  27. void setup() {
  28.   Serial.begin(115200);
  29.   delay(10);
  30.  
  31.  
  32.   pinMode(relayPin, OUTPUT);
  33.   digitalWrite(relayPin, LOW);
  34.  
  35.   Serial.print(F("Setting static ip to : "));
  36.   Serial.println(ip);
  37.  
  38.   // Connect to WiFi network
  39.   //5
  40.   Serial.println();
  41.   Serial.println();
  42.   Serial.print("Connecting to ");
  43.   Serial.println(ssid);
  44.   WiFi.config(ip, gateway, subnet);
  45.   WiFi.begin(ssid, password);
  46.   //Trying to connect it will display dots
  47.   while (WiFi.status() != WL_CONNECTED) {
  48.     delay(500);
  49.     Serial.print(".");
  50.   }
  51.   Serial.println("");
  52.   Serial.println("WiFi connected");
  53.  
  54.   // Start the server
  55.   server.begin();
  56.   Serial.println("Server started");
  57.  
  58.   // Print the IP address
  59.   Serial.print("Use this URL : ");
  60.   Serial.print("http://");
  61.   Serial.print(WiFi.localIP());
  62.   Serial.println("/");
  63. }
  64.  
  65. //void loop is where you put all your code. it is a funtion that returns nothing and will
  66. //repeat over and over again
  67. //6
  68. void loop() {
  69.   // Check if a client has connected
  70.   WiFiClient client = server.available();
  71.   if (!client) {
  72.     return;
  73.   }
  74.  
  75.   // Wait until the client sends some data
  76.   Serial.println("new client");
  77.   while(!client.available()){
  78.     delay(1);
  79.   }
  80.  
  81.   // Read the first line of the request
  82.   String request = client.readStringUntil('\r');
  83.   Serial.println(request);
  84.   client.flush();
  85.  
  86.   //Match the request, checking to see what the currect state is
  87.   value = LOW;
  88.   if (request.indexOf("/relay=ON") != -1) {
  89.     digitalWrite(relayPin, HIGH);
  90.     value = HIGH;
  91.   }
  92.   if (request.indexOf("/relay=OFF") != -1){
  93.     digitalWrite(relayPin, LOW);
  94.     value = LOW;
  95.   }
  96.   // Return the response, build the html page
  97.   //7
  98.  
  99.   // showButtons();
  100.  
  101.   // client.print("Relay is now: ");
  102.  
  103.   // if(value == HIGH) {
  104.     // client.print("Engaged (ON)");  
  105.   // } else {
  106.     // client.print("Disengaged (OFF)");
  107.   // }
  108.   // client.println("<br><br><br>");
  109.   // client.println("<a href=\"/relay=ON\">Click here to engage (Turn ON) the relay.</a> <br><br><br>");
  110.   // client.println("<a href=\"/relay=OFF\">Click here to disengage (Turn OFF) the relay.</a><br>");
  111.   // client.println("</html>");
  112.   showButtons(client);
  113.  
  114.   delay(1);
  115.   Serial.println("Client disconnected");
  116.   Serial.println("");
  117.  
  118. }//END
  119.  
  120. void showButtons(WiFiClient client) {
  121.   client.println("HTTP/1.1 200 OK");
  122.   client.println("Content-Type: text/html");
  123.   client.println(""); //  do not forget this one
  124.   client.println("<!DOCTYPE HTML>");
  125.   client.println("<html>");
  126.  
  127.   client.print("Relay is now: ");
  128.  
  129.   if(value == HIGH) {
  130.     client.print("Engaged (ON)");  
  131.   } else {
  132.     client.print("Disengaged (OFF)");
  133.   }
  134.  
  135.   client.println("<a href=\"/relay=ON\"><input type=\"button\" value=\"Relay On\" /></a><br><br><a href=\"/relay=OFF\"><input type=\"button\" value=\"Relay Off\" /></a>");
  136.  
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement