【问题标题】:Arduino ESP8266 01 MQTT Raspberry piArduino ESP8266 01 MQTT 树莓派
【发布时间】:2018-03-03 03:07:30
【问题描述】:

我想在 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】:

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

    【讨论】:

    • 感谢您的回复。但是当我将条件 if (topic=="home/office/esp1/gpio2") 更改为 if (topic=='home/office/esp1/gpio2') 时,我没有编译错误
    猜你喜欢
    • 2015-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-29
    • 2013-02-08
    • 2017-05-03
    • 1970-01-01
    相关资源
    最近更新 更多