完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
我有问题(NodeMCU ESP8266)。我从 D1 D2(+Vcc 和 Gnd)成功运行了一个 SHT30 湿度/温度传感器,但我正在尝试添加一个按钮(D7 + Gnd + 30kΩ 在 D7 和 3.3V 之间)和一个红色 LED (D5) 和绿色 LED (D6) ) 具有共同点 以下代码一直有效,直到我取消注释中的一个或多个 `pinMode` 语句
代码:全选void setup() { 在这一点上,即使代码成功编译,当我加载它时,卡也不会启动,我得到重置/十六进制转储 代码:全选00:11:31.280 -> 00:11:31.280 -> ets Jan 8 2013,rst cause:4, boot mode:(3,6) 00:11:31.280 -> 00:11:31.280 -> wdt reset 00:11:31.280 -> load 0x4010f000, len 3460, room 16 00:11:31.280 -> tail 4 00:11:31.280 -> chksum 0xcc 00:11:31.280 -> load 0x3fff20b8, len 40, room 4 00:11:31.280 -> tail 4 00:11:31.280 -> chksum 0xc9 00:11:31.280 -> csum 0xc9 00:11:31.280 -> v00048310 00:11:31.280 -> ~ld 00:11:39.756 -> 源代码是: 代码:全选#include "Wire.h" //I2C library #include "SHT31.h" //I2C sensor library #include #include <ticker.h> // non-blocking delay library #include #define WIFI_SSID "********" #define WIFI_PASSWORD "***********" // Raspberri Pi Mosquitto MQTT Broker #define MQTT_HOST IPAddress(192, 168, ***, ***) #define MQTT_PORT 1883 String hostname = "BathroomHumidTemp"; // Temperature & humidity MQTT Topics #define MQTT_PUB_TEMP "esp/dht/b/temp" #define MQTT_PUB_HUM "esp/dht/b/humid" // I2C & DHT uint32_t start; uint32_t stop; // Initialize DHT sensor SHT31 sht; // pin assignments: const int BUTTON_PIN = 7; // the number of the pushbutton pin const int RED_LED_PIN = 5; // the number of the RED LED pin const int GREEN_LED_PIN = 6; // the number of the GREEN LED pin int buttonState = 0; // variable for reading the pushbutton status // Variables to hold sensor readings float temp; float hum; AsyncMqttClient mqttClient; Ticker mqttReconnectTimer; WiFiEventHandler wifiConnectHandler; WiFiEventHandler wifiDisconnectHandler; Ticker wifiReconnectTimer; unsigned long previousMillis = 0; // Stores last time temperature was published const long interval = 10000; // Interval at which to publish sensor readings void connectToWifi() { Serial.println("Connecting to Wi-Fi..."); WiFi.setHostname(hostname.c_str()); WiFi.begin(WIFI_SSID, WIFI_PASSWORD); } void onWifiConnect(const WiFiEventStationModeGotIP& event) { Serial.println("Connected to Wi-Fi."); connectToMqtt(); } void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) { Serial.println("Disconnected from Wi-Fi."); mqttReconnectTimer.detach(); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi wifiReconnectTimer.once(2, connectToWifi); } void connectToMqtt() { Serial.println("Connecting to MQTT..."); mqttClient.connect(); } void onMqttConnect(bool sessionPresent) { Serial.println("Connected to MQTT."); Serial.print("Session present: "); Serial.println(sessionPresent); } void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) { Serial.println("Disconnected from MQTT."); if (WiFi.isConnected()) { mqttReconnectTimer.once(2, connectToMqtt); } } void onMqttSubscribe(uint16_t packetId, uint8_t qos) { Serial.println("Subscribe acknowledged."); Serial.print(" packetId: "); Serial.println(packetId); Serial.print(" qos: "); Serial.println(qos); } void onMqttUnsubscribe(uint16_t packetId) { Serial.println("Unsubscribe acknowledged."); Serial.print(" packetId: "); Serial.println(packetId); } void onMqttPublish(uint16_t packetId) { Serial.print("Publish acknowledged."); Serial.print(" packetId: "); Serial.println(packetId); } void setup() { // // initialize the LED pins as an output: // pinMode(RED_LED_PIN, OUTPUT); // pinMode(GREEN_LED_PIN, OUTPUT); // // initialize the pushbutton pin as an pull-up input: // // the pull-up input pin will be HIGH when the switch is open and LOW when the switch is closed. // pinMode(BUTTON_PIN, INPUT_PULLUP); Serial.begin(115200); Serial.println(); Wire.begin(); sht.begin(0x44); //Sensor I2C Address Wire.setClock(100000); uint16_t stat = sht.readStatus(); Serial.print(stat, HEX); Serial.println(); wifiConnectHandler = WiFi.onStationModeGotIP(onWifiConnect); wifiDisconnectHandler = WiFi.onStationModeDisconnected(onWifiDisconnect); mqttClient.onConnect(onMqttConnect); mqttClient.onDisconnect(onMqttDisconnect); //mqttClient.onSubscribe(onMqttSubscribe); //mqttClient.onUnsubscribe(onMqttUnsubscribe); mqttClient.onPublish(onMqttPublish); mqttClient.setServer(MQTT_HOST, MQTT_PORT); // If your broker requires authentication (username and password), set them below //mqttClient.setCredentials("REPlACE_WITH_YOUR_USER", "REPLACE_WITH_YOUR_PASSWORD"); connectToWifi(); } void loop() { // // read the state of the pushbutton value: // buttonState = digitalRead(BUTTON_PIN); // // control LED according to the state of button // if(buttonState == HIGH) // If button is pressing // digitalWrite(RED_LED_PIN, HIGH); // turn on LED // else // otherwise, button is not pressing // digitalWrite(RED_LED_PIN, LOW); // turn off LED unsigned long currentMillis = millis(); // Every X number of seconds (interval = 10 seconds) // it publishes a new MQTT message if (currentMillis - previousMillis >= interval) { // Save the last time a new reading was published previousMillis = currentMillis; sht.read(); // New DHT sensor readings hum = sht.getHumidity(); // Read temperature as Celsius (the default) temp = sht.getTemperature(); // Read temperature as Fahrenheit (isFahrenheit = true) //temp = dht.readTemperature(true); // Publish an MQTT message on topic esp/dht/b/temp uint16_t packetIdPub1 = mqttClient.publish(MQTT_PUB_TEMP, 1, true, String(temp).c_str()); Serial.printf("Publishing on topic %s at QoS 1, packetId: %i ", MQTT_PUB_TEMP, packetIdPub1); Serial.printf("Message: %.2f n", temp); // Publish an MQTT message on topic esp/dht/b/humid uint16_t packetIdPub2 = mqttClient.publish(MQTT_PUB_HUM, 1, true, String(hum).c_str()); Serial.printf("Publishing on topic %s at QoS 1, packetId %i: ", MQTT_PUB_HUM, packetIdPub2); Serial.printf("Message: %.2f n", hum); } } |
|
相关推荐
|
|
只有小组成员才能发言,加入小组>>
541浏览 6评论
454浏览 5评论
有没有办法在不使用混杂模式的情况下实现Wifi驱动程序接收缓冲区访问中断呢?
434浏览 5评论
436浏览 4评论
409浏览 4评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-18 16:20 , Processed in 0.716256 second(s), Total 76, Slave 59 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号