Skocz do zawartości

Pomiar prądu + czujnik poziomu cieczy


Pomocna odpowiedź

(edytowany)
4 minuty temu, _LM_ napisał:

W razie wątpliwości w to co napisałem powyżej

wemos-d1-r2-pinout.thumb.jpg.c6f65f08a41673629dfc19b86e6eb578.jpg

Niestety na niekompatybilności opisów arduinowo/chińskich nic nie poradzę. 

 

Pokazujesz inny moduł, mam inne oznakowanie PINów na płytce.

Może inaczej, co wg Ciebie powinienem napisać w kodzie i do którego PIN podłączyć wejście, aby to zadziałało?

obraz.thumb.png.3270a78d56e6de5f0413f934885451bf.png

Edytowano przez Krisk
15 minut temu, Krisk napisał:

zamiast D2 powinienem napisać GPIO4

zamiast D2 powinieneś wpisać po prostu numer GPIO do którego podłączyłeś pływak.

Jeśli masz podłączony pod GPIO4, to wpisujesz po prostu 4.

  • Lubię! 1

To tak nie do końca... metodą prób i błędów doszedłem do tego, że jak podłaczyłem pod PIN D7 to w kodzie musiałem użyć D9... I działa

Ale PIN D11 w kodzie i płytce się zgadzają, wtf...

Może mam złą płytkę wybraną? (LOLIN (WeMOS) D1 R1)

 

(edytowany)

W sumie nie dziwię się że się zakręciłeś, mało że płytki mają różne oznaczenia GPIO w tych samych miejscach, to jeszcze te mapowania że D2 raz jest GPIO2 a innym razem GPIO4. No mi by to nie pomogło na początku drogi. 

Edytowano przez _LM_

Esp8266 różnią się od siebie tylko pojemnością flash, pinologia jest zawsze taka sama. Nie podpowiem którą płytkę wybrać w IDE bo dla mnie to zawsze jest ten sam układ tylko inaczej nazwany. Niestety musisz sprawdzić samemu który pin wyprowadzony to faktycznie ten którego chcesz użyć taki urok tych klonów. 

13 minut temu, Krisk napisał:

A jaką płytkę powinienem wybrać?

Teoretycznie dokładnie tą, jakiej używasz. Zwróć uwagę, że w menu jest jakieś 5 pozycji, których nazwa zawiera LOLIN (WeMOS).

Twórcy Arduino IDE chcieli zadbać o przenoszalność programów pomiędzy różnymi płytkami, o różnie nazywanych wyprowadzeniach i stąd potrzeba stosowania definicji "tłumaczących", zawartych w plikach pins_arduino.h. Jak widać jednak różnie to działa, przede wszystkim dlatego że jest wiele różnych oznaczeń handlowych. Pozostaje - jak pisze @_LM_ - samemu sprawdzić doświadczalnie jak to jest połączone.

Lub znaleźć jednak jakiegoś gotowca w sieci.

Hej,

Znalazłem fajny kod, który prawie pasuje do mojego pomysłu, tylko on jest na 1 kontaktron, próbuję go przerobić na 2, jednak ten drugi cały czas nie jest widoczny, czy ktoś mógłby na to zerknąć i pomóc- gdzie robię błąd?

Link do oryginalnego programu:

https://randomnerdtutorials.com/esp8266-nodemcu-door-status-telegram/

A to przerobiony:

/* 
  Rui Santos
  Complete project details at https://RandomNerdTutorials.com/esp8266-nodemcu-door-status-telegram/
  
  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files.
  
  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.
*/

#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <ArduinoJson.h>

// Set GPIOs for LED and reedswitch
const int reedSwitch = 0;
const int reedSwitch1 = 15;
const int led = 13; //optional
const int led1 = 12;

// Detects whenever the door changed state
bool changeState = false;
bool changeState1 = false;

// Holds reedswitch state (1=opened, 0=close)
bool state;
bool state1;
String doorState;
String doorState1;

// Auxiliary variables (it will only detect changes that are 1500 milliseconds apart)
unsigned long previousMillis = 0; 
const long interval = 1500;

const char* ssid = "xxxxxxx";
const char* password = "xxxxxxx";

// Initialize Telegram BOT
#define BOTtoken "xxxxxxxxxxxxxx:xxxxxxxxxxxxxxxxxxx"  // your Bot Token (Get from Botfather)

// Use @myidbot to find out the chat ID of an individual or a group
// Also note that you need to click "start" on a bot before it can
// message you
#define CHAT_ID "xxxxxxxxxxxxx"

X509List cert(TELEGRAM_CERTIFICATE_ROOT);
WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);

// Runs whenever the reedswitch changes state
ICACHE_RAM_ATTR void zmianaStanuPlywaka1() {
  Serial.println("State changed");
  changeState = true;
  }

ICACHE_RAM_ATTR void zmianaStanuPlywaka2() {
  Serial.println("State changed2");
  changeState1 = true;
}
void setup() {
  // Serial port for debugging purposes
  Serial.begin(115200);
  configTime(0, 0, "pool.ntp.org");      // get UTC time via NTP
  client.setTrustAnchors(&cert); // Add root certificate for api.telegram.org
  

  // Read the current door state
  pinMode(reedSwitch, INPUT_PULLUP);
  pinMode(reedSwitch1, INPUT_PULLUP);
  state = digitalRead(reedSwitch);
  state1 = digitalRead(reedSwitch1);

  // Set LED state to match door state
  pinMode(led, OUTPUT);
  pinMode(led1, OUTPUT);
  digitalWrite(led, state);
  digitalWrite(led, state1);

  
  // Set the reedswitch pin as interrupt, assign interrupt function and set CHANGE mode
  attachInterrupt(digitalPinToInterrupt(reedSwitch), zmianaStanuPlywaka1, CHANGE);

 

  // Connect to Wi-Fi
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
    }
    Serial.println("");
    Serial.println("WiFi connected");  

    bot.sendMessage(CHAT_ID, "Bot started up", "");
}

void loop() {
  if (changeState){
    unsigned long currentMillis = millis();
    if(currentMillis - previousMillis >= interval) {
      previousMillis = currentMillis;
      // If a state has occured, invert the current door state   
        state = !state;
        if(state) {
          doorState = "bez wody";
        }
        else{
          doorState = "woda";
        }
        digitalWrite(led, state);
        changeState = false;
        Serial.println(state);
        Serial.println(doorState);
        
        //Send notification
        bot.sendMessage(CHAT_ID, "Poziom plywaka 1 " + doorState, "");
    }  

  if (changeState1){
    unsigned long currentMillis = millis();
    if(currentMillis - previousMillis >= interval) {
      previousMillis = currentMillis;
      // If a state has occured, invert the current door state   
        state1 = !state1;
        if(state1) {
          doorState1 = "bez wody";
        }
        else{
          doorState1 = "woda";
        }
        digitalWrite(led1, state1);
        changeState1 = false;
        Serial.println(state1);
        Serial.println(doorState1);
        
        //Send notification
        bot.sendMessage(CHAT_ID, "Poziom plywaka 2 " + doorState1, "");
    }  

  }
  }
}

 

attachInterrupt(digitalPinToInterrupt(reedSwitch1), zmianaStanuPlywaka2, CHANGE);

Musisz dodac drugie przerwanie do tego drugiego kontaktronu to jak wyżej, i powinno działać (jeśli reszta jest ok)

Dodałem to drugie przerwanie system się załącza, ale i tak tylko 1 czujnik reaguje, ma ktoś jakiś pomysł?

Tak wygląda teraz kod

/* 
  Rui Santos
  Complete project details at https://RandomNerdTutorials.com/esp8266-nodemcu-door-status-telegram/
  
  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files.
  
  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.
*/

#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <ArduinoJson.h>

// Set GPIOs for LED and reedswitch
const int reedSwitch = 0;
const int reedSwitch1 = 15;
const int led = 13; //optional
const int led1 = 12;

// Detects whenever the door changed state
bool changeState = false;
bool changeState1 = false;

// Holds reedswitch state (1=opened, 0=close)
bool state;
bool state1;
String doorState;
String doorState1;

// Auxiliary variables (it will only detect changes that are 1500 milliseconds apart)
unsigned long previousMillis = 0; 
const long interval = 1500;

const char* ssid = "xxxxxxx";
const char* password = "xxxxxxx";

// Initialize Telegram BOT
#define BOTtoken "xxxxxxxxxxxxxxxx"  // your Bot Token (Get from Botfather)

// Use @myidbot to find out the chat ID of an individual or a group
// Also note that you need to click "start" on a bot before it can
// message you
#define CHAT_ID "xxxxxxxxxx"

X509List cert(TELEGRAM_CERTIFICATE_ROOT);
WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);

// Runs whenever the reedswitch changes state
ICACHE_RAM_ATTR void zmianaStanuPlywaka1() {
  Serial.println("State changed 1");
  changeState = true;
  }
ICACHE_RAM_ATTR void zmianaStanuPlywaka2() {
  Serial.println("State changed 2");
  changeState1 = true;
}
void setup() {
  // Serial port for debugging purposes
  Serial.begin(115200);
  configTime(0, 0, "pool.ntp.org");      // get UTC time via NTP
  client.setTrustAnchors(&cert); // Add root certificate for api.telegram.org

  // Read the current door state
  pinMode(reedSwitch, INPUT_PULLUP);
  pinMode(reedSwitch1, INPUT_PULLUP);
  state = digitalRead(reedSwitch);
  state1 = digitalRead(reedSwitch1);

  // Set LED state to match door state
  pinMode(led, OUTPUT);
  pinMode(led1, OUTPUT);
  digitalWrite(led, state);
  digitalWrite(led1, state1);

  
  // Set the reedswitch pin as interrupt, assign interrupt function and set CHANGE mode
  attachInterrupt(digitalPinToInterrupt(reedSwitch), zmianaStanuPlywaka1, CHANGE);
  attachInterrupt(digitalPinToInterrupt(reedSwitch1), zmianaStanuPlywaka2, CHANGE);
 

  // Connect to Wi-Fi
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
    }
    Serial.println("");
    Serial.println("WiFi connected");  

    bot.sendMessage(CHAT_ID, "System aktywny", "");
}

void loop() {
  if (changeState){
    unsigned long currentMillis = millis();
    if(currentMillis - previousMillis >= interval) {
      previousMillis = currentMillis;
      // If a state has occured, invert the current door state   
        state = !state;
        if(state) {
          doorState = "bez wody";
         
        }
        else{
          doorState = "woda";
        }
         digitalWrite(led, state);
        changeState = false;
        Serial.println(state);
        Serial.println(doorState);
        
        //Send notification
        bot.sendMessage(CHAT_ID, "Poziom plywaka 1 " + doorState, "");
    }  

  if (changeState1){
    unsigned long currentMillis = millis();
    if(currentMillis - previousMillis >= interval) {
      previousMillis = currentMillis;
      // If a state has occured, invert the current door state   
        state1 = !state1;
        if(state1) {
          doorState1 = "bez wody";
        }
        else{
          doorState1 = "woda";
        }
        digitalWrite(led1, state1);
        changeState1 = false;
        Serial.println(state1);
        Serial.println(doorState1);
        
        //Send notification
        bot.sendMessage(CHAT_ID, "Poziom plywaka 2 " + doorState1, "");
    }  

  }
  }
}

 

W procedurach obsługi przerwań nie powinno się stosować polecenia Serial.print().

Staramy się, by procedury te wykonywały się jak najszybciej i nie zawierały wywołań funkcji korzystających z przerwań.

  • Lubię! 1

Bądź aktywny - zaloguj się lub utwórz konto!

Tylko zarejestrowani użytkownicy mogą komentować zawartość tej strony

Utwórz konto w ~20 sekund!

Zarejestruj nowe konto, to proste!

Zarejestruj się »

Zaloguj się

Posiadasz własne konto? Użyj go!

Zaloguj się »
×
×
  • Utwórz nowe...