Arduino-ESP8266 detects temperature and humidity and uploads to Alibaba Cloud
hardware preparation
ESP8266-12F development board
DHT11 or DHT22 temperature and humidity sensor
Arduino IDE environment
Before we use the ESP8266 development board to compile and use under the Arduino IDE, we need to configure the development environment of the ESP8266 under Arduino.
In the Arduino preferences, add the additional development board manager website:
http://arduino.esp8266.com/stable/package_esp8266com_index.json
Then download and install the development board of ESP8266 in Tools - Development Board - Development Board Manager.
After installation, select in the development board: NodeMCU Development Board
Alibaba Cloud
Enter the Alibaba Cloud official website,
log in and enter the Alibaba Cloud Internet of Things.
Create a new project
, create a new product
, create a new property
, and create two new properties, one is temperature and the other is humidity (note that the identifier in temperature is temperature , and the identifier for humidity is humidity )
PS: Note, here One point, in the attributes we create here, the data type should be consistent with the data type in the code. The data type we create in the figure is int type, and the corresponding data type defined in our code should also be int type, float, In the same way, the detection range of the DHT sensor can be accurate to two decimal places, so it is recommended to set the data type to double or float . Add a new test device Get the key after successfully adding it. Save and record the key!
Physical wiring
DHT11 | ESP8266 |
---|---|
VCC | 3V |
GND | G |
S | D4 |
IDE library file configuration
ArduinoJson
AliyunIoTSDK
PubSubClient
Crypto
AWS-SDK-ESP8266
easy problems
library file is missing
Note that because AliyunSDK depends on many libraries, the above libraries must be installed.
If there is an error and the lack of SHA256.h , it means that the Crypto library is not installed
. SHA256.h is an encryption algorithm. If you need to download it separately - SHA256.h
MQTT connection failed
If the MQTT connection fails on the serial port, you need to modify the default SIZE and KEEPALIVE of PubSubClient.h to be 1024 and 60 respectively .
code section
#include <ESP8266WiFi.h>
static WiFiClient espClient;
/*温湿度*/
#include "DHT.h"
#define DHTTYPE DHT11
#define dht_dpin D4
DHT dht(dht_dpin, DHTTYPE);
/*阿里云SDK*/
#include <AliyunIoTSDK.h>
/*配置阿里云设备信息*/
#define PRODUCT_KEY "***********" //产品KEY
#define DEVICE_NAME "********" //设备名字
#define DEVICE_SECRET "******************" //设备密钥
#define REGION_ID "cn-shanghai" //连接服务器
/* 配置WIFI */
#define WIFI_SSID "你的wifi名称"
#define WIFI_PASSWD "你的wifi密码"
void setup()
{
Serial.begin(115200);
dht.begin();
// 初始化WIFI
wifiInit(WIFI_SSID, WIFI_PASSWD);
// 传入WIFI-client 设备产品信息
AliyunIoTSDK::begin(espClient, PRODUCT_KEY, DEVICE_NAME, DEVICE_SECRET, REGION_ID);
//AliyunIoTSDK::send("temperature", t); 如有需要可发送模拟数据
}
void loop()
{
AliyunIoTSDK::loop();
float h = dht.readHumidity();
float t = dht.readTemperature();
AliyunIoTSDK::send("temperature", t);
AliyunIoTSDK::send("humidity", h);
delay(800);
}
// 初始化 wifi 连接
void wifiInit(const char *ssid, const char *passphrase)
{
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, passphrase);
while (WiFi.status() != WL_CONNECTED)
{
delay(1000);
Serial.println("WiFi not Connect");
}
Serial.println("Connected to AP");
}
Actual renderings: You can see the current real-time temperature and humidity under
the device details - running status ( check real-time refresh )
I wish you all the best! ! !