Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. Serveri kood
  2.  
  3. #include <ESP8266WiFi.h>
  4. #include <WiFiClient.h>
  5. #include <ESP8266WebServer.h>
  6.  
  7. const char *ssid = "Peldik";
  8. const char *password = "password";
  9.  
  10. ESP8266WebServer server(80);
  11.  
  12. void handleRoot() {
  13. server.send(200, "text/html", "<h1>Hello! from arduino-er!</h1>");
  14. digitalWrite(D4,HIGH);
  15. }
  16.  
  17. char* htmlBody_help = "<h1>Help</h1><br/>\n"
  18. "Visit http://192.168.4.1/ to access web server.<br/>\n"
  19. "Visit http://192.168.4.1/help to access this page.<br/>\n";
  20.  
  21. void handleHelp(){
  22. server.send(200, "text/html", htmlBody_help);
  23. digitalWrite(D4, HIGH);
  24. }
  25.  
  26. void handlePage(){
  27.  
  28. server.send(200, "text/html", "<h1> Valjas</h1>");
  29. digitalWrite(D4, LOW);
  30.  
  31. }
  32.  
  33. void setup() {
  34. delay(200);
  35. Serial.begin(9600);
  36. Serial.println();
  37.  
  38. WiFi.softAP(ssid, password);
  39.  
  40. IPAddress apip = WiFi.softAPIP();
  41. Serial.print("visit: \n");
  42. Serial.println(apip);
  43. server.on("/", handleRoot);
  44. server.on("/help", handleHelp);
  45. server.on("/page", handlePage);
  46. server.begin();
  47. Serial.println("HTTP server üleval");
  48. pinMode(D4, OUTPUT);
  49. }
  50.  
  51. void loop() {
  52. server.handleClient();
  53. }
  54.  
  55.  
  56.  
  57. Klient
  58.  
  59.  
  60. #include <Arduino.h>
  61.  
  62. #include <ESP8266WiFi.h>
  63. #include <ESP8266WiFiMulti.h>
  64.  
  65. #include <ESP8266HTTPClient.h>
  66.  
  67. #define USE_SERIAL Serial
  68.  
  69. ESP8266WiFiMulti WiFiMulti;
  70.  
  71.  
  72. int buttonstate= 0;
  73. const int buttonpin= 16;
  74. void setup() {
  75.  
  76. USE_SERIAL.begin(115200);
  77. // USE_SERIAL.setDebugOutput(true);
  78.  
  79. USE_SERIAL.println();
  80. USE_SERIAL.println();
  81. USE_SERIAL.println();
  82.  
  83. const int buttonpin= 16;
  84. pinMode(buttonpin, INPUT);
  85.  
  86.  
  87. int buttonstate= 0;
  88.  
  89. for(uint8_t t = 4; t > 0; t--) {
  90. USE_SERIAL.printf("[SETUP] WAIT %d...\n", t);
  91. USE_SERIAL.flush();
  92. delay(200);
  93. }
  94.  
  95. WiFiMulti.addAP("Peldik", "password");
  96.  
  97. }
  98.  
  99. void loop() {
  100. // wait for WiFi connection
  101. if((WiFiMulti.run() == WL_CONNECTED)) {
  102.  
  103. HTTPClient http;
  104.  
  105. USE_SERIAL.print("[HTTP] begin...\n");
  106. // configure traged server and url
  107. //http.begin("https://192.168.1.12/test.html", "7a 9c f4 db 40 d3 62 5a 6e 21 bc 5c cc 66 c8 3e a1 45 59 38"); //HTTPS
  108.  
  109.  
  110. USE_SERIAL.print("[HTTP] GET...\n");
  111. // start connection and send HTTP header
  112. int httpCode = http.GET();
  113.  
  114. // httpCode will be negative on error
  115. if(httpCode > 0) {
  116. // HTTP header has been send and Server response header has been handled
  117. USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);
  118. /* buttonstate = digitalRead(buttonpin);
  119. // file found at server
  120. if(httpCode == HTTP_CODE_OK) {
  121. String payload = http.getString();
  122. USE_SERIAL.println(payload);
  123. if(payload == "1"){
  124. digitalWrite(D4, HIGH);
  125. } else {
  126. digitalWrite(D4, LOW);
  127. }*/
  128. buttonstate= digitalRead(buttonpin);
  129. if(buttonstate == HIGH){
  130. http.begin("http://192.168.4.1/help");
  131. }
  132. if(buttonstate == LOW){
  133. http.begin("http://192.168.4.1/page");
  134. }} else {
  135. USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
  136. }
  137.  
  138. http.end();
  139. }
  140.  
  141.  
  142. delay(200);
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement