1. Overview
The IoT cloud upload project of temperature and humidity data collects ambient temperature and humidity data in real time by integrating temperature and humidity sensors and Internet of Things technology, and upload it to the cloud server.
In this project, we integrate IoT cloud technology with the temperature and humidity sensor to collect the values in real time and then save to cloud.
Users can remotely monitor data changes through mobile APP or web pages and manage environment state in real time.
Beyond that, it not only improves the accuracy and timeliness of data acquisition, but also supports historical data query and abnormal alarm through cloud storage and analysis. It is widely applied to environmental monitoring for fields of agriculture, warehousing and home furnishing.
2. Download Remote Control APP
Android system:
Search ESP RainMaker
in Google and click install. Log in an account, if you do not have the corresponding account you need to register one.
ios system:
Search ESP RainMaker
in App Store, log in your account to download it.
3. Wiring Diagram
4. Test Code
Before uploading test code, set Partition Scheme to “RainMaker”.
/*
Project name: 17_xht11_iot
Function: Remote monitoring of temperature and humidity data
Author: keyestudio
Hardware connection:
- Connect to port 4
Library:
- SimpleTimer.h
Develop software version:
- It is recommended to use the same software version as the one used at the time of development to avoid incompatibility
- Arduino IDE 2.3.3
- ESP32 2.0.12
Cautions:
- Make sure you are connected to the correct interface
- Before uploading test code, please correctly connect to the development board and port
- Set ESP32 Tools -> Partition Scheme:"RainMaker"
*/
#include "RMaker.h"
#include "WiFi.h"
#include "WiFiProv.h"
#include
#include
SimpleTimer Timer;
//Define the XHT11 read data at pin IO27
#define xht11_Pin 27
DHT dht(xht11_Pin, DHT11);
static uint8_t gpio_reset = 0; // Press BOOT to reset WiFi Details
// define the Node Name
char nodeName[] = "Keyes_ESP32_SmartHome";
//Here you can modify the name and key of the Bluetooth you want
const char *service_name = "keyestudio";
const char *pop = "keyes123";
// define the Chip Id
uint32_t espChipId = 5;
float temperature1 = 0;
float humidity1 = 0;
static TemperatureSensor temperature("Temperature");
static TemperatureSensor humidity("Humidity");
void sysProvEvent(arduino_event_t *sys_event) {
switch (sys_event->event_id) {
case ARDUINO_EVENT_PROV_START:
#if CONFIG_IDF_TARGET_ESP32
Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on BLE\n", service_name, pop);
printQR(service_name, pop, "ble");
#else
Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on SoftAP\n", service_name, pop);
printQR(service_name, pop, "softap");
#endif
break;
case ARDUINO_EVENT_WIFI_STA_CONNECTED:
Serial.printf("\nConnected to Wi-Fi!\n");
break;
}
}
void readSensor() {
float h = dht.readHumidity();
float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
} else {
humidity1 = h;
temperature1 = t;
}
}
void sendSensor() {
readSensor();
temperature.updateAndReportParam("Temperature", temperature1);
humidity.updateAndReportParam("Temperature", humidity1);
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(gpio_reset, INPUT);
dht.begin(); // Enabling DHT sensor
Node my_node;
my_node = RMaker.initNode(nodeName);
my_node.addDevice(temperature);
my_node.addDevice(humidity);
Timer.setInterval(10000); //send Data after every 10 seconds
delay(1000);
//This is optional
RMaker.enableOTA(OTA_USING_PARAMS);
//If you want to enable scheduling, set time zone for your region using setTimeZone().
//The list of available values are provided here https://rainmaker.espressif.com/docs/time-service.html
//RMaker.setTimeZone("Asia/Shanghai");
// Alternatively, enable the Timezone service and let the phone apps set the appropriate timezone
RMaker.enableTZService();
RMaker.enableSchedule();
Serial.printf("\nStarting ESP-RainMaker\n");
RMaker.start();
WiFi.onEvent(sysProvEvent);
#if CONFIG_IDF_TARGET_ESP32
WiFiProv.beginProvision(WIFI_PROV_SCHEME_BLE, WIFI_PROV_SCHEME_HANDLER_FREE_BTDM, WIFI_PROV_SECURITY_1, pop, service_name);
#else
WiFiProv.beginProvision(WIFI_PROV_SCHEME_SOFTAP, WIFI_PROV_SCHEME_HANDLER_NONE, WIFI_PROV_SECURITY_1, pop, service_name);
#endif
}
void loop() {
// put your main code here, to run repeatedly:
// Read GPIO0 (external button to reset device
if (digitalRead(gpio_reset) == LOW) { //Push button pressed
Serial.printf("Reset Button Pressed!\n");
// Key debounce handling
delay(100);
int startTime = millis();
while (digitalRead(gpio_reset) == LOW) delay(50);
int endTime = millis();
if ((endTime - startTime) > 10000) {
// If key pressed for more than 10secs, reset all
Serial.printf("Reset to factory.\n");
RMakerFactoryReset(2);
} else if ((endTime - startTime) > 3000) {
Serial.printf("Reset Wi-Fi.\n");
// If key pressed for more than 3secs, but less than 10, reset Wi-Fi
RMakerWiFiReset(2);
}
}
delay(100);
if (WiFi.status() == WL_CONNECTED) {
//Serial.println("WiFi Connected");
if (Timer.isReady()) {
Serial.println("Sending Sensor Data");
sendSensor();
Timer.reset(); // Reset a second timer
}
}
}
5. Test Result
After uploading the test code, open the serial monitor and set the baud rate to 112500. Press BOOT button on the development board and hold it for 4 seconds:
Wait for the serial monitor to print a QR code for the connection:
Copy it to browser to get another QR code:
Open the ESP RainMaker APP to log in and click Add Device
. Then the system prompts for permission of camera, location, and nearby device. You need to authorize them.
The wifi connection for the ESP32 is sent via Bluetooth, so we need to scan the QR code on the browser to pair it with Bluetooth.
Bluetooth name and pairing key are keyestudio
and keyes123
, which can be modified as you like.
const char *service_name = "keyestudio";
const char *pop = "keyes123";
Wifi scanning will be performed after Bluetooth pairing. For ESP32, you can either choose a scanned wifi, or click Join Other Network
to add a Wifi manually (Note that this wifi band must be 2.4G which is same as ESP32’s).
Then click Start
.
Wait for successful configuration and click Done
, and the temperature and humidity values are shown on the web page. Slide down the page to refresh them.
Note if the operation fails, you need to start from scratch.