• Система автоматизации с открытым исходным кодом на базе esp8266/esp32 микроконтроллеров и приложения IoT Manager. Наша группа в Telegram

Помогите объединить парное реле и HC-SR501

Leegard

New member
Никак не получается слепить одно из двух. По отдельности каждый скетч работает, а вместе не хочет
Ошибки:
C:\Users\Alex\Documents\Arduino\rele\rele.ino: In function 'void handleNewMessages(int)':
rele:101:3: error: a function-definition is not allowed here before '{' token
101 | {
| ^
rele:137:3: error: a function-definition is not allowed here before '{' token
137 | {
| ^
C:\Users\Alex\Documents\Arduino\rele\rele.ino: At global scope:
rele:153:1: error: expected declaration before '}' token
153 | }
| ^
exit status 1
a function-definition is not allowed here before '{' token


Код:
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>

// Wifi network station credentials
#define WIFI_SSID "Xiaomi"
#define WIFI_PASSWORD "xxxxxxxx"
// Telegram BOT Token (Get from Botfather)
#define BOT_TOKEN "xxxxxxxxxxxxxxxxxxxxxxxxxx"

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; // last time messages' scan has been done

const int led1Pin = 3;
int led1Status = 0;
const int led2Pin = 13;
int led2Status = 0;

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 == "/ledon1")
    {
      digitalWrite(led1Pin, LOW); // turn the LED on (HIGH is the voltage level)
      led1Status = 1;
      bot.sendMessage(chat_id, "Led1 is ON", "");
    }

    if (text == "/ledoff1")
    {
      led1Status = 0;
      digitalWrite(led1Pin, HIGH); // turn the LED off (LOW is the voltage level)
      bot.sendMessage(chat_id, "Led1 is OFF", "");
    }
      if (text == "/status")
    {
      if (led1Status)
      {
        bot.sendMessage(chat_id, "Led1 is ON", "");
      }
      else
      {
        bot.sendMessage(chat_id, "Led1 is OFF", "");
      }
      if (text == "/ledon2")
    {
      digitalWrite(led2Pin, LOW); // turn the LED on (HIGH is the voltage level)
      led2Status = 1;
      bot.sendMessage(chat_id, "Led1 is ON", "");
    }

    if (text == "/ledoff2")
    {
      led2Status = 0;
      digitalWrite(led2Pin, HIGH); // turn the LED off (LOW is the voltage level)
      bot.sendMessage(chat_id, "Led1 is OFF", "");
    }

    if (text == "/status")
    {
      if (led2Status)
      {
        bot.sendMessage(chat_id, "Led2 is ON", "");
      }
      else
      {
        bot.sendMessage(chat_id, "Led2 is OFF", "");
      }
    }

    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 += "/ledon1 : to switch the Led ON\n";
      welcome += "/ledoff1 : to switch the Led OFF\n";
      welcome += "/ledon2 : to switch the Led ON\n";
      welcome += "/ledoff2 : to switch the Led OFF\n";
      welcome += "/status : Returns current status of LED\n";
      bot.sendMessage(chat_id, welcome, "Markdown");
    }
  }
}

void setup()
  {
  Serial.begin(115200);
  Serial.println();

  pinMode(led1Pin, OUTPUT);
  delay(10);
  digitalWrite(led1Pin, HIGH);
  pinMode(led2Pin, OUTPUT);
  delay(10);
  digitalWrite(led2Pin, HIGH);

  configTime(0, 0, "pool.ntp.org");
  secured_client.setTrustAnchors(&cert);
  Serial.print("Connecting to Wifi SSID ");
  Serial.print(WIFI_SSID);
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  while (WiFi.status() != WL_CONNECTED)
  {
    Serial.print(".");
    delay(500);
  }
  Serial.print("\nWiFi connected. IP address: ");
  Serial.println(WiFi.localIP());

  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();
  }
}
}
 

Leegard

New member
уже мозг кипит от них
вроде все что открыл - закрыл, а ругается
 

esp340

Active member
Значит не все или не там закрыли где нужно.
Например, мне ИДЕ подсказывает что для ф-ции void handleNewMessages(int numNewMessages) закрывающая скобка стоит в самом конце. А где должна быть? Подумайте
 
Сверху Снизу