Hello from Germany.
Hope you can help me. I downloaded an Arduino file and tried to convert it to cpp. But there are still 2 errors. I found out that the problem is C or C++. I have no experiance.
Could you help me to fix the code. What exactly have i do change and where?
Thanks
Just one media file allowed for me… so here i thesecond faulty code area
void callback(char* topic, byte* message, unsigned int length) {
Serial.print("Message arrived on topic: “);
Serial.print(topic);
Serial.print(”. Message: ");
String messageTemp;
for (int i = 0; i < length; i++) {
Serial.print((char)message[i]);
messageTemp += (char)message[i];
}
Serial.println();
if (String(topic) == inTopic) {
if(messageTemp == “ON”){
Serial.print(“Changing output to ON\n”);
digitalWrite(OutputPin, LOW); //Invertiertes Signal
client.publish(state_topic, “ON”);
delay(200);
}
else if(messageTemp == “OFF”){
Serial.print(“Changing output to OFF\n”);
digitalWrite(OutputPin, HIGH); //Invertiertes Signal
client.publish(state_topic, “OFF”);
delay(200);
}
}
}
There is 1 error and 1 warning.
For converting ino files manually see the documentation Convert Arduino file to C++ manually — PlatformIO latest documentation
Please post code or error logs as preformatted text.
Simply copy your code / log (ctrl-c
).
Then click the preformatted text (</>
icon in the toolbar or hit ctrl-e
) and paste in your code / log (ctrl-v
).
```
type or paste code here
```
Thanks in advance
und Grüße aus Schleswig-Holstein 
Hello,
here is the code. Can you also doublecheck line 23 “void setup_wifi();” and line 35 “void reconnect();”. I added “void” because of hints from the internet. Before that i had 3 error messages and 1 warning.
Thanks
und Grüße aus dem südlichen Schleswig-Holstein
#include <Arduino.h>
#include <PubSubClient.h>
#include <ArduinoOTA.h>
#define wifi_ssid "Your_SSID"
#define wifi_password "Your_Password"
#define mqtt_server "MQTT_Server_IP"
#define mqtt_user "MQTT_username"
#define mqtt_password "MQTT_PW"
#define ESPHostname "MQTT_Relay"
#define state_topic "MQTT_Relay/state"
#define inTopic "MQTT_Relay/command"
WiFiClient espClient;
PubSubClient client(espClient);
const int OutputPin = 14; // der Output Pin wo das Relay angehängt ist
void setup() {
Serial.begin(115200);
void setup_wifi();
ArduinoOTA.setHostname(ESPHostname);
// ArduinoOTA.setPassword("admin");
ArduinoOTA.begin();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
pinMode(OutputPin, OUTPUT);
digitalWrite(OutputPin, HIGH); // Relay OFF - Invertiertes Signal deswegen HIGH
}
void loop() {
if (!client.connected()) {
void reconnect();
}
client.loop();
ArduinoOTA.handle();
}
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(wifi_ssid);
WiFi.begin(wifi_ssid, wifi_password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* message, unsigned int length) {
Serial.print("Message arrived on topic: ");
Serial.print(topic);
Serial.print(". Message: ");
String messageTemp;
for (int i = 0; i < length; i++) {
Serial.print((char)message[i]);
messageTemp += (char)message[i];
}
Serial.println();
if (String(topic) == inTopic) {
if(messageTemp == "ON"){
Serial.print("Changing output to ON\n");
digitalWrite(OutputPin, LOW); //Invertiertes Signal
client.publish(state_topic, "ON");
delay(200);
}
else if(messageTemp == "OFF"){
Serial.print("Changing output to OFF\n");
digitalWrite(OutputPin, HIGH); //Invertiertes Signal
client.publish(state_topic, "OFF");
delay(200);
}
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Create a random client ID
String clientId = "MQTT_Relay-";
clientId += String(random(0xffff), HEX);
// Attempt to connect
if (client.connect(clientId.c_str(), mqtt_user, mqtt_password)) {
Serial.println("connected");
// Once connected, publish an announcement...
client.publish(state_topic, "MQTT_Relay alive");
// ... and resubscribe
client.subscribe(inTopic);
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
Unfortunately, you have not followed the instructions correctly.
You must declare functions before they can be called.
#include <Arduino.h>
#include <ArduinoOTA.h>
#include <PubSubClient.h>
#define wifi_ssid "Your_SSID"
#define wifi_password "Your_Password"
#define mqtt_server "MQTT_Server_IP"
#define mqtt_user "MQTT_username"
#define mqtt_password "MQTT_PW"
#define ESPHostname "MQTT_Relay"
#define state_topic "MQTT_Relay/state"
#define inTopic "MQTT_Relay/command"
WiFiClient espClient;
PubSubClient client(espClient);
const int OutputPin = 14; // der Output Pin wo das Relay angehängt ist
// forward declaration
void callback(char* topic, byte* message, unsigned int length);
void setup_wifi();
void setup() {
Serial.begin(115200);
void setup_wifi();
ArduinoOTA.setHostname(ESPHostname);
// ArduinoOTA.setPassword("admin");
ArduinoOTA.begin();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
pinMode(OutputPin, OUTPUT);
digitalWrite(OutputPin, HIGH); // Relay OFF - Invertiertes Signal deswegen HIGH
//call setupWiFi()
setup_wifi();
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
ArduinoOTA.handle();
}
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(wifi_ssid);
WiFi.begin(wifi_ssid, wifi_password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* message, unsigned int length) {
Serial.print("Message arrived on topic: ");
Serial.print(topic);
Serial.print(". Message: ");
String messageTemp;
for (int i = 0; i < length; i++) {
Serial.print((char)message[i]);
messageTemp += (char)message[i];
}
Serial.println();
if (String(topic) == inTopic) {
if (messageTemp == "ON") {
Serial.print("Changing output to ON\n");
digitalWrite(OutputPin, LOW); // Invertiertes Signal
client.publish(state_topic, "ON");
delay(200);
} else if (messageTemp == "OFF") {
Serial.print("Changing output to OFF\n");
digitalWrite(OutputPin, HIGH); // Invertiertes Signal
client.publish(state_topic, "OFF");
delay(200);
}
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Create a random client ID
String clientId = "MQTT_Relay-";
clientId += String(random(0xffff), HEX);
// Attempt to connect
if (client.connect(clientId.c_str(), mqtt_user, mqtt_password)) {
Serial.println("connected");
// Once connected, publish an announcement...
client.publish(state_topic, "MQTT_Relay alive");
// ... and resubscribe
client.subscribe(inTopic);
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
Alternatively, you can simply put the functions in the correct order. In most cases it is sufficient to put setup()
and loop()
at the end.
Your original code didn’t call setup_wifi()
.
I added this at the end of your setup()
function.
I would follow the instructions if I had the slightest idea of programming. Das sind Böhmische Dörfer für mich.
Then here is a super short crash course for you:
Either make a forward declaration (as described in the documentation):
#include <Arduino.h>
// forward declaration of functionA
void functionA();
void setup() {
Serial.begin(115200);
Serial.println("Hello world");
}
void loop() {
functionA();
}
// definition of functionA
void functionA() {
Serial.println("I'm alive");
delay(1000);
}
Or you define the functions in the correct order:
#include <Arduino.h>
void setup() {
Serial.begin(115200);
Serial.println("Hello world");
}
// definition of functionA
void functionA() {
Serial.println("I'm alive");
delay(1000);
}
void loop() {
functionA();
}
Thanks. I understand your short example 
1 Like