Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <WiFiClient.h>
- #include <ESP8266mDNS.h>
- #include <ESP8266WebServer.h>
- const char* ssid = "xxxxxxx"; // your ssid
- const char* password = "xxxxxxx"; // your wiFi password
- const char* host = "19.168.1.2"; // ip of your db server
- int processLED = BUILTIN_LED; // output device D0
- int onTime = 0; //time processLED stays on (ms)
- int brightness; // brightness level of processLED (0-1023)
- ESP8266WebServer server(80); // Create a webserver object that listens for HTTP request on port 80
- void handleRoot(); // function prototypes for HTTP handlers
- void handleSubmit();
- void handleNotFound();
- void setup(void){
- Serial.begin(115200); // Start the Serial communication to send messages to the computer
- delay(10);
- Serial.println('\n');
- // We start by connecting to a 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('\n');
- Serial.print("Connected to ");
- Serial.println(WiFi.SSID()); // Tell us what network we're connected to
- Serial.print("IP address:\t");
- Serial.println(WiFi.localIP()); // Send the IP address of the ESP8266 to the computer
- if (MDNS.begin("esp8266")) { // Start the mDNS responder for esp8266.local
- Serial.println("mDNS responder started");
- } else {
- Serial.println("Error setting up MDNS responder!");
- }
- server.on("/", HTTP_GET, handleRoot); // Call the 'handleRoot' function when a client requests URI "/"
- server.on("/submit", HTTP_POST, handleSubmit); // Call the 'handleSubmit' function when a POST request is made to URI "/submit"
- server.onNotFound(handleNotFound); // When a client requests an unknown URI (i.e. something other than "/"), call function "handleNotFound"
- server.begin(); // Actually start the server
- Serial.println("HTTP server started");
- }
- void loop(void){
- server.handleClient(); // Listen for HTTP requests from clients
- }
- void handleRoot() { // When URI / is requested, send a web page with a button to toggle the LED
- server.send(200, "text/html", "<form action=\"/submit\" method=\"POST\"><input type=\"text\" name=\"brightness\" placeholder=\"Brightness (0-1023)\"></br><input type=\"text\" name=\"onTime\" placeholder=\"onTime (ms)\"></br><input type=\"submit\" value=\"Submit\"></form>");
- }
- void handleSubmit() { // If a POST request is made to URI /login
- String time1 = server.arg("brightness");
- String press1 = server.arg("onTime");
- server.send(200, "text/html", "<h1>Brightness: " + brightness + " </h1><h1>On Time: " + onTime +" milliseconds</h1>");
- WiFiClient client;
- const int httpPort = 80;
- if (!client.connect(host, httpPort)) {
- Serial.println("connection failed");
- return;
- }
- String data;
- Serial.print("Requesting POST: ");
- // Send request to the server:
- data = "brightness=";
- data.concat(brightness);
- data.concat("&onTime=");
- data.concat(onTime);
- client.println("POST /test/mmtest/add.php HTTP/1.1"); // path to the add.php file
- client.println("Host: 192.168.1.2"); // SERVER ADDRESS HERE TOO
- client.println("Content-Type: application/x-www-form-urlencoded");
- client.print("Content-Length: ");
- client.println(data.length());
- client.println();
- client.print(data);
- delay(500); // Can be changed
- if (client.connected()) {
- client.stop(); // DISCONNECT FROM THE SERVER
- }
- Serial.println();
- Serial.println("closing connection");
- //delay(5000);
- analogWrite(processLED, brightness);
- delay(onTime);
- analogWrite(processLED,0);
- }
- void handleNotFound(){
- server.send(404, "text/plain", "404: Not found"); // Send HTTP status 404 (Not Found) when there's no handler for the URI in the request
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement