Example Code for Arduino - Obtain Temperature & Humidity

Last revision 2026/02/05

Hardware Preparation

Software Preparation

Arduino IDE

Download and install the SHT3x Library. (About how to install the library?)

Connection Diagram

Other Preparation Work

  1. Connect to WiFi "ESP32 S3", password: 12345678

  2. Access website http://192.168.4.1/GET to get the temperature & humidity information in local area network.

  3. Refresh the temp & humi webpage to get the latest sensor data.

Sample Code

/*
Connect SHT30 to ESP-S3, get temperature & humidity via LAN
*/

#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiAP.h>
#include <DFRobot_SHT3x.h>
//Select 0x45 when ADR is connected to VDD; select 0x44 when ADR goes to GND
//Default to 0x45, unnecessary to connect RST(reset pin)
DFRobot_SHT3x sht3x(&Wire,/*address=*/0x45,/*RST=*/4);

//Comment out the codes above when using SPI, and run the codes below
//DFRobot_SHT3x   sht3x;

// Set WIFI name and password
const char *ssid = "ESP32 S3";//WIFI Name 
const char *password = "12345678";//Password 

WiFiServer server(80);//Port 80 is the default web server port

//Display the last sensor feedback status 
void setup() {
  //pinMode(myLED, OUTPUT);

  Serial.begin(115200);
  Serial.println();
  Serial.println("Configuring access point...");

  //Delete password if you want an open network 
  WiFi.softAP(ssid, password);
  IPAddress myIP = WiFi.softAPIP();
  Serial.print("AP IP address: ");
  Serial.println(myIP);
  server.begin();

  Serial.println("Server started");
  //Init sensor 
  while (sht3x.begin() != 0) {
        Serial.println("Failed to Initialize the chip, please confirm the wire connection");
        delay(1000);
      }
      Serial.print("Chip serial number");
      Serial.println(sht3x.readSerialNumber());
      if(!sht3x.softReset()){
         Serial.println("Failed to Initialize the chip....");
       }
}

void loop() {

WiFiClient client = server.available();   // Detect waiting for connection

  if (client) {                             // Connection detecting 
    Serial.println("New Client."); 
    String currentLine = "";                // Create a String variable to store data 
    while (client.connected()) {            // Loop when keep connecting
      if (client.available()) {             // Detect is there is data on the connection 
        char c = client.read();             // Read the received data
        //Serial.write(c);                    // Print on serial monitor
        if (c == '\n') {                    // If an newline character is read

          //Clear cached content 
          if (currentLine.length() == 0) {
            client.print(" ");
            break;
          } else {    // Clear cached data in variable if there is one newline character 
            currentLine = "";
          }
        } else if (c != '\r') {  // If characters except Carriage Return is obtained
          currentLine += c;      // Add the obtained character to end of the variable
        }

        // Check if /GET is obtained at the end
        if (currentLine.endsWith("/GET")) {
             //Read temperature and humidity 
             float   temp = sht3x.getTemperatureC();
             float   humi = sht3x.getHumidityRH();  
             //Print on webpage 
             client.print("temp (C): "); client.println(temp);
             client.print("humi (%RH): "); client.println(humi);
        }
      }
    }
    // disconnect 
    client.stop();
    Serial.println("Client Disconnected.");
  }
  }

Result

You can access the website through a mobile phone, computer, etc. to obtain the temperature and humidity under the LAN.

Other Supplementary Information

Member function

  • WiFi.softAP(ssid, password)

    • Description: Similar to opening the hotspot with the set WIFI account

    • Parameter:

      • ssid: WIFI name

      • password: WIFI password

  • WiFi.softAPIP()

    • Description: WIFI IP address

Was this article helpful?

TOP