1. 主页
  2. 文档
  3. 小钉锤物联网云平台文档
  4. 应用案例
  5. nodemcu+dht22+诺基亚5110显示屏=温湿度计

nodemcu+dht22+诺基亚5110显示屏=温湿度计

如何连接WIFI?

程序代码:

#include "DHT.h"
#include <PubSubClient.h>
#include <ESP8266WiFi.h>
#include <ArduinoJson.h>
#include "Nokia_5110.h"

#define WIFI_AP "xdclink"
#define WIFI_PASSWORD "xxxxx"
//定义LCD针脚

#define RST D2
#define CE D3
#define DC D4
#define DIN D5
#define CLK D6
Nokia_5110 lcd = Nokia_5110(RST, CE, DC, DIN, CLK);

#define TOKEN "123481"

// DHT
#define DHTPIN D7
#define DHTTYPE DHT22

char thingsboardServer[] = "cn.xdc.link";

WiFiClient wifiClient;

// Initialize DHT sensor.
DHT dht(DHTPIN, DHTTYPE);

PubSubClient client(wifiClient);

int status = WL_IDLE_STATUS;
unsigned long lastSend;

void setup()
{
  Serial.begin(115200);        
      WiFiManager wifiManager;
      wifiManager.autoConnect("xdc.link");
      Serial.println("connected...yeey :)");
  dht.begin();
  delay(10);

  InitWiFi();
  client.setServer( thingsboardServer, 1883 );
  lastSend = 0;
}

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

  if ( millis() - lastSend > 1000 ) { // Update and send only after 1 seconds
    getAndSendTemperatureAndHumidityData();
    lastSend = millis();
  }

  client.loop();
}

void getAndSendTemperatureAndHumidityData()
{
  Serial.println("Collecting temperature data.");

  // Reading temperature or humidity takes about 250 milliseconds!
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.print(" *C ");

  String temperature = String(t);
  String humidity = String(h);


  // Just debug messages
  Serial.print( "Sending temperature and humidity : [" );
  Serial.print( temperature ); Serial.print( "," );
  Serial.print( humidity );
  Serial.print( "]   -> " );

//设置显示屏,显示内容
lcd.clear();
lcd.setContrast(50); //设置对比度
lcd.setCursor(0, 0);  //设置光标位置
lcd.println("xdc.link");
lcd.println("------------");
lcd.println("Temperature: `C");
    lcd.println(t);  //显示温度
    lcd.println("Humidity:  %");
    lcd.println(h); //显示湿度
     
    delay(8000);
    
  // Prepare a JSON payload string
  String payload = "{";
  payload += "\"temperature\":"; payload += temperature; payload += ",";
  payload += "\"humidity\":"; payload += humidity;
  payload += "}";

  // Send payload
  char attributes[100];
  payload.toCharArray( attributes, 100 );
  client.publish( "v1/devices/me/telemetry", attributes );
  Serial.println( attributes );

}

void InitWiFi()
{
  Serial.println("Connecting to AP ...");
  // attempt to connect to WiFi network

 // WiFi.begin(WIFI_AP, WIFI_PASSWORD);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("Connected to AP");
}


void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    status = WiFi.status();
    if ( status != WL_CONNECTED) {
     // WiFi.begin(WIFI_AP, WIFI_PASSWORD);
      while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
      }
      Serial.println("Connected to AP");
    }
    Serial.print("Connecting to ThingsBoard node ...");
    // Attempt to connect (clientId, username, password)
    if ( client.connect("ESP8266 Device", TOKEN, NULL) ) {
      Serial.println( "[DONE]" );
    } else {
      Serial.print( "[FAILED] [ rc = " );
      Serial.print( client.state() );
      Serial.println( " : retrying in 5 seconds]" );
      // Wait 5 seconds before retrying
      delay( 5000 );
    }
  }
}
这篇文章对您有用吗? 1

我们要如何帮助您?