Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <CytronWiFiShield.h>
- #include <CytronWiFiClient.h>
- #include <CytronWiFiServer.h>
- #include <SoftwareSerial.h>
- const char *ssid = "alina";
- const char *pass = "Bangang5592";
- IPAddress ip(192, 168, 1 ,188);
- ESP8266Server server(80);
- int ledState = 0;
- int led1State = 0;
- const char htmlHeader[] = "HTTP/1.1 200 OK\r\n"
- "Content-Type: text/html\r\n"
- "Connection: close\r\n\r\n"
- "<!DOCTYPE HTML>\r\n"
- "<html>\r\n";
- void setup() {
- pinMode(6, OUTPUT);
- pinMode(7, OUTPUT);
- // put your setup code here, to run once:
- Serial.begin(9600);
- while (!Serial) {
- ; // wait for serial port to connect. Needed for Leonardo only
- }
- if(!wifi.begin(2, 3))
- {
- Serial.println(F("Error talking to shield"));
- while(1);
- }
- Serial.println(wifi.firmwareVersion());
- Serial.print(F("Mode: "));Serial.println(wifi.getMode());// 1- station mode, 2- softap mode, 3- both
- Serial.println(F("Setup wifi config"));
- wifi.config(ip);
- Serial.println(F("Start wifi connection"));
- if(!wifi.connectAP(ssid, pass))
- {
- Serial.println(F("Error connecting to WiFi"));
- while(1);
- }
- Serial.print(F("Connected to "));Serial.println(wifi.SSID());
- Serial.println(F("IP address: "));
- Serial.println(wifi.localIP());
- wifi.updateStatus();
- 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
- server.begin();
- }
- void loop() {
- // put your main code here, to run repeatedly:
- ESP8266Client client = server.available();
- if(client.available()>0)
- {
- String req = client.readStringUntil('\r');
- // First line of HTTP request looks like "GET /path HTTP/1.1"
- // Retrieve the "/path" part by finding the spaces
- int addr_start = req.indexOf(' ');
- int addr_end = req.indexOf(' ', addr_start + 1);
- if (addr_start == -1 || addr_end == -1) {
- Serial.print(F("Invalid request: "));
- Serial.println(req);
- return;
- }
- req = req.substring(addr_start + 1, addr_end);
- Serial.print(F("Request: "));
- Serial.println(req);
- client.flush();
- if(req.equals("/"))
- {
- client.print(htmlHeader);
- String htmlBody = "Hello from ESP8266 at ";
- htmlBody += "</html>\r\n\r\n";
- client.print(htmlBody);
- }
- //input location
- else if(req.equals("/analog"))
- {
- client.print(htmlHeader);
- String htmlBody = String(analogRead(A0));
- client.print(htmlBody);
- }
- //output on location
- else if(req.equals("/on"))
- {
- client.print(htmlHeader);
- String htmlBody="Pin 6 is now ";
- htmlBody += "on";
- htmlBody += "</html>\r\n";
- client.print(htmlBody);
- ledState = 1;
- }
- //output off location
- else if(req.equals("/off"))
- {
- client.print(htmlHeader);
- String htmlBody="Pin 6 is now ";
- htmlBody += "off";
- htmlBody += "</html>\r\n";
- client.print(htmlBody);
- ledState = 0;
- }
- else if(req.equals("/onn"))
- {
- client.print(htmlHeader);
- String htmlBody="Pin 7 is now ";
- htmlBody += "on";
- htmlBody += "</html>\r\n";
- client.print(htmlBody);
- led1State = 1;
- }
- else if(req.equals("/offf"))
- {
- client.print(htmlHeader);
- String htmlBody="Pin 7 is now ";
- htmlBody += "off";
- htmlBody += "</html>\r\n";
- client.print(htmlBody);
- led1State = 0;
- }
- else
- client.print("HTTP/1.1 404 Not Found\r\n\r\n");
- client.stop();
- }
- digitalWrite(6, ledState);
- digitalWrite(7, led1State);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement