mansi_singhal2

Code for Esp8266 Wi-Fi module with IP Address 192.168.0.14

Jul 25th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.39 KB | None | 0 0
  1. ####### Code for Esp8266 Wi-Fi module with IP Address 192.168.0.14
  2. #include <ESP8266WiFi.h>
  3. const char* ssid = "dlink-FA0C";
  4. int flag;
  5. const char* password = "1234567890";
  6.  
  7. IPAddress ip(192,168,0,14);
  8. IPAddress gateway(192,168,0,1);
  9. IPAddress subnet(255,255,255,0);
  10.  
  11. unsigned char status_led=0;
  12. int id;
  13. WiFiServer server(80);
  14. void setup() {
  15.   Serial.begin(9600);
  16.   pinMode(D0,OUTPUT);
  17.   flag=0;
  18.   pinMode(D1,OUTPUT);
  19.   pinMode(D2,OUTPUT);
  20.   pinMode(D3,OUTPUT);
  21.   pinMode(D4,OUTPUT);
  22.   pinMode(D5,OUTPUT);
  23.   pinMode(D6,OUTPUT);
  24.   pinMode(D7,OUTPUT);
  25.   pinMode(D8,OUTPUT);
  26.   digitalWrite(D0, LOW);
  27.   digitalWrite(D1, LOW);
  28.   digitalWrite(D2, LOW);
  29.   digitalWrite(D3, LOW);
  30.   digitalWrite(D4, LOW);
  31.   digitalWrite(D5, LOW);
  32.   digitalWrite(D6, LOW);
  33.   digitalWrite(D7, LOW);
  34.   digitalWrite(D8, LOW);
  35.   // Connect to WiFi network
  36.   Serial.println();
  37.   Serial.println();
  38.   Serial.print("Connecting to ");
  39.   Serial.println(ssid);
  40.   WiFi.config(ip,gateway,subnet);
  41.   WiFi.begin(ssid, password);
  42.   while (WiFi.status() != WL_CONNECTED) {
  43.     delay(500);
  44.     Serial.print(".");
  45.   }
  46.   Serial.println("");
  47.   Serial.println("WiFi connected");
  48.   // Start the server
  49.   server.begin();
  50.   Serial.println("Server started at...");
  51.   Serial.println(WiFi.localIP());
  52.  }
  53.  
  54. void loop() {
  55.   int x = analogRead(A0);
  56. int y = x-520;
  57. trigger(y);
  58.  WiFiClient s;
  59. String host = "192.168.0.17";
  60.   int httpPort=80;
  61.   String urlsend = "/values.php?id="+String(y);
  62.   Serial.println("req url:");
  63.   Serial.println(urlsend);
  64. /* while (!serverupdate.connect(host, httpPort)) {
  65.     Serial.println("connection failed");delay(100);
  66.   }*/
  67.    // serverupdate.print(String("GET") + urlsend + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n");  
  68.     if(s.connect(host, httpPort)){
  69.      s.println("GET "+ urlsend);
  70.     s.println("Host: "+host);
  71.     s.println("Connection: close");
  72.    
  73.     delay(200);
  74.     }
  75.     else
  76.     {
  77.       Serial.print("failed");
  78.     }
  79. delay(200);
  80.   // Check if a client has connected
  81.   WiFiClient client = server.available();
  82.   if (!client) {
  83.     return;
  84.   }
  85.   // Wait until the client sends some data
  86.   Serial.println("new client");
  87.   if(! client.available())
  88.   {
  89.     delay (1);
  90.   }
  91.    // Read the first line of the request
  92.   String req = client.readStringUntil('\r');
  93.   Serial.println(req);
  94.   client.flush();
  95.  
  96.   {
  97. //=============================D8===========================
  98.   if (req.indexOf("/coridooroff") != -1){
  99.     status_led=0;
  100.     digitalWrite(D8, LOW);
  101.     id=90;
  102.     updatestatus(id);
  103.   }
  104.   else if(req.indexOf("/coridooron") != -1){
  105.     status_led=1;
  106.     digitalWrite(D8,HIGH);
  107.     Serial.println("D7 LED ON");
  108.     id=91;
  109.     updatestatus(id);
  110.   }
  111. //=============================D0===========================
  112.   if (req.indexOf("/livingroomoff") != -1){
  113.     status_led=0;
  114.     digitalWrite(D0, LOW);
  115.     Serial.println("D0 OFF");
  116.      id=10;
  117.     updatestatus(id);
  118.    
  119.   }
  120.   else if(req.indexOf("/livingroomon") != -1)
  121.   {
  122.     status_led=1;
  123.     digitalWrite(D0,HIGH);
  124.     Serial.println("D0 LED ON");
  125.     id=11;
  126.     updatestatus(id);
  127.   }
  128.   //===================================D1================================
  129.   if (req.indexOf("/aclivingoff") != -1){
  130.     status_led=0;
  131.     digitalWrite(D1, LOW);
  132.     Serial.println("D1 LED OFF");
  133.     id=20;
  134.     updatestatus(id);
  135.   }
  136.   else if(req.indexOf("/aclivingon") != -1){
  137.    
  138.     status_led=1;
  139.     digitalWrite(D1,HIGH);
  140.     Serial.println("D1 LED ON");
  141.     id=21;
  142.     updatestatus(id);
  143.   }
  144. //================================================================================
  145.  
  146.   //===================================D2================================
  147.   if (req.indexOf("/kitchenoff") != -1){
  148.     status_led=0;
  149.     digitalWrite(D2, LOW);
  150.     Serial.println("D2 LED OFF");
  151.     id=30;
  152.     updatestatus(id);
  153.   }
  154.   else if(req.indexOf("/kitchenon") != -1){
  155.     status_led=1;
  156.     digitalWrite(D2,HIGH);
  157.     Serial.println("D2 LED ON");
  158.     id=31;
  159.     updatestatus(id);
  160.   }
  161. //================================================================================
  162.  
  163.   //===================================D3================================
  164.   if (req.indexOf("/diningfanoff") != -1) {
  165.     status_led=0;
  166.     digitalWrite(D3, LOW);
  167.     Serial.println("D3 LED OFF");
  168.     id=40;
  169.     updatestatus(id);
  170.   }
  171.   else if(req.indexOf("/diningfanon") != -1){
  172.     status_led=1;
  173.     digitalWrite(D3,HIGH);
  174.     Serial.println("D3 LED ON");
  175.     id=41;
  176.     updatestatus(id);
  177.   }
  178. //================================================================================
  179.  
  180.   //===================================D4================================
  181.   if (req.indexOf("/utilityoff") != -1){
  182.     status_led=0;
  183.     digitalWrite(D4, LOW);
  184.     Serial.println("D4 LED OFF");
  185.     id=50;
  186.     updatestatus(id);
  187.   }
  188.   else if(req.indexOf("/utilityon") != -1){
  189.     status_led=1;
  190.     digitalWrite(D4,HIGH);
  191.     Serial.println("D4 LED ON");
  192.     id=51;
  193.     updatestatus(id);
  194.   }
  195. //================================================================================
  196.  
  197.   //===================================D5================================
  198.   if (req.indexOf("/entryoff") != -1) {
  199.     status_led=0;
  200.     digitalWrite(D5, LOW);
  201.     Serial.println("D5 LED OFF");
  202.     id=60;
  203.     updatestatus(id);
  204.   }
  205.   else if(req.indexOf("/entryon") != -1){
  206.     status_led=1;
  207.     digitalWrite(D5,HIGH);
  208.     Serial.println("D5 LED ON");
  209.     id=61;
  210.     updatestatus(id);
  211.   }
  212. //================================================================================
  213.  
  214.   //===================================D6================================
  215.   if (req.indexOf("/gayzeroff") != -1)  {
  216.     status_led=0;
  217.     digitalWrite(D6, LOW);
  218.     Serial.println("D6 LED OFF");
  219.     id=70;
  220.     updatestatus(id);
  221.   }
  222.   else if(req.indexOf("/gayzeron") != -1){
  223.     status_led=1;
  224.     digitalWrite(D6,HIGH);
  225.     Serial.println("D6 LED ON");
  226.     id=71;
  227.     updatestatus(id);
  228.   }
  229. //================================================================================
  230.  
  231.   if (req.indexOf("/auto") != -1)  {
  232.     flag=1;
  233.     Serial.println("flag = 1 ");
  234.   }
  235.   else if(req.indexOf("/manual") != -1){
  236.     flag=0;  
  237.     Serial.println("flag = 0");
  238.   }
  239.  
  240.  
  241.   //===================================D7 TV toggling================================
  242.   if (req.indexOf("/dininglightoff") != -1)  {
  243.     status_led=0;
  244.     digitalWrite(D7, LOW);
  245.     Serial.println("D7 LED OFF");
  246.     id=80;
  247.     updatestatus(id);
  248.   }
  249.   else if(req.indexOf("/dininglighton") != -1){
  250.     status_led=1;
  251.     digitalWrite(D7,HIGH);
  252.     Serial.println("D7 LED ON");
  253.     id=81;
  254.     updatestatus(id);
  255.   }
  256. //================================================================================
  257.  
  258.   }
  259.  {
  260.  //====================================Commands for user==============================================
  261.  client.println("<a href=\"/coridooron\"\"> <button > Coridoor </button> </a>");
  262.  client.println("<a href=\"/coridooroff\"\"> <button >  OFF </button> </a><br>");
  263. //==================================================================================
  264.  client.println("<a href=\"/aclivingon\"\"> <button > AC </button> </a>");
  265.  client.println("<a href=\"/aclivingoff\"\"> <button > OFF </button> </a><br>");
  266.  //==================================================================================
  267.  //==================================================================================
  268.  client.println("<a href=\"/kitchenon\"\"> <button > Kitchen </button> </a>");
  269.  client.println("<a href=\"/kitchenoff\"\"> <button > OFF </button> </a><br>");
  270. //==================================================================================
  271.  client.println("<a href=\"/diningfanon\"\"> <button > Dining Fan </button> </a>");
  272.  client.println("<a href=\"/diningfanoff\"\"> <button > 0FF </button> </a><br>");
  273. //==================================================================================
  274.  //==================================================================================
  275.  client.println("<a href=\"/utilityon\"\"> <button > Utility </button> </a>");
  276.  client.println("<a href=\"/utilityoff\"\"> <button > OFF </button> </a><br>");
  277. //==================================================================================
  278.  //==================================================================================
  279.  client.println("<a href=\"/entryon\"\"> <button > Entry </button> </a>");
  280.  client.println("<a href=\"/entryoff\"\"> <button > OFF </button> </a><br>");
  281. //==================================================================================
  282.  //==================================================================================
  283.  client.println("<a href=\"/gayzeron\"\"> <button > Gayzer </button> </a>");
  284.  client.println("<a href=\"/gayzeroff\"\"> <button > OFF </button> </a><br>");
  285. //==================================================================================
  286.  //==================================================================================
  287.  client.println("<a href=\"/dininglighton\"\"> <button > Dining Light </button> </a>");
  288.  client.println("<a href=\"/dininglightoff\"\"> <button > OFF </button> </a><br>");
  289. //==================================================================================
  290.  //==================================================================================
  291.  client.println("<a href=\"/livingroomon\"\"> <button > Living Room </button> </a>");
  292.  client.println("<a href=\"/livingroomoff\"\"> <button > OFF </button> </a><br>");
  293. //==================================================================================
  294.  
  295.  //==================================================================================
  296.  client.println("<a href=\"/manual\"\"> <button > Ac manual mode </button> </a>");
  297.  client.println("<a href=\"/auto\"\"> <button > AC auto mode  </button> </a><br>");
  298. //==================================================================================
  299.  }
  300. Serial.println("");
  301. }
  302.  
  303.     //====================================updating the light status============================================================
  304.  
  305.  void updatestatus(int id)
  306.  {
  307.   WiFiClient serverupdate;
  308.   Serial.println("==============================================================");
  309.   String xy=String(id);
  310.   Serial.println(xy);
  311.   String host = "192.168.0.17";
  312.   int httpPort=80;
  313.   String urlsend = "/led.php?id="+String(xy);
  314.   Serial.println("req url:");
  315.   Serial.println(urlsend);
  316. /* while (!serverupdate.connect(host, httpPort)) {
  317.     Serial.println("connection failed");delay(100);
  318.   }*/
  319.     if(serverupdate.connect(host, httpPort)){
  320.      serverupdate.println("GET "+ urlsend);
  321.     serverupdate.println("Host: "+host);
  322.     serverupdate.println("Connection: close");
  323.     Serial.println("Client disonnected");
  324. delay(200);
  325.     }
  326.     else
  327.     {
  328.       Serial.print("failed");
  329.     }
  330.    
  331.   }  
  332.   //====================================================================================================
  333. //==========================================================================================================
  334.  
  335. void trigger(int y){
  336.   if(flag==1){
  337.     Serial.println("kshitij");
  338.   if(y>35)
  339.   {
  340.     digitalWrite(D1,HIGH);
  341.     updatestatus(21);
  342.   }
  343.   else if (y<30)
  344.   {
  345.     digitalWrite(D1,LOW);
  346.     updatestatus(20);
  347.   }
  348.   }
  349. }
  350. /*
  351. void set0()
  352. {
  353.   WiFiClient stup;
  354.   char* ip="192.168.43.59";
  355.   int http
  356.  
  357.  
  358.  
  359. }
  360. */
  361. //==========================================================================================================
Add Comment
Please, Sign In to add comment