Arduino ESP8266 01 MQTT Raspberry pi

问题描述 投票:0回答:1

我想在MQTT中使用ESP 8266 01和arduino uno作为客户端。 ESP8266 01的代码已成功编译,我在Arduino uno中遇到错误错误是'messageTemp'未在此范围内声明

我还想到在客户端使用stm32xx。如果我为此编译

退出状态1

ISO C ++禁止指针和整数之间的比较[-fpermissive]

以上是我得到的错误

1)ESP代码

 // 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 = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";
const char* mqtt_server = "YOUR_RPi_IP_Address";

 // Initializes the espClient
WiFiClient espClient;
PubSubClient client(espClient);

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());
}


 if(topic=="home/office/esp1/gpio2"){
  Serial.print("Changing GPIO 2 to ");
  if(messageTemp == "1"){
//    digitalWrite(ledGPIO2, HIGH);
    Serial.print("On");
  }
  else if(messageTemp == "0"){
  //  digitalWrite(ledGPIO2, LOW);
    Serial.print("Off");
  }
  }
Serial.println();
}


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");  

     client.subscribe("home/office/esp1/gpio2");
    } 
  else 
 {
      Serial.print("failed, rc=");
     Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
   }
}
void setup() {

  Serial.begin(115200);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
   }


void loop() {

   if (!client.connected()) {
    reconnect();
   }
  client.loop();
  }

Arduino UNO代码

void setup(){
  Serial.begin(9600);
  pinMode(12, OUTPUT);
 digitalWrite(12, HIGH);

 String messageTemp;
 }

 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();

 }
 void loop ()
{
  if  (Serial.available()){
   char topic = Serial.read();
  {

    if (topic=="home/office/esp1/gpio2")
  {
       Serial.print("Changing GPIO 2 to ");
      if(messageTemp == "1"){
        digitalWrite(12, HIGH);
        Serial.print("On");
      }
      else if(messageTemp == "0")
       {
      digitalWrite(12, LOW);
       Serial.print("Off");
     }

}}}}
mqtt
1个回答
0
投票

ESP代码看起来不是您提供的完整源代码。对于Arduino UNO,如果你打算使用messageTemp作为全局变量,那么你需要在函数之外只声明它一次。在上面的代码中,您在setup()callback()中声明了两次,然后将它们视为局部变量。即使在loop()中,也没有声明,这应该会产生编译错误。

© www.soinside.com 2019 - 2024. All rights reserved.