Advertisement
feliciayoon

Untitled

Sep 25th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.79 KB | None | 0 0
  1. #include <CytronWiFiShield.h>
  2. #include <CytronWiFiClient.h>
  3. #include <CytronWiFiServer.h>
  4. #include <SoftwareSerial.h>
  5.  
  6. const char *ssid = "alina";
  7. const char *pass = "Bangang5592";
  8. IPAddress ip(192, 168, 1 ,188);
  9. ESP8266Server server(80);
  10. int ledState = 0;
  11. int led1State = 0;
  12. const char htmlHeader[] = "HTTP/1.1 200 OK\r\n"
  13. "Content-Type: text/html\r\n"
  14. "Connection: close\r\n\r\n"
  15. "<!DOCTYPE HTML>\r\n"
  16. "<html>\r\n";
  17.  
  18. void setup() {
  19. pinMode(6, OUTPUT);
  20. pinMode(7, OUTPUT);
  21. // put your setup code here, to run once:
  22. Serial.begin(9600);
  23. while (!Serial) {
  24. ; // wait for serial port to connect. Needed for Leonardo only
  25. }
  26.  
  27. if(!wifi.begin(2, 3))
  28. {
  29. Serial.println(F("Error talking to shield"));
  30. while(1);
  31. }
  32. Serial.println(wifi.firmwareVersion());
  33. Serial.print(F("Mode: "));Serial.println(wifi.getMode());// 1- station mode, 2- softap mode, 3- both
  34. Serial.println(F("Setup wifi config"));
  35. wifi.config(ip);
  36. Serial.println(F("Start wifi connection"));
  37. if(!wifi.connectAP(ssid, pass))
  38. {
  39. Serial.println(F("Error connecting to WiFi"));
  40. while(1);
  41. }
  42. Serial.print(F("Connected to "));Serial.println(wifi.SSID());
  43. Serial.println(F("IP address: "));
  44. Serial.println(wifi.localIP());
  45. wifi.updateStatus();
  46. Serial.println(wifi.status()); //2- wifi connected with ip, 3- got connection with servers or clients, 4- disconnect with clients or servers, 5- no wifi
  47.  
  48. server.begin();
  49. }
  50.  
  51. void loop() {
  52. // put your main code here, to run repeatedly:
  53. ESP8266Client client = server.available();
  54.  
  55. if(client.available()>0)
  56. {
  57. String req = client.readStringUntil('\r');
  58. // First line of HTTP request looks like "GET /path HTTP/1.1"
  59. // Retrieve the "/path" part by finding the spaces
  60. int addr_start = req.indexOf(' ');
  61. int addr_end = req.indexOf(' ', addr_start + 1);
  62. if (addr_start == -1 || addr_end == -1) {
  63. Serial.print(F("Invalid request: "));
  64. Serial.println(req);
  65. return;
  66. }
  67. req = req.substring(addr_start + 1, addr_end);
  68. Serial.print(F("Request: "));
  69. Serial.println(req);
  70. client.flush();
  71.  
  72. if(req.equals("/"))
  73. {
  74.  
  75. client.print(htmlHeader);
  76. String htmlBody = "Hello from ESP8266 at ";
  77. htmlBody += "</html>\r\n\r\n";
  78. client.print(htmlBody);
  79. }
  80. //input location
  81. else if(req.equals("/analog"))
  82. {
  83. client.print(htmlHeader);
  84.  
  85.  
  86. String htmlBody = String(analogRead(A0));
  87.  
  88. client.print(htmlBody);
  89. }
  90. //output on location
  91. else if(req.equals("/on"))
  92. {
  93. client.print(htmlHeader);
  94. String htmlBody="Pin 6 is now ";
  95. htmlBody += "on";
  96. htmlBody += "</html>\r\n";
  97. client.print(htmlBody);
  98. ledState = 1;
  99.  
  100. }
  101.  
  102. //output off location
  103. else if(req.equals("/off"))
  104. {
  105. client.print(htmlHeader);
  106. String htmlBody="Pin 6 is now ";
  107. htmlBody += "off";
  108. htmlBody += "</html>\r\n";
  109. client.print(htmlBody);
  110. ledState = 0;
  111.  
  112. }
  113. else if(req.equals("/onn"))
  114. {
  115. client.print(htmlHeader);
  116. String htmlBody="Pin 7 is now ";
  117. htmlBody += "on";
  118. htmlBody += "</html>\r\n";
  119. client.print(htmlBody);
  120. led1State = 1;
  121.  
  122. }
  123. else if(req.equals("/offf"))
  124. {
  125. client.print(htmlHeader);
  126. String htmlBody="Pin 7 is now ";
  127. htmlBody += "off";
  128. htmlBody += "</html>\r\n";
  129. client.print(htmlBody);
  130. led1State = 0;
  131.  
  132. }
  133. else
  134. client.print("HTTP/1.1 404 Not Found\r\n\r\n");
  135.  
  136. client.stop();
  137.  
  138. }
  139. digitalWrite(6, ledState);
  140. digitalWrite(7, led1State);
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement