PCF8574N a spinani LED

Odpovědět
Kony
Příspěvky: 382
Registrován: 09 dub 2020, 11:43
Reputation: 0

PCF8574N a spinani LED

Příspěvek od Kony » 09 lis 2022, 18:52

Ahoj, snazim se pomocí tohoto kodu :

Kód: Vybrat vše

#include <ESP8266WiFi.h>
#include <WiFiClient.h> 
#include <ESP8266HTTPClient.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include "PCF8574.h"
#include <Wire.h>

PCF8574 pcf(0x24);

const char* wifiName = "";
const char* wifiPass = "";
#define BOT_TOKEN ""
const unsigned long BOT_MTBS = 1000; // mean time between scan messages
X509List cert(TELEGRAM_CERTIFICATE_ROOT);
WiFiClientSecure secured_client;
UniversalTelegramBot bot(BOT_TOKEN, secured_client);
unsigned long bot_lasttime;

void handleNewMessages(int numNewMessages)
{
  Serial.print("handleNewMessages ");
  Serial.println(numNewMessages);

  for (int i = 0; i < numNewMessages; i++)
  {
    String chat_id = bot.messages[i].chat_id;
    String text = bot.messages[i].text;

    String from_name = bot.messages[i].from_name;
    if (from_name == "")
      from_name = "Guest";

    if (text == "/1otevri")
    {
      pcf.write(0, HIGH); // turn the LED on (HIGH is the voltage level)
      bot.sendMessage(chat_id, "Otevřena kóje 1", "");
      Serial.println("Otevreno 1");
      pcf.write(0, LOW);
      Serial.println("Zavreno 1");
      bot.sendMessage(chat_id, "Zavřena kóje 1", "");
    }

    if (text == "/2otevri")
    {
      pcf.write(1, HIGH); // turn the LED off (LOW is the voltage level)
      bot.sendMessage(chat_id, "Otevřena kóje 2", "");
      Serial.println("Otevreno");
      pcf.write(1, LOW);
      Serial.println("Zavreno");
      bot.sendMessage(chat_id, "Zavřena kóje 2", "");
    }

    if (text == "/3otevri")
    {
      pcf.write(2, HIGH); // turn the LED off (LOW is the voltage level)
      bot.sendMessage(chat_id, "Otevřena kóje 3", "");
      Serial.println("Otevreno");
      pcf.write(2, LOW);
      Serial.println("Zavreno");
      bot.sendMessage(chat_id, "Zavřena kóje 3", "");
    }

     if (text == "/4otevri")
    {
      pcf.write(3, HIGH); // turn the LED off (LOW is the voltage level)
      bot.sendMessage(chat_id, "Otevřena kóje 4", "");
      Serial.println("Otevreno");
      pcf.write(3, LOW);
      Serial.println("Zavreno");
      bot.sendMessage(chat_id, "Zavřena kóje 4", "");
    }

    if (text == "/5otevri")
    {
      pcf.write(4, HIGH); // turn the LED off (LOW is the voltage level)
      bot.sendMessage(chat_id, "Otevřena kóje 5", "");
      Serial.println("Otevreno");
      pcf.write(4, LOW);
      Serial.println("Zavreno");
      bot.sendMessage(chat_id, "Zavřena kóje 5", "");
    }

    if (text == "/start")
    {
      String welcome = "Welcome to Universal Arduino Telegram Bot library, " + from_name + ".\n";
      welcome += "This is Flash Led Bot example.\n\n";
      welcome += "/1on : to switch the Led 1 ON\n";
      welcome += "/1off : to switch the Led 1 OFF\n";
      welcome += "/2on : to switch the Led 2 ON\n";
      welcome += "/2off : to switch the Led 2 OFF\n";
      bot.sendMessage(chat_id, welcome, "Markdown");
    }
  }
}

void setup() {
  Serial.begin(9600);
  Serial.println();
  pcf.begin();
  pcf.write(0, OUTPUT);
  pcf.write(0, LOW);
  pcf.write(1, OUTPUT);
  pcf.write(1, LOW);
  pcf.write(2, OUTPUT);
  pcf.write(2, LOW);
  pcf.write(3, OUTPUT);
  pcf.write(3, LOW);
  pcf.write(4, OUTPUT);
  pcf.write(4, LOW);
  configTime(0, 0, "pool.ntp.org");      // get UTC time via NTP
  secured_client.setTrustAnchors(&cert); // Add root certificate for api.telegram.org
  
  Serial.print("Connecting to ");
  Serial.println(wifiName);

  WiFi.begin(wifiName, wifiPass);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());   //You can get IP address assigned to ESP
   // Check NTP/Time, usually it is instantaneous and you can delete the code below.
  Serial.print("Retrieving time: ");
  time_t now = time(nullptr);
  while (now < 24 * 3600)
  {
    Serial.print(".");
    delay(100);
    now = time(nullptr);
  }
  Serial.println(now);
}

void loop() {

 if (millis() - bot_lasttime > BOT_MTBS)
  {
    int numNewMessages = bot.getUpdates(bot.last_message_received + 1);

    while (numNewMessages)
    {
      Serial.println("got response");
      handleNewMessages(numNewMessages);
      numNewMessages = bot.getUpdates(bot.last_message_received + 1);
    }

    bot_lasttime = millis();
  }
}
ovladat LED pripojené na PCF8574N pomoci Telegram.... Je to na NodeMCU, ale dela to různé věci, po napsání příkazu na telegram se jednou LEd rozsvítí, když zadám na další LED, tak se třeba vubec nerozsvítí ta správna, nebo zase zůstane pořád svítit...

už nevím kde hledat chybu

Pablo74
Příspěvky: 43
Registrován: 03 lis 2019, 17:00
Reputation: 0

Re: PCF8574N a spinani LED

Příspěvek od Pablo74 » 11 lis 2022, 17:00

Nějak se v tom kódu ztrácím, ale jestli to chápu dobře, máš v poli bot.messages[] jednotlivý zprávy (příkazy), ve funkci handleNewMessages() procházíš polem od počátku do konce (podle indexu) a pokud je zpráva příkazem (např. /1otevri), tento provedeš.

Ale vypadá to, že to pole zpráv nijak nečistíš, tj. provedeš příkaz otevření/zavření (ty to ověřuješ na LED diodách), takže jakmile přijde nová zpráva, přidá se do pole bot.messages[] na konec; znovu procházíš pole od začátku a znovu provádíš už provedený příkazy.

Je to tak?

Odpovědět

Kdo je online

Uživatelé prohlížející si toto fórum: Žádní registrovaní uživatelé a 11 hostů