Building an API based temperature sensor

15 Aug 2018 • Programming

The goal of this short summary is to explain how you can build a small API based temperature sensor that costs less than 10$ and can be used anywhere. I use this sensor at my office to log the temperature and then display it on a smart dashboard.


We will be using the following parts. You can get most of them on Aliexpress.

Putting it all together

This is actually fairly easy. Solder 4 wires to the ground, VCC, SDA and SCL pins of your temperature sensor. Then connect these wires as follows to your Wemos D1:

Make sure to get a micro USB cable and a charger to power everything.

You can use my board as a reference on how your board should look:

Programming the Wemos D1

Before we can start programming we must make sure we have all the necessary tools available. We will need a copy of the Arduino editor. In the Arduino editor open Tools -> Boards -> Boards Manager and activate the esp8266 boards. If you cannot find them in the menu make sure to first add under File -> Preferences as a Additional Board Manager URL.

Having done all the required set-up work we can now move on to programming our board. I have decided to flash my board with the code below. Feel free to use it too, you will however need to change your WiFi credentials and your API settings.

In the code below the board measures the temperature every 2 minutes and then sends the measurements via a get request and an API key to a server. This is definitely not the most secure or elegant way of transmitting temperature data, but since this is a small side project it probably is good enough.

#include <Wire.h>
#include "Adafruit_MCP9808.h"
#include <Esp.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>

// Create the MCP9808 temperature sensor object
Adafruit_MCP9808 tempsensor = Adafruit_MCP9808();

void setup() {
  WiFi.begin("ssid", "password");   //WiFi connection

  while (WiFi.status() != WL_CONNECTED) {
    //Wait for the WiFI connection completion
    Serial.println("Waiting for connection");

  Serial.println("WiFi works");

  if (!tempsensor.begin())
    Serial.println("Couldn't find MCP9808!");
    while (1);
    Serial.println("Temperature works");

void loop() {
  if(WiFi.status()== WL_CONNECTED){   //Check WiFi connection status
   Serial.println("beginning measurement and submission");
    HTTPClient http;    //Declare object of class HTTPClient
    float c = tempsensor.readTempC();
    String postString = "temperature="+ String(c) + "&apiKey=apiKey";
    http.begin("" + postString);
    //Specify request destination
    http.addHeader("Content-Type", "text/plain");
    //Specify content-type header
    Serial.println("Measured" + String(c));
    int httpCode = http.GET();   //Send the request
    String payload = http.getString();      //Get the response payload
    Serial.println(payload);    //Print request response payload
    http.end();  //Close connection
    // if the WiFi connection didn't work,
    // we will restart our board and try again.
    Serial.println("Error in WiFi connection");   
    // reboot   
  delay(120000);  //Send a request every 120 seconds


We will need to handle the request we have sent from the sensor on a server. The following code is a very simple example:

from flask import Flask, request

app = Flask(__name__)

@app.route('/temp', methods=['GET'])
def submit_temperature_data():
    print(request.args["apiKey"] + '\n')
    print(request.args["temperature"] + '\n')
    return ''

if __name__ == "__main__":

If you don’t feel like deploying the code to a server you can use your local development machine and a service like ngrok. It has saved me from a great deal of pain, since this simplifies debugging.


Instead of having a very light-weight Flask server, I have opted for a sinatra server (running on top of ruby) and then logging the temperature to a MongoDB database which is then further processed to be used on my other small project FlashDash.

I hope that if you are planning a similar project that my project can be of some help to you.


The arduino code example and the soldering schematics have been taken / adapted from:

Have any feedback?

Please feel free to send me a mail! I would love to hear from you.