Laurent Hoeltgen

An IoT Weather Station

2021-12-30  ·  3 min read  ·  Programming

A weather station with a web frontend

An IoT Weather Station

I recently bought an Arduino MKR Wi-Fi 1010 and decided to build a small IoT device using the Arduino IoT Cloud. Since I have DHT 11 sensor lying around at home I decided to build a simple weather station. The temperature sensor is connected to the Arduino and the Arduino connects over my Wi-Fi at home to the Internet and provides information on temperature, humidity, and the heat index.

First Impressions

Going from 0 to the first data points on the web view was surprisingly simple.

Dashboard with signal data

There were two places where I struggled a bit.

The Source Code

The C++ code for the sketch is really simple. Note that the variables for the temperature and humidity need to be declared on the web frontend so that they are known to the widgets as well as the Arduino. Also, a lot of code is automatically generated for you. You just have to insert the parts where you read the data and set up the sensor.

/*
  Arduino IoT Cloud Variables description

  The following variables are automatically generated and updated when changes are made to the Thing

  float heatindex;
  float humidity;
  float temperature;

  Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
  which are called when their values are changed from the Dashboard.
  These functions are generated with the Thing and added at the end of this sketch.
*/

#include "DHT.h"
#include "thingProperties.h"

namespace {
  constexpr uint8_t DHTPIN = 2;
  constexpr uint8_t DHTTYPE = DHT11;
  DHT dht(DHTPIN, DHTTYPE);
}

void setup() {
  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(1500); 

  // Defined in thingProperties.h
  initProperties();

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  
  /*
     The following function allows you to obtain more information
     related to the state of network and IoT Cloud connection and errors
     the higher number the more granular information you’ll get.
     The default is 0 (only errors).
     Maximum is 4
 */
  setDebugMessageLevel(4);
  ArduinoCloud.printDebugInfo();
  
  pinMode(LED_BUILTIN, OUTPUT);
  
  dht.begin();
  Serial.println("Setup completed.");
}

void loop() {
  delay(2000);
  ArduinoCloud.update();

  humidity = dht.readHumidity();
  temperature = dht.readTemperature();
  if (isnan(humidity) || isnan(temperature))
  {
    Serial.println("Failed to read from DHT sensor!");
    Serial.println(humidity);
    Serial.println(temperature);
    return;
  }
  
  heatindex = dht.computeHeatIndex(temperature, humidity, false);
  Serial.print("Humidity: ");
  Serial.print(humidity);
  Serial.println("%");

  Serial.print("Temperature: ");
  Serial.print(temperature);
  Serial.println(" degrees");
  
  Serial.print("Heat index: ");
  Serial.println(heatindex);
  return;
}

How to cite this page

Hoeltgen, Laurent: An IoT Weather Station, 2021-12-30.

BibLaTeX code:
@online{iotweather,
  author   = {Hoeltgen, Laurent},
  title    = {An IoT Weather Station},
  date     = {2021-12-30},
  language = english
  url      = {https://www.laurenthoeltgen.name/content/blog/
              iotweather}
}
Download BibLaTeX file

CC BY-SA 4.0 Laurent Hoeltgen. Last modified: September 19, 2025.
Website built with Franklin.jl and the Julia programming language.
Privacy Policy · Terms