Web of Things Arduino: Temperature sensor

12 Sep 2021 \
4 minutes \
written by Philipp Blum
Categories: Tutorial

A simple temperature sensor connected to an ESP32 which exposes a WoT TD and REST API to get the temperature.

Prerequisites #

You should check, if you have the BMP280 or BME280. They both can measure temperature. So, for our use-case it doesn’t make a difference. Normally, when you a cheap module, you end up with the less featured BMP280. The only difference between those two is that the BME280 is also able to measure humidity, besides the default functionality of pressure and temperature.

Connect your ESP32 to the BMP280 via I2C #

ESP32 Pin Bosch BMP280
VCC (3.3V) VCC (VDD)
GND GND
D21 (IO21) SDA
D22 (IO22) SCL (SCK)

Create a new project in the Arduino IDE #

Create a new project under File → New in the Arduino IDE.

Copy the temperature sensor code #

Copy the temperature sensor code into your Arduino IDE file.

Change the configuration #

You have to change the varaibles ssid and password in order to be able to connect to your router.

Flash the temperature sensor code #

You should open the serial monitor before you flash the code. You can do so under Tools → Serial Monitor. Now, flash the code under Sketch → Upload. Once the flashing it done, the ESP32 should restart. You should be able to see some text in the serial monitor, as well as a blinking LED on the ESP32 board. In case the ESP32 doesn’t restart, just push the button labeled EN.

Test the temperature sensor #

Before we use the REST API of the temperature sensor, we should test, if everything works as expected.

Get WoT Thing Description

Open Insomnia or your REST Client. Create a new HTTP GET Rest in your client. Give this request some meaningful title, such as Get TD. Use the URL http://[YOUR_IP]/.well-known/wot-thing-description. Replace [YOUR_IP] with the IP of your ESP32. The IP address should be logged in the serial monitor. Send the request and wait for the response. The response should be a valid WoT TD. It should look like this

    {
    "id": "uri:bme280",
    "title": "BME280 Weather Sensor",
    "@context": [
      "https://www.w3.org/2019/wot/td/v1"
    ],
    "base": "http://192.168.178.25/",
    "securityDefinitions": {
      "nosec_sc": {
        "scheme": "nosec"
      }
    },
    "security": [
      "nosec_sc"
    ],
    "@type": [
      "TemperatureSensor"
    ],
    "forms": [
      {
        "rel": "properties",
        "op": [
          "readallproperties",
          "writeallproperties"
        ],
        "href": "/things/bme280/properties"
      },
      {
        "href": "ws://192.168.178.25/things/bme280"
      }
    ],
    "properties": {
      "humidity": {
        "type": "object",
        "properties": {
          "humidity": {
            "type": "number",
            "unit": "percent"
          }
        },
        "readOnly": true,
        "title": "Humidity",
        "minimum": 0,
        "maximum": 100,
        "@type": "LevelProperty",
        "forms": [
          {
            "href": "/things/bme280/properties/humidity"
          }
        ]
      },
      "pressure": {
        "type": "object",
        "properties": {
          "pressure": {
            "type": "number",
            "unit": "hPa"
          }
        },
        "readOnly": true,
        "title": "Pressure",
        "forms": [
          {
            "href": "/things/bme280/properties/pressure"
          }
        ]
      },
      "temperature": {
        "type": "object",
        "properties": {
          "temperature": {
            "type": "number",
            "unit": "degree celsius"
          }
        },
        "@type": "TemperatureProperty",
        "forms": [
          {
            "href": "/things/bme280/properties/temperature"
          }
        ]
      }
    },
    "href": "/things/bme280"
  }
  

As you can see, this json describes all functionality available for the device. Our temperature sensor has three available PropertyAffordances. humidity, pressure and temperature. We are only going to test temperature, since it is available for both sensors, BMP280 and BME280. The temperature is available under the endpoint /things/bme280/properties/temperature.

Test the temperature PropertyAffordance

The temperature PropertyAffordance is described as following

{
    "temperature": {
      "type": "number",
      "unit": "degree celsius",
      "@type": "TemperatureProperty",
      "forms": [
        {
          "href": "/things/bme280/properties/temperature"
        }
      ]
    }
}

In order to test if our API works, we have to create another GET Request in your REST Client. Give it another meaningful name, such as Get temperature. Use http://[YOUR_IP]/things/bme280/properties/temperature as URL. Replace [YOUR_IP] with the IP address of your temperature sensor. Send the request and your response should look like this.

{
  "temperature": 20.8
}

If you get a reasonable temperature back, your sensor device works and we can use it. We have other tutorials which utilize this temperature sensor. You may want to check them out in order to understand the true power of Web of Things.