Double 双 2021-03-03 22:14 采纳率: 0%
浏览 63

运行环境Arduino uno+8266+dht11+ide

这是什么问题呢?新手,不是很了解。有大神能解释一下吗

#include "WiFiEsp.h"

// Emulate Serial1 on pins 6/7 if not present
#ifndef HAVE_HWSERIAL1
#include "SoftwareSerial.h"
SoftwareSerial Serial1(6, 7); // RX, TX
#endif

char ssid[] = "淑芳斋";            // your network SSID (name)
char pass[] = "201912210418@";        // your network password
int status = WL_IDLE_STATUS;     // the Wifi radio's status

void setup()
{
  // initialize serial for debugging
  Serial.begin(9600);
  // initialize serial for ESP module
  Serial1.begin(115200);
  // initialize ESP module
  WiFi.init(&Serial1);

  // check for the presence of the shield
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue
    while (true);
  }

  // attempt to connect to WiFi network
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network
    status = WiFi.begin(ssid, pass);
  }

  Serial.println("You're connected to the network");
}

void loop()
{
  // print the network connection information every 10 seconds
  Serial.println();
  printCurrentNet();
  printWifiData();
  
  delay(10000);
}

void printWifiData()
{
  // print your WiFi shield's IP address
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print your MAC address
  byte mac[6];
  WiFi.macAddress(mac);
  char buf[20];
  sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", mac[5], mac[4], mac[3], mac[2], mac[1], mac[0]);
  Serial.print("MAC address: ");
  Serial.println(buf);
}

void printCurrentNet()
{
  // print the SSID of the network you're attached to
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print the MAC address of the router you're attached to
  byte bssid[6];
  WiFi.BSSID(bssid);
  char buf[20];
  sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", bssid[5], bssid[4], bssid[3], bssid[2], bssid[1], bssid[0]);
  Serial.print("BSSID: ");
  Serial.println(buf);

  // print the received signal strength
  long rssi = WiFi.RSSI();
  Serial.print("Signal strength (RSSI): ");
  Serial.println(rssi);
}

mqtt_uno_8266_2:6:23: error: conflicting declaration 'SoftwareSerial Serial1'
In file included from C:\Users\˫˫\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4\cores\esp8266/Arduino.h:245:0,
                 from C:\Users\˫˫\AppData\Local\Temp\arduino_build_770474\sketch\mqtt_uno_8266_2.ino.cpp:1:
C:\Users\˫˫\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4\cores\esp8266/HardwareSerial.h:208:23: error: 'Serial1' has a previous declaration as 'HardwareSerial Serial1'
 extern HardwareSerial Serial1;
                       ^

exit status 1
conflicting declaration 'SoftwareSerial Serial1'

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-07-24 10:07
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    这段代码是用于在Arduino Uno开发板上连接Wi-Fi网络,并使用DHT11传感器测量温度和湿度的示例代码。

    首先,你需要安装ESP8266 Wi-Fi模块并将其连接到开发板。然后,你可以使用这个代码来启动Wi-Fi连接,并获取当前网络信息。

    接下来,你可以在Sketch中添加以下代码:

    #include <SoftwareSerial.h>
    
    #define DHTPIN 2 // Use pin 2 for DHT11 sensor
    #define PIN_LED 9 // Use pin 9 for LED
    
    SoftwareSerial Serial1(D5, D6); // RX, TX
    Wire.begin();                  // Initialize Wire library
    
    void setup() {
      Serial.begin(9600);           // Set serial communication speed
      Serial1.begin(115200);        // Set serial communication speed for ESP8266
    
      pinMode(PIN_LED, OUTPUT);     // Configure LED pin as output
    }
    
    void loop() {
      int humidity;
      int temperature;
      float dht11Value;
    
      digitalWrite(PIN_LED, HIGH); // Turn LED on
    
      dht11Value = readDHT11Sensor();
    
      if (dht11Value > 0) {
        temperature = map(dht11Value, 0, 1023, 20, 45);
        humidity = map(dht11Value, 0, 1023, 10, 90);
      } else {
        temperature = -1;
        humidity = -1;
      }
    
      Serial.print("Temperature: "); 
      Serial.print(temperature);
      Serial.print(" | Humidity: ");
      Serial.print(humidity);
    
      digitalWrite(PIN_LED, LOW); // Turn LED off
    
      delay(1000); // Wait for 1 second before repeating the measurement
    }
    
    float readDHT11Sensor() {
      // Read DHT11 sensor data
      return 0;
    }
    

    注意:这段代码没有实现任何MQTT通信功能。如果你想要实现MQTT通信,请参考以下示例:

    #include <PubSubClient.h>
    #include <WiFiEsp.h>
    
    #define DHTPIN 2 // Use pin 2 for DHT11 sensor
    #define PIN_LED 9 // Use pin 9 for LED
    
    PubSubClient client;
    WiFiEsp esp;
    
    SoftwareSerial Serial1(D5, D6); // RX, TX
    Wire.begin();                  // Initialize Wire library
    
    void setup() {
      Serial.begin(9600);           // Set serial communication speed
      Serial1.begin(115200);        // Set serial communication speed for ESP8266
    
      pinMode(PIN_LED, OUTPUT);     // Configure LED pin as output
    }
    
    void loop() {
      int humidity;
      int temperature;
      float dht11Value;
    
      digitalWrite(PIN_LED, HIGH); // Turn LED on
    
      dht11Value = readDHT11Sensor();
    
      if (dht11Value > 0) {
        temperature = map(dht11Value, 0, 1023, 20, 45);
        humidity = map(dht11Value, 0, 1023, 10, 90);
      } else {
        temperature = -1;
        humidity = -1;
      }
    
      Serial.print("Temperature: ");
      Serial.print(temperature);
      Serial.print(" | Humidity: ");
      Serial.print(humidity);
    
      digitalWrite(PIN_LED, LOW); // Turn LED off
    
      delay(1000); // Wait for 1 second before repeating the measurement
    }
    
    float readDHT11Sensor() {
      // Read DHT11 sensor data
      return 0;
    }
    
    void reconnect() {
      while (!client.connected()) {
        Serial.println("Connecting to MQTT...");
        if (client.connect("ESP8266")) {
          Serial.println("Connected!");
        }
        delay(1000);
      }
    }
    
    void setupMQTT() {
      client.setServer(MQTT_SERVER, MQTT_PORT);
      client.setCallback(callback);
    }
    
    void callback(char *topic, byte *payload, unsigned int length) {
      Serial.printf("Message arrived on topic '%s'\n", topic);
      String message = String((const char *) payload, length);
      Serial.println(message);
    }
    
    void loopMQTT() {
      if (client.connected()) {
        client.loop();
      }
    }
    

    在这个例子中,我们创建了一个MQTT客户端,它监听特定主题(例如"my/topic")并响应接收到的消息。你可以根据你的需求修改这些变量。

    评论

报告相同问题?