Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ####### Code for Esp8266 Wi-Fi module with IP address 192.168.0.15
- #include <ESP8266WiFi.h>
- const char* ssid = "dlink-FA0C";
- const char* password = "1234567890";
- unsigned char status_led=0;
- int id;
- WiFiServer server(80);
- IPAddress ip(192,168,0,15);
- IPAddress gateway(192,168,0,1);
- IPAddress subnet(255,255,255,0);
- void setup() {
- Serial.begin(9600);
- pinMode(D0,OUTPUT);
- pinMode(D1,OUTPUT);
- pinMode(D2,OUTPUT);
- pinMode(D3,OUTPUT);
- pinMode(D4,OUTPUT);
- pinMode(D5,OUTPUT);
- pinMode(D6,OUTPUT);
- pinMode(D7,OUTPUT);
- pinMode(D8,OUTPUT);
- digitalWrite(D0, HIGH);
- digitalWrite(D0, LOW);
- digitalWrite(D1, LOW);
- digitalWrite(D2, LOW);
- digitalWrite(D3, LOW);
- digitalWrite(D4, LOW);
- digitalWrite(D5, LOW);
- digitalWrite(D6, LOW);
- digitalWrite(D7, LOW);
- digitalWrite(D8, LOW);
- // Connect to WiFi network
- Serial.println();
- Serial.println();
- WiFi.config(ip,gateway,subnet);
- Serial.print("Connecting to ");
- Serial.println(ssid);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("");
- Serial.println("WiFi connected");
- // Start the server
- server.begin();
- Serial.println("Server started at...");
- Serial.println(WiFi.localIP());
- }
- void loop() {
- // Check if a client has connected
- WiFiClient client = server.available();
- if (!client) {
- return;
- }
- // Wait until the client sends some data
- Serial.println("new client");
- while (! client.available()){
- delay (1);
- }
- // Read the first line of the request
- String req = client.readStringUntil('\r');
- Serial.println(req);
- client.flush();
- //=============================D8===========================
- if (req.indexOf("/br2_lampoff") != -1){
- status_led=0;
- digitalWrite(D8, LOW);
- Serial.println("D7 LED OFF");
- id=90;
- updatestatus(id);
- }
- else if(req.indexOf("/br2_lampon") != -1){
- status_led=1;
- digitalWrite(D8,HIGH);
- Serial.println("D7 LED ON");
- id=91;
- updatestatus(id);
- }
- //=============================D0===========================
- if (req.indexOf("/br1_lightoff") != -1) {
- status_led=0;
- digitalWrite(D0, LOW);
- Serial.println("D0 OFF");
- id=10;
- updatestatus(id);
- }
- else if(req.indexOf("/br1_lighton") != -1){
- status_led=1;
- digitalWrite(D0,HIGH);
- Serial.println("D0 LED ON");
- id=11;
- updatestatus(id);
- }
- //===================================D1================================
- if (req.indexOf("/br1_tvoff") != -1){
- status_led=0;
- digitalWrite(D1, LOW);
- Serial.println("D1 LED OFF");
- id=20;
- updatestatus(id);
- }
- else if(req.indexOf("/br1_tvon") != -1) {
- status_led=1;
- digitalWrite(D1,HIGH);
- Serial.println("D1 LED ON");
- id=21;
- updatestatus(id);
- }
- //================================================================================
- //===================================D2================================
- if (req.indexOf("/br1_acoff") != -1){
- status_led=0;
- digitalWrite(D2, LOW);
- Serial.println("D2 LED OFF");
- id=30;
- updatestatus(id);
- }
- else if(req.indexOf("/br1_acon") != -1) {
- status_led=1;
- digitalWrite(D2,HIGH);
- Serial.println("D2 LED ON");
- id=31;
- updatestatus(id);
- }
- //================================================================================
- //===================================D3================================
- if (req.indexOf("/br2_acoff") != -1) {
- status_led=0;
- digitalWrite(D3, LOW);
- Serial.println("D3 LED OFF");
- id=40;
- updatestatus(id);
- }
- else if(req.indexOf("/br2_acon") != -1){
- status_led=1;
- digitalWrite(D3,HIGH);
- Serial.println("D3 LED ON");
- id=41;
- updatestatus(id);
- }
- //================================================================================
- //===================================D4================================
- if (req.indexOf("/br1_lampoff") != -1) {
- status_led=0;
- digitalWrite(D4, LOW);
- Serial.println("D4 LED OFF");
- id=50;
- updatestatus(id);
- }
- else if(req.indexOf("/br1_lampon") != -1){
- status_led=1;
- digitalWrite(D4,HIGH);
- Serial.println("D4 LED ON");
- id=51;
- updatestatus(id);
- }
- //================================================================================
- //===================================D5================================
- if (req.indexOf("/wr1_lightoff") != -1) {
- status_led=0;
- digitalWrite(D5, LOW);
- Serial.println("D5 LED OFF");
- id=60;
- updatestatus(id);
- }
- else if(req.indexOf("/wr1_lighton") != -1){
- status_led=1;
- digitalWrite(D5,HIGH);
- Serial.println("D5 LED ON");
- id=61;
- updatestatus(id);
- }
- //================================================================================
- //===================================D6================================
- if (req.indexOf("/wr2_lightoff") != -1){
- status_led=0;
- digitalWrite(D6, LOW);
- Serial.println("D6 LED OFF");
- id=70;
- updatestatus(id);
- }
- else if(req.indexOf("/wr2_lighton") != -1) {
- status_led=1;
- digitalWrite(D6,HIGH);
- Serial.println("D6 LED ON");
- id=71;
- updatestatus(id);
- }
- //================================================================================
- //===================================D7================================
- if (req.indexOf("/br2_lightoff") != -1){
- status_led=0;
- digitalWrite(D7, LOW);
- Serial.println("D7 LED OFF");
- id=80;
- updatestatus(id);
- }
- else if(req.indexOf("/br2_lighton") != -1){
- status_led=1;
- digitalWrite(D7,HIGH);
- Serial.println("D7 LED ON");
- id=81;
- updatestatus(id);
- }
- //================================================================================
- //====================================Commands for user==============================================
- client.println("<a href=\"/br1_lighton\"\"> <button > BedRoom1 Light </button> </a>");
- client.println("<a href=\"/br1_lightoff\"\"> <button > LED OFF </button> </a><br>");
- //==================================================================================
- client.println("<a href=\"/br1_tvon\"\"> <button > Bedroom1 TV </button> </a>");
- client.println("<a href=\"/br1_tvoff\"\"> <button > LED OFF </button> </a><br>");
- //==================================================================================
- //==================================================================================
- client.println("<a href=\"/br1_acon\"\"> <button > bedroom1 ac </button> </a>");
- client.println("<a href=\"/br1_acoff\"\"> <button > LED OFF </button> </a><br>");
- //==================================================================================
- client.println("<a href=\"/br2_acon\"\"> <button > BEdroom2 ac </button> </a>");
- client.println("<a href=\"/br2_acoff\"\"> <button > LED 0FF </button> </a><br>");
- //==================================================================================
- //==================================================================================
- client.println("<a href=\"/br1_lampon\"\"> <button > bedrom 1 lamp </button> </a>");
- client.println("<a href=\"/br1_lampoff\"\"> <button > OFF </button> </a><br>");
- //==================================================================================
- //==================================================================================
- client.println("<a href=\"/wr1_lighton\"\"> <button > washroomlight 1 </button> </a>");
- client.println("<a href=\"/wr1_lightoff\"\"> <button > LED OFF </button> </a><br>");
- //==================================================================================
- //==================================================================================
- client.println("<a href=\"/wr2_lighton\"\"> <button > washroom 2 light </button> </a>");
- client.println("<a href=\"/wr2_lightoff\"\"> <button > OFF </button> </a><br>");
- //==================================================================================
- //==================================================================================
- client.println("<a href=\"/br2_lighton\"\"> <button > bedroom2 light </button> </a>");
- client.println("<a href=\"/br2_ligltoff\"\"> <button > LED OFF </button> </a><br>");
- //==================================================================================
- //==================================================================================
- client.println("<a href=\"/br2_lampon\"\"> <button > bedroom2 lamp </button> </a>");
- client.println("<a href=\"/br2_lampoff\"\"> <button > LED OFF </button> </a><br>");
- //==================================================================================
- Serial.println("");
- }
- //====================================updating the light status============================================================
- void updatestatus(int id)
- {
- WiFiClient serverupdate;
- // Serial.println("==============================================================");
- String xy=String(id);
- Serial.println(xy);
- String host = "192.168.0.17";
- int httpPort=80;
- String urlsend = "/led.php?id="+String(xy);
- //Serial.println("req url:");
- //Serial.println(urlsend);
- /* while (!serverupdate.connect(host, httpPort)) {
- Serial.println("connection failed");delay(100);
- }*/
- // serverupdate.print(String("GET") + urlsend + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n");
- if(serverupdate.connect(host, httpPort)){
- serverupdate.println("GET "+ urlsend);
- serverupdate.println("Host: "+host);//192.168.0.17");
- serverupdate.println("Connection: close");
- //Serial.println("Client disonnected");
- delay(200);
- }
- else
- {
- Serial.print("Server COnnection failed");
- }
- }
- //====================================================================================================
- //==========================================================================================================
- /*
- void set0()
- {
- WiFiClient stup;
- char* ip="192.168.43.59";
- int http
- }
- */
- //==========================================================================================================
Add Comment
Please, Sign In to add comment