Example Code for Arduino-WIFI to obtain temperature and humidity

This example is based on Example 8.3 to further learn the information transfer of Wifi under the LAN. You can learn how to access the IP address under the LAN to obtain the temperature and humidity sensor status of the SHT30 in another place.

Hardware Preparation

Software Preparation

Wiring diagram

Wiring diagram

Steps

  1. Connect to WIFI "Beetle ESP32 C3", WIFI password has been set: 12345678
  2. Visit the website http://192.168.4.1/GET to get the temperature and humidity information in the LAN
  3. In the temperature and humidity display page, you can update the sensor data by refreshing

Sample Code

/*
In this example, SHT30 is connected to ESPC3 to obtain temperature and humidity through LAN
*/

#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiAP.h>
#include <DFRobot_SHT3x.h>
//When ADR is connected to VDD, 0x45 can be selected; when ADR is connected to GND, 0x44 can be selected
//The default is 0x45, RST (reset pin) does not need to be connected
DFRobot_SHT3x sht3x(&Wire,/*address=*/0x45,/*RST=*/4);

//To use SPI, you need to comment the above code and use the following code
//DFRobot_SHT3x   sht3x;

// Set WIFI name and password
const char *ssid = "Beetle ESP32 C3";//WIFI name
const char *password = "12345678";//password

WiFiServer server(80);//The default web service port is 80

// Display the last sensor status feedback status

void setup() {
  //pinMode(myLED, OUTPUT);

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

  //If you want to open the network without a password, please delete the password
  WiFi.softAP(ssid, password);
  IPAddress myIP = WiFi.softAPIP();
  Serial.print("AP IP address: ");
  Serial.println(myIP);
  server.begin();

  Serial.println("Server started");
  //Initialize the 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) {                             // check if connected
    Serial.println("New Client."); 
    String currentLine = "";                // create String variable to hold the data
    while (client.connected()) {            // keep looping while keeping the connection
      if (client.available()) {             // check if the connection has data
        char c = client.read();             // read received data
        //Serial.write(c);                    // print on serial monitor
        if (c == '\n') {                    // if newline is read

          //Clear the cached content
          if (currentLine.length() == 0) {
            client.print(" ");
            break;
          } else {    // Clear variable cached data if there is a newline
            currentLine = "";
          }
        } else if (c != '\r') {  // If you get characters other than carriage return
          currentLine += c;      // The obtained character is added to the end of the variable
        }

        // Check if /GET is at the end
        if (currentLine.endsWith("/GET")) {
             //Read temperature and humidity
             float   temp = sht3x.getTemperatureC();
             float   humi = sht3x.getHumidityRH();  
             //Print on web page 
             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 following results (temperature and humidity under the LAN).

Result

Member function

  • WiFi.softAP(ssid, password)

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

    Parameter:

  • ssid: WIFI name

  • password: WIFI password

  • WiFi.softAPIP()

    Description: WIFI IP address

Was this article helpful?

TOP