Advertisement
Guest User

Untitled

a guest
May 24th, 2022
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <SPI.h>
  3.  
  4. const char* ssid = "Kony";
  5. const char* password = "Heslo";
  6. char server[] = "server.eu";
  7. unsigned long cas = 0;
  8. String line;
  9. WiFiClient client;
  10.  
  11. void setup() {
  12. // Nastartuj seriovou linku, do ktere budu vypisovat provozni informace
  13. pinMode(0, OUTPUT);
  14. Serial.begin(9600);
  15.  
  16. // Pripojeni k Wi-Fi
  17. Serial.println();
  18. Serial.print("Pripojuji k ");
  19. Serial.println(ssid);
  20. WiFi.begin(ssid, password);
  21.  
  22. // Dokud nejsem pripojeny k Wi-Fi,zapisuj do seriove linky tecky progressbaru
  23. while (WiFi.status() != WL_CONNECTED) {
  24. delay(100);
  25. Serial.print(".");
  26. }
  27.  
  28. // Jsem pripojeny k Wi-Fi a mohu pokracovat
  29. Serial.println();
  30. Serial.println("WiFi pripojena!");
  31.  
  32. // Napis IP adresu, kterou mikropocitac dostal
  33. Serial.print("Pouzij k pripojeni tuto adresu: ");
  34. Serial.print("http://");
  35. Serial.println(WiFi.localIP());
  36. }
  37.  
  38. // Smycka loop se opakuje stale dokola
  39.  
  40. void loop() {
  41. if (millis() - cas >= 6000 || cas == 0) {
  42. cas = millis();
  43. if (client.connect(server, 80)) { //starts client connection, checks for connection
  44. client.println("GET http://server.eu/doma/test/3Dzasuvka.php");
  45. client.println("Host: server.eu");
  46. client.println("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
  47. client.println("Connection: close");
  48. client.println();
  49. delay(1000);
  50. while (client.connected()) {
  51. String line = client.readStringUntil('\n');
  52. // Serial.println(line); //ak chceme vypísať HTTP header
  53. if (line == "\r") {
  54. break;
  55. }
  56. }
  57. String line = client.readStringUntil('\n');
  58. Serial.println("Nacitany payload response:");
  59. Serial.println(line); //odpoveď webservera - naše dáta
  60. if(line=="0"){
  61. digitalWrite(0, LOW);
  62. Serial.println("Vypnuto");
  63. }
  64. else if(line=="1"){
  65. digitalWrite(0, HIGH);
  66. Serial.println("Zapnuto");
  67. }
  68. }
  69. else {
  70. Serial.println("Pripojenie na webserver sa nepodarilo");
  71. }
  72. client.stop();
  73. }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement