Advertisement
Guest User

Untitled

a guest
May 20th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. //#include <WiFiClient.h>
  3. #include <ESP8266WebServer.h>
  4. #include <LiquidCrystal.h>
  5. //#include <WiFi.h>
  6.  
  7. #define LED_BUILTIN 14
  8. ESP8266WebServer server(80);
  9.  
  10. //const char ssid[] = "internal.IOT";
  11. //const char pass[] = "IoTlab32768";
  12. const char ssid[] = "AndroidAP";
  13. const char pass[] = "txl5734";
  14. WiFiClient client;
  15. const char* host = "www.example.com";
  16.  
  17. const int rs = 12, en = 13, d4 = 4, d5 = 0, d6 = 2, d7 = 14;
  18. LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
  19.  
  20. boolean myBoolean;
  21. void handleRoot() {
  22. server.send(200, "text/html", "<h1>You are connected</h1><a href='/0'>TurnOff</a><a href='/1'>TurnOn</a>");
  23. }
  24.  
  25.  
  26. void changeOFF()
  27. {
  28. myBoolean = false;
  29. server.send(200, "text/html", "<h1>You are connected</h1><a href='/0'>TurnOff</a><a href='/1'>TurnOn</a>");
  30.  
  31. }
  32. void changeON()
  33. {
  34. myBoolean = true;
  35. server.send(200, "text/html", "<h1>You are connected</h1><a href='/0'>TurnOff</a><a href='/1'>TurnOn</a>");
  36. }
  37.  
  38. void setupWIFI(){
  39.  
  40. int status = WL_IDLE_STATUS;
  41. WiFi.begin(ssid, pass);
  42. if ( status != WL_CONNECTED) {
  43. Serial.println("Couldn't get a wifi connection");
  44. while(true);
  45. }
  46. // if you are connected, print out info about the connection:
  47. else {
  48. Serial.println("Connected to network");
  49. }
  50. }
  51.  
  52. void wifiConnection(){
  53. if (client.connect(host, 80)) {
  54. Serial.println("connected to server");
  55. // Make a HTTP request:
  56. client.println("GET /search?q=arduino HTTP/1.1");
  57. client.println("Host: www.google.com");
  58. client.println("Connection: close");
  59. client.println();
  60. }
  61. }
  62.  
  63. void wifiLoop(){
  64. while (client.available()) {
  65. char c = client.read();
  66. Serial.write(c);
  67. }
  68.  
  69. // if the server's disconnected, stop the client:
  70. if (!client.connected()) {
  71. Serial.println();
  72. Serial.println("disconnecting from server.");
  73. client.stop();
  74.  
  75. // do nothing forevermore:
  76. while (true);
  77. }
  78. }
  79.  
  80. void setupWifiAP(){
  81.  
  82. WiFi.mode(WIFI_STA);
  83. WiFi.disconnect();
  84.  
  85. int n = WiFi.scanNetworks();
  86.  
  87. for (int i = 0; i < n; i++)
  88. {
  89. Serial.println(WiFi.SSID(i));
  90. }
  91.  
  92. boolean result = WiFi.softAP("xd", "12345678");
  93. if(result == true)
  94. {
  95. Serial.println("Ready");
  96. }
  97. else
  98. {
  99. Serial.println("Failed!");
  100. }
  101. Serial.println(WiFi.softAPIP());
  102. server.on("/", handleRoot);
  103. server.on("/0",changeOFF);
  104. server.on("/1",changeON);
  105. server.begin();
  106. Serial.println("HTTP server started");
  107. }
  108.  
  109. void setup() {
  110. // put your setup code here, to run once:
  111. pinMode(14, OUTPUT);
  112. lcd.begin(16, 2);
  113. myBoolean = true;
  114. Serial.begin(9600);
  115.  
  116. setupWIFI();
  117. wifiConnection();
  118.  
  119. //setupWifiAP();
  120. }
  121.  
  122. void loop() {
  123.  
  124. Serial.printf("Stations connected = %d\n", WiFi.softAPgetStationNum());
  125. Serial.printf("%d\n", myBoolean);
  126. if(myBoolean)
  127. digitalWrite(14, HIGH);
  128. else
  129. digitalWrite(14, LOW);
  130.  
  131. lcd.setCursor(0,0);
  132. lcd.print("Connected " + String(WiFi.softAPgetStationNum()));
  133.  
  134. wifiLoop();
  135. //lcd.setCursor(1,0);
  136. //lcd.print("Connected "+ String(WiFi.softAPgetStationNum()));
  137. server.handleClient();
  138.  
  139.  
  140. delay(500);
  141.  
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement