1. // nodeMCU Amica V2 Module mit ESP8266 12E - WiFiWebClient-1
  2. // Board: NodeMCU 1.0 (ESP12E Module)
  3.  
  4. #include <ESP8266WiFi.h>
  5. #define ssid "mrge-ap-bu46"
  6. #define password ""
  7. #define host "10.50.20.xx"
  8. #define httpPort 80
  9.  
  10. void setup() {
  11. Serial.begin(115200); Serial.println("\nWiFiWebClient-1");
  12. WiFi.mode(WIFI_STA);
  13. WiFi.begin(ssid, password);
  14. Serial.println("Verbinde mit " + String(ssid));
  15. while (WiFi.status() != WL_CONNECTED) {
  16. Serial.print("."); delay(500);
  17. }
  18. Serial.println("\nWiFi verbunden mit " + String(ssid));
  19. Serial.println("IP address: " + WiFi.localIP().toString());
  20. }
  21.  
  22. int value = 0;
  23.  
  24. void loop() {
  25. delay(5000);
  26. ++value; // Anzahl der Anfragen an den Webserver
  27.  
  28. Serial.println("Verbinde mit " + String(host));
  29.  
  30. WiFiClient client;
  31.  
  32. if (!client.connect(host, httpPort)) {
  33. Serial.println("connection failed");
  34. return;
  35. }
  36. Serial.println("WiFi verbunden per " + String(ssid) + " mit " + String(host));
  37.  
  38. String path = "/test.php";
  39. String query = "?progname=WiFiWebClient-1";
  40. query += "&name=sigismund";
  41. query += "&mw=" + String(analogRead(A0));
  42. query += "&value=" + String(value);
  43.  
  44. Serial.println( "URL: http://" + String(host) + path + query);
  45.  
  46. // Senden der Anfrage an den Webserver
  47. client.print("GET " + path + query + " HTTP/1.1\r\n" +
  48. "Host: " + host + "\r\n" +
  49. "Connection: close\r\n\r\n");
  50. unsigned long timeout = millis();
  51.  
  52. while (client.available() == 0) { // Warte auf Antwort des Webserver
  53. if (millis() - timeout > 5000) {
  54. Serial.println(">>> Client Timeout !");
  55. client.stop();
  56. return;
  57. }
  58. }
  59.  
  60. // Lesen der Antwort des Webservers und Ausgabe auf den Seriellen Monitor
  61. while (client.available()) {
  62. String line = client.readStringUntil('\r');
  63. Serial.print(line);
  64. }
  65.  
  66. Serial.println("\nclosing connection");
  67. }