DataLogging with ESP32 Module

Abstract : in this Post we show an example of the use of the ESP32 module (with the ESP-WROOM-32 development board) for the remote acquisition of an analog signal. The ESP32 module natively offers support for WiFi and a series of GPIO, both digital and analog, also boasts a very low consumption and very small dimensions. These features make the ESP32 module particularly suitable for the so-called IoT (Internet of Things) applications where reduced consumption, connectivity and small dimensions are particularly important.

DataLogging & IoT

IoT (“Internet of Thinghs”) technology is becoming increasingly used in today’s world. Of course, for those who deal with and use technology, it is clear that this is not a really new thing : it is simply the integration, in a single small and smart “object” with computational capacity, of different pre-existing technologies, such as WiFi, Bluetooth and sensors. In the meantime, numerous hardware and software platforms have been created that allow the design and use of objects and services based on the IoT paradigm.

For remote datalogging tasks we decided to use an IoT approach. We used an ESP32 module that acquires the analog data via ADC and makes it available on the WiFi network via a web server. The web server responds to an http query by returning the value of the data. The ESP32 module is accessed by a Raspberry Pi microcomputer which has the function of storing and managing data.

The diagram below shows the architecture of the system.

ESP32 Module

(from https://circuits4you.com/)

ESP32-WROOM-32 is a powerful, generic Wi-Fi+BT+BLE MCU module that targets a wide variety of applications, ranging from low-power sensor networks to the most demanding tasks, such as voice encoding, music streaming and MP3 decoding.

At the core of this module is the ESP32 chip. The chip embedded is designed to be scalable and adaptive. There are two CPU cores that can be individually controlled, and the CPU clock frequency is adjustable from 80 MHz to 240 MHz. The user may also power off the CPU and make use of the low-power co-processor to constantly monitor the peripherals for changes or crossing of thresholds. ESP32 integrates a rich set of peripherals, ranging from capacitive touch sensors, Hall sensors, SD card interface, Ethernet, high-speed SPI, UART, I2S and I2C.

The integration of Bluetooth, Bluetooth LE and Wi-Fi ensures that a wide range of applications can be targeted, and that the module is “future proof” : using Wi-Fi allows a large physical range and direct connection to the internet through a Wi-Fi router, while using Bluetooth allows the user to conveniently connect to the phone or broadcast low energy beacons for its detection.

The images below shows the module with the Power LED and the On board LED already connected to GPIO2.

ESP32 Wroom 32 DevKit has total 25 GPIOs out of that few pins are Input only Pins. It features also a number of peripherals :

  • 18 Analog-to-Digital Converter (ADC) channels
  • 10 Capacitive sensing GPIOs
  • 3 UART interfaces
  • 3 SPI interfaces
  • 2 I2C interfaces
  • 16 PWM output channels
  • 2 Digital-to-Analog Converters (DAC)
  • 2 I2S interfaces

The scheme below shows the pin-out of the module.

Hardware

The ESP32 module has been inserted in a simple plastic box (metal is not good because it would shield the WiFi signal). The card is powered through a 3.3 V regulator so the external power supply voltage can vary without problems from 6 to 12 V. The pin of the card configured with analog input is brought to the external BNC connector. The image below shows the module inside the box.

Software

For programming the ESP32 module you can safely use the IDE for Arduino : simply configure the IDE by loading the type of ESP32 board you are using, for details you can refer to the numerous tutorials that are on the network. Programming is also very simple as there are numerous examples on the net. Here we give only some details on the most significant points of the firmware that we have loaded on our ESP32 module.

In the program we define a series of variables and constants including the name and password of our WiFi network (which can then be read from a configuration file), the IP address chosen for our module which will thus have a static IP, the pin the status LED pre-wired on the board and the pin of the analog input that we want to acquire.

char ssid[] = WiFi_SSID;          // network name
char pass[] = WiFi_PASS;          // network password
const int LED = 2;                // built-in LED
const int analogPin = 36;         // analog pin
IPAddress local_ip(192,168,1,61); // IP Address of the shield
IPAddress gateway(192,168,1,1);   // IP Address of the gateway
IPAddress subnet(255,255,0,0);    // IP Address of the subnet

In the setup the analog input is configured in order to exploit the whole range, the analog input is converted by a 12 bit ADC therefore the conversion result goes from 0 to 4095, with attenuation of 6dB the full scale corresponds to 2.2V. The WiFi part of the card is then configured and the connection to the WiFi network is made. The web server is then instantiated on the card and will respond to our http queries.

void setup() {
...
analogSetPinAttenuation(analogPin,ADC_6db); // Attenuation 2, full scale 2,2V
...
// WIFI_AP : solo Access Point
// WIFI_STA : solo Client
// WIFI_AP_STA : duale, sia AP che Client, default
// WIFI_OFF : WiFi OFF
WiFi.mode(WIFI_AP_STA);
WiFi.config(local_ip, gateway, subnet);
WiFi.disconnect();
...
// Connecting to WiFi
while(status != WL_CONNECTED){
  Serial.print("Attempting to connect to SSID: ");
  Serial.println(ssid);
  // connect to WPA network
  status = WiFi.begin(ssid,pass);
  delay(5000); // delay 5 seconds
}
...
// Starting server
server.begin();
...
}

The code to read the requests of a client connecting to our server is presented below. You instantiate a client object that waits for connections. When a connection is detected, the http request is read (in our case absolutely trivial) and the response is prepared. In our case the response is the reading of the acquired analog signal.

void loop() {
  // Listen for incoming clients
  WiFiClient client = server.available();
  if(client) {
    // get a client
    Serial.println("New client");
    // an http request ends with a blank line
    bool currentLineIsBlank = true;
    // string to hold incoming data from client
    String currentLine = "";
    while(client.connected()) {
      if(client.available()){
        // reading from the client
        char c = client.read();
        Serial.write(c);
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          client.println("HTTP/1.1 200 OK");
          client.println("Content-type:text/html");
          client.println();

          analogSetPinAttenuation(analogPin,ADC_6db);
          int sensorReading = analogRead(analogPin);
          Serial.print("sensorReading: ");
          Serial.println(sensorReading);
          client.print("sensorReading: ");
          client.print(sensorReading);
          client.println();
          break;
        } 
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
          currentLine = "";
        } else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
          currentLine += c;
        }
      } 
    }
    // close the connection
    client.stop();
    Serial.println("Client disconnected");
  }
}

After loading the firmware onto the card, you can query the http service exposed by our web server : simply use a common browser and enter the IP address of the ESP module. If everything went well, the server will respond with the acquired analog input value, as shown in the following image.

Of course, the reading of the analog signal can also take place automatically using for example a python program that runs on a Raspberry connected on the same WiFi network. In the latter case, the connection to the web server and the reading of the analog signal is obtained with a python code similar to the following:

#Lettura dati da server web ESP32 IP= 192.168.1.61
response = urllib.urlopen('http://192.168.1.61/')
ESP32Response = response.read()
#Lettura dati ADC da ESP32
ADC_Value0 = int(int(ESP32Response[15:])/2)

If you liked this post you can share it on the “social” Facebook, Twitter or LinkedIn with the buttons below. This way you can help us! Thank you !

Donation

If you like this site and if you want to contribute to the development of the activities you can make a donation, thank you !

Check Also

Fluorescence quenching and Stern-Volmer equation

Abstract: in this article we describe some fluorescence quenching measures in some substances. The phenomenon …