the curious

Wow, it has been a while

I’m sorry.

I know, the massive amount of readers have been sitting impatiently, waiting for me to post more. Sorry.

I want to write about my recent projects. A lot has happened. In the next couple of posts I will try to clean up the mess of undocumented projects and go through them one at a time. As a teaser, here is the list:

  • CoreXY 3D-printer
  • Control of DALI-lights through a MQTT-DALI bridge (a lot of software was written)
  • A number of iterations on my mobile robot, both hard- and software
  • Some plugins written for various open-source projects
  • Building a home server, learning virtualization, networking, etc. from scratch one step at a time
  • MQTT Doorbell (yes everyone has to make one)
  • A lot of 3D prints, some functional, interested?
  • I guess personal education is a project too (which has take most of the time since last post)
  • ???

 

Which should I begin with? Don’t answer all at once!

(By writing this post I am not promising any time frame for the writing of the next post ™. I am not to be held liable in the event of an acute case of impatience for you, the reader. No robots were harmed in the creation of this blog. I don’t assume anyone is reading this.)

Temperature logger client and server

I found myself with two tiny, fancy wifi chips in hand – the ESP8266. It has a microcontroller on it, and some clever people added support for it in the Arduino IDE (https://github.com/esp8266/Arduino). They also added plenty of examples to make anything you want!
I remembered that I also had a temperature sensor lying around – the DS18B20 which is a very nice, one-wire temperature sensor. There are some Arduino libraries that make interfacing with this sensor very easy; I used the OneWire library and the Arduino-Temperature-Control-Library. The latter includes an example sketch on how to read the temperature data from the sensor.

I wanted to make something with all this! So I thought hard and long (not really) and figured I would write a sketch for the ESP8266 that would read the sensor and send the temperature reading to another device. This other device I decided to be a RPi, and I would write the server to pick up the temperature readings with Flask as I’ve used before.

This is my schematic for the WiFi-enabled temperature sensor:

ds18b20_server_bb

For the ESP8266 firmware I needed to figure out how to make a HTTP POST request. The examples for the ESP8266 only showed how to make a HTTP GET request. I found my answer on stackoverflow and wrote this request:

POST /temp/ HTTP/1.0
User-Agent: TempClient/0.2
Host: localhost
Content-Type: application/x-www-form-urlencoded
Content-Length: 10

temp=22.22

I figured that the “Content-Length” would always be 10, as the temperature reading from the DS18B20 would be with two significant digits. (I might run into problems if I measure a temperature below 10 degrees, eg. 9.37 degrees…).

With this on hand I could now write my firmware in the Arduino IDE:

/*
 *   This sketch uses libraries OneWire and DallasTemperature to read the 
 *   temperature reading from the DS18B20 sensor on GPIO pin 2 on the ESP8266. 
 *   The temperature reading is then send by HTTP-POST request to a server.
 */

#include <ESP8266WiFi.h>
#include <OneWire.h>
#include <DallasTemperature.h>

// Insert wifi settings here instead of *****
const char* ssid = "******";
const char* password = "*******";

// Insert server ip or web address
const char* host = "*******";
const unsigned int delaytime = 10000;

// Data wire is plugged into port 2 on the ESP8266
#define ONE_WIRE_BUS 2

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);

// Create an instance of the server
// specify the port to listen on as an argument

float temp;

void setup() {
  Serial.begin(115200);
  delay(10);
  Serial.println("DS18B20 TEMPERATURE LOG CLIENT");
  sensors.begin();
  // Connect to WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  
  // Print the IP address
  Serial.println(WiFi.localIP());
  
}

void loop() {
    
    Serial.print("connecting to ");
    Serial.println(host);
    
    // Use WiFiClient class to create TCP connections
    WiFiClient client;
    const int httpPort = 5000;
    if (!client.connect(host, httpPort)) {
      Serial.println("connection failed");
      return;
    }

    sensors.requestTemperatures();
    temp = sensors.getTempCByIndex(0);
    Serial.println("Temperature: ");
    Serial.println(temp);

    String url = "/temp/";

    // Create the POST request string. Content should be of length 10 as the temperature 
    // will be  of length 5 (eg. 21.34).
    String request = String("POST ");
    request += url;
    request += " HTTP/1.0\r\n";
    request += "User-Agent: TempClient/0.2\r\n";
    request += "Host: ";
    request += host;
    request += "\r\n";
    request += "Content-Type: application/x-www-form-urlencoded\r\n";
    request += "Content-Length: 10\r\n\r\n";
    request += "temp=";
    request += temp;

    Serial.print("Requesting URL: ");
    Serial.println(url);
    Serial.println("With request:");
    Serial.println(request);
    
    client.print(request);
    delay(10);

    // Pick up response if any.. 
    Serial.println("Request sent. Response: ");
    while(client.available()){
    String line = client.readStringUntil('\r');
    Serial.print(line);
    }
    Serial.println();
    Serial.println("closing connection");
    
    delay(delaytime);
}

The Flask code for the server is available on github. Feel free to do anything you want with it. Right now it only supports one temperature sensor, but it shouldn’t be too hard to add support for multiple devices and other types of sensors.

Below is the resulting web page. This was a fun one to write!

1

2