Друзья, помогите, пожалуйста, с кодом.
Задача отправлять в топик MQTT нажатие кнопки, т.е. нажата - сигнал идёт, не нажата - не идёт
Плата Lolin. Кнопки подключены к D1, D2, D3.
//#include <OneWire.h>
#include <MQTT.h>
#include <PubSubClient.h>
#include <MQTTClient.h>
#include <WiFi.h>
#include <Button.h>
Button btn1(2); // указываем пин для первой кнопки
Button btn2(3); // указываем пин для второй кнопки
Button btn3(4); // указываем пин для третьей кнопки
char *ssid = "***"; // Имя вайфай точки доступа
const char *password = "***"; // Пароль от точки доступа
const char *mqtt_server = "***"; // Имя сервера MQTT Cluster
const int mqtt_port = ***; // Порт для подключения к серверу MQTT
const char *mqtt_user = "***"; // Логин от сервер
const char *mqtt_pass = "***"; // Пароль от сервера
#define BUFFER_SIZE 100
WiFiClient wclient;
**PubSubClient client(wclient, mqtt_server, mqtt_port);**
void setup() {
// подключаемся к wi-fi
if (WiFi.status() != WL_CONNECTED) {
Serial.print("Connecting to ");
Serial.print(ssid);
Serial.println("...");
WiFi.begin(ssid, password);
//if (WiFi.waitForConnectResult() != WL_CONNECTED)
// return;
//Serial.println("WiFi connected");
}
// подключаемся к MQTT серверу
if (WiFi.status() == WL_CONNECTED) {
if (!client.connected()) {
Serial.println("Connecting to MQTT server");
**if (client.connect(MQTT::Connect("arduinoClient2")**
.set_auth(mqtt_user, mqtt_pass))) {
Serial.println("Connected to MQTT server");
client.subscribe("BTN1"); // подписывааемся на топик с данными для 1-ой кнопки btn1
client.subscribe("BTN2"); // подписывааемся на топик с данными для 2-ой кнопки btn2
client.subscribe("BTN3"); // подписывааемся на топик с данными для 3-ой кнопки btn3
} else {
Serial.println("Could not connect to MQTT server");
}
}
//if (client.connected()){
// client.loop();
//TempSend();
//}
}
}
//конец основного цикла
void loop() {
**btn1(2, INPUT_PULLUP);**
** btn2(3, INPUT_PULLUP);**
** btn3(4, INPUT_PULLUP);**
** if (digitalRead(btn1) == LOW) {**
** MQTT.send("BTN1", "pressed");**
** }**
** if (digitalRead(btn2) == LOW) {**
** MQTT.send("BTN2", "pressed");**
** }**
** if (digitalRead(btn3) == LOW) {**
** MQTT.send("BTN3", "pressed");**
}
delay(100);
}
Сегодня обновилась библиотека PubSubClient.h =, и к кнопкам добавилась проблема:
C:\Users\3ЛТП\Documents\Arduino\MP_V01\MP_V01.ino:12:14: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
C:\Users\3ЛТП\Documents\Arduino\MP_V01\MP_V01.ino:23:52: error: no matching function for call to ‘PubSubClient::PubSubClient(WiFiClient&, const char*&, const int&)’
In file included from C:\Users\3ЛТП\Documents\Arduino\MP_V01\MP_V01.ino:3:
c:\Users\3���\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:129:4: note: candidate: ‘PubSubClient::PubSubClient(const char*, uint16_t, std::function<void(char*, unsigned char*, unsigned int)>, Client&, Stream&)’
129 | PubSubClient(const char*, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client, Stream&);
| ^~~~~~~~~~~~
c:\Users\3���\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:129:4: note: candidate expects 5 arguments, 3 provided
c:\Users\3���\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:128:4: note: candidate: ‘PubSubClient::PubSubClient(const char*, uint16_t, std::function<void(char*, unsigned char*, unsigned int)>, Client&)’
128 | PubSubClient(const char*, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client);
| ^~~~~~~~~~~~
c:\Users\3���\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:128:4: note: candidate expects 4 arguments, 3 provided
c:\Users\3���\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:127:4: note: candidate: ‘PubSubClient::PubSubClient(const char*, uint16_t, Client&, Stream&)’
127 | PubSubClient(const char*, uint16_t, Client& client, Stream&);
| ^~~~~~~~~~~~
c:\Users\3���\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:127:4: note: candidate expects 4 arguments, 3 provided
c:\Users\3���\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:126:4: note: candidate: ‘PubSubClient::PubSubClient(const char*, uint16_t, Client&)’
126 | PubSubClient(const char*, uint16_t, Client& client);
| ^~~~~~~~~~~~
c:\Users\3���\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:126:17: note: no known conversion for argument 1 from ‘WiFiClient’ to ‘const char*’
126 | PubSubClient(const char*, uint16_t, Client& client);
| ^~~~~~~~~~~
c:\Users\3���\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:125:4: note: candidate: ‘PubSubClient::PubSubClient(uint8_t*, uint16_t, std::function<void(char*, unsigned char*, unsigned int)>, Client&, Stream&)’
125 | PubSubClient(uint8_t , uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client, Stream&);
| ^~~~~~~~~~~~
c:\Users\3���\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:125:4: note: candidate expects 5 arguments, 3 provided
c:\Users\3���\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:124:4: note: candidate: 'PubSubClient::PubSubClient(uint8_t, uint16_t, std::function<void(char*, unsigned char*, unsigned int)>, Client&)’
124 | PubSubClient(uint8_t , uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client);
| ^~~~~~~~~~~~
c:\Users\3���\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:124:4: note: candidate expects 4 arguments, 3 provided
c:\Users\3���\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:123:4: note: candidate: 'PubSubClient::PubSubClient(uint8_t, uint16_t, Client&, Stream&)’
123 | PubSubClient(uint8_t , uint16_t, Client& client, Stream&);
| ^~~~~~~~~~~~
c:\Users\3���\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:123:4: note: candidate expects 4 arguments, 3 provided
c:\Users\3���\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:122:4: note: candidate: 'PubSubClient::PubSubClient(uint8_t, uint16_t, Client&)’
122 | PubSubClient(uint8_t , uint16_t, Client& client);
| ^~~~~~~~~~~~
c:\Users\3���\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:122:17: note: no known conversion for argument 1 from ‘WiFiClient’ to 'uint8_t’ {aka ‘unsigned char*’}
122 | PubSubClient(uint8_t , uint16_t, Client& client);
| ^~~~~~~~~
c:\Users\3���\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:121:4: note: candidate: 'PubSubClient::PubSubClient(IPAddress, uint16_t, std::function<void(char, unsigned char*, unsigned int)>, Client&, Stream&)’
121 | PubSubClient(IPAddress, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client, Stream&);
| ^~~~~~~~~~~~
c:\Users\3���\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:121:4: note: candidate expects 5 arguments, 3 provided
c:\Users\3���\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:120:4: note: candidate: ‘PubSubClient::PubSubClient(IPAddress, uint16_t, std::function<void(char*, unsigned char*, unsigned int)>, Client&)’
120 | PubSubClient(IPAddress, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client);
| ^~~~~~~~~~~~
c:\Users\3���\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:120:4: note: candidate expects 4 arguments, 3 provided
c:\Users\3���\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:119:4: note: candidate: ‘PubSubClient::PubSubClient(IPAddress, uint16_t, Client&, Stream&)’
119 | PubSubClient(IPAddress, uint16_t, Client& client, Stream&);
| ^~~~~~~~~~~~
c:\Users\3���\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:119:4: note: candidate expects 4 arguments, 3 provided
c:\Users\3���\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:118:4: note: candidate: ‘PubSubClient::PubSubClient(IPAddress, uint16_t, Client&)’
118 | PubSubClient(IPAddress, uint16_t, Client& client);
| ^~~~~~~~~~~~
c:\Users\3���\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:118:17: note: no known conversion for argument 1 from ‘WiFiClient’ to ‘IPAddress’
118 | PubSubClient(IPAddress, uint16_t, Client& client);
| ^~~~~~~~~
c:\Users\3���\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:117:4: note: candidate: ‘PubSubClient::PubSubClient(Client&)’
117 | PubSubClient(Client& client);
| ^~~~~~~~~~~~
c:\Users\3���\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:117:4: note: candidate expects 1 argument, 3 provided
c:\Users\3���\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:116:4: note: candidate: ‘PubSubClient::PubSubClient()’
116 | PubSubClient();
| ^~~~~~~~~~~~
c:\Users\3���\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:116:4: note: candidate expects 0 arguments, 3 provided
c:\Users\3���\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:88:7: note: candidate: ‘PubSubClient::PubSubClient(const PubSubClient&)’
88 | class PubSubClient : public Print {
| ^~~~~~~~~~~~
c:\Users\3���\Documents\Arduino\libraries\PubSubClient\src/PubSubClient.h:88:7: note: candidate expects 1 argument, 3 provided
C:\Users\3ЛТП\Documents\Arduino\MP_V01\MP_V01.ino: In function ‘void setup()’:
C:\Users\3ЛТП\Documents\Arduino\MP_V01\MP_V01.ino:42:26: error: ‘MQTT’ has not been declared
C:\Users\3ЛТП\Documents\Arduino\MP_V01\MP_V01.ino: In function ‘void loop()’:
C:\Users\3ЛТП\Documents\Arduino\MP_V01\MP_V01.ino:62:23: error: no match for call to ‘(Button) (int, int)’
C:\Users\3ЛТП\Documents\Arduino\MP_V01\MP_V01.ino:63:23: error: no match for call to ‘(Button) (int, int)’
C:\Users\3ЛТП\Documents\Arduino\MP_V01\MP_V01.ino:64:23: error: no match for call to ‘(Button) (int, int)’
C:\Users\3ЛТП\Documents\Arduino\MP_V01\MP_V01.ino:66:19: error: cannot convert ‘Button’ to ‘uint8_t’ {aka ‘unsigned char’}
In file included from C:\Users\3���\AppData\Local\arduino\sketches\9892DD7420FD487A608BC501251FDBD9\sketch\MP_V01.ino.cpp:1:
C:\Users\3���\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\cores\esp8266/Arduino.h:171:25: note: initializing argument 1 of ‘int digitalRead(uint8_t)’
171 | int digitalRead(uint8_t pin);
| ~~~~~~^
C:\Users\3ЛТП\Documents\Arduino\MP_V01\MP_V01.ino:67:5: error: ‘MQTT’ was not declared in this scope; did you mean ‘MQTT_H’?
C:\Users\3ЛТП\Documents\Arduino\MP_V01\MP_V01.ino:69:19: error: cannot convert ‘Button’ to ‘uint8_t’ {aka ‘unsigned char’}
In file included from C:\Users\3���\AppData\Local\arduino\sketches\9892DD7420FD487A608BC501251FDBD9\sketch\MP_V01.ino.cpp:1:
C:\Users\3���\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\cores\esp8266/Arduino.h:171:25: note: initializing argument 1 of ‘int digitalRead(uint8_t)’
171 | int digitalRead(uint8_t pin);
| ~~~~~~^
C:\Users\3ЛТП\Documents\Arduino\MP_V01\MP_V01.ino:70:5: error: ‘MQTT’ was not declared in this scope; did you mean ‘MQTT_H’?
C:\Users\3ЛТП\Documents\Arduino\MP_V01\MP_V01.ino:72:19: error: cannot convert ‘Button’ to ‘uint8_t’ {aka ‘unsigned char’}
In file included from C:\Users\3���\AppData\Local\arduino\sketches\9892DD7420FD487A608BC501251FDBD9\sketch\MP_V01.ino.cpp:1:
C:\Users\3���\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\cores\esp8266/Arduino.h:171:25: note: initializing argument 1 of ‘int digitalRead(uint8_t)’
171 | int digitalRead(uint8_t pin);
| ~~~~~~^
C:\Users\3ЛТП\Documents\Arduino\MP_V01\MP_V01.ino:73:5: error: ‘MQTT’ was not declared in this scope; did you mean ‘MQTT_H’?
grpc: error while marshaling: string field contains invalid UTF-8
Compilation error: no matching function for call to ‘PubSubClient::PubSubClient(WiFiClient&, const char*&, const int&)’
Прошу помощи дельным советом. Уровень знаний у меня 2 по 10-ти бальной шкале, сильно не пинайте.
Тут if (client.connect(MQTT::Connect(“arduinoClient2”) замена на MQTT_H ничего не даёт, хотя в ошибках упоминается.