hi,
i received the following error massage when trying to compile me sketch:
/home/pi/Arduino/sketch_dec24a/sketch_dec24a.ino: In function 'void loop()':
sketch_dec24a:164:47: error: invalid conversion from 'int' to 'const char*' [-fpermissive]
client.publish("Master/pH/pH1",Value_D7 );
^
In file included from /home/pi/Arduino/sketch_dec24a/sketch_dec24a.ino:17:0:
/home/pi/Downloads/arduino-1.8.13/libraries/pubsubclient/src/PubSubClient.h:151:12: error: initializing argument 2 of 'boolean PubSubClient::publish(const char*, const char*)' [-fpermissive]
boolean publish(const char* topic, const char* payload);
^
exit status 1
invalid conversion from 'int' to 'const char*' [-fpermissive]
my code is:
[ltr]// Loading the ESP8266WiFi library and the PubSubClient library
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
// Change the credentials below, so your ESP8266 connects to your router
const char* ssid = "Gjkblk-WgM";
const char* password = "FD#%G@@00";
// Change the variable to your Raspberry Pi IP address, so it connects to your MQTT broker
const char* mqtt_server = "192.168.0.72";
// Initializes the espClient
WiFiClient espClient;
PubSubClient client(espClient);
// Connect an LED to each GPIO of your ESP8266
const int pump1_Gpio2 = 2;
const int pump2_Gpio5 = 5;
int Value_D7;
int Value_D8;
const int Pin_D7 = 13;
int Pin_D8 = 15;
// Don't change the function below. This functions connects your ESP8266 to your router
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("WiFi connected - ESP IP address: ");
Serial.println(WiFi.localIP());
}
// This functions is executed when some device publishes a message to a topic that your ESP8266 is subscribed to
// Change the function below to add logic to your program, so when a device publishes a message to a topic that
// your ESP8266 is subscribed you can actually do something
void callback(String 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();
// Feel free to add more if statements to control more GPIOs with MQTT
// If a message is received on the topic home/office/esp1/gpio2, you check if the message is either 1 or 0. Turns the ESP GPIO according to the message
if(topic=="Master/pumps/Pump1"){
Serial.print("Changing GPIO 2 to ");
if(messageTemp == "1"){
digitalWrite(pump1_Gpio2, HIGH);
Serial.print("On");
}
else if(messageTemp == "0"){
digitalWrite(pump1_Gpio2, LOW);
Serial.print("Off");
}
}
if(topic=="Master/pumps/Pump2"){
Serial.print("Changing GPIO 5 to ");
if(messageTemp == "1"){
digitalWrite(pump2_Gpio5, HIGH);
Serial.print("On");
}
else if(messageTemp == "0"){
digitalWrite(pump2_Gpio5, LOW);
Serial.print("Off");
}
}
Serial.println();
}
// This functions reconnects your ESP8266 to your MQTT broker
// Change the function below if you want to subscribe to more topics with your ESP8266
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("ESP8266Client")) {
Serial.println("connected");
// Subscribe or resubscribe to a topic
// You can subscribe to more topics (to control more LEDs in this example)
client.subscribe("Master/pumps/Pump1");
client.subscribe("Master/pumps/Pump2");
client.subscribe("Master/pH/pH1");
client.subscribe("Master/pH/pH2");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
// The setup function sets your ESP GPIOs to Outputs, starts the serial communication at a baud rate of 115200
// Sets your mqtt broker and sets the callback function
// The callback function is what receives messages and actually controls the LEDs
void setup() {
// PH 1,2
Serial.begin(9600); //Start serial monitor
pinMode(Pin_D7,OUTPUT);
pinMode(Pin_D8,OUTPUT);
pinMode(A0,INPUT);
// End PH 1,2
pinMode(pump1_Gpio2, OUTPUT);
pinMode(pump2_Gpio5, OUTPUT);
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
// For this project, you don't need to change anything in the loop function.
// Basically it ensures that you ESP is connected to your broker
void loop() {
// PH
digitalWrite(Pin_D7, HIGH); //Turn D7 On
delay(1000); //Wait for sensor
Value_D7 = analogRead(0); //Read Analog pin as D7
client.publish("Master/pH/pH1",Value_D7 );
digitalWrite(Pin_D7, LOW); //Turn D7 Off
//Repeat for D8
digitalWrite(Pin_D8, HIGH); //Turn D8 On
delay(1000); //Wait for sensor
Value_D8 = analogRead(0); //Read Analog pin as D8
digitalWrite(Pin_D8, LOW); //Turn D8 Off
delay(1000); //Wait for sensor
//Print the results to the serial monitor
Serial.print("D7 = ");
Serial.print(Value_D7);
Serial.print(" / D8 = ");
Serial.println(Value_D8);
// End PH
if (!client.connected()) {
reconnect();
}
client.loop();
}
[/ltr]