I need some extra eyes, as I’m struggling with conversion from Arduino to PlatformIO.
I have the following ESP8266 code and cannot seem to compile the MQTT portion. It worked fine when I was programming this in Arduino, but now I’ve switched to VScode/PaltformIO I get the following errors.
None of the “PubSubClient client” declaration work…
#include <Arduino.h>
#include <DNSServer.h>
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <AsyncElegantOTA.h>
#include <PubSubClient.h>
#include <SSID_access.h>
#define LED 2
#define updated 20210217
String topic = "jlight";
String mqtt_server = "mqtt.mydomain";
uint16_t mqtt_port = 51883;
String Jstate = "off";
String clientId = "ESP"; // IP address added later
int latest = micros();
// reconnect every 5 seconds
long lastReconnectAttempt = 0;
// auto publish every hour
long publishDelay = 3600000;
long lastPublish = -publishDelay;
long publish = 0;
DNSServer dnsServer;
AsyncWebServer server(80);
WiFiClient wifiClient;
PubSubClient client(mqtt_server, mqtt_port, callback, wifiClient);
//PubSubClient client(mqtt_server, mqtt_port, wifiClient);
//PubSubClient client(wifiClient);
boolean reconnect() {
Serial.print("Attempting MQTT connection from ");
Serial.print(WiFi.localIP());
Serial.print(" as ");
clientId = "ESP";
clientId += WiFi.localIP().toString();
Serial.println(clientId);
// Attempt to connect
if (client.connect(clientId.c_str(),mqtt_user,mqtt_password)) {
Serial.println("connected");
// Once connected, publish an announcement...
client.publish("jlight", (char*)Jstate.c_str());
// ... and resubscribe
client.subscribe("jlight/#");
}
return client.connected();
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] '");
String pl ="";
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
pl += (char)payload[i];
}
Serial.println("'");
// skip this message if triggered too soon.
long now = millis();
if (String(topic) == "j-light/set") {
Serial.print("Set command received: ");
Serial.println(pl);
// skip this message if triggered too soon. .
if ( (now-publish) < 3000 ) {
Serial.print("skipping the process as its been only ");
Serial.print(String(now-lastPublish));
Serial.println(" msec.");
} else {
publish = now;
if (pl == "off"){
Jstate = "off";
} else if (pl == "on") {
Jstate = "on";
}
// force a publish
lastPublish = -publishDelay;
}
}
}
// =============================================================================== //
// HTTP processing //
// =============================================================================== //
String handleJon() {
Jstate = "on";
String page;
page += "<!DOCTYPE html><html><head><title>Johs's ESP8266 server</title>";
page += "<meta http-equiv='refresh' content='2;url=/'/>";
page += "</head><body>";
page += "Turning on the J LED light.</br>";
page += "</body></html>";
return String(page);
}
String handleJoff() {
Jstate = "off";
String page;
page += "<!DOCTYPE html><html><head><title>Johs's ESP8266 server</title>";
page += "<meta http-equiv='refresh' content='2;url=/'/>";
page += "</head><body>";
page += "Turning off the J LED light.</br>";
page += "</body></html>";
return String(page);
}
String handleRoot(){
String JonBC = "off";
String JoffBC = "off";
(Jstate == "on")?JonBC="style='background-color:#0066FF;'":JonBC="";
(Jstate == "off")?JoffBC="style='background-color:#0066FF;'":JoffBC="";
String page = "<!DOCTYPE html><html><head><title>Johs's ESP8266 server</title>"
"<style>.button{background-color: #00b0f0; border: none; color: white; padding: 40px 64px; text-allign: center; text-decoration: none;display: inline-block; font-size:32px;}</style>"
"</head><body>"
"Hi! I am Josh's ESP8266.</p><hr>"
"<a href='j-on' class='button' ";
page += JonBC;
page += ";'>J on</a>"
"<a href='j-off' class='button' ";
page += JoffBC;
page += ";'>J off</a>"
"<hr><p>Got updated OTA! ";
page += updated;
page += "</body></html>";
return String(page);
}
void setup() {
// =============================================================================== //
// setup WIFI //
// =============================================================================== //
Serial.begin(115200);
pinMode(LED,OUTPUT);
WiFi.mode(WIFI_STA);
WiFi.begin(MY_SSID, SSID_PSW);
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(MY_SSID);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
// =============================================================================== //
// HTTP processing //
// =============================================================================== //
server.on("/j-on", HTTP_GET, [](AsyncWebServerRequest *request){
request->send(200, "text/html", handleJon());
});
server.on("/j-off", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(200, "text/html", handleJoff());
});
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(200, "text/html", handleRoot());
});
AsyncElegantOTA.begin(&server); // Start ElegantOTA
server.begin();
Serial.println("HTTP server started");
// =============================================================================== //
// MQTT processing //
// =============================================================================== //
//client.setServer(mqtt_server, 51883); //mqtt_port);
client.setCallback(callback);
//if (client.connect((char*) clientID.c_str())) {
if (client.connect(clientId)) {
Serial.println("Connected to MQTT broker");
Serial.print("Topic is: ");
Serial.println(topic);
if (client.publish(topic, Jstate)) {
Serial.println("Publish ok");
}
else {
Serial.println("Publish failed");
}
}
else {
Serial.println("MQTT connect failed");
Serial.println("Will reset and try again...");
abort();
}
}
void loop() {
// put your main code here, to run repeatedly:
// Check MQTT
long now = millis();
if (!client.connected()) {
if (now - lastReconnectAttempt > 5000) {
lastReconnectAttempt = now;
// Attempt to reconnect
if (reconnect()) {
lastReconnectAttempt = 0;
}
}
} else {
// Client connected
client.loop();
}
// changed auto publish time to 1/2 hour infrequent
if ((now - lastPublish) > publishDelay){
boolean retained = true;
if(client.publish("jlight", (char*)Jstate.c_str(),retained)) {
Serial.println("Publish ok");
}
else {
Serial.println("Publish failed");
}
lastPublish = now;
}
// Update LED state
if(Jstate == "off"){
digitalWrite(LED,HIGH);
Serial.print(".");
}
if(Jstate == "on"){
digitalWrite(LED,LOW);
Serial.print("o");
}
// Need to load new config?
AsyncElegantOTA.loop();
}
Errors:
Processing esp12e (platform: espressif8266; board: esp12e; framework: arduino)
-----------------------------------------------------------------------------------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/espressif8266/esp12e.html
PLATFORM: Espressif 8266 (2.6.3) > Espressif ESP8266 ESP-12E
HARDWARE: ESP8266 80MHz, 80KB RAM, 4MB Flash
PACKAGES:
- framework-arduinoespressif8266 3.20704.0 (2.7.4)
- tool-esptool 1.413.0 (4.13)
- tool-esptoolpy 1.30000.201119 (3.0.0)
- toolchain-xtensa 2.40802.200502 (4.8.2)
LDF: Library Dependency Finder -> http://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 33 compatible libraries
Scanning dependencies...
Dependency Graph
|-- <AsyncElegantOTA> 2.2.5
| |-- <ESP8266WiFi> 1.0
| |-- <ESPAsyncTCP-esphome> 1.2.3
| | |-- <ESP8266WiFi> 1.0
| |-- <ESPAsyncWebServer-esphome> 1.2.7
| | |-- <ESPAsyncTCP-esphome> 1.2.3
| | | |-- <ESP8266WiFi> 1.0
| | |-- <Hash> 1.0
| | |-- <ESP8266WiFi> 1.0
|-- <ESPAsyncTCP-esphome> 1.2.3
| |-- <ESP8266WiFi> 1.0
|-- <ESPAsyncWebServer-esphome> 1.2.7
| |-- <ESPAsyncTCP-esphome> 1.2.3
| | |-- <ESP8266WiFi> 1.0
| |-- <Hash> 1.0
| |-- <ESP8266WiFi> 1.0
|-- <PubSubClient> 2.8.0
|-- <DNSServer> 1.1.1
| |-- <ESP8266WiFi> 1.0
|-- <ESP8266WiFi> 1.0
Building in release mode
Compiling .pio/build/esp12e/src/main.cpp.o
src/main.cpp:31:45: error: 'callback' was not declared in this scope
PubSubClient client(mqtt_server, mqtt_port, callback, wifiClient);
^
src/main.cpp: In function 'void callback(char*, byte*, unsigned int)':
src/main.cpp:83:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < length; i++) {
^
src/main.cpp: In function 'void setup()':
src/main.cpp:213:30: error: no matching function for call to 'PubSubClient::connect(String&)'
if (client.connect(clientId)) {
^
src/main.cpp:213:30: note: candidates are:
In file included from src/main.cpp:7:0:
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:145:12: note: boolean PubSubClient::connect(const char*)
boolean connect(const char* id);
^
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:145:12: note: no known conversion for argument 1 from 'String' to 'const char*'
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:146:12: note: boolean PubSubClient::connect(const char*, const char*, const char*)
boolean connect(const char* id, const char* user, const char* pass);
^
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:146:12: note: candidate expects 3 arguments, 1 provided
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:147:12: note: boolean PubSubClient::connect(const char*, const char*, uint8_t, boolean, const char*)
boolean connect(const char* id, const char* willTopic, uint8_t willQos, boolean willRetain, const char* willMessage);
^
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:147:12: note: candidate expects 5 arguments, 1 provided
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:148:12: note: boolean PubSubClient::connect(const char*, const char*, const char*, const char*, uint8_t, boolean, const char*)
boolean connect(const char* id, const char* user, const char* pass, const char* willTopic, uint8_t willQos, boolean willRetain, const char* willMessage);
^
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:148:12: note: candidate expects 7 arguments, 1 provided
In file included from src/main.cpp:7:0:
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:149:12: note: boolean PubSubClient::connect(const char*, const char*, const char*, const char*, uint8_t, boolean, const char*, boolean)
boolean connect(const char* id, const char* user, const char* pass, const char* willTopic, uint8_t willQos, boolean willRetain, const char* willMessage, boolean cleanSession);
^
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:149:12: note: candidate expects 8 arguments, 1 provided
src/main.cpp:218:37: error: no matching function for call to 'PubSubClient::publish(String&, String&)'
if (client.publish(topic, Jstate)) {
^
src/main.cpp:218:37: note: candidates are:
In file included from src/main.cpp:7:0:
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:151:12: note: boolean PubSubClient::publish(const char*, const char*)
boolean publish(const char* topic, const char* payload);
^
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:151:12: note: no known conversion for argument 1 from 'String' to 'const char*'
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:152:12: note: boolean PubSubClient::publish(const char*, const char*, boolean)
boolean publish(const char* topic, const char* payload, boolean retained);
^
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:152:12: note: candidate expects 3 arguments, 2 provided
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:153:12: note: boolean PubSubClient::publish(const char*, const uint8_t*, unsigned int)
boolean publish(const char* topic, const uint8_t * payload, unsigned int plength);
^
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:153:12: note: candidate expects 3 arguments, 2 provided
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:154:12: note: boolean PubSubClient::publish(const char*, const uint8_t*, unsigned int, boolean)
boolean publish(const char* topic, const uint8_t * payload, unsigned int plength, boolean retained);
^
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:154:12: note: candidate expects 4 arguments, 2 provided
*** [.pio/build/esp12e/src/main.cpp.o] Error 1
Here is the error for the other setup:
WiFiClient wifiClient;
//PubSubClient client(mqtt_server, mqtt_port, callback, wifiClient);
PubSubClient client(mqtt_server, mqtt_port, wifiClient);
//PubSubClient client(wifiClient);
error:
Building in release mode
Compiling .pio/build/esp12e/src/main.cpp.o
src/main.cpp:32:55: error: no matching function for call to 'PubSubClient::PubSubClient(String&, uint16_t&, WiFiClient&)'
PubSubClient client(mqtt_server, mqtt_port, wifiClient);
^
src/main.cpp:32:55: note: candidates are:
In file included from src/main.cpp:7:0:
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:129:4: note: PubSubClient::PubSubClient(const char*, uint16_t, std::function<void(char*, unsigned char*, unsigned int)>, Client&, Stream&)
PubSubClient(const char*, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client, Stream&);
^
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:129:4: note: candidate expects 5 arguments, 3 provided
.pio/libdeps/esp12e/PubSubClient/src/PubSubClient.h:128:4: note: PubSubClient::PubSubClient(const char*, uint16_t, std::function<void(char*, unsigned char*, unsigned int)>, Client&)
PubSubClient(const char*, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client);
^
And all the libraries are up-to-date…