Advertisement
sspence65

ESP8266 Server/Client POST

Nov 14th, 2017
847
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.07 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <WiFiClient.h>
  3. #include <ESP8266mDNS.h>
  4. #include <ESP8266WebServer.h>
  5.  
  6. const char* ssid = "xxxxxxx"; // your ssid
  7. const char* password = "xxxxxxx"; // your wiFi password
  8. const char* host = "19.168.1.2"; // ip of your db server
  9.  
  10. int processLED = BUILTIN_LED; // output device D0
  11. int onTime = 0; //time processLED stays on (ms)
  12. int brightness; // brightness level of processLED (0-1023)
  13.  
  14. ESP8266WebServer server(80); // Create a webserver object that listens for HTTP request on port 80
  15.  
  16. void handleRoot(); // function prototypes for HTTP handlers
  17. void handleSubmit();
  18. void handleNotFound();
  19.  
  20. void setup(void){
  21. Serial.begin(115200); // Start the Serial communication to send messages to the computer
  22. delay(10);
  23. Serial.println('\n');
  24.  
  25.  
  26. // We start by connecting to a WiFi network
  27. Serial.println();
  28. Serial.println();
  29. Serial.print("Connecting to ");
  30. Serial.println(ssid);
  31.  
  32. WiFi.begin(ssid, password);
  33.  
  34. while (WiFi.status() != WL_CONNECTED) {
  35. delay(500);
  36. Serial.print(".");
  37. }
  38.  
  39. Serial.println('\n');
  40. Serial.print("Connected to ");
  41. Serial.println(WiFi.SSID()); // Tell us what network we're connected to
  42. Serial.print("IP address:\t");
  43. Serial.println(WiFi.localIP()); // Send the IP address of the ESP8266 to the computer
  44.  
  45. if (MDNS.begin("esp8266")) { // Start the mDNS responder for esp8266.local
  46. Serial.println("mDNS responder started");
  47. } else {
  48. Serial.println("Error setting up MDNS responder!");
  49. }
  50.  
  51. server.on("/", HTTP_GET, handleRoot); // Call the 'handleRoot' function when a client requests URI "/"
  52. server.on("/submit", HTTP_POST, handleSubmit); // Call the 'handleSubmit' function when a POST request is made to URI "/submit"
  53. server.onNotFound(handleNotFound); // When a client requests an unknown URI (i.e. something other than "/"), call function "handleNotFound"
  54.  
  55. server.begin(); // Actually start the server
  56. Serial.println("HTTP server started");
  57. }
  58.  
  59. void loop(void){
  60. server.handleClient(); // Listen for HTTP requests from clients
  61. }
  62.  
  63. void handleRoot() { // When URI / is requested, send a web page with a button to toggle the LED
  64. 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>");
  65. }
  66.  
  67. void handleSubmit() { // If a POST request is made to URI /login
  68.  
  69. String time1 = server.arg("brightness");
  70. String press1 = server.arg("onTime");
  71. server.send(200, "text/html", "<h1>Brightness: " + brightness + " </h1><h1>On Time: " + onTime +" milliseconds</h1>");
  72.  
  73. WiFiClient client;
  74. const int httpPort = 80;
  75. if (!client.connect(host, httpPort)) {
  76. Serial.println("connection failed");
  77. return;
  78. }
  79.  
  80. String data;
  81.  
  82. Serial.print("Requesting POST: ");
  83. // Send request to the server:
  84. data = "brightness=";
  85. data.concat(brightness);
  86. data.concat("&onTime=");
  87. data.concat(onTime);
  88.  
  89. client.println("POST /test/mmtest/add.php HTTP/1.1"); // path to the add.php file
  90. client.println("Host: 192.168.1.2"); // SERVER ADDRESS HERE TOO
  91. client.println("Content-Type: application/x-www-form-urlencoded");
  92. client.print("Content-Length: ");
  93. client.println(data.length());
  94. client.println();
  95. client.print(data);
  96.  
  97. delay(500); // Can be changed
  98. if (client.connected()) {
  99. client.stop(); // DISCONNECT FROM THE SERVER
  100. }
  101. Serial.println();
  102. Serial.println("closing connection");
  103. //delay(5000);
  104.  
  105. analogWrite(processLED, brightness);
  106. delay(onTime);
  107. analogWrite(processLED,0);
  108.  
  109.  
  110. }
  111.  
  112. void handleNotFound(){
  113. 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
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement