Paste Search Dynamic
Recent pastes
PONT WIFI
  1. //Libraries
  2. #include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266WiFi/src/ESP8266WiFi.h
  3. //#include <Wire.h>
  4. #include <EEPROM.h>
  5. #include "readmux.h"
  6.  
  7. //Constants
  8.  
  9. //#define LED D13
  10.  
  11.  
  12. #define REL_VCC D5
  13. #define REL_IN  D6
  14.  
  15. //Parameters
  16. String g_request;
  17.  
  18. char* g_ssid = "PONT WIFI";
  19. char* g_password = "xxxx";
  20.  
  21. String g_nom = "ESP8266";
  22. //Objects
  23. WiFiServer g_server(80);
  24.  
  25.  
  26. int g_mux[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  27.  
  28. #define EEPROM_TL 0x20
  29. #define EEPROM_TH 0x24
  30.  
  31. #define DEFAULT_THRESHOLD_LOW 60
  32. #define DEFAULT_THRESHOLD_HIGH 200
  33.  
  34. int g_threshold_low = 0;
  35. int g_threshold_high = 0;
  36.  
  37.  
  38. int g_power = 0;
  39. int g_threshold = 0;
  40.  
  41. void initEeprom() {
  42.   Serial.println("initEeprom() :");
  43.   EEPROM.get(EEPROM_TL, g_threshold_low);
  44.   if(0 == g_threshold_low) {
  45.     g_threshold_low = DEFAULT_THRESHOLD_LOW;
  46.     EEPROM.put(EEPROM_TL, g_threshold_low);
  47.   }
  48.    
  49.  
  50.   EEPROM.get(EEPROM_TH, g_threshold_high);
  51.   if(0 == g_threshold_high) {
  52.     g_threshold_high = DEFAULT_THRESHOLD_HIGH;
  53.     EEPROM.put(EEPROM_TH, g_threshold_high);
  54.   }
  55.  
  56.   Serial.println("TL:");
  57.   Serial.println(g_threshold_low);
  58.  
  59.   Serial.println("TH:");
  60.   Serial.println(g_threshold_high);
  61.  
  62.   delay(5);
  63.  
  64.   g_threshold = g_threshold_low;
  65.  
  66. }
  67.  
  68. void _power_on() {
  69.     Serial.println("Power ON");
  70.     //digitalWrite(POWER_PIN, HIGH);
  71.     digitalWrite(REL_IN, LOW);
  72.     g_power = 1;
  73.     g_threshold = g_threshold_high;
  74.     Serial.print("Threshold : ");
  75.     Serial.println(g_threshold);
  76. }
  77. void _power_off() {
  78.   Serial.println("Power OFF");
  79.   //digitalWrite(POWER_PIN, LOW);
  80.   digitalWrite(REL_IN, HIGH);
  81.   g_power = 0;
  82.   g_threshold = g_threshold_low;
  83.   Serial.print("Threshold : ");
  84.   Serial.println(g_threshold);
  85. }
  86.  
  87.  
  88. void scanNetworks() {
  89.     Serial.println("Scanning WiFi networks...");
  90.     int n = WiFi.scanNetworks();
  91.     if (n == 0) {
  92.       Serial.println("no networks found");
  93.     } else {
  94.       Serial.print(n);
  95.       Serial.println(" networks found");
  96.       for (int i = 0; i < n; ++i) {
  97.         // Print SSID and RSSI for each network found
  98.         Serial.print(i + 1);
  99.         Serial.print(": [");
  100.         Serial.print(WiFi.SSID(i));
  101.         Serial.print("] (");
  102.         Serial.print(WiFi.RSSI(i));
  103.         Serial.print(" dB)");
  104.         Serial.println((WiFi.encryptionType(i) == ENC_TYPE_NONE) ? 0 : WiFi.encryptionType(i));
  105.       }
  106.     }
  107. }
  108. void setup(){
  109.   pinMode(ledVCC, OUTPUT);
  110.  
  111.   delay(500);
  112.  
  113.   Serial.begin(9600);
  114.   /*while (!Serial) {
  115.     //delay(10); // wait for serial port to connect. Needed for native USB
  116.     digitalWrite(LED, HIGH);
  117.     delay(100);
  118.     digitalWrite(LED, LOW);
  119.   }*/
  120.   Serial.println("Serial OK. Initialize System...");
  121.  
  122.   initEeprom();
  123.  
  124.  
  125.   muxSetup();
  126.  
  127.  
  128.  
  129.   scanNetworks();
  130.   //Init ESPBrowser
  131.   Serial.print(F("Connecting to "));Serial.println(g_ssid);
  132.   WiFi.disconnect( true );
  133.   delay( 1 );
  134.   WiFi.begin(g_ssid, g_password);
  135.  
  136.   // Set your Static IP address
  137.   IPAddress local_IP(192, 168, 1, 184);
  138.   // Set your Gateway IP address
  139.   IPAddress gateway(192, 168, 1, 1);
  140.  
  141.   IPAddress subnet(255, 255, 255, 0);
  142.   IPAddress primaryDNS(8, 8, 8, 8);   //optional
  143.   IPAddress secondaryDNS(192, 168, 1, 1); //optional
  144.  
  145.   if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) {
  146.       Serial.println("STA Failed to configure");
  147.   }
  148.    
  149.    // Connect to Wifi network.
  150.   while (WiFi.status() != WL_CONNECTED)
  151.   {
  152.     digitalWrite(ledVCC, HIGH);
  153.     delay(100);
  154.     digitalWrite(ledVCC, LOW);
  155.  
  156.     delay(200);
  157.  
  158.    
  159.     Serial.print(F("Status : "));
  160.     Serial.print(WiFi.status());
  161.     Serial.println(F(""));
  162.   }
  163.    digitalWrite(ledVCC, HIGH);
  164.    g_server.begin();
  165.    Serial.println();
  166.    Serial.println(F("ESPBrowser initialized"));
  167.    Serial.print(F("IP Address: "));
  168.    Serial.println(WiFi.localIP());
  169.   //Init WiFi server
  170.    g_server.begin();
  171.    
  172. }
  173. void loop(){
  174.   g_mux[0] = readMux(0);
  175.   g_mux[1] = readMux(1);
  176.  
  177.   int __value = g_mux[0];
  178.  
  179.  if(__value<g_threshold && !g_power) {
  180.     _power_on();
  181.   }else {
  182.     if(__value > g_threshold && g_power) {
  183.       _power_off();
  184.     }
  185.   }
  186.  
  187.  WiFiClient client = g_server.available();
  188.  if (!client) {
  189.   delay(250);
  190.   return;
  191.  }
  192.  // Wait until the client sends some data
  193.  //Serial.println("new client");
  194.  for(int i=0;i<100 && !client.available();i++){
  195.    delay(1);
  196.  }
  197.  
  198.  
  199.  if(client.available()){
  200.    // Read the first line of the request
  201.    String request = client.readStringUntil('r');
  202.    Serial.println(request);
  203.    client.flush();
  204.    handleRequest(client, request);
  205.    
  206.  }
  207. }
  208. void handleRequest(WiFiClient &client, String request){/* function handleRequest */
  209. ////Handle web client request
  210. if (request.indexOf("/light1on") > 0)  {
  211.    digitalWrite(ledVCC, LOW);
  212.  }
  213. if (request.indexOf("/light1off") > 0)  {
  214.    digitalWrite(ledVCC, HIGH);
  215.  }
  216.  #define SETPARAM_URL "/setparam/"
  217.  int __th = request.indexOf(SETPARAM_URL);
  218. if (__th > 0)  {
  219.    __th += String(SETPARAM_URL).length();
  220.    int __th2 = request.indexOf(" HTTP/");
  221.    String __ps = request.substring(__th, __th2);
  222.    char __buf[1024];
  223.    __ps.toCharArray(__buf, sizeof(__buf));
  224.    char *__p = __buf;
  225.    char *__str;
  226.    int __i = 0;
  227.    String __params[2] = {"", ""};
  228.    while ((__str = strtok_r(__p, ":", &__p)) != null) // delimiter is the semicolon
  229.    {
  230.      Serial.println(__str);
  231.      __params[__i] =__str;
  232.      __i++;
  233.    }  
  234.    if(__params[0] == "TL") {
  235.       g_threshold_low = __params[1].toInt();
  236.       EEPROM.put(EEPROM_TL, g_threshold_low);
  237.    } else if(__params[0] == "TH") {
  238.       g_threshold_high = __params[1].toInt();
  239.       EEPROM.put(EEPROM_TL, g_threshold_high);
  240.    } else if(__params[0] == "T") {
  241.       g_threshold = __params[1].toInt();
  242.       Serial.print("g_threshold changed to [");
  243.       Serial.print(g_threshold);
  244.       Serial.print("]rn");
  245.    
  246.    } else {
  247.     Serial.print("Bad param:[");
  248.     Serial.print(__params[0]);
  249.     Serial.print("]rn");
  250.    
  251.    }
  252.    //digitalWrite(ledVCC, HIGH);
  253.  }
  254.  
  255. if (request.indexOf("/mux") > 0)  {
  256.    //digitalWrite(ledVCC, HIGH);
  257.    client.println("{"mux0": "+String(g_mux[0])+", "
  258.                   " "mux1": "+String(g_mux[1])+", "
  259.                   " "var0": "+String(g_threshold_low)+", "
  260.                   " "var1": "+String(g_threshold_high)+", "
  261.                   " "var2": "+String(g_threshold)+", "
  262.                   " "var3": "+String(g_power)+", "
  263.                   " "var4": "+String(millis())+
  264.                   "}");
  265.    return;
  266.  }
  267.  webpage(client);//Return webpage
  268. }
  269. void webpage(WiFiClient client){/* function webpage */
  270. ////Send wbepage to client
  271.  client.println("HTTP/1.1 200 OK");
  272.  client.println("Content-Type: text/html");
  273.  client.println(""); //  do not forget this one
  274.  client.println("<!DOCTYPE HTML>");
  275.  client.println("<html>");
  276.  client.println("<head>");
  277.  client.println("<meta name='apple-mobile-web-app-capable' content='yes' />");
  278.  client.println("<meta name='apple-mobile-web-app-status-bar-style' content='black-translucent' />");
  279.  client.println("</head>");
  280.  client.println("<body bgcolor = '#ddd'>");
  281.  client.println("<hr/><hr>");
  282.  client.println("<h1 style='color : #333;'><center> "+g_nom+" Device Control </center></h1>");
  283.  client.println("<hr/><hr>");
  284.  client.println("<br><br>");
  285.  client.println("<br><br>");
  286.  client.println("<center>");
  287.  client.println("<table cellspacing="0" border="1">");
  288.  client.println("<tr><td>Uptime</td><td width="100"><span id="ts"></span>");
  289.  client.println("<tr><td>Lum</td><td width="100"><span id="mux_0"></span>");
  290.  client.println("<tr><td>Mux 1</td><td width="100"><span id="mux_1"></span>");
  291.  client.println("<tr><td>Threshold L</td><td width="100"><span id="threshold_low"></span>");
  292.  client.println("<tr><td>Threshold H</td><td width="100"><span id="threshold_high"></span>");
  293.  client.println("<tr><td>Threshold</td><td width="100"><span id="threshold"></span>");
  294.  client.println("<tr><td>Power</td><td width="100"><span id="power"></span>");
  295.  client.println("</table></center>");
  296.  client.println("<br><br>");
  297.  client.println("<center>");  
  298.  client.println("Set param<input id="param_id" value="T"></input> = <input id="param_val" value="0"></input>&nbsp;<button onclick="changeParam()">OK</button>");
  299.  client.println("</center>");
  300.  
  301.  client.println("<br><br>");
  302.  client.println("<br><br>");    
  303.  
  304.   client.println("<script type="text/javascript">rn"
  305.     "httpRequest = new XMLHttpRequest();rn"
  306.     "function muxResult() {rn"
  307.     "if (httpRequest.readyState === XMLHttpRequest.DONE) {rn"
  308.     "      if (httpRequest.status === 200) {rn"
  309.     "        var data = JSON.parse(httpRequest.responseText);rn"
  310.     "        document.getElementById("ts").innerHTML = parseInt(data.var4/1000) + ' s';rn"
  311.     "        document.getElementById("mux_0").innerHTML = data.mux0;rn"
  312.     "        document.getElementById("mux_1").innerHTML = data.mux1;rn"
  313.     "        document.getElementById("threshold_low").innerHTML = data.var0;rn"
  314.     "        document.getElementById("threshold_high").innerHTML = data.var1;rn"
  315.     "        document.getElementById("threshold").innerHTML = data.var2;rn"
  316.     "        document.getElementById("power").innerHTML = data.var3;rn"
  317.     "        console.log(data);rn"
  318.     "      } else {rn"
  319.     "        console.log('data read error');rn"
  320.     "      }rn"
  321.     "    }rn"
  322.     "}rn"
  323.     "function readMux() {rn"
  324.     "httpRequest.open('GET', '/mux', true);rn"
  325.     "httpRequest.send();rn"
  326.     "httpRequest.onreadystatechange = muxResult;rn"
  327.     "}rn"
  328.     "setInterval(readMux, 1000);rn"
  329.     "function changeParam() {rn"
  330.       "var httpRequestParam = new XMLHttpRequest();rn"
  331.       "var _params = document.getElementById('param_id').value + ':' + document.getElementById('param_val').value;rn"
  332.       "httpRequestParam.open('GET', '/setparam/'+_params, true);rn"
  333.       "httpRequestParam.send();rn"
  334.     "}rn"
  335.     "</script>");
  336.   client.println("</body></html>");
  337.   client.println();
  338.   delay(1);
  339. }
Parsed in 0.098 seconds