Sari la conținut
ELFORUM - Forumul electronistilor

Ceas RGB WIFI


Kreator

Postări Recomandate

Am mai bibilit codul de la ceas, in sensul ca l-am facut sa caute intre doua retele, si am scapat de o librarie pentru temperatura, iar acum afiseaza temperaturile negative in mod corect.

Concret: la pornire, ceasul incearca sa se conecteze la ssid1 (la mine este reteaua wifi de la router), daca nu reuseste incearca sa se conecteze la ssid2 (in cazul meu, hotspot de pe telefonul mobil). Daca nu reuseste nici in acest caz, atunci ceasul porneste "offline". Conectarea la ssid1 este semnalizata prin aprinderea unui pct in stanga sus a ecranului, ial la ssid2 prin aprinderea unui pct in stanga jos a ecreanului, In mod offline - ecranul e fara puncte.:scarpin

Daca a pornit neconectat sau a pierdut reteaua si intre timp o retea din cele doua devine disponibila, atunci el se conecteaza la aceasta (cautarea se face din 10 in 10 secunde comutand intre cele doua ssid).

Am ales aceasta modalitate deoarece ceasul va sta afara, pe terasa din curte unde de obicei nu "bate" wifi-ul, si atunci, daca vreau sa ma conectez la el pornesc hotspot-ul de pe mobil.

Conectarea la modul am facut-o  alocand static un IP functie de MAC si apoi forward IP:port pt conexiunea prin router, iar daca conexiunea e facuta cu hotspot, adresa de IP alocata ESP-ului se poate afla vizualizand detaliile dispozitivelor conectate la hotspot.

Acum acest ceas functioneaza exact asa cum am vrut eu (chiar si cu delay-uri) :limb:

L.E.

Se poate implementa si un update de ora de la un server UTC dar nu e cazul, precizia DS3231 este foarte buna, la modulele folosite sub un minut pe an. (dar la cat s-au scumpit modulele DS3221 - mai scumpe decat un ESP - aceasta varianta devine de luat in seama!!!:nas:

 

// CEAS RGB - varianta cu doua retele
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <Wire.h>
#include <Adafruit_NeoPixel.h>
#include <OneWire.h>

// Pinul conectat la DS18b20
OneWire ds(D6);

// Pinul conectat INPUT Data display
#define LED_PIN D7

// Nr de leduri al display-ului
#define LED_COUNT 256

// Intensitatea, 0 {min) to 255 {max)
// #define BRIGHTNESS 5 // Set BRIGHTNESS to about 1/5 {max = 255)

// DS3231_I2C_ADDRESS
#define DS3231_I2C_ADDRESS 0x68
#define EEPROM_I2C_ADDRESS 0x57

// Declare our NeoPixel strip object:
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);

// Replace with your network credentials
const char* ssid1 = "*******"; //reteaua WiFi1 la care se conecteaza
const char* password1 = "*******"; // parola pt conectare la retea WiFi1
const char* ssid2 = "*******"; //reteaua WiFi2 la care se conecteaza
const char* password2 = "*******"; // parola pt conectare la retea WiFi2

String sliderValue1 = "128"; // valoare initiala pt slider1
String sliderValue2 = "128"; // valoare initiala pt slider2
String sliderValue3 = "128"; // valoare initiala pt slider3
String sliderValue4 = "5";   // valoare initiala pt slider4

String ora_new = "20";       // valoare initiala pt ora_new
String minut_new = "05";     // valoare initiala pt minut_new
String ora_change = "0";     // valoare initiala pt ora_change (daca se schimba ora trece in 1)
String minut_change = "0";   // valoare initiala pt minut_change (daca se schimba minut trece in 1)

String sliderValue1_new = "0"; 
String sliderValue2_new = "0"; 
String sliderValue3_new = "0"; 
String sliderValue4_new = "0"; 

String sliderValue1_change = "0";   // valoare initiala pt sliderValue1_change (daca se schimba ora trece in 1)
String sliderValue2_change = "0";   // valoare initiala pt sliderValue2_change (daca se schimba minut trece in 1)
String sliderValue3_change = "0";   // valoare initiala pt sliderValue3_change (daca se schimba minut trece in 1)
String sliderValue4_change = "0";   // valoare initiala pt sliderValue4_change (daca se schimba minut trece in 1)

String ora_val = "20";       // valoare initiala pt ora
String minut_val = "14";     // valoare initiala pt minut


const char* PARAM_INPUT1 = "value1"; //valoare folosita pentru cautarea valorii lui slider1 
                                     // vezi in   xhr.open("GET", "/slider?value1="+sliderValue1, true);
const char* PARAM_INPUT1_CHANGE = "value1_change"; //valoare folosita pentru alerta schimbarii slider1
                                     // vezi in   xhr.open("GET", "/slider?value1_change="+sliderValue1, true);                                     
const char* PARAM_INPUT2 = "value2"; //valoare folosita pentru cautarea valorii lui slider2
                                     // vezi in   xhr.open("GET", "/slider?value2="+sliderValue2, true);
const char* PARAM_INPUT2_CHANGE = "value2_change"; //valoare folosita pentru cautarea valorii lui slider4
                                     // vezi in   xhr.open("GET", "/slider?value2_change="+sliderValue2, true);
const char* PARAM_INPUT3 = "value3"; //valoare folosita pentru cautarea valorii lui slider3
                                     // vezi in   xhr.open("GET", "/slider?value3="+sliderValue3, true);
const char* PARAM_INPUT3_CHANGE = "value3_change"; //valoare folosita pentru cautarea valorii lui slider4
                                     // vezi in   xxhr.open("GET", "/slider?value3_change="+sliderValue3, true); 
const char* PARAM_INPUT4 = "value4"; //valoare folosita pentru cautarea valorii lui slider4
                                     // vezi in   xhr.open("GET", "/slider?value4="+sliderValue4, true);
const char* PARAM_INPUT4_CHANGE = "value4_change"; //valoare folosita pentru cautarea valorii lui slider4
                                     // vezi in   xhr.open("GET", "/slider?value4_change="+sliderValue4, true);                                    

const char* PARAM_INPUT_ORA = "value_ora"; //valoare folosita pentru cautarea valorii ora
                                     // vezi in /xhr.open("GET", "/slider?value_ora="+ora_new, true);
const char* PARAM_INPUT_MINUT = "value_minut"; //valoare folosita pentru cautarea valorii minut
                                     // vezi in  xhr.open("GET", "/slider?value_minut="+minut_new, true);

// Create AsyncWebServer object on port 80
AsyncWebServer server(80);
AsyncWebSocket ws("/ws");

byte rosu = 100, verde = 0, albastru = 50, BRIGHTNESS = 5;
byte red, green, blue, umin, zmin, uhour, zhour;
byte dsp_ora [32], dsp_th [32], dsp_tot[96];
byte ma[8], sa[8], za[8], uua[8], utmp[8], ztmp[8], stmp[8], ltmp[8], rtmp[8], mtmp[8];
byte dsp[32], dsp_tmp[32];
byte second, minute, hour, dp;
byte th0, th1, tsgn, kt=0, k, k_con = 0;
byte s0, s1, m0, m1, h0, h1;
byte array_disp[256];
float t_read; 
int xxx;
byte addr_DS[8];  //to hold 64-bit ROM Codes of DS18B20
byte data_temperature[9];   //buffer to hold data coming from Scratchpad memory of DS18B20
byte dsRes[] = {0x00, 0x00, 0x7F};//, 0x1F(0 R1 R0 11111)/0x3F/0x5F/0x7F for 9-10-11-12-bit Resolution
byte x = 0, wifi_en = 0, wifi_prim = 0, fr_temp, t_sec = 0, t_secp = 0, wifi_id = 0, wifi_stat = 0;
unsigned int t_rea, int_temp;

// SET resolution of DS18B20
const byte resolution = 9; // 9-10-11-12-bit Resolution

// Set LED GPIO
const int ledPin1 = LED_BUILTIN;

// pin atasat la INT RTC
const byte interruptPin = D5;

// litere si cifre
const byte alfabet [17] [7]=  {{126, 255, 195, 195, 255, 126,   0},      //0
                               {  1, 255, 255,  97,  32,   0,   0},      //1
                               { 99, 243, 219, 207, 199,  99,   0},      //2
                               { 102,255, 219, 219, 195, 102,   0},      //3
                               {  5, 255, 255, 101,  60  ,28,   0},      //4
                               { 78, 223, 211, 211, 251, 250,   0},      //5
                               { 78, 223, 211, 211, 255, 126,   0},      //6
                               {224, 240, 223, 207, 192, 192,   0},      //7
                               {110, 255, 219, 219, 255, 110,   0},      //8
                               {126, 255, 203, 203, 251, 114,   0},      //9
                               {  0,   0,   0,   0,   0,   0,   0},      //blank
                               {102, 102,   0,   0,   0,   0,   0},      //dp
                               {102, 195, 195, 195, 255, 126,   0},      //C
                               {112, 248, 136, 248, 112,   0,   0},      //`
                               {110, 255, 211, 211, 255, 255,   0},      //B
                               { 48,  48,  24,  63,  63,   0,   0},      //r
                               { 24,  24,  24,   0,   0,   0,   0},};    //-



// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val) {
     return( (val/10*16) + (val%10) );
}

// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val) {
     return( (val/16*10) + (val%16) );
}

byte readEEPROM(int deviceaddress, unsigned int eeaddress ) {
  byte rdata = 0xFF;
  Wire.beginTransmission(deviceaddress);
  Wire.write((int)(eeaddress >> 8));      //writes the MSB
  Wire.write((int)(eeaddress & 0xFF));    //writes the LSB
  Wire.endTransmission();
  delay(10);
  Wire.requestFrom(deviceaddress,1);
  if (Wire.available()) 
    rdata = Wire.read();
  return rdata;
  }
  
// incepe serverul html
const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML><html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>CEAS RGB</title>
<style>
html {
    font-family: Arial, Helvetica, sans-serif;
    display: inline-block;
    text-align: center;
  }
  h1 {
    font-size: 1.8rem;
    color: white;
  }
  p {
    font-size: 1.4rem;
  }
  .topnav {
    overflow: hidden;
    background-color: #034078;
  }
  body {
    margin: 0;
  }
  .content {
    padding: 30px;
  }
  .card-grid {
    max-width: 500px;
    margin: 0 auto;
    display: grid;
    grid-gap: 1rem;
    grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  }
  .card {
    background-color: white;
    box-shadow: 2px 2px 12px 1px rgba(140,140,140,.5);
  }
  }
  .card-title {
    font-size: 1.2rem;
    font-weight: bold;
    color: #034078
  }
  .state {
    font-size: 1.2rem;
    color:#1282A2;
  }
  .slider {
    -webkit-appearance: none;
    margin: 0 auto;
    width: 90%;
    height: 15px;
    border-radius: 10px;
    background: #C0C0C0;
    outline: none;
  }
  .slider::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 30px;
    height: 30px;
    border-radius: 50 ;
    background: #034078;
    cursor: pointer;
  }
  .switch {
    padding-left: 5;
  }
    
</style>
</head>
<body>
    <div class="topnav">
        <h1><span style="color:white">CEAS  </span><span style="color:#ff3300">R</span><span style="color:#33cc33">G</span><span style="color:#3399ff">B</span></h1>
    </div>
    <p> </p>
    <span class="inlineinput"><input type="number" onchange="update_ora(this)" id="ora" value="%VAL_ORA%" style='display: inline; width: 120px; font-size:4rem ; text-align: center; 
    color:#ff3300;font-weight: bold; box-shadow: 2px 2px 12px 1px rgba(140,140,140,.5); class="slider"'/></span>
    <span style='display: inline; font-size: 4rem ; text-align: center; color:#ff3300 ; font-weight: bold;'>:</span>
    <span class="inlineinput"> <input type="number" onchange="update_minut(this)" id="minut" value="%VAL_MINUT%"  style='display: inline; width: 120px; font-size:4rem ; text-align: center; 
    color:#ff3300; font-weight: bold;box-shadow: 2px 2px 12px 1px rgba(140,140,140,.5); class="slider"'/></span>
    

    <div class="content">
        <div class="card-grid">
            <div class="card">
                <p class="switch"> <input type="range" onchange="Slider1_change(this)" oninput="updateSlider1(this)" id="Sliderx1" min="0" max="255" value="%SLIDERVALUE1%" step="1" class="slider"></p>
                <p class="state"><span style='color:#ff0000; font-weight: bold;'>ROSU=<span id="textSlider1Value">%SLIDERVALUE1%</span></p>
            </div>
            <div class="card">
                <p class="switch"> <input type="range" onchange="Slider2_change(this)" oninput="updateSlider2(this)" id="Sliderx2" min="0" max="255" value="%SLIDERVALUE2%" step="1" class="slider"></p>
                <p class="state"><span style='color:#33cc33; font-weight: bold;'>VERDE=<span id="textSlider2Value">%SLIDERVALUE2%</span></p>
            </div>
            <div class="card">
                <p class="switch"> <input type="range" onchange="Slider3_change(this)" oninput="updateSlider3(this)" id="Sliderx3" min="0" max="255" value="%SLIDERVALUE3%" step="1" class="slider"></p>
                <p class="state"><span style='color:#0000ff; font-weight: bold;'>ALBASTRU=<span id="textSlider3Value">%SLIDERVALUE3%</span></p>
            </div>
            <div class="card">
                <p class="switch"> <input type="range" onchange="Slider4_change(this)" oninput="updateSlider4(this)" id="Sliderx4" min="1" max="255" value="%SLIDERVALUE4%" step="1" class="slider"></p>
                <p class="state"><span style='color: #797979; font-weight: bold;'>STRALUCIRE=<span id="textSlider4Value">%SLIDERVALUE4%</span></p>
            </div>
        </div>
    </div>
 
<script>


function updateSlider1(element) {
  var sliderValue1 = document.getElementById("Sliderx1").value;
  document.getElementById("textSlider1Value").innerHTML = sliderValue1;
  console.log(sliderValue1);
  var xhr = new XMLHttpRequest();
  xhr.open("GET", "/slider?value1="+sliderValue1, true);
  xhr.send();
}

function Slider1_change(element) {
  var sliderValue1 = document.getElementById("Sliderx1").value;
  document.getElementById("textSlider1Value").innerHTML = sliderValue1;
  console.log(sliderValue1);
  var xhr = new XMLHttpRequest();
  xhr.open("GET", "/slider?value1_change="+sliderValue1, true);
  xhr.send();
}

function updateSlider2(element) {
  var sliderValue2 = document.getElementById("Sliderx2").value;
  document.getElementById("textSlider2Value").innerHTML = sliderValue2;
  console.log(sliderValue2);
  var xhr = new XMLHttpRequest();
  xhr.open("GET", "/slider?value2="+sliderValue2, true);
  xhr.send();
}

function Slider2_change(element) {
  var sliderValue2 = document.getElementById("Sliderx2").value;
  document.getElementById("textSlider2Value").innerHTML = sliderValue2;
  console.log(sliderValue2);
  var xhr = new XMLHttpRequest();
  xhr.open("GET", "/slider?value2_change="+sliderValue2, true);
  xhr.send();
}


function updateSlider3(element) {
  var sliderValue3 = document.getElementById("Sliderx3").value;
  document.getElementById("textSlider3Value").innerHTML = sliderValue3;
  console.log(sliderValue3);
  var xhr = new XMLHttpRequest();
  xhr.open("GET", "/slider?value3="+sliderValue3, true);
  xhr.send();
}

function Slider3_change(element) {
  var sliderValue3 = document.getElementById("Sliderx3").value;
  document.getElementById("textSlider3Value").innerHTML = sliderValue3;
  console.log(sliderValue3);
  var xhr = new XMLHttpRequest();
  xhr.open("GET", "/slider?value3_change="+sliderValue3, true);
  xhr.send();
}

function updateSlider4(element) {
  var sliderValue4 = document.getElementById("Sliderx4").value;
  document.getElementById("textSlider4Value").innerHTML = sliderValue4;
  console.log(sliderValue4);
  var xhr = new XMLHttpRequest();
  xhr.open("GET", "/slider?value4="+sliderValue4, true);
  xhr.send();
}

function Slider4_change(element) {
  var sliderValue4 = document.getElementById("Sliderx4").value;
  document.getElementById("textSlider4Value").innerHTML = sliderValue4;
  console.log(sliderValue4);
  var xhr = new XMLHttpRequest();
  xhr.open("GET", "/slider?value4_change="+sliderValue4, true);
  xhr.send();
}

function update_ora(element) {
  var ora_new = document.getElementById("ora").value;
  console.log(ora_new);
   var xhr = new XMLHttpRequest();
  xhr.open("GET", "/slider?value_ora="+ora_new, true);
  xhr.send();
}

function update_minut(element) {
  var minut_new = document.getElementById("minut").value;
  console.log(minut_new);
   var xhr = new XMLHttpRequest();
  xhr.open("GET", "/slider?value_minut="+minut_new, true);
  xhr.send();
}

</script>
</body>
</html>
)rawliteral";
// sfarsitul serverului html


// Replaces placeholder with button section in your web page
String processor(const String& var){
  //Serial.println(var);
  if (var == "SLIDERVALUE1"){
    return sliderValue1;
  }
  if (var == "SLIDERVALUE2"){
    return sliderValue2;
  }

  if (var == "SLIDERVALUE3"){
    return sliderValue3;
  }

  if (var == "SLIDERVALUE4"){
    return sliderValue4;
  }

  if (var == "VAL_ORA"){
    return ora_val;
  }
  
  if (var == "VAL_MINUT"){
    return minut_val;
  }

  return String();
}

void setup(){
  // Serial port for debugging purposes
  Serial.begin(115200);
  Wire.begin(D2, D1); 
  ds.reset();
  ds.search(addr_DS);  //collect 64-bit ROM code from sensor (DS1)t

  switch (resolution) {
     case 9:
        dsRes[2] = 0x1F;
        break;
     case 10:
        dsRes[2] = 0x3F;
        break;
     case 11:
        dsRes[2] = 0x5F;
        break;
     case 12:
        dsRes[2] = 0x7F;
        break;
  }


     // init display
     strip.begin();           // INITIALIZE NeoPixel strip object (REQUIRED)
     strip.show();            // Turn OFF all pixels ASAP
     strip.setBrightness(BRIGHTNESS);
     strip.clear();
     strip.show();
     delay(500); 
     /// Square wave is output on the INT/SQW pin
     Wire.beginTransmission(DS3231_I2C_ADDRESS); // transmit to DS3231 I2C_ADDRESS = 0x68
     Wire.write(byte(0x0E));                     // transmit adress memory  
     Wire.write(0);                              // sends 0 = square output to INT pin
     Wire.endTransmission();                     // stop transmitting
     ////
     pinMode(interruptPin, INPUT);
     pinMode(LED_BUILTIN, OUTPUT);
     //setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte dayOfMonth, byte month, byte year)
     //setDS3231time(0, 9, 00, 7, 18, 04, 22);
     
     sliderValue1 = String(readEEPROM(EEPROM_I2C_ADDRESS, 1));
     sliderValue2 = String(readEEPROM(EEPROM_I2C_ADDRESS, 2));
     sliderValue3 = String(readEEPROM(EEPROM_I2C_ADDRESS, 3));
     sliderValue4 = String(readEEPROM(EEPROM_I2C_ADDRESS, 4));
     
  // Connect to Wi-Fi
     WiFi.setSleepMode(WIFI_NONE_SLEEP);
     WiFi.mode(WIFI_STA);
     // Se incearca conectarea la prima retea WiFi
     WiFi.begin(ssid1, password1);
     while (WiFi.status() != WL_CONNECTED) {
           delay(1000);
           Serial.print("Se incearca conectarea la reteaua ");Serial.print(ssid1);
           Serial.print(". Incercarea nr. ");Serial.println(k_con + 1);
           k_con = k_con + 1;
           if (k_con == 10) {
               Serial.print("WiFi.status() = ");Serial.println(WiFi.status());
               Serial.print("WL_CONNECTED = ");Serial.println(WL_CONNECTED);
               Serial.print("Nu s-a conectat la ");Serial.println(ssid1);
               wifi_en = 0;
               wifi_stat = 0;
               break;
           }
     }
     
     if (WiFi.status() == WL_CONNECTED) {
         Serial.print("WiFi.status() = ");Serial.println(WiFi.status());
         Serial.print("WL_CONNECTED = ");Serial.println(WL_CONNECTED);
         Serial.print("Conectat la ");Serial.print(ssid1);
         Serial.print(" din "); Serial.print(k_con);Serial.println(" incercari.");
         wifi_en = 128;
         wifi_stat = 128;
     }
     
     // Se incearca conectarea la a doua retea WiFi
     if (wifi_en == 0) {
         k_con = 0;
         WiFi.begin(ssid2, password2);
         while (WiFi.status() != WL_CONNECTED) {
               delay(1000);
               Serial.print("Se incearca conectarea la reteaua ");Serial.print(ssid2);
               Serial.print(". Incercarea nr. ");Serial.println(k_con + 1);
               k_con = k_con + 1;
               if (k_con == 10) {
                   Serial.print("WiFi.status() = ");Serial.println(WiFi.status());
                   Serial.print("WL_CONNECTED = ");Serial.println(WL_CONNECTED);
                   Serial.print("Nu s-a conectat la ");Serial.println(ssid2);
                   wifi_en = 0;
                   wifi_stat = 0;
                   break;
               }
         }      
         if (WiFi.status() == WL_CONNECTED) {
            Serial.print("WiFi.status() = ");Serial.println(WiFi.status());
            Serial.print("WL_CONNECTED = ");Serial.println(WL_CONNECTED);
            Serial.print("Conectat la ");Serial.print(ssid2);
            Serial.print(" din "); Serial.print(k_con);Serial.println(" incercari.");
            wifi_en = 128;
            wifi_stat = 1;
        }
     }
     // daca nu s-a reusit conectarea la nici o retea, se porneste in mod offline 
     send_web_page();
}

void conv_temp_without_adress(){
  ds.write(0x4E);
  ds.write_bytes(dsRes, 3);  //set resolution bit
  //----------------------------------------------
  ds.reset();       //bring 1-Wire into idle state
  ds.write(0xCC); //slect with DS-1 with address addr_DS
  ds.write(0x44);    //conversion command
}
void read_temp_without_adress(){
  ds.reset();
  ds.write(0xCC);  //selectimg the desired DS18B20
  ds.write(0xBE);    //Function command to read Scratchpad Memory (9Byte)
  ds.read_bytes(data_temperature, 9); //data comes from DS and are saved into buffer data[8]
  //------------------------------------------------------------
  unsigned int rawTemp = (int)(data_temperature[1] << 4); //upper 4-bit of integer part of temperature
  byte n = data_temperature[0];   //n = lower 4-bit of integer part and 4-bit fractional part of temperature
  data_temperature[0] = data_temperature[0] >> 4; //lower 4-bit integer part of temperature
  rawTemp = rawTemp | (int)data_temperature[0];  //8-bit (including sign bit) integer part of temperature
  t_read = (float)rawTemp + 0.5 * bitRead(n, 3) + 0.25 * bitRead(n, 2) + 0.125
            * bitRead(n, 1) + 0.0625 * bitRead(n, 0); //temp in float format

     t_rea = t_read;
     fr_temp = t_rea & 0x0F;


     //Serial.print("t_rea = ");Serial.println(t_rea);
     //Serial.print("fr_temp = ");Serial.println(fr_temp);
     
     if (bitRead(t_rea, 15)) { // daca e negativ
        int_temp = t_rea >> 4;
        int_temp = int_temp | 0xF000;
        for(int i = 0; i <= 15; i++) {
           bitWrite(int_temp, i, !bitRead(int_temp, i));
        } 
        tsgn = 16;
        if (!fr_temp ) {
             int_temp = int_temp + 1;
        }
     }
     else {
        int_temp = t_rea;
        tsgn = 10;
     }
     //Serial.print("int_temp = ");Serial.println(int_temp);
     th0 = int_temp / 10;     
     th1 = int_temp % 10;
}
void send_web_page() {
  // Print ESP Local IP Address
  Serial.println(WiFi.localIP());

  // Route for root / web page
  server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/html", index_html, processor);
  });

  // Send a GET request to <ESP_IP>/slider?value=<inputMessage>
  server.on("/slider", HTTP_GET, [] (AsyncWebServerRequest *request) {
    String inputMessageslider1;
    String inputMessageslider2;
    String inputMessageslider3;
    String inputMessageslider4;
    String inputMessageora;
    String inputMessageminut;
    
    // GET input1 value on <ESP_IP>/slider1?value1=<inputMessageslider1>
    if (request->hasParam(PARAM_INPUT1)) {
      inputMessageslider1 = request->getParam(PARAM_INPUT1)->value();
      sliderValue1 = inputMessageslider1;
    }
    else {
      inputMessageslider1 = "No message sent";
    }
    if (request->hasParam(PARAM_INPUT1_CHANGE)) {
      sliderValue1_change = "1";
      inputMessageslider1 = request->getParam(PARAM_INPUT1_CHANGE)->value();
      sliderValue1_new = inputMessageslider1;
    }
    else {
      inputMessageslider1 = "No message sent";
    }
    
    // GET input1 value on <ESP_IP>/slider2?value2=<inputMessageslider2>
    if (request->hasParam(PARAM_INPUT2)) {
      inputMessageslider2 = request->getParam(PARAM_INPUT2)->value();
      sliderValue2 = inputMessageslider2;
    }
    else {
      inputMessageslider2 = "No message sent";
    }
    if (request->hasParam(PARAM_INPUT2_CHANGE)) {
      sliderValue2_change = "1";
      inputMessageslider2 = request->getParam(PARAM_INPUT2_CHANGE)->value();
      sliderValue2_new = inputMessageslider2;
    }
    else {
      inputMessageslider2 = "No message sent";
    }
    
 
    // GET input1 value on <ESP_IP>/slider3?value3=<inputMessageslider3>
    if (request->hasParam(PARAM_INPUT3)) {
      inputMessageslider3 = request->getParam(PARAM_INPUT3)->value();
      sliderValue3 = inputMessageslider3;
    }
    else {
      inputMessageslider3 = "No message sent";
    }  
    if (request->hasParam(PARAM_INPUT3_CHANGE)) {
      sliderValue3_change = "1";
      inputMessageslider3 = request->getParam(PARAM_INPUT3_CHANGE)->value();
      sliderValue3_new = inputMessageslider3;
    }
    else {
      inputMessageslider3 = "No message sent";
    } 

    if (request->hasParam(PARAM_INPUT4)) {
      inputMessageslider4 = request->getParam(PARAM_INPUT4)->value();
      sliderValue4 = inputMessageslider4;
     }
    else {
      inputMessageslider4 = "No message sent";
    }

    if (request->hasParam(PARAM_INPUT4_CHANGE)) {
      sliderValue4_change = "1";
      inputMessageslider4 = request->getParam(PARAM_INPUT4_CHANGE)->value();
      sliderValue4_new = inputMessageslider4;
    }
    else {
      inputMessageslider4 = "No message sent";
    }


    // GET input1 value on <ESP_IP>/slider4?value4=<inputMessageslider4>  

    if (request->hasParam(PARAM_INPUT_ORA)) {
      ora_change = "1";
      inputMessageora = request->getParam(PARAM_INPUT_ORA)->value();
      ora_new =  inputMessageora;
    }
    else {
      inputMessageora = "No message sent";
    }

    if (request->hasParam(PARAM_INPUT_MINUT)) {
      minut_change = "1";
      inputMessageminut = request->getParam(PARAM_INPUT_MINUT)->value();
      minut_new = inputMessageminut;
    }
    else {
      inputMessageminut = "No message sent";
    }


    
    request->send(200, "text/plain", "OK");
  });
  
  // Start server
  server.begin();
  
}

void I2Cwrite(byte ID, byte adress, byte value) {
      Wire.beginTransmission(ID); //select ID device
      Wire.write(adress); // move pointer to adress address 
      Wire.write(value); // sends value
      Wire.endTransmission();
     }

void I2CRead(uint8_t ID, uint8_t adress, byte *xxx) {
     Wire.beginTransmission(ID);
     Wire.write(adress); // set DS3231 register pointer to 00h
     Wire.endTransmission();
     Wire.requestFrom((uint8_t) ID, (uint8_t) 1);
     // request seven bytes of data from DS3231 starting from register 00h
     *xxx = Wire.read();
}

void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte dayOfMonth, byte month, byte year) {
     // sets time and date data to DS3231
     Wire.beginTransmission(DS3231_I2C_ADDRESS);
     Wire.write(0); // set next input to start at the seconds register
     Wire.write(decToBcd(second)); // set seconds
     Wire.write(decToBcd(minute)); // set minutes
     Wire.write(decToBcd(hour)); // set hours
     Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday)
     Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31)
     Wire.write(decToBcd(month)); // set month
     Wire.write(decToBcd(year)); // set year (0 to 99)
     Wire.endTransmission();
}
void writeEEPROM(int deviceaddress, unsigned int eeaddress, byte data ) {
     Wire.beginTransmission(deviceaddress);
     Wire.write((int)(eeaddress >> 8));      //writes the MSB
     Wire.write((int)(eeaddress & 0xFF));    //writes the LSB
     Wire.write(data);
     delay(10);
     Wire.endTransmission();
     delay(10);
}
  
void read_RTC() {
     Wire.beginTransmission(DS3231_I2C_ADDRESS);
     Wire.write(0); // set DS3231 register pointer to 00h
     Wire.endTransmission();
     Wire.requestFrom(DS3231_I2C_ADDRESS, 3);
     // request seven bytes of data from DS3231 starting from register 00h
     second = bcdToDec(Wire.read() & 0x7f);
     s0 = second /10;
     s1 = second %10;
     minute = bcdToDec(Wire.read());
     m0 = minute/10;
     m1 = minute %10;
     hour = bcdToDec(Wire.read() & 0x3f);
     h0 = hour/10;
     h1 = hour %10;
    
    if (hour <= 9) {
        ora_val = "0" + String(hour);
    }    
    else {
       ora_val = String(hour);
    }

    if (minute <= 9) {
        minut_val = "0" + String(minute);
    }    
    else {
        minut_val = String(minute);
    }
}



//Prepare hour data
 void prepare_to_show_hour(byte m, byte s_, byte z_, byte u_i, byte dp1){
     
     for(int i = 0; i <= 31; i++) {
         dsp_ora [i] = 0;
     }
        
     for(int i=0; i<=2; i++) { 
        if (dp ==1) {
           dsp_ora [i+14] = alfabet [11] [i];
        }
        else {
           dsp_ora [i+14] = alfabet [10] [i];
        }
     }
      
      for(int i = 0; i <= 5; i++) { 
        dsp_ora [i] = alfabet [u_i] [i];
        dsp_ora [i+7] = alfabet [z_] [i];
        dsp_ora [i+17] = alfabet [s_] [i];
        dsp_ora [i+24] = alfabet [m] [i];
      }
      
      dsp_ora [31] = wifi_id;
      
      for(int i = 0; i <= 31; i++) {
        dsp_tot[i] = dsp_ora [i] ;
        dsp_tot[i+64] = dsp_ora [i] ;
        }

     for(int i=0; i<=255; i++) { // For each pixel in strip...
         k = i % 16;
         byte j = i/8;
         byte k1 = 8*(j+1)-(i % 8) - 1;
         int bt = bitRead(dsp_ora [j], i%8);
         array_disp[i] = bt;
     }
 }
 
// Prepare Temperature data
void prepare_to_show_temp(byte tsgn, byte iz_, byte iu){
     for(int i = 0; i <= 31; i++) {
         dsp_th [i] = 0;
     }
     for(int i = 0; i <= 5; i++) { 
        dsp_th [i+1] = alfabet [12] [i];
        dsp_th [i+8] = alfabet [13] [i];
        dsp_th [i+14] = alfabet [iu] [i];
        dsp_th [i+21] = alfabet [iz_] [i];
        dsp_th [i+28] = alfabet [tsgn] [i];
     }

       
        dsp_th [31] = wifi_id;

     for(int i = 0; i <= 31; i++) {
        dsp_tot[i+32] = dsp_th [i] ;
     }
     for(int i=0; i<=255; i++) { // For each pixel in strip...
         k = i % 16;
         byte j = i/8;
         byte k1 = 8*(j+1)-(i % 8) - 1;
         int bt = bitRead(dsp_th [j], i%8);
         array_disp[i] = bt;
         array_disp[255] =  bitRead(wifi_en, 0);
     }    
}

void tranz_display(byte kk) {
     for(byte i = 0; i <= 31; i++) {
         dsp_tmp[i] = dsp_tot[i+64 - kk];
     }
   
     for(int i=0; i<=255; i++) { // For each pixel in strip...
         k = i % 16;
         byte j = i/8;
         byte k1 = 8*(j+1)-(i % 8) - 1;
         int bt = bitRead(dsp_tmp[j], i%8);
         array_disp[i] = bt;
     }   
}

void read_sliders() {
     rosu = sliderValue1.toInt();
     verde = sliderValue2.toInt();
     albastru = sliderValue3.toInt(); 
     BRIGHTNESS= sliderValue4.toInt();
}

void change_controls(){
    if (ora_change.toInt() == 1) {
       ora_change = "0";
       I2Cwrite(DS3231_I2C_ADDRESS, 2, decToBcd(ora_new.toInt()));
    }

    if (minut_change.toInt() == 1) {
       minut_change = "0";
       I2Cwrite(DS3231_I2C_ADDRESS, 1, decToBcd(minut_new.toInt()));
       I2Cwrite(DS3231_I2C_ADDRESS, 0, 0);
    }
    
    if (sliderValue1_change.toInt() == 1) {
       sliderValue1_change  = "0";
       writeEEPROM(EEPROM_I2C_ADDRESS, 1, sliderValue1_new.toInt()); 
    }
    if (sliderValue2_change.toInt() == 1) {
       sliderValue2_change  = "0";
       writeEEPROM(EEPROM_I2C_ADDRESS, 2, sliderValue2_new.toInt()); 
    }
    if (sliderValue3_change.toInt() == 1) {
       sliderValue3_change  = "0";
       writeEEPROM(EEPROM_I2C_ADDRESS, 3, sliderValue3_new.toInt()); 
    }
 
    if (sliderValue4_change.toInt() == 1) {
       sliderValue4_change  = "0";
       writeEEPROM(EEPROM_I2C_ADDRESS, 4, sliderValue4_new.toInt()); 
    }  
}

void show_data(){ 
     read_sliders();
     strip.setBrightness(BRIGHTNESS);  
     dp = digitalRead(interruptPin);
     digitalWrite(LED_BUILTIN, !dp);   
     for(int i=0; i<=255; i++) { // For each pixel in strip...
         byte k = i % 16;
         byte j = i/8;
         byte k1 = 8*(j+1)-(i % 8) - 1;
         byte bt = array_disp[i]; 
         red = rosu * bt ;
         green =  verde * bt ;
         blue =  albastru * bt ; 
         if (k <=7) {
            strip.setPixelColor(i, red,green,blue);         //  Set pixel's color (in RAM)
         }
         else {
            strip.setPixelColor(k1, red,green,blue);         //  Set pixel's color (in RAM)
         }
     }
     
     strip.show();
}
void show_hour(){
     prepare_to_show_hour(h0, h1, m0, m1, dp);
     prepare_to_show_temp(tsgn, th0, th1);
     tranz_display(0);
     show_data(); 
}
void show_hour_to_temp(){
     dp = 1;
     for(int yy = 0; yy <= 32; yy++) { 
         tranz_display(yy);
         show_data();
         delay(40);                
     }
}

void show_temp(){
     tranz_display(32);
     show_data();
     for(int yy = 0; yy <= 300; yy++) {
        show_data();
        delay(1); 
     }
}

void show_temp_to_hour(){
     for(int yy = 0; yy <= 32; yy++) { 
        tranz_display(yy+32);
        show_data();
        delay(40);                
     }
}

  
void loop() {
  if (dp != digitalRead(interruptPin)) {
      //Serial.println( ESP.getFreeHeap());
    
     if (WiFi.status() == WL_CONNECTED) {
         wifi_en =128;
         if (wifi_prim == 0) {
            Serial.println("ESP is now conected!!!"); 
            Serial.print("IP adress = "); Serial.println(WiFi.localIP()); 
            wifi_prim = 1 ; 
            wifi_id = wifi_stat;          
         }
         
     }
     else {
         wifi_en =0;
         if (wifi_prim == 1) {
            Serial.println("ESP is disconected!!!"); 
            wifi_prim = 0 ; 
            wifi_id = 0;           
         } 
         // daca e deconectat, la 10 sec se comuta intre cele doua retele  
         t_sec =  (second/10) % 2;
         if (t_sec != t_secp) {
            t_secp = t_sec;
            Serial.println("Se schimba reteaua");
            if (t_sec) {
                Serial.print("Se incearca conectarea la reteaua ");Serial.println(ssid1);
                WiFi.begin(ssid1, password1);  
                wifi_stat = 128;         
            }
            else{
                Serial.print("Se incearca conectarea la reteaua ");Serial.println(ssid2);
                WiFi.begin(ssid2, password2); 
                wifi_stat = 1; 
            }
         }
         
     }
     
      
      kt = kt + 1;
      if (kt == 28) {
         kt = 0;
      }      
      dp = digitalRead(interruptPin);
      digitalWrite(LED_BUILTIN, !dp);
      
      if ((kt % 4) == 0) {
         conv_temp_without_adress();
      }
      else {
        if ((kt % 4) == 2) {
            read_temp_without_adress();
        } 
      }
  }
     
    change_controls();
    read_RTC(); 
    show_hour(); 

    if  (dp == 1) {
        if ((second == 5) || (second == 25) || (second == 45)) {
           show_hour_to_temp();
           show_temp();
           show_temp_to_hour();   
        }
    }

      ws.cleanupClients();          
}

 

Editat de Kreator
Link spre comentariu
  • Răspunsuri 46
  • Creat
  • Ultimul Răspuns

Top autori în acest subiect

Top autori în acest subiect

Imagini postate

Vizitator

Si, cand ziceai ca 'gata am terminat', vezi ca exista libraria ESP8266WiFiMulti.
Ai incercat sa faci cam acelasi lucru.
Bafta si felicitari pentru munca depusa.


 

Link spre comentariu

Creează un cont sau autentifică-te pentru a adăuga comentariu

Trebuie să fi un membru pentru a putea lăsa un comentariu.

Creează un cont

Înregistrează-te pentru un nou cont în comunitatea nostră. Este simplu!

Înregistrează un nou cont

Autentificare

Ai deja un cont? Autentifică-te aici.

Autentifică-te acum



×
×
  • Creează nouă...

Informații Importante

Am plasat cookie-uri pe dispozitivul tău pentru a îmbunătății navigarea pe acest site. Poți modifica setările cookie, altfel considerăm că ești de acord să continui.Termeni de Utilizare si Ghidări