Advertisement
Guest User

Untitled

a guest
Feb 27th, 2020
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.01 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <ESP8266WebServer.h>
  3.  
  4. const char* ssid = "IoTBerlin-Meetup"; // Wifi name (SSID)
  5. const char* password = "s3mant1cs!"; // Wifi password
  6.  
  7. ESP8266WebServer server(80); // Server port
  8. String Temp = "";
  9.  
  10. int d2pin = D4; // RGB LED - Red
  11. int d2state = 255;
  12.  
  13. int d5pin = 14; // RGB LED - Green
  14. int d5state = 255;
  15.  
  16. int d6pin = 12; // RGB LED - Blue
  17. int d6state = 255;
  18.  
  19. int d7pin = D7;
  20. int d7state = 0;
  21.  
  22. int analogPin = A0;
  23.  
  24. String HTML_header = "<!DOCTYPE html><html><head> <meta content=\"text/html; charset=ISO-8859-1\" http-equiv=\"content-type\"> <meta http-equiv=\"refresh\" content=\"5\"> <title>WebSchalter</title></head><style> html { background-color: grey; } body { margin: auto; margin-top: 60px; } button { height: 120px; width: 100%; font-size: 48px; background-color: cornflowerblue; border: none; color: white; margin-bottom: 10px; } button:active { background-color: darkturquoise; color: white; border: none; } button.on{ background-color: red; } a { text-decoration: none; } </style><body><a href=\"\args?d2pin=255&d5pin=0&d6pin=0\"><button>RED</button></a><a href=\"\args?d2pin=0&d5pin=255&d6pin=0\"><button>GREEN</button></a><a href=\"\args?d2pin=0&d5pin=0&d6pin=255\"><button>BLUE</button></a>";
  25. String HTML_d7_off = "<a href=\"/args?d7pin=1\"><button>d7 - LED</button></a>";
  26. String HTML_d7_on = "<a href=\"/args?d7pin=0\"><button class=\"on\">d7 - LED</button></a>";
  27.  
  28. String HTML_footer = "</body></html>";
  29. /* ***** ***** ***** ***** Setup ***** ***** ***** ***** */
  30. void setup()
  31. {
  32. pinMode(d2pin , OUTPUT); // D2 set as Output
  33. pinMode(d5pin , OUTPUT); // D5 set as Output
  34. pinMode(d6pin , OUTPUT); // D6 set as Output
  35. pinMode(d7pin , OUTPUT); // D7 set as Output
  36. Serial.begin(115200); // start serial communication
  37. connectToWifi(); // start wifi
  38. }
  39.  
  40. /* ***** Webpage: executed when "http://<ip address>/" is called ***** */
  41.  
  42. void the_webpage() {
  43. Temp = HTML_header; // top part of the html
  44.  
  45. if (d7state == 1) { // d7 part of html page – the html code for a button
  46. Temp += HTML_d7_on;
  47. } else {
  48. Temp += HTML_d7_off;
  49. }
  50.  
  51. Temp += "<button>LDR: ";
  52. //Temp += analogRead(analogPin); // putting out the analog value
  53. Temp += "</button>";
  54.  
  55.  
  56. //Temp += HTML_footer; // bottom part of the html page
  57. server.send(200, "text/html", Temp); // serving the html page
  58. }
  59.  
  60. /* ***** ***** ***** ***** Event Functions ***** ***** ***** ***** */
  61.  
  62. void handleArgs() {
  63.  
  64. if (server.arg("d2pin")!= ""){ // if specific argument is not empty
  65. d2state = server.arg("d2pin").toInt(); // set d2 pin to received argument
  66. }
  67. if (server.arg("d5pin")!= ""){ // if specific argument is not empty
  68. d5state = server.arg("d5pin").toInt(); // set d5 pin to received argument
  69. Serial.println(d5state);
  70. }
  71. if (server.arg("d6pin")!= ""){ // if specific argument is not empty
  72. d6state = server.arg("d6pin").toInt(); // set d6 pin to received argument
  73. }
  74. if (server.arg("d7pin")!= ""){ // if specific argument is not empty
  75. d7state = server.arg("d7pin").toInt(); // set d7 pin to received argument
  76. }
  77.  
  78. the_webpage(); // deliver webpage
  79. }
  80.  
  81. /* ***** ***** ***** Wifi Functions ***** ***** ***** */
  82.  
  83. void showStates(){ // map all states to pins
  84. digitalWrite(d7pin, d7state); // LED
  85. analogWrite(d2pin, d2state); // RGB LED red
  86. analogWrite(d5pin, d5state); // RGB LED green
  87. analogWrite(d6pin, d6state); // RGB LED blue
  88. }
  89.  
  90. /* ***** ***** ***** ***** Loop ***** ***** ***** ***** */
  91.  
  92. void loop() {
  93. server.handleClient(); // update function for server
  94. showStates(); // map states to pins
  95. delay(10); // some time to process
  96. }
  97.  
  98. /* ***** ***** ***** Wifi Functions ***** ***** ***** */
  99.  
  100. void connectToWifi() {
  101. Serial.println();
  102. Serial.print("Connect to"); Serial.println(ssid);
  103. WiFi.begin(ssid, password);
  104.  
  105. while (WiFi.status() != WL_CONNECTED) {
  106. delay(500);
  107. Serial.print(".");
  108. }
  109.  
  110. Serial.println("");
  111. Serial.println("Wifi connected");
  112.  
  113.  
  114. delay(2000);
  115. Serial.println();
  116. Serial.println("IP addresse of the web server is: ");
  117. Serial.println(WiFi.localIP()); // output the ip
  118. server.on("/", the_webpage); // show website on root call
  119. server.on("/args", handleArgs); // associate the handler function to the path
  120. server.begin(); // start the server
  121. Serial.println ("Webserver running, waiting for ESP8266 ...");
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement