Supla Pushover x4 cd

Masz pomysł na funkcjonalność lub koncepcję na rozwój projektu. Opisz wszystko tutaj.
piomar2
Posty: 268
Rejestracja: pt kwie 26, 2019 5:32 pm
Lokalizacja: Bukowice

czw lut 27, 2020 8:55 pm

byniom pisze:
czw lut 27, 2020 8:22 pm
piomar2 a możesz napisać kod który by działał odwrotnie ? czyli jak temperatura na DS spadnie poniżej zadanej ?
Proszę potestować (bin w zał)

Kod: Zaznacz cały

//wifi Dir_DS
//Ustawienie 192.168.4.1
//link wejściowy z dowolnego DS-a w cloudzie
//Config - D3 20 sek
//Działa przy obniżeniu temp

#include <FS.h>       // ---- esp board manager 2.4.2 
#include <WiFiClientSecure.h>
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <WiFiManager.h>     //- https://github.com/tzapu/WiFiManager/tree/0.14 ----------
#include <ArduinoJson.h>     //--https://github.com/bblanchon/ArduinoJson/tree/v5.13.2 ---
#include <Ticker.h>      //for LED status
#include "Pushover.h"
Ticker ticker;

const char* host = "svr.supla.org";
const int httpsPort = 443;

char Supla_server[41];
char D_Link_1[61];
char T_Link_2[2];
char update_username[60];
char update_password[60];

byte mac[6];
String url = "/direct/xx/xxxxxxxxx/xxx";
bool dimm = false;         
unsigned long dimm_milis ;
int C_W_state = HIGH;            
int last_C_W_state = HIGH;       
unsigned long time_last_C_W_change = 0;   
long C_W_delay = 10000;               // config delay 10 seconds  ------------ opóźnienie konfiguracji 10 sekund

bool shouldSaveConfig = false;
bool initialConfig = false;
unsigned long link_delay = 5000; 
#define onboard_led 2    //D4  
#define Config_PIN 0     //D3    // wifi config

int timeout           = 120; // seconds to run for wifi Manager
byte a1=0;
byte b1=0;
float temp1;
int temp2;

void tick() {
  int state = digitalRead(onboard_led);  
  digitalWrite(onboard_led, !state);     
}
void saveConfigCallback () {
  Serial.println("Should save config");
  shouldSaveConfig = true;
}
void ondemandwifiCallback () {
 
  WiFiManagerParameter custom_Supla_server("server", "supla server", Supla_server, 41);
  WiFiManagerParameter custom_D_Link_1("Linka", "Link_z_DS18B20_read", D_Link_1, 61);
  WiFiManagerParameter custom_T_Link_2("Linkb", "Temp_max", T_Link_2, 2);
  WiFiManagerParameter custom_update_username("userkey", "userkey", update_username, 60,"required");
  WiFiManagerParameter custom_update_password("apitoken", "apitoken", update_password, 60,"required");


  
  WiFiManager wifiManager;
  wifiManager.setBreakAfterConfig(true);
  wifiManager.setSaveConfigCallback(saveConfigCallback);
  
  wifiManager.addParameter(&custom_Supla_server);
  wifiManager.addParameter(&custom_D_Link_1);
  wifiManager.addParameter(&custom_T_Link_2);
  wifiManager.addParameter(&custom_update_username);
  wifiManager.addParameter(&custom_update_password);


  wifiManager.setCustomHeadElement("<style>html{ background-color: #01DF3A;}</style><div class='s'><svg version='1.1' id='l' x='0' y='0' viewBox='0 0 200 200' xml:space='preserve'><path d='M59.3,2.5c18.1,0.6,31.8,8,40.2,23.5c3.1,5.7,4.3,11.9,4.1,18.3c-0.1,3.6-0.7,7.1-1.9,10.6c-0.2,0.7-0.1,1.1,0.6,1.5c12.8,7.7,25.5,15.4,38.3,23c2.9,1.7,5.8,3.4,8.7,5.3c1,0.6,1.6,0.6,2.5-0.1c4.5-3.6,9.8-5.3,15.7-5.4c12.5-0.1,22.9,7.9,25.2,19c1.9,9.2-2.9,19.2-11.8,23.9c-8.4,4.5-16.9,4.5-25.5,0.2c-0.7-0.3-1-0.2-1.5,0.3c-4.8,4.9-9.7,9.8-14.5,14.6c-5.3,5.3-10.6,10.7-15.9,16c-1.8,1.8-3.6,3.7-5.4,5.4c-0.7,0.6-0.6,1,0,1.6c3.6,3.4,5.8,7.5,6.2,12.2c0.7,7.7-2.2,14-8.8,18.5c-12.3,8.6-30.3,3.5-35-10.4c-2.8-8.4,0.6-17.7,8.6-22.8c0.9-0.6,1.1-1,0.8-2c-2-6.2-4.4-12.4-6.6-18.6c-6.3-17.6-12.7-35.1-19-52.7c-0.2-0.7-0.5-1-1.4-0.9c-12.5,0.7-23.6-2.6-33-10.4c-8-6.6-12.9-15-14.2-25c-1.5-11.5,1.7-21.9,9.6-30.7C32.5,8.9,42.2,4.2,53.7,2.7c0.7-0.1,1.5-0.2,2.2-0.2C57,2.4,58.2,2.5,59.3,2.5z M76.5,81c0,0.1,0.1,0.3,0.1,0.6c1.6,6.3,3.2,12.6,4.7,18.9c4.5,17.7,8.9,35.5,13.3,53.2c0.2,0.9,0.6,1.1,1.6,0.9c5.4-1.2,10.7-0.8,15.7,1.6c0.8,0.4,1.2,0.3,1.7-0.4c11.2-12.9,22.5-25.7,33.4-38.7c0.5-0.6,0.4-1,0-1.6c-5.6-7.9-6.1-16.1-1.3-24.5c0.5-0.8,0.3-1.1-0.5-1.6c-9.1-4.7-18.1-9.3-27.2-14c-6.8-3.5-13.5-7-20.3-10.5c-0.7-0.4-1.1-0.3-1.6,0.4c-1.3,1.8-2.7,3.5-4.3,5.1c-4.2,4.2-9.1,7.4-14.7,9.7C76.9,80.3,76.4,80.3,76.5,81z M89,42.6c0.1-2.5-0.4-5.4-1.5-8.1C83,23.1,74.2,16.9,61.7,15.8c-10-0.9-18.6,2.4-25.3,9.7c-8.4,9-9.3,22.4-2.2,32.4c6.8,9.6,19.1,14.2,31.4,11.9C79.2,67.1,89,55.9,89,42.6z M102.1,188.6c0.6,0.1,1.5-0.1,2.4-0.2c9.5-1.4,15.3-10.9,11.6-19.2c-2.6-5.9-9.4-9.6-16.8-8.6c-8.3,1.2-14.1,8.9-12.4,16.6C88.2,183.9,94.4,188.6,102.1,188.6z M167.7,88.5c-1,0-2.1,0.1-3.1,0.3c-9,1.7-14.2,10.6-10.8,18.6c2.9,6.8,11.4,10.3,19,7.8c7.1-2.3,11.1-9.1,9.6-15.9C180.9,93,174.8,88.5,167.7,88.5z'/></svg>");

  wifiManager.setMinimumSignalQuality();
    wifiManager.setConfigPortalTimeout(timeout);

    if (!wifiManager.startConfigPortal("Dir_DS")) {
      Serial.println("failed to connect and hit timeout");
      delay(3000);
      ESP.restart();
      delay(5000);
    }
    Serial.println("connected...yeey :)");

    strcpy(Supla_server, custom_Supla_server.getValue());
    strcpy(D_Link_1, custom_D_Link_1.getValue());
    strcpy(T_Link_2, custom_T_Link_2.getValue());
    strcpy(update_username, custom_update_username.getValue());
    strcpy(update_password, custom_update_password.getValue());  
}
ICACHE_RAM_ATTR void no_dimm(){
            dimm_milis = millis() + 30000 ;
                dimm = false; 
}
void setup() {  //------------------------------------------------ Setup ----------------------------------------------

  Serial.begin(115200);

  pinMode(Config_PIN, INPUT_PULLUP);
  pinMode(onboard_led, OUTPUT);

  if (WiFi.SSID()==""){   
    initialConfig = true;
 }

  Serial.println("mounting FS..."); 
  if (SPIFFS.begin()) {
    Serial.println("mounted file system");
    if (SPIFFS.exists("/config.json")) {
      //file exists, reading and loading
      Serial.println("reading config file");
      File configFile = SPIFFS.open("/config.json", "r");
      if (configFile) {
        Serial.println("opened config file");
        size_t size = configFile.size();
        std::unique_ptr<char[]> buf(new char[size]);
        configFile.readBytes(buf.get(), size);
        DynamicJsonBuffer jsonBuffer;         
        JsonObject& json = jsonBuffer.parseObject(buf.get());
        Serial.println(jsonBuffer.size());
        json.printTo(Serial);
        if (json.success()) {
          Serial.println("\nparsed json");
          strcpy(Supla_server, json["Supla_server"]);
          strcpy(D_Link_1, json["D_Link_1"]);
           strcpy(T_Link_2, json["T_Link_2"]);
           strcpy(update_username, json["userkey"]);
          strcpy(update_password, json["apitoken"]);
           
            host = Supla_server;
              url = D_Link_1;
        } else {
          Serial.println("failed to load json config");          
        }
      }
    }
  } else {
    Serial.println("failed to mount FS");
 } 

  WiFi.mode(WIFI_STA);
  dimm_milis = millis() + 30000 ;
  attachInterrupt(Config_PIN, no_dimm, FALLING); 
}

void loop() { 

   if (initialConfig){ondemandwifiCallback();initialConfig = false;}

   int C_W_read = digitalRead(Config_PIN);{    
   if (C_W_read != last_C_W_state) {            
     time_last_C_W_change = millis();    
   }
   if ((millis() - time_last_C_W_change) > C_W_delay) {     
     if (C_W_read != C_W_state) {     
       Serial.println("Triger sate changed");
       C_W_state = C_W_read;       
       if (C_W_state == LOW) {
        ondemandwifiCallback () ;
       }
     }
    }
   last_C_W_state = C_W_read;            
   }

  if (millis() > dimm_milis){ 
    if (dimm == false){
   
    dimm = true ;     
    }    
    dimm_milis = millis() + 30000 ;
     } 
       
    if ((WiFi.status() == WL_CONNECTED) && (millis() > link_delay)){
      direct_Link();
       link_delay = link_delay + 5000;
        if (a1==1 && b1==1){
        Serial.println ("Wysylam powiadomienie"); 
  Pushover po = Pushover(update_password,update_username); 
  po.setDevice("Device");
  po.setMessage("TEMP spadla ponizej ustawionej");
  po.setSound("mechanical");
  po.send();
    b1=0;
        }
    }
  
  if (shouldSaveConfig) {
    Serial.println("saving config");
    
    DynamicJsonBuffer jsonBuffer;
    JsonObject& json = jsonBuffer.createObject();
    json["Supla_server"] = Supla_server;
    json["D_Link_1"] = D_Link_1;
    json["T_Link_2"] = T_Link_2;
    json["userkey"] = update_username;
    json["apitoken"] = update_password;
    
    File configFile = SPIFFS.open("/config.json", "w");
    if (!configFile) {
      Serial.println("failed to open config file for writing");
    }
    json.prettyPrintTo(Serial);
    json.printTo(configFile);
    configFile.close();
    Serial.println("config saved");
   
    shouldSaveConfig = false;
    WiFi.mode(WIFI_STA);
    ticker.detach();
    digitalWrite(onboard_led, HIGH);
    ESP.restart();
      delay(5000); 
 }
  
  if (WiFi.status() != WL_CONNECTED) {
      WiFi_up();
 }
}

void WiFi_up() {
  WiFi.begin(); 
  
  for (int x = 20; x > 0; x--) 
  {
    if (WiFi.status() == WL_CONNECTED) 
    {
      break;                           
    }
    else                                 
    {
      Serial.print(".");                
      delay(500);                      
    }
  }
  if (WiFi.status() == WL_CONNECTED)
  {
    
    Serial.println("");
    Serial.println("Connected");
    Serial.println("Adres IP: ");
    Serial.print(WiFi.localIP());
    Serial.print(" / ");
    Serial.print(WiFi.subnetMask());
    Serial.print(" / ");
    Serial.println(WiFi.gatewayIP());
    long rssi = WiFi.RSSI();
    Serial.print("(RSSI): ");
    Serial.print(rssi);
    Serial.println(" dBm");   
  }
  else    
  {
    Serial.println("");
    Serial.println("connection failed");
     
  }
}
void direct_Link() {
  WiFiClientSecure client;
  Serial.print("connecting to ");
  Serial.println(host);
  if (!client.connect(host, httpsPort)) {
    Serial.println("connection failed ----------------------");
    return;
  }
  Serial.print("requesting URL: ");
  Serial.println(url);
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "User-Agent: ESP8266\r\n" +
               "Connection: close\r\n\r\n");
  Serial.println("request sent");
  while (client.connected()) {
    String line = client.readStringUntil('\n');
    if (line == "\r") {
      Serial.println("headers received");
      break;
    }
  }
   String line = client.readStringUntil('\n');
  if (line.indexOf("true") >0) {
    yield();
  Serial.println("reply was:  ");Serial.println("==========");  
 
  Serial.println(line);

  Serial.println("==========");Serial.println("closing connection");
    DynamicJsonBuffer jsonBuffer;
  JsonObject& root = jsonBuffer.parseObject(line);

  temp1 = root["temperature"]; 
   temp2 = atoi(T_Link_2); 


  //--------------T------------------
 Serial.println (temp1);
Serial.println (temp2);
       if (temp1>temp2){
    a1=0;
   }
  if (temp1<=temp2 && a1==0){  
   Serial.println ("Ponizej TEMP 2"); 
    a1=1;
    b1=1;
    }
 

  } else {Serial.println("failed --------------------");
 } 
}
Załączniki
Zdal_czyt_DS18B20_2.ino.d1_mini.zip
(291.86 KiB) Pobrany 9 razy
piomar2
Posty: 268
Rejestracja: pt kwie 26, 2019 5:32 pm
Lokalizacja: Bukowice

sob lut 29, 2020 1:12 pm

Witajcie!
Rozwijam temat woltomierza viewtopic.php?f=6&t=5210&p=47912&hilit= ... erz#p47912 , którego używam od długiego czasu do pomiaru napięcia akumulatorów w domu (awaryjne zasilanie). Wczoraj zdarzyło się znaczne przekroczenie napięcia ładowania z automatycznej ładowarki (uszkodzenie!?) dlatego połączyłem powiadomienia Pushover o "za niskim i za wysokim napięciem". Program z wemosem daje 2 powiadomienia.

1: Jeżeli napięcie na aku wzrośnie ponad wartość ustawioną w configu (format xx.xx) układ daje powiadomienie 1 raz - ponowne daje dopiero jeśli napięcie zmaleje i ponownie wzrośnie.
2: Jeżeli napięcie na aku zmaleje poniżej ustawionej w configu. Też daje 1 powiadomienie (jeżeli napięcie wzrośnie to da następne)

Program działa (jak poprzednie z odczytem temperatury) czytając link bezpośredni, który musimy utworzyć w woltomierzu w cloudzie .

Jak ktoś chce się pobawić to zachęcam. Uwagi mile widziane. :)

W zał bin

Kod: Zaznacz cały

//wifi Dir_DS
//Ustawienie 192.168.4.1
//link wejściowy z dowolnego DS-a w cloudzie
//Config - D3 20 sek
//Powiadomienie o za niskiej i za wysokiej wartości napięcia


#include <FS.h>       // ---- esp board manager 2.4.2 
#include <WiFiClientSecure.h>
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <WiFiManager.h>     //- https://github.com/tzapu/WiFiManager/tree/0.14 ----------
#include <ArduinoJson.h>     //--https://github.com/bblanchon/ArduinoJson/tree/v5.13.2 ---
#include <Ticker.h>      //for LED status
#include "Pushover.h"
Ticker ticker;

const char* host = "svr.supla.org";
const int httpsPort = 443;

char Supla_server[41];
char D_Link_1[61];
char T_Link_2[5];
char T_Link_3[5];
char update_username[60];
char update_password[60];

byte mac[6];
String url = "/direct/xx/xxxxxxxxx/xxx";
bool dimm = false;         
unsigned long dimm_milis ;
int C_W_state = HIGH;            
int last_C_W_state = HIGH;       
unsigned long time_last_C_W_change = 0;   
long C_W_delay = 10000;               // config delay 10 seconds  ------------ opóźnienie konfiguracji 10 sekund

bool shouldSaveConfig = false;
bool initialConfig = false;
unsigned long link_delay = 5000; 
#define onboard_led 2    //D4  
#define Config_PIN 0     //D3    // wifi config

int timeout           = 120; // seconds to run for wifi Manager
byte a1=0;
byte b1=0;
byte a2=0;
byte b2=0;
float temp1;
float temp2;
float temp3;// = 12.20;

void tick() {
  int state = digitalRead(onboard_led);  
  digitalWrite(onboard_led, !state);     
}
void saveConfigCallback () {
  Serial.println("Should save config");
  shouldSaveConfig = true;
}
void ondemandwifiCallback () {
 
  WiFiManagerParameter custom_Supla_server("server", "supla server", Supla_server, 41);
  WiFiManagerParameter custom_D_Link_1("Linka", "Link_z_DS18B20_read", D_Link_1, 61);
  WiFiManagerParameter custom_T_Link_2("Linkb", "VOLTY_max", T_Link_2, 5);
  WiFiManagerParameter custom_T_Link_3("Linkc", "VOLTY_min", T_Link_3, 5);
  WiFiManagerParameter custom_update_username("userkey", "userkey", update_username, 60,"required");
  WiFiManagerParameter custom_update_password("apitoken", "apitoken", update_password, 60,"required");


  
  WiFiManager wifiManager;
  wifiManager.setBreakAfterConfig(true);
  wifiManager.setSaveConfigCallback(saveConfigCallback);
  
  wifiManager.addParameter(&custom_Supla_server);
  wifiManager.addParameter(&custom_D_Link_1);
  wifiManager.addParameter(&custom_T_Link_2);
  wifiManager.addParameter(&custom_T_Link_3);
  wifiManager.addParameter(&custom_update_username);
  wifiManager.addParameter(&custom_update_password);


  wifiManager.setCustomHeadElement("<style>html{ background-color: #01DF3A;}</style><div class='s'><svg version='1.1' id='l' x='0' y='0' viewBox='0 0 200 200' xml:space='preserve'><path d='M59.3,2.5c18.1,0.6,31.8,8,40.2,23.5c3.1,5.7,4.3,11.9,4.1,18.3c-0.1,3.6-0.7,7.1-1.9,10.6c-0.2,0.7-0.1,1.1,0.6,1.5c12.8,7.7,25.5,15.4,38.3,23c2.9,1.7,5.8,3.4,8.7,5.3c1,0.6,1.6,0.6,2.5-0.1c4.5-3.6,9.8-5.3,15.7-5.4c12.5-0.1,22.9,7.9,25.2,19c1.9,9.2-2.9,19.2-11.8,23.9c-8.4,4.5-16.9,4.5-25.5,0.2c-0.7-0.3-1-0.2-1.5,0.3c-4.8,4.9-9.7,9.8-14.5,14.6c-5.3,5.3-10.6,10.7-15.9,16c-1.8,1.8-3.6,3.7-5.4,5.4c-0.7,0.6-0.6,1,0,1.6c3.6,3.4,5.8,7.5,6.2,12.2c0.7,7.7-2.2,14-8.8,18.5c-12.3,8.6-30.3,3.5-35-10.4c-2.8-8.4,0.6-17.7,8.6-22.8c0.9-0.6,1.1-1,0.8-2c-2-6.2-4.4-12.4-6.6-18.6c-6.3-17.6-12.7-35.1-19-52.7c-0.2-0.7-0.5-1-1.4-0.9c-12.5,0.7-23.6-2.6-33-10.4c-8-6.6-12.9-15-14.2-25c-1.5-11.5,1.7-21.9,9.6-30.7C32.5,8.9,42.2,4.2,53.7,2.7c0.7-0.1,1.5-0.2,2.2-0.2C57,2.4,58.2,2.5,59.3,2.5z M76.5,81c0,0.1,0.1,0.3,0.1,0.6c1.6,6.3,3.2,12.6,4.7,18.9c4.5,17.7,8.9,35.5,13.3,53.2c0.2,0.9,0.6,1.1,1.6,0.9c5.4-1.2,10.7-0.8,15.7,1.6c0.8,0.4,1.2,0.3,1.7-0.4c11.2-12.9,22.5-25.7,33.4-38.7c0.5-0.6,0.4-1,0-1.6c-5.6-7.9-6.1-16.1-1.3-24.5c0.5-0.8,0.3-1.1-0.5-1.6c-9.1-4.7-18.1-9.3-27.2-14c-6.8-3.5-13.5-7-20.3-10.5c-0.7-0.4-1.1-0.3-1.6,0.4c-1.3,1.8-2.7,3.5-4.3,5.1c-4.2,4.2-9.1,7.4-14.7,9.7C76.9,80.3,76.4,80.3,76.5,81z M89,42.6c0.1-2.5-0.4-5.4-1.5-8.1C83,23.1,74.2,16.9,61.7,15.8c-10-0.9-18.6,2.4-25.3,9.7c-8.4,9-9.3,22.4-2.2,32.4c6.8,9.6,19.1,14.2,31.4,11.9C79.2,67.1,89,55.9,89,42.6z M102.1,188.6c0.6,0.1,1.5-0.1,2.4-0.2c9.5-1.4,15.3-10.9,11.6-19.2c-2.6-5.9-9.4-9.6-16.8-8.6c-8.3,1.2-14.1,8.9-12.4,16.6C88.2,183.9,94.4,188.6,102.1,188.6z M167.7,88.5c-1,0-2.1,0.1-3.1,0.3c-9,1.7-14.2,10.6-10.8,18.6c2.9,6.8,11.4,10.3,19,7.8c7.1-2.3,11.1-9.1,9.6-15.9C180.9,93,174.8,88.5,167.7,88.5z'/></svg>");

  wifiManager.setMinimumSignalQuality();
    wifiManager.setConfigPortalTimeout(timeout);

    if (!wifiManager.startConfigPortal("Dir_VOLTY")) {
      Serial.println("failed to connect and hit timeout");
      delay(3000);
      ESP.restart();
      delay(5000);
    }
    Serial.println("connected...yeey :)");

    strcpy(Supla_server, custom_Supla_server.getValue());
    strcpy(D_Link_1, custom_D_Link_1.getValue());
    strcpy(T_Link_2, custom_T_Link_2.getValue());
    strcpy(T_Link_3, custom_T_Link_3.getValue());
    strcpy(update_username, custom_update_username.getValue());
    strcpy(update_password, custom_update_password.getValue());  
}
ICACHE_RAM_ATTR void no_dimm(){
            dimm_milis = millis() + 30000 ;
                dimm = false; 
}
void setup() {  //------------------------------------------------ Setup ----------------------------------------------

  Serial.begin(115200);

  pinMode(Config_PIN, INPUT_PULLUP);
  pinMode(onboard_led, OUTPUT);

  if (WiFi.SSID()==""){   
    initialConfig = true;
 }

  Serial.println("mounting FS..."); 
  if (SPIFFS.begin()) {
    Serial.println("mounted file system");
    if (SPIFFS.exists("/config.json")) {
      //file exists, reading and loading
      Serial.println("reading config file");
      File configFile = SPIFFS.open("/config.json", "r");
      if (configFile) {
        Serial.println("opened config file");
        size_t size = configFile.size();
        std::unique_ptr<char[]> buf(new char[size]);
        configFile.readBytes(buf.get(), size);
        DynamicJsonBuffer jsonBuffer;         
        JsonObject& json = jsonBuffer.parseObject(buf.get());
        Serial.println(jsonBuffer.size());
        json.printTo(Serial);
        if (json.success()) {
          Serial.println("\nparsed json");
          strcpy(Supla_server, json["Supla_server"]);
          strcpy(D_Link_1, json["D_Link_1"]);
           strcpy(T_Link_2, json["T_Link_2"]);
           strcpy(T_Link_3, json["T_Link_3"]);
           strcpy(update_username, json["userkey"]);
          strcpy(update_password, json["apitoken"]);
           
            host = Supla_server;
              url = D_Link_1;
        } else {
          Serial.println("failed to load json config");          
        }
      }
    }
  } else {
    Serial.println("failed to mount FS");
 } 

  WiFi.mode(WIFI_STA);
  dimm_milis = millis() + 30000 ;
  attachInterrupt(Config_PIN, no_dimm, FALLING); 
}

void loop() { 

   if (initialConfig){ondemandwifiCallback();initialConfig = false;}

   int C_W_read = digitalRead(Config_PIN);{    
   if (C_W_read != last_C_W_state) {            
     time_last_C_W_change = millis();    
   }
   if ((millis() - time_last_C_W_change) > C_W_delay) {     
     if (C_W_read != C_W_state) {     
       Serial.println("Triger sate changed");
       C_W_state = C_W_read;       
       if (C_W_state == LOW) {
        ondemandwifiCallback () ;
       }
     }
    }
   last_C_W_state = C_W_read;            
   }

  if (millis() > dimm_milis){ 
    if (dimm == false){
   
    dimm = true ;     
    }    
    dimm_milis = millis() + 30000 ;
     } 
       
    if ((WiFi.status() == WL_CONNECTED) && (millis() > link_delay)){
      direct_Link();
       link_delay = link_delay + 5000;
  //-----------------------------------------------     
        if (a1==1 && b1==1){
        Serial.println ("Wysylam powiadomienie"); 
  Pushover po = Pushover(update_password,update_username); 
  po.setDevice("Device");
  po.setMessage("Za wysokie VOLTY");
  po.setSound("updown");
  po.send();
    b1=0;
        }
 //-----------------------------------------------     
    if (a2==1 && b2==1){
        Serial.println ("Wysylam powiadomienie"); 
  Pushover po = Pushover(update_password,update_username); 
  po.setDevice("Device");
  po.setMessage("Za niskie VOLTY");
  po.setSound("mechanical");
  po.send();
    b2=0;
        }


 //-----------------------------------------------     
    }
    if (shouldSaveConfig) {
    Serial.println("saving config");
    
    DynamicJsonBuffer jsonBuffer;
    JsonObject& json = jsonBuffer.createObject();
    json["Supla_server"] = Supla_server;
    json["D_Link_1"] = D_Link_1;
    json["T_Link_2"] = T_Link_2;
    json["T_Link_3"] = T_Link_3;
    json["userkey"] = update_username;
    json["apitoken"] = update_password;
    
    File configFile = SPIFFS.open("/config.json", "w");
    if (!configFile) {
      Serial.println("failed to open config file for writing");
    }
    json.prettyPrintTo(Serial);
    json.printTo(configFile);
    configFile.close();
    Serial.println("config saved");
   
    shouldSaveConfig = false;
    WiFi.mode(WIFI_STA);
    ticker.detach();
    digitalWrite(onboard_led, HIGH);
    ESP.restart();
      delay(5000); 
 }
  
  if (WiFi.status() != WL_CONNECTED) {
      WiFi_up();
 }
}

void WiFi_up() {
  WiFi.begin(); 
  
  for (int x = 20; x > 0; x--) 
  {
    if (WiFi.status() == WL_CONNECTED) 
    {
      break;                           
    }
    else                                 
    {
      Serial.print(".");                
      delay(500);                      
    }
  }
  if (WiFi.status() == WL_CONNECTED)
  {
    
    Serial.println("");
    Serial.println("Connected");
    Serial.println("Adres IP: ");
    Serial.print(WiFi.localIP());
    Serial.print(" / ");
    Serial.print(WiFi.subnetMask());
    Serial.print(" / ");
    Serial.println(WiFi.gatewayIP());
    long rssi = WiFi.RSSI();
    Serial.print("(RSSI): ");
    Serial.print(rssi);
    Serial.println(" dBm");   
  }
  else    
  {
    Serial.println("");
    Serial.println("connection failed");
     
  }
}
void direct_Link() {
  WiFiClientSecure client;
  Serial.print("connecting to ");
  Serial.println(host);
  if (!client.connect(host, httpsPort)) {
    Serial.println("connection failed ----------------------");
    return;
  }
  Serial.print("requesting URL: ");
  Serial.println(url);
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "User-Agent: ESP8266\r\n" +
               "Connection: close\r\n\r\n");
  Serial.println("request sent");
  while (client.connected()) {
    String line = client.readStringUntil('\n');
    if (line == "\r") {
      Serial.println("headers received");
      break;
    }
  }
   String line = client.readStringUntil('\n');
  if (line.indexOf("true") >0) {
    yield();
  Serial.println("reply was:  ");Serial.println("==========");  
 
  Serial.println(line);

  Serial.println("==========");Serial.println("closing connection");
    DynamicJsonBuffer jsonBuffer;
  JsonObject& root = jsonBuffer.parseObject(line);

  temp1 = root["temperature"]; 
  temp2 = atof(T_Link_2); 
  temp3 = atof(T_Link_3);

Serial.println (temp1);
Serial.println (temp2);
Serial.println (temp3);
digitalWrite(onboard_led, HIGH);

  //-------------->>> VOLTY----------------
       if (temp1<temp2){
    a1=0;
   }
  if (temp1>=temp2 && a1==0){  
   Serial.print ("Przek Napięcia: "); 
   Serial.println (temp2);
    a1=1;
    b1=1;
    }
//--------------<<< VOLTY----------------
      if (temp1>temp3){
    a2=0;
   }
  if (temp1<=temp3 && a2==0){  
   Serial.println ("Niskie Napięcie"); 
    a2=1;
    b2=1;
    }
 
  } else {Serial.println("failed --------------------");
 } 
}
Załączniki
Zdal_czyt_VOLTY_1.ino.d1_mini.zip
(292.19 KiB) Pobrany 11 razy
Awatar użytkownika
Zibi
Posty: 197
Rejestracja: śr lip 31, 2019 9:20 am
Lokalizacja: Białogard

sob lut 29, 2020 7:06 pm

Kolejne bardzo ciekawe powiadomienie. Super. Jutro przetestuję.
piomar2
Posty: 268
Rejestracja: pt kwie 26, 2019 5:32 pm
Lokalizacja: Bukowice

śr mar 04, 2020 9:17 am

Witajcie!

Na zapotrzebowanie kolegi @Grunger "skleciłem" programik, który mierzy temperaturę za pomocą DS18B20 i podaję ją w cloudzie...
:lol: :lol: :lol: ...
ale oprócz tego ma możliwość ustawienia temperatury minimalnej i maksymalnej przy przekroczeniu których, wysyła dwa powiadomienia Pushover (przy <min i przy >max).Program przetestowałem od min -8 stopni do max 95 stopni. Myślę, że może się komuś przydać np. do dozoru pieca...
Nagrywamy 32Mbit, wifi - "Push_temp", config D3 - 20sek.(192.168.4.1)
Bin w załączniku.
-----
Push_DS_2.png
Push_DS_2.png (64.18 KiB) Przejrzano 298 razy

Kod: Zaznacz cały

//Ustawienie 192.168.4.1
//Config - D3 20 sek
//Powiadomienie min i max temperatury
//wemos D1 mini
//menadżer płytek 2.4.2
//DS18B20 na D4 - 2

/* pushover
bike
bugle
cashregister
classical
cosmic
falling
gamelan
incoming
intermission
magic
mechanical
pianobar
siren
spacealarm
tugboat
alien
climb
persistent
echo
updown
none 
*/ 

#include <FS.h>  // ---- esp board manager 2.4.2 --- iwip Variant V2 higher Bandwidth
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#define SUPLADEVICE_CPP
#include <SuplaDevice.h>
#include <math.h>
#include <DNSServer.h>
#include <WiFiManager.h> 
#include <ArduinoJson.h> //-- https://github.com/bblanchon/ArduinoJson/tree/v5.13.2 --
#include <EEPROM.h>
#include <ESP8266WebServer.h>
#include <ESPEFC.h>  // modification of ESP8266HTTPUpdateServer that includes erases flash and wifi credentials
#include "Pushover.h"
#include <Wire.h>
#include <OneWire.h>
#include <DallasTemperature.h>
extern "C"
{
#include "user_interface.h"
}

byte a1=0;
byte a2=0;

byte b1=0;
byte b2=0;

float temp0;
int temp1;
int temp2;

OneWire oneWire(2);// DS18B20
DallasTemperature sensors(&oneWire);

double get_temperature(int channelNumber, double last_val) {

    double t = -275;
      if ( sensors.getDeviceCount() > 0 )
      {
       sensors.requestTemperatures();
     t = sensors.getTempCByIndex(0);
     temp0 = t;
      };

      return t;  
}

#define config_pin 0    // D3  wifi config
#define status_led 2    // D4  status Led 
bool pr_wifi = true;
bool start = true;           
int s;             
unsigned long wifi_checkDelay = 20000;
unsigned long wifimilis;
int C_W_state = HIGH; 
int last_C_W_state = HIGH;
unsigned long time_last_C_W_change = 0; 
long C_W_delay = 10000;  // config delay 10 seconds - opóźnienie konfiguracji 10 sekund
WiFiClient client;
ESP8266WebServer httpServer(81);
ESPEFC httpUpdater;
const char* shost = ".org";
const char* update_path = "/";
char Supla_server[80];
char Location_id[15];
char Location_Pass[34];

char T_Link_1[2];
char T_Link_2[2];
char Supla_name[51];
char update_username[60];
char update_password[60];
byte mac[6];
bool shouldSaveConfig = false;
bool initialConfig = false;
int timeout = 180;  

void saveConfigCallback () {  //callback notifying us of the need to save config
  Serial.println("Should save config");
  shouldSaveConfig = true;
}
void ondemandwifiCallback () {
  digitalWrite(status_led, LOW);
  httpServer.stop();
  WiFiManagerParameter custom_Supla_server("server", "supla server", Supla_server, 80);
  WiFiManagerParameter custom_Location_id("ID", "Location id", Location_id, 15);
  WiFiManagerParameter custom_Location_Pass("Password", "Location Pass", Location_Pass, 34);
  WiFiManagerParameter custom_T_Link_1("Linka", "Temp_min", T_Link_1, 2);
  WiFiManagerParameter custom_T_Link_2("Linkb", "Temp_max", T_Link_2, 2);
  WiFiManagerParameter custom_Supla_name("name", "Supla Device Name", Supla_name, 51,"required");
  WiFiManagerParameter custom_update_username("userkey", "userkey", update_username, 60,"required");
  WiFiManagerParameter custom_update_password("apitoken", "apitoken", update_password, 60,"required");

  WiFiManager wifiManager;
  wifiManager.setBreakAfterConfig(true);
  wifiManager.setSaveConfigCallback(saveConfigCallback);
  
  wifiManager.addParameter(&custom_Supla_server);
  wifiManager.addParameter(&custom_Location_id);
  wifiManager.addParameter(&custom_Location_Pass);
  wifiManager.addParameter(&custom_T_Link_1);
  wifiManager.addParameter(&custom_T_Link_2);
  wifiManager.addParameter(&custom_Supla_name);
  wifiManager.addParameter(&custom_update_username);
  wifiManager.addParameter(&custom_update_password);

  wifiManager.setCustomHeadElement("<style>html{ background-color: #01DF3E;}</style><div class='s'><svg version='1.1' id='l' x='0' y='0' viewBox='0 0 200 200' xml:space='preserve'><path d='M59.3,2.5c18.1,0.6,31.8,8,40.2,23.5c3.1,5.7,4.3,11.9,4.1,18.3c-0.1,3.6-0.7,7.1-1.9,10.6c-0.2,0.7-0.1,1.1,0.6,1.5c12.8,7.7,25.5,15.4,38.3,23c2.9,1.7,5.8,3.4,8.7,5.3c1,0.6,1.6,0.6,2.5-0.1c4.5-3.6,9.8-5.3,15.7-5.4c12.5-0.1,22.9,7.9,25.2,19c1.9,9.2-2.9,19.2-11.8,23.9c-8.4,4.5-16.9,4.5-25.5,0.2c-0.7-0.3-1-0.2-1.5,0.3c-4.8,4.9-9.7,9.8-14.5,14.6c-5.3,5.3-10.6,10.7-15.9,16c-1.8,1.8-3.6,3.7-5.4,5.4c-0.7,0.6-0.6,1,0,1.6c3.6,3.4,5.8,7.5,6.2,12.2c0.7,7.7-2.2,14-8.8,18.5c-12.3,8.6-30.3,3.5-35-10.4c-2.8-8.4,0.6-17.7,8.6-22.8c0.9-0.6,1.1-1,0.8-2c-2-6.2-4.4-12.4-6.6-18.6c-6.3-17.6-12.7-35.1-19-52.7c-0.2-0.7-0.5-1-1.4-0.9c-12.5,0.7-23.6-2.6-33-10.4c-8-6.6-12.9-15-14.2-25c-1.5-11.5,1.7-21.9,9.6-30.7C32.5,8.9,42.2,4.2,53.7,2.7c0.7-0.1,1.5-0.2,2.2-0.2C57,2.4,58.2,2.5,59.3,2.5z M76.5,81c0,0.1,0.1,0.3,0.1,0.6c1.6,6.3,3.2,12.6,4.7,18.9c4.5,17.7,8.9,35.5,13.3,53.2c0.2,0.9,0.6,1.1,1.6,0.9c5.4-1.2,10.7-0.8,15.7,1.6c0.8,0.4,1.2,0.3,1.7-0.4c11.2-12.9,22.5-25.7,33.4-38.7c0.5-0.6,0.4-1,0-1.6c-5.6-7.9-6.1-16.1-1.3-24.5c0.5-0.8,0.3-1.1-0.5-1.6c-9.1-4.7-18.1-9.3-27.2-14c-6.8-3.5-13.5-7-20.3-10.5c-0.7-0.4-1.1-0.3-1.6,0.4c-1.3,1.8-2.7,3.5-4.3,5.1c-4.2,4.2-9.1,7.4-14.7,9.7C76.9,80.3,76.4,80.3,76.5,81z M89,42.6c0.1-2.5-0.4-5.4-1.5-8.1C83,23.1,74.2,16.9,61.7,15.8c-10-0.9-18.6,2.4-25.3,9.7c-8.4,9-9.3,22.4-2.2,32.4c6.8,9.6,19.1,14.2,31.4,11.9C79.2,67.1,89,55.9,89,42.6z M102.1,188.6c0.6,0.1,1.5-0.1,2.4-0.2c9.5-1.4,15.3-10.9,11.6-19.2c-2.6-5.9-9.4-9.6-16.8-8.6c-8.3,1.2-14.1,8.9-12.4,16.6C88.2,183.9,94.4,188.6,102.1,188.6z M167.7,88.5c-1,0-2.1,0.1-3.1,0.3c-9,1.7-14.2,10.6-10.8,18.6c2.9,6.8,11.4,10.3,19,7.8c7.1-2.3,11.1-9.1,9.6-15.9C180.9,93,174.8,88.5,167.7,88.5z'/></svg>");
  wifiManager.setMinimumSignalQuality(8);
  wifiManager.setConfigPortalTimeout(timeout);

    if (!wifiManager.startConfigPortal("Push_temp")) {
      Serial.println("Not connected to WiFi but continuing anyway.");
    } else {
      
      Serial.println("connected...yeey :)");  //if you get here you have connected to the WiFi
    }
    //read updated parameters
    strcpy(Supla_server, custom_Supla_server.getValue());
    strcpy(Location_id, custom_Location_id.getValue());
    strcpy(Location_Pass, custom_Location_Pass.getValue());
    strcpy(T_Link_1, custom_T_Link_1.getValue());
    strcpy(T_Link_2, custom_T_Link_2.getValue());
    strcpy(Supla_name, custom_Supla_name.getValue());
    strcpy(update_username, custom_update_username.getValue());
    strcpy(update_password, custom_update_password.getValue());
   
  WiFi.softAPdisconnect(true);   //  close AP
}

void status_func(int status, const char *msg) {       
 s=status;                                              
}
void setup() {  //------- Setup ----------------

  wifi_set_sleep_type(NONE_SLEEP_T);

  Serial.begin(115200);
  delay(200);
  pinMode(status_led,OUTPUT);
  sensors.begin();
  sensors.setResolution(10);//rozdzielczość DS-a
   
  EEPROM.write(1,1);
  
  if (WiFi.SSID()==""){
    initialConfig = true;
  } 
  
  WiFi.mode(WIFI_STA); // Force to station mode because if device was switched off while in access point mode it will start up next time in access point mode.

   uint8_t mac[WL_MAC_ADDR_LENGTH];
   WiFi.macAddress(mac);
   char GUID[SUPLA_GUID_SIZE] = {mac[WL_MAC_ADDR_LENGTH - 6], mac[WL_MAC_ADDR_LENGTH - 5], mac[WL_MAC_ADDR_LENGTH - 4], mac[WL_MAC_ADDR_LENGTH - 3],                                
                                 mac[WL_MAC_ADDR_LENGTH - 2], mac[WL_MAC_ADDR_LENGTH - 1], mac[WL_MAC_ADDR_LENGTH - 1], mac[WL_MAC_ADDR_LENGTH - 2], 
                                 mac[WL_MAC_ADDR_LENGTH - 3], mac[WL_MAC_ADDR_LENGTH - 4], mac[WL_MAC_ADDR_LENGTH - 5], mac[WL_MAC_ADDR_LENGTH - 6]};
 
  SuplaDevice.addDS18B20Thermometer();
// ************************************************************
  if (SPIFFS.begin()) {
    Serial.println("mounted file system");
    if (SPIFFS.exists("/config.json")) {
      Serial.println("reading config file");
      File configFile = SPIFFS.open("/config.json", "r");
      if (configFile) {
        Serial.println("opened config file");
        size_t size = configFile.size();
        std::unique_ptr<char[]> buf(new char[size]);
        configFile.readBytes(buf.get(), size);
        DynamicJsonBuffer jsonBuffer;         
        JsonObject& json = jsonBuffer.parseObject(buf.get());
        json.printTo(Serial);   //print config data to serial on startup
        if (json.success()) {
          Serial.println("\nparsed json");

          strcpy(Supla_server, json["Supla_server"]);
          strcpy(Location_id, json["Location_id"]);
          strcpy(Location_Pass, json["Location_Pass"]);
          strcpy(T_Link_1, json["T_Link_1"]);
          strcpy(T_Link_2, json["T_Link_2"]);
          strcpy(Supla_name, json["Supla_name"]);         
          strcpy(update_username, json["userkey"]);
          strcpy(update_password, json["apitoken"]);
        } else {
          Serial.println("failed to load json config");
          initialConfig = true;
        }
      }
    }
  } else {
    Serial.println("failed to mount FS");
  }
  wifi_station_set_hostname(Supla_name);  

  SuplaDevice.setStatusFuncImpl(&status_func); 
  SuplaDevice.setName(Supla_name);

  int LocationID = atoi(Location_id);
  SuplaDevice.begin(GUID,              // Global Unique Identifier 
                    mac,               // Ethernet MAC address
                    Supla_server,      // SUPLA server address
                    LocationID,        // Location ID 
                    Location_Pass);    // Location Password                           
digitalWrite(status_led, HIGH);

}
//----------------------------------------------------------------
void loop() 
{
  temp1 = atoi(T_Link_1);
  temp2 = atoi(T_Link_2); 
  Serial.println(temp0);
  Serial.println(temp1);
  Serial.println(temp2);
  Serial.println("  ");
  
//--------------<<< ----------------
       if (temp0>temp1){
    a1=0;
   }
  if (temp0<=temp1 && a1==0){  
    a1=1;
    b1=1;
    }
 //-------------->>> ---------------
       if (temp0<temp2){
    a2=0;
   }
  if (temp0>=temp2 && a2==0){  
    a2=1;
    b2=1;
    }
   //-----------------------------------------------  
   if (a1==1 && b1==1){
        Serial.println ("temp < min"); 
        b1=0;
  Pushover po = Pushover(update_password,update_username); 
  po.setDevice("Device");
  po.setMessage("temp < min");
  po.setSound("alien");
  po.send();
   }
   
   if (a2==1 && b2==1){
        Serial.println ("temp > max");
        b2=0; 
  Pushover po = Pushover(update_password,update_username); 
  po.setDevice("Device");
  po.setMessage("temp > max");
  po.setSound("bike");
  po.send(); 
     }
 
 //-----------------------------------  
  if (initialConfig){
    ondemandwifiCallback () ;
  }
  if (shouldSaveConfig) {
    Serial.println("saving config");
    DynamicJsonBuffer jsonBuffer;
    JsonObject& json = jsonBuffer.createObject();
    json["Supla_server"] = Supla_server;
    json["Location_id"] = Location_id;
    json["Location_Pass"] = Location_Pass;
    json["T_Link_1"] = T_Link_1;
    json["T_Link_2"] = T_Link_2;
    json["Supla_name"] = Supla_name;
    json["userkey"] = update_username;
    json["apitoken"] = update_password;
    File configFile = SPIFFS.open("/config.json", "w");
    if (!configFile) {
      Serial.println("failed to open config file for writing");
    }
    json.prettyPrintTo(Serial);
    json.printTo(configFile);
    configFile.close();
    Serial.println("config saved");
    shouldSaveConfig = false;
    initialConfig = false; 
    WiFi.mode(WIFI_STA);
    ESP.restart();
    delay(5000); 
  }
  if (WiFi.status() != WL_CONNECTED) { 
    WiFi_up();
  }
  
    int C_W_read = digitalRead(config_pin);{  
   if (C_W_read != last_C_W_state) {            
     time_last_C_W_change = millis();
   }
   if ((millis() - time_last_C_W_change) > C_W_delay) {     
     if (C_W_read != C_W_state) {     
       Serial.println("Triger sate changed");
       C_W_state = C_W_read;       
       if (C_W_state == LOW) {
        ondemandwifiCallback () ;
       }
     }
    }
   last_C_W_state = C_W_read;            
   }
   
   if (WiFi.status() == WL_CONNECTED){
    SuplaDevice.iterate();      
    if (pr_wifi == true){
    Serial.println("");
    Serial.println("CONNECTED");
    Serial.print("local IP: ");
    Serial.println(WiFi.localIP());
    Serial.print("subnetMask: ");
    Serial.println(WiFi.subnetMask());
    Serial.print("gatewayIP: ");
    Serial.println(WiFi.gatewayIP());
     long rssi = WiFi.RSSI();
    Serial.print("Signal Strength (RSSI): ");
    Serial.print(rssi);
    Serial.println(" dBm"); 
     pr_wifi = false;       
     httpUpdater.setup(&httpServer, update_path, update_username, update_password);
     httpServer.begin();
     delay(50);
    }
    if (pr_wifi == false){
     httpServer.handleClient(); 
     yield();
    
    }
    }
    }

// Supla.org ethernet layer
    int supla_arduino_tcp_read(void *buf, int count) {
        _supla_int_t size = client.available();       
        if ( size > 0 ) {
            if ( size > count ) size = count;
            return client.read((uint8_t *)buf, size);
        };    
        return -1;
    };    
    int supla_arduino_tcp_write(void *buf, int count) {
        return client.write((const uint8_t *)buf, count);
    };    
    bool supla_arduino_svr_connect(const char *server, int port) {
          return client.connect(server, 2015);
    }    
    bool supla_arduino_svr_connected(void) {
          return client.connected();
    }    
    void supla_arduino_svr_disconnect(void) {
         client.stop();
    }    
    void supla_arduino_eth_setup(uint8_t mac[6], IPAddress *ip) {
    }

SuplaDeviceCallbacks supla_arduino_get_callbacks(void) {
          SuplaDeviceCallbacks cb;          
          cb.tcp_read = &supla_arduino_tcp_read;
          cb.tcp_write = &supla_arduino_tcp_write;
          cb.eth_setup = &supla_arduino_eth_setup;
          cb.svr_connected = &supla_arduino_svr_connected;
          cb.svr_connect = &supla_arduino_svr_connect;
          cb.svr_disconnect = &supla_arduino_svr_disconnect;
          cb.get_temperature = &get_temperature;
          cb.get_temperature_and_humidity = NULL;
          cb.get_rgbw_value = NULL;
          cb.set_rgbw_value = NULL;         
          return cb;
}

void WiFi_up(){ // conect to wifi
 
  if (millis() > wifimilis)  {
  WiFi.begin();
  delay(500);
  pr_wifi = true;
  Serial.println("CONNECTING WIFI"); 
  wifimilis = (millis() + wifi_checkDelay) ;
  }
}
Załączniki
Push_DS_min_max.ino.d1_mini.zip
(309.23 KiB) Pobrany 11 razy
emg
Posty: 29
Rejestracja: sob sty 04, 2020 2:56 pm
Lokalizacja: Wrocław
Kontaktowanie:

śr mar 04, 2020 1:57 pm

piomar2 pisze:
sob lut 15, 2020 10:13 am
Dla Gucio2607 i emg i ...komu to się przyda dołożyłem przekaźnik, który jest wyzwalany jednym z 4 powiadomień. Może to być syrenka itp. Przekaźnik załączony jest do momentu, kiedy wyłączymy układ przyciskiem na D5 lub w cloudzie. Pamiętajmy, że push wróci do czuwania po ponownym uaktywnieniu. (syrenkę podłączajcie po skonfigurowaniu :lol: )
Zastosowałem taki moduł przekaźnika i tak jest podłączony do wemosa (napięcie na RX nie przekracza 3v3).

Testujcie...


Pu.jpg
A może opcja na 30s/60s albo jakiś wybór czasu? Ja bym chciał dołączyć to do dzwonka, żeby przyszło powiadomienie i dodatkowo np. zapaliło mi wieczorem latarnie przed chatą i zgasiło za jakiś czas. Ciągłe świecenie aż do resetu odpada w tym przypadku :D
piomar2
Posty: 268
Rejestracja: pt kwie 26, 2019 5:32 pm
Lokalizacja: Bukowice

śr mar 04, 2020 2:37 pm

Czyli potrzebujesz programik, który po naciśnięciu przycisku dzwonka wyśle 1 powiadomienie oraz załączy przekaźnik. Przekaźnik w cloudzie ustawisz jako "Automat schodowy" a tam ustawisz już sobie czas. Czy o to Tobie chodzi?
emg
Posty: 29
Rejestracja: sob sty 04, 2020 2:56 pm
Lokalizacja: Wrocław
Kontaktowanie:

śr mar 04, 2020 5:06 pm

Dokladnie tak. Jeden przycisk dzwonkowy obsłuży mi przez tranzystory, abo przekaźnik, standardowy dzwonek i wemosa więc przydalaby się taka mozliwosc.
piomar2
Posty: 268
Rejestracja: pt kwie 26, 2019 5:32 pm
Lokalizacja: Bukowice

śr mar 04, 2020 7:41 pm

emg pisze:
śr mar 04, 2020 5:06 pm
Dokladnie tak. Jeden przycisk dzwonkowy obsłuży mi przez tranzystory, abo przekaźnik, standardowy dzwonek i wemosa więc przydalaby się taka mozliwosc.
Mam dla Ciebie program. Trochę przewrotnie skonstruowany...
Na priva.
piomar2
Posty: 268
Rejestracja: pt kwie 26, 2019 5:32 pm
Lokalizacja: Bukowice

czw mar 05, 2020 1:31 pm

Witajcie !

Na zapotrzebowanie @emg prosty programik do dzwonka przy drzwiach wejściowych.
Program działa następująco: po naciśnięciu przycisku D2 do masy wemos wysyła powiadomienie Pushover na telefon oraz uruchamia przekaźnik (poziom wysoki na D5). Czas trwania działania przekaźnika (oświetlenie przed wejściem?) ustalamy w cloudzie, gdzie moduł ma 1 urządzenie ustawione jako "Automat schodowy" i tam ustawiamy czas.

Sposób konfiguracji jest nietypowy dlatego opiszę w pkt.:

- czyścimy wemosa,
- wgrywamy przy ustawieniach DOUT i 32Mbit lub kompilujemy źródło i wgrywamy przez arduino,
- resetujemy wemosa i podłączamy się do wifi "Dzwonek",
- wypełniamy tabelkę configu za wyjątkiem pola Link ,
- zapisujemy i czekamy aż komp wróci sam do swojego wifi,
- logujemy się do clouda i po włączeniu rejestracji czekamy aż pojawi się nowy "magiczny" zielony klocuszek a w środku 1 urządzenie
(przekaźnik), które ustawiamy jako "Automat schodowy" nap. na 30 sek.
- kolejnym krokiem jest stworzenie do tego "Automatu schodowego" linku bezpośredniego z opcją "włącz" i zapisujemy go na boku - kartce
(potrzebny kawałek /direct/xxx/xxxxxxxxxxxx/turn-on)
- następnie zwierając na 15 sek D3 do masy wchodzimy ponownie do configu i dopisujemy jeszcze raz swoje wifi, hasło do niego oraz Link (zapisany boku - kartce).
- zapisujemy i czekamy aż komp wróci sam do swojego wifi,

I to FINAŁ. Możemy cieszyć się nowym gadżetem.

Kod: Zaznacz cały

//wemos D1 mini
//D3 - konfiguracja 5 sek
//D2 - przycisk wyzwalania do masy
//D5 - przekaźnik aktywny - stan wysoki


#include <FS.h>  // ---- esp board manager 2.4.2 --- iwip Variant V2 higher Bandwidth
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#define SUPLADEVICE_CPP
#include <SuplaDevice.h>
#include <math.h>
#include <DNSServer.h>
#include <WiFiManager.h> 
#include <ArduinoJson.h> //-- https://github.com/bblanchon/ArduinoJson/tree/v5.13.2 --
#include <EEPROM.h>
#include <ESP8266WebServer.h>
#include <ESPEFC.h>  // modification of ESP8266HTTPUpdateServer that includes erases flash and wifi credentials
#include "Pushover.h"
extern "C"
{
#include "user_interface.h"
}

#define config_pin 0    //   wifi config
#define status_led 3    // D4  status Led 

bool pr_wifi = true;
bool start = true;           
int s;             
unsigned long wifi_checkDelay = 20000;
unsigned long wifimilis;
int C_W_state = HIGH; 
int last_C_W_state = HIGH;
unsigned long time_last_C_W_change = 0; 
long C_W_delay = 5000; 
WiFiClient client;
ESP8266WebServer httpServer(81);
ESPEFC httpUpdater;
const char* shost = ".org";
const char* update_path = "/";
char Supla_server[80];
char Location_id[15];
char Location_Pass[34];
char D_Link_1[61];

char Supla_name[51];
char update_username[60];
char update_password[60];
byte mac[6];
bool shouldSaveConfig = false;
bool initialConfig = false;
int timeout = 180;          // seconds to run the wifi config

void saveConfigCallback () {  //callback notifying us of the need to save config
  Serial.println("Should save config");
  shouldSaveConfig = true;
}
void ondemandwifiCallback () {
  digitalWrite(status_led, LOW);
  httpServer.stop();
  WiFiManagerParameter custom_Supla_server("server", "supla server", Supla_server, 80);
  WiFiManagerParameter custom_Location_id("ID", "Location id", Location_id, 15);
  WiFiManagerParameter custom_Location_Pass("Password", "Location Pass", Location_Pass, 34);
  WiFiManagerParameter custom_D_Link_1("Linka", "Link", D_Link_1, 61);
  
  WiFiManagerParameter custom_Supla_name("name", "Supla Device Name", Supla_name, 51,"required");
  WiFiManagerParameter custom_update_username("userkey", "userkey", update_username, 60,"required");
  WiFiManagerParameter custom_update_password("apitoken", "apitoken", update_password, 60,"required");

  WiFiManager wifiManager;
  wifiManager.setBreakAfterConfig(true);
  wifiManager.setSaveConfigCallback(saveConfigCallback);
  
  wifiManager.addParameter(&custom_Supla_server);
  wifiManager.addParameter(&custom_Location_id);
  wifiManager.addParameter(&custom_Location_Pass);
  wifiManager.addParameter(&custom_D_Link_1);
  
  wifiManager.addParameter(&custom_Supla_name);
  wifiManager.addParameter(&custom_update_username);
  wifiManager.addParameter(&custom_update_password);

  wifiManager.setCustomHeadElement("<style>html{ background-color: #01DF3E;}</style><div class='s'><svg version='1.1' id='l' x='0' y='0' viewBox='0 0 200 200' xml:space='preserve'><path d='M59.3,2.5c18.1,0.6,31.8,8,40.2,23.5c3.1,5.7,4.3,11.9,4.1,18.3c-0.1,3.6-0.7,7.1-1.9,10.6c-0.2,0.7-0.1,1.1,0.6,1.5c12.8,7.7,25.5,15.4,38.3,23c2.9,1.7,5.8,3.4,8.7,5.3c1,0.6,1.6,0.6,2.5-0.1c4.5-3.6,9.8-5.3,15.7-5.4c12.5-0.1,22.9,7.9,25.2,19c1.9,9.2-2.9,19.2-11.8,23.9c-8.4,4.5-16.9,4.5-25.5,0.2c-0.7-0.3-1-0.2-1.5,0.3c-4.8,4.9-9.7,9.8-14.5,14.6c-5.3,5.3-10.6,10.7-15.9,16c-1.8,1.8-3.6,3.7-5.4,5.4c-0.7,0.6-0.6,1,0,1.6c3.6,3.4,5.8,7.5,6.2,12.2c0.7,7.7-2.2,14-8.8,18.5c-12.3,8.6-30.3,3.5-35-10.4c-2.8-8.4,0.6-17.7,8.6-22.8c0.9-0.6,1.1-1,0.8-2c-2-6.2-4.4-12.4-6.6-18.6c-6.3-17.6-12.7-35.1-19-52.7c-0.2-0.7-0.5-1-1.4-0.9c-12.5,0.7-23.6-2.6-33-10.4c-8-6.6-12.9-15-14.2-25c-1.5-11.5,1.7-21.9,9.6-30.7C32.5,8.9,42.2,4.2,53.7,2.7c0.7-0.1,1.5-0.2,2.2-0.2C57,2.4,58.2,2.5,59.3,2.5z M76.5,81c0,0.1,0.1,0.3,0.1,0.6c1.6,6.3,3.2,12.6,4.7,18.9c4.5,17.7,8.9,35.5,13.3,53.2c0.2,0.9,0.6,1.1,1.6,0.9c5.4-1.2,10.7-0.8,15.7,1.6c0.8,0.4,1.2,0.3,1.7-0.4c11.2-12.9,22.5-25.7,33.4-38.7c0.5-0.6,0.4-1,0-1.6c-5.6-7.9-6.1-16.1-1.3-24.5c0.5-0.8,0.3-1.1-0.5-1.6c-9.1-4.7-18.1-9.3-27.2-14c-6.8-3.5-13.5-7-20.3-10.5c-0.7-0.4-1.1-0.3-1.6,0.4c-1.3,1.8-2.7,3.5-4.3,5.1c-4.2,4.2-9.1,7.4-14.7,9.7C76.9,80.3,76.4,80.3,76.5,81z M89,42.6c0.1-2.5-0.4-5.4-1.5-8.1C83,23.1,74.2,16.9,61.7,15.8c-10-0.9-18.6,2.4-25.3,9.7c-8.4,9-9.3,22.4-2.2,32.4c6.8,9.6,19.1,14.2,31.4,11.9C79.2,67.1,89,55.9,89,42.6z M102.1,188.6c0.6,0.1,1.5-0.1,2.4-0.2c9.5-1.4,15.3-10.9,11.6-19.2c-2.6-5.9-9.4-9.6-16.8-8.6c-8.3,1.2-14.1,8.9-12.4,16.6C88.2,183.9,94.4,188.6,102.1,188.6z M167.7,88.5c-1,0-2.1,0.1-3.1,0.3c-9,1.7-14.2,10.6-10.8,18.6c2.9,6.8,11.4,10.3,19,7.8c7.1-2.3,11.1-9.1,9.6-15.9C180.9,93,174.8,88.5,167.7,88.5z'/></svg>");
  wifiManager.setMinimumSignalQuality(8);
  wifiManager.setConfigPortalTimeout(timeout);

    if (!wifiManager.startConfigPortal("Dzwonek")) {
      Serial.println("Not connected to WiFi but continuing anyway.");
    } else {
      
      Serial.println("connected...yeey :)");  //if you get here you have connected to the WiFi
    }
    //read updated parameters
    strcpy(Supla_server, custom_Supla_server.getValue());
    strcpy(Location_id, custom_Location_id.getValue());
    strcpy(Location_Pass, custom_Location_Pass.getValue());
    strcpy(D_Link_1, custom_D_Link_1.getValue());
   
    strcpy(Supla_name, custom_Supla_name.getValue());
    strcpy(update_username, custom_update_username.getValue());
    strcpy(update_password, custom_update_password.getValue());
   
  WiFi.softAPdisconnect(true);   //  close AP
}
         

void status_func(int status, const char *msg) {     //    ------------------------ Status --------------------------
 s=status;                                          //    -- to check if we are registered and ready before restore from memory
}
void setup() {  //------- Setup ----------------

  wifi_set_sleep_type(NONE_SLEEP_T);

  Serial.begin(115200);
  delay(200);
 // pinMode(status_led,OUTPUT);
  //pinMode(3,OUTPUT);
  
  //**************************************
    pinMode(4, INPUT_PULLUP);//"updown"
      
  //**************************************
  EEPROM.write(1,1);
  
  if (WiFi.SSID()==""){
    initialConfig = true;
  } 
  
  WiFi.mode(WIFI_STA); // Force to station mode because if device was switched off while in access point mode it will start up next time in access point mode.

   uint8_t mac[WL_MAC_ADDR_LENGTH];
   WiFi.macAddress(mac);
   char GUID[SUPLA_GUID_SIZE] = {mac[WL_MAC_ADDR_LENGTH - 6], mac[WL_MAC_ADDR_LENGTH - 5], mac[WL_MAC_ADDR_LENGTH - 4], mac[WL_MAC_ADDR_LENGTH - 3],                                
                                 mac[WL_MAC_ADDR_LENGTH - 2], mac[WL_MAC_ADDR_LENGTH - 1], mac[WL_MAC_ADDR_LENGTH - 1], mac[WL_MAC_ADDR_LENGTH - 2], 
                                 mac[WL_MAC_ADDR_LENGTH - 3], mac[WL_MAC_ADDR_LENGTH - 4], mac[WL_MAC_ADDR_LENGTH - 5], mac[WL_MAC_ADDR_LENGTH - 6]};
          
  SuplaDevice.addRelay(14, LOW);  // HIGH == LOW, and LOW == HIGH 
 
   if (SPIFFS.begin()) {
    Serial.println("mounted file system");
    if (SPIFFS.exists("/config.json")) {
      Serial.println("reading config file");
      File configFile = SPIFFS.open("/config.json", "r");
      if (configFile) {
        Serial.println("opened config file");
        size_t size = configFile.size();
        std::unique_ptr<char[]> buf(new char[size]);

        configFile.readBytes(buf.get(), size);
        DynamicJsonBuffer jsonBuffer;         
        JsonObject& json = jsonBuffer.parseObject(buf.get());
        json.printTo(Serial);   //print config data to serial on startup
        if (json.success()) {
          Serial.println("\nparsed json");

          strcpy(Supla_server, json["Supla_server"]);
          strcpy(Location_id, json["Location_id"]);
          strcpy(Location_Pass, json["Location_Pass"]);
          strcpy(D_Link_1, json["D_Link_1"]);
          
          strcpy(Supla_name, json["Supla_name"]);         
          strcpy(update_username, json["userkey"]);
          strcpy(update_password, json["apitoken"]);
        } else {
          Serial.println("failed to load json config");
          initialConfig = true;
        }
      }
    }
  } else {
    Serial.println("failed to mount FS");
  }
  wifi_station_set_hostname(Supla_name);  

  SuplaDevice.setStatusFuncImpl(&status_func); 
  SuplaDevice.setName(Supla_name);

  int LocationID = atoi(Location_id);
  SuplaDevice.begin(GUID,              // Global Unique Identifier 
                    mac,               // Ethernet MAC address
                    Supla_server,      // SUPLA server address
                    LocationID,        // Location ID 
                    Location_Pass);    // Location Password                           

}
//----------------------------------------------------------------
void loop() 
{
 
  if (digitalRead(4)==LOW){  
   direct_Link(1);// Link
  Pushover po = Pushover(update_password,update_username); 
  po.setDevice("Device");
  po.setMessage("DZWONEK !!!");
  po.setSound("updown");
  po.send();
         }
  
  if (initialConfig){
    ondemandwifiCallback () ;
  }
  if (shouldSaveConfig) {
    Serial.println("saving config");
    DynamicJsonBuffer jsonBuffer;
    JsonObject& json = jsonBuffer.createObject();
    json["Supla_server"] = Supla_server;
    json["Location_id"] = Location_id;
    json["Location_Pass"] = Location_Pass;
    json["D_Link_1"] = D_Link_1;
    
    json["Supla_name"] = Supla_name;
    json["userkey"] = update_username;
    json["apitoken"] = update_password;
    File configFile = SPIFFS.open("/config.json", "w");
    if (!configFile) {
      Serial.println("failed to open config file for writing");
    }
    json.prettyPrintTo(Serial);
    json.printTo(configFile);
    configFile.close();
    Serial.println("config saved");
    shouldSaveConfig = false;
    initialConfig = false; 
    WiFi.mode(WIFI_STA);
    ESP.restart();
    delay(5000); 
  }
  if (WiFi.status() != WL_CONNECTED) { 
    WiFi_up();
  }
 
  int C_W_read = digitalRead(config_pin);{  
   if (C_W_read != last_C_W_state) {            
     time_last_C_W_change = millis();
   }
   if ((millis() - time_last_C_W_change) > C_W_delay) {     
     if (C_W_read != C_W_state) {     
       Serial.println("Triger sate changed");
       C_W_state = C_W_read;       
       if (C_W_state == LOW) {
        ondemandwifiCallback () ;
       }
     }
    }
   last_C_W_state = C_W_read;            
   }
   
   if (WiFi.status() == WL_CONNECTED){
    SuplaDevice.iterate();      
    if (pr_wifi == true){
    Serial.println("");
    Serial.println("CONNECTED");
    Serial.print("local IP: ");
    Serial.println(WiFi.localIP());
    Serial.print("subnetMask: ");
    Serial.println(WiFi.subnetMask());
    Serial.print("gatewayIP: ");
    Serial.println(WiFi.gatewayIP());
     long rssi = WiFi.RSSI();
    Serial.print("Signal Strength (RSSI): ");
    Serial.print(rssi);
    Serial.println(" dBm"); 
     pr_wifi = false;       
     httpUpdater.setup(&httpServer, update_path, update_username, update_password);
     httpServer.begin();
     delay(50);
    }
    if (pr_wifi == false){
     httpServer.handleClient(); 
     yield();
    
    }
    }
   }

//*****************KONIEC**********************************

// Supla.org ethernet layer
    int supla_arduino_tcp_read(void *buf, int count) {
        _supla_int_t size = client.available();       
        if ( size > 0 ) {
            if ( size > count ) size = count;
            return client.read((uint8_t *)buf, size);
        };    
        return -1;
    };    
    int supla_arduino_tcp_write(void *buf, int count) {
        return client.write((const uint8_t *)buf, count);
    };    
    bool supla_arduino_svr_connect(const char *server, int port) {
          return client.connect(server, 2015);
    }    
    bool supla_arduino_svr_connected(void) {
          return client.connected();
    }    
    void supla_arduino_svr_disconnect(void) {
         client.stop();
    }    
    void supla_arduino_eth_setup(uint8_t mac[6], IPAddress *ip) {
    }

SuplaDeviceCallbacks supla_arduino_get_callbacks(void) {
          SuplaDeviceCallbacks cb;          
          cb.tcp_read = &supla_arduino_tcp_read;
          cb.tcp_write = &supla_arduino_tcp_write;
          cb.eth_setup = &supla_arduino_eth_setup;
          cb.svr_connected = &supla_arduino_svr_connected;
          cb.svr_connect = &supla_arduino_svr_connect;
          cb.svr_disconnect = &supla_arduino_svr_disconnect;
          cb.get_temperature = NULL;
          cb.get_temperature_and_humidity = NULL;
          cb.get_rgbw_value = NULL;
          cb.set_rgbw_value = NULL;         
          return cb;
}

void WiFi_up(){ // conect to wifi
 
  if (millis() > wifimilis)  {
  WiFi.begin();
  delay(500);
  pr_wifi = true;
  Serial.println("CONNECTING WIFI"); 
  wifimilis = (millis() + wifi_checkDelay) ;
  }
}
void direct_Link(int link_num) {
        shost = Supla_server;
        String url = "";
        
           if (link_num == 1){ url = D_Link_1;}
    
      else{return;}
      
  WiFiClientSecure dclient;
  Serial.print("connecting to ");
  Serial.println(shost);
  if (!dclient.connect(shost, 443)) {
    Serial.println("connection failed ----------");
    return;
  }
  Serial.print("requesting URL: ");
  Serial.println(url);
  dclient.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + shost + "\r\n" +
               "User-Agent: ESP8266\r\n" +
               "Connection: close\r\n\r\n");
  Serial.println("request sent");
  while (dclient.connected()) {
    String line = dclient.readStringUntil('\n');
    if (line == "\r") {
      Serial.println("headers received");
      break;
    }
  }
  String line = dclient.readStringUntil('}');
  line = line + "}";
  if (line.indexOf("true") >0) {
  Serial.println("reply was:");Serial.println("===");  
  Serial.println(line);
  Serial.println("===");Serial.println("closing connection");
        
  } else {Serial.println("failed -----------");
      
  } 
}
Załączniki
Push_1_dzwonek.ino.d1_mini.zip
(306.74 KiB) Pobrany 11 razy
Awatar użytkownika
Robert Błaszczak
Posty: 911
Rejestracja: sob gru 22, 2018 8:55 pm
Lokalizacja: Zielona Góra
Kontaktowanie:

czw mar 05, 2020 4:44 pm

Ja bym dodał do tego obsługę drugiego przekaźnika załączającego fizyczny dzwonek :D
Pozdrawiam
Robert Błaszczak

Strona prywatna: https://www.blaszczak.pl
Jakość powietrza: https://robert.aqi.eco
ODPOWIEDZ