I've been out of the scene for quite some time for personal reasons. Trying to get back into things that make me happy. I need help with this sketch. I think the errors may be due to changes in the IDE. Pretty sure it was working fine before. @PaulRB, @pert
In this sketch just want to connect to the wifi and mqtt server.
/*
Connect Lolin ESP8266-Pro to Mosquitto MQTT on Raspberry Pi3(b)
Nick Sebring
04-27-2025
*/
//#include <EEPROM.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
//#include <Wire.h>
#include <SPI.h>
// Connect to the WiFi
const char* ssid = "mySSID";
const char* password = "myPASSWORD";
const char* mqtt_server = "192.168.#.###";
WiFiClient espClient;
PubSubClient client(espClient);
boolean reconnect() { // **********************************************************
// Loop until we're reconnected
while (!client.connected()) {
Serial.print (F("Contacting MQTT server..."));
// Attempt to connect
if (client.connect("BME280")) { //assign a "client name". Each wemos must have a unique name
Serial.println (F("connected"));
// ... SUBSCRIBE TO TOPICS
// client.subscribe("Upsatairs/Mbr/BME280/TempF");
// client.subscribe("Upstairs/Mbr/BME280/TempC");
// client.subscribe("Upstairs/Mbr/BME280/Humidity");
// client.subscribe("Upstairs/Mbr/BME280/Pressure");
return client.connected();
Serial.print (F("Failed to connect. "));
Serial.println (F(" Attempting connection again in 3 seconds"));
// Wait 3 seconds before retrying
// delay(3000);
return 0;
}
}
}
void setup()
{
{
Serial.begin(9600);
client.setServer(mqtt_server, 1883);
}
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print (F("Connecting to "));
Serial.println(ssid);
WiFi.begin(ssid, password);
WiFi.mode(WIFI_STA);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
//Serial.begin(9600);
Serial.print (F("."));
}
Serial.println (F(""));
Serial.println (F("WiFi connected"));
// Print the IP address
Serial.print (F("Local IP: "));
Serial.println(WiFi.localIP());
// *********************************************************************************
void loop(){
if (!client.connected()) {
reconnect();
}
client.loop();
}
Errors:
C:\Users\marin\OneDrive\Documents\Arduino\MQTT_Connect\MQTT_Connect.ino: In function 'void setup()':
C:\Users\marin\OneDrive\Documents\Arduino\MQTT_Connect\MQTT_Connect.ino:82:12: error: a function-definition is not allowed here before '{' token
82 | void loop(){
| ^
C:\Users\marin\OneDrive\Documents\Arduino\MQTT_Connect\MQTT_Connect.ino:88:1: error: expected '}' at end of input
88 | }
| ^
C:\Users\marin\OneDrive\Documents\Arduino\MQTT_Connect\MQTT_Connect.ino:54:1: note: to match this '{'
54 | {
| ^
C:\Users\marin\OneDrive\Documents\Arduino\MQTT_Connect\MQTT_Connect.ino: In function 'boolean reconnect()':
C:\Users\marin\OneDrive\Documents\Arduino\MQTT_Connect\MQTT_Connect.ino:50:1: error: control reaches end of non-void function [-Werror=return-type]
50 | }
| ^
cc1plus.exe: some warnings being treated as errors
exit status 1
Compilation error: a function-definition is not allowed here before '{' token
That sketch never worked as it is now presented, and the problems have nothing to do with the IDE.
A simple auto formatting starting with the setup() function reveals that you are missing the closing } for the setup() function, and the loop() function is now embedded within the setup() function, which is obviously incorrect.
@marine_hm_again if the abovementioned issues with your presented code resulted from multiple cut-and-pastes not accurately getting the whole thing, please be aware that there is a command in the IDE, "copy for forum", that makes posting your code accurately a painless action. Please use it in future.
If the payload is 0 or 1 then why read its contents with a for loop ?
Exactly what is being sent, ie what data type, how many bytes and is the payload terminated with a line ending ?
What is the length of the payload received by the Arduino ?
I'm pretty sure that was for my sketch that had multiple subscribed topics.
IE..
if (strcmp(topic, "Remote/Relay1") == 0) {
if (strcmp(topic, "Remote/Relay2") == 0) {
if (strcmp(topic, "Remote/Relay3") == 0) {
If you're talking about:
for (int i = 0; i < length; i++) {
Honestly, I don't remember. Bits and pieces I had help with. NO, I had help with quite a bit.
Sometimes, I miss a curly bracket, and I'm totally hosed!