Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- //#include <WiFiClient.h>
- #include <ESP8266WebServer.h>
- #include <LiquidCrystal.h>
- //#include <WiFi.h>
- #define LED_BUILTIN 14
- ESP8266WebServer server(80);
- //const char ssid[] = "internal.IOT";
- //const char pass[] = "IoTlab32768";
- const char ssid[] = "AndroidAP";
- const char pass[] = "txl5734";
- WiFiClient client;
- const char* host = "www.example.com";
- const int rs = 12, en = 13, d4 = 4, d5 = 0, d6 = 2, d7 = 14;
- LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
- boolean myBoolean;
- void handleRoot() {
- server.send(200, "text/html", "<h1>You are connected</h1><a href='/0'>TurnOff</a><a href='/1'>TurnOn</a>");
- }
- void changeOFF()
- {
- myBoolean = false;
- server.send(200, "text/html", "<h1>You are connected</h1><a href='/0'>TurnOff</a><a href='/1'>TurnOn</a>");
- }
- void changeON()
- {
- myBoolean = true;
- server.send(200, "text/html", "<h1>You are connected</h1><a href='/0'>TurnOff</a><a href='/1'>TurnOn</a>");
- }
- void setupWIFI(){
- int status = WL_IDLE_STATUS;
- WiFi.begin(ssid, pass);
- if ( status != WL_CONNECTED) {
- Serial.println("Couldn't get a wifi connection");
- while(true);
- }
- // if you are connected, print out info about the connection:
- else {
- Serial.println("Connected to network");
- }
- }
- void wifiConnection(){
- if (client.connect(host, 80)) {
- Serial.println("connected to server");
- // Make a HTTP request:
- client.println("GET /search?q=arduino HTTP/1.1");
- client.println("Host: www.google.com");
- client.println("Connection: close");
- client.println();
- }
- }
- void wifiLoop(){
- while (client.available()) {
- char c = client.read();
- Serial.write(c);
- }
- // if the server's disconnected, stop the client:
- if (!client.connected()) {
- Serial.println();
- Serial.println("disconnecting from server.");
- client.stop();
- // do nothing forevermore:
- while (true);
- }
- }
- void setupWifiAP(){
- WiFi.mode(WIFI_STA);
- WiFi.disconnect();
- int n = WiFi.scanNetworks();
- for (int i = 0; i < n; i++)
- {
- Serial.println(WiFi.SSID(i));
- }
- boolean result = WiFi.softAP("xd", "12345678");
- if(result == true)
- {
- Serial.println("Ready");
- }
- else
- {
- Serial.println("Failed!");
- }
- Serial.println(WiFi.softAPIP());
- server.on("/", handleRoot);
- server.on("/0",changeOFF);
- server.on("/1",changeON);
- server.begin();
- Serial.println("HTTP server started");
- }
- void setup() {
- // put your setup code here, to run once:
- pinMode(14, OUTPUT);
- lcd.begin(16, 2);
- myBoolean = true;
- Serial.begin(9600);
- setupWIFI();
- wifiConnection();
- //setupWifiAP();
- }
- void loop() {
- Serial.printf("Stations connected = %d\n", WiFi.softAPgetStationNum());
- Serial.printf("%d\n", myBoolean);
- if(myBoolean)
- digitalWrite(14, HIGH);
- else
- digitalWrite(14, LOW);
- lcd.setCursor(0,0);
- lcd.print("Connected " + String(WiFi.softAPgetStationNum()));
- wifiLoop();
- //lcd.setCursor(1,0);
- //lcd.print("Connected "+ String(WiFi.softAPgetStationNum()));
- server.handleClient();
- delay(500);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement