Gerrit Niezen

Maker of open-source software and hardware.

Feature image

I have a tiny growhouse standing outside that I've used for seedlings in the past, and potentially want to use for growing veggies hydroponically in the future. It occurred to me that I don't know how hot or cold it gets inside.

Given that it is standing against the side of the house, it gets a lot of sunlight at certain times of day and almost nothing at other times. When I know it's warm outside I open up the flap to make sure it doesn't get too hot, but I'd like to know exactly how hot it gets.

I've worked with DS18B20 temperature sensors in the past, so I knew that they are ubiquitous and pretty easy to work with. You can buy the bare chip, but I went with the waterproof version at the end of a 3 metre lead from Amazon for £3.99 including P&P. The reason for getting a temperature sensor on a lead is so that I could keep the prototype (with microcontroller on a breadboard) inside the house connected to power and just run the lead outside through the window, so that it's the only part that needs to be waterproof.

I then had to decide which microcontroller to use. Originally I wanted to use my C.H.I.P. from Next Thing Co. but it looks like they recently went bust, so I dediced to give the ESP8266 chip a try. There are a bunch of NodeMCUs at my local hackspace available for less than a fiver, and they seem like a pretty neat prototyping platform, as it's breadboard-compatible and then just plugs into USB. I've tried using the built-in Lua interpreter in the past, but I found it to be a bit flaky so decided to go down the Arduino Core route instead.

I took me forever to get the basic DS18B20 Arduino code example to work. It turns out that I had my wires swapped: There are two types of DS18B20 sensors with red, yellow and green wires and I had the less common type where the yellow wire instead of the green one is ground. One good thing came out of this: On that page describing the sensor types I discovered that there is an ESP8266 port of Espruino, which would allow me to write actual JavaScript code, not “NodeJS-like” code which is actually Lua[1].

Man, was this a game changer. Espruino has great example code, and a simple web-based editor which allows you to connect to the device over WiFi and update your code over the air (OTA) almost instantly. Compared to the Arduino Core where it can take quite long to update your code over USB, this felt magical. I know Arduino Core has OTA capability too, but I haven't tried it yet.

So, without further ado, here is the code I use to connect to the temperature sensor, cobbled together from various Espruino examples. It takes a sensor reading every minute (in Celsius) and runs a web server that displays a temperature graph. It also toggles the on-board LED when it takes a reading. I specifically did not want to use an IoT platform/service, as they don't tend to stay around for long and the ESP8266 is perfectly capable of running its own web server:

temperature graph

var wifi = require('Wifi');
var http = require('http');

var led = Pin(NodeMCU.D4); // on-board LED
var toggle = 1;
var ow = new OneWire(NodeMCU.D3); // data pin connected to D3
var sensor = require("DS18B20").connect(ow);
var history = new Float32Array(30); // store 30 readings

function updateLed(){
  digitalWrite(led, toggle);
  toggle=!toggle;
}

setInterval(function() {
  updateLed();

  var temp = sensor.getTemp();
  console.log(temp);
  // move history back
  for (var i=1; i<history.length; i++)
    history[i-1]=history[i];
  // insert new history at end
  history[history.length-1] = temp;
}, 60000);

function onPageRequest(req, res) {
  res.writeHead(200, {'Content-Type': 'text/html'});
  res.write('<html><head><meta charset="utf-8"/><meta http-equiv="refresh" content="60"></head>'+
            '<body><canvas id="canvas" width="200" height="200" style="border:1px solid #888;"></canvas><script>');
  res.write('var d='+JSON.stringify(history)+';'+
'var c=document.getElementById("canvas").getContext("2d");'+
'c.moveTo(0,100 - (d[0]-d[d.length-1])*10);'+
'for (i in d) {'+
'var x = i*200/(d.length-1); var y = 100 - (d[i]-d[d.length-1])*10;'+
'c.lineTo(x, y);'+
'if (i % 5 === 0) c.fillText(Number.parseFloat(d[i]).toFixed(1),x,y - 2);}'+
'c.stroke()'+
'</script>');
  res.end('</body></html>');
}

function onInit() {
  wifi.restore();
  http.createServer(onPageRequest).listen(80);
  console.log("Server created at " + wifi.getIP().ip);
}

onInit();

What's also great about Espruino is that it saves your WiFi credentials separately on the flash memory, which means you don't accidentally expose them in your example code. 😉


  1. which is where NodeMCU gets its name from ↩︎

#Hydroponics #Electronics

Comment on this post

Feature image

It looks like a lot of people are migrating to GitLab in the wake of the Microsoft acquisition news.

I came across a tweet this morning by John O' Nolan, founder of Ghost[1]:

Hope this is true. Github could use some fresh leadership, and Microsoft under Satya is probably the best possible home for it. https://t.co/7v2ztLXP2U

— John O'Nolan @ 🇨🇦 (@JohnONolan) June 3, 2018

I wish I could be this optimistic, but I really think it's going the other way. Even under Satya Nadella, Microsoft is a company that kills it acquisitions:

.@Microsoft's open source playbook: https://t.co/d2JMknhXLv
1. embrace <-– we are here
2. extend
3. extinguish

— manic pixie dream boy 🇳🇿 (@ahdinosaur) June 4, 2018

The reason this hits so close to home for me is that during 2009 to 2012 I was working on my PhD as part of a larger EU project called SOFIA[2]. This project was led by Nokia, right around the time that they got bought by Microsoft. Nokia (now under Microsoft) were suddenly not all interested in the work we were doing. Instead of the stuff I worked on being productised and used in the real world, the only place where they ended up was in my thesis. that's to not even mention the impact that Microsoft's embrace-extend-extinguish strategy of Nokia has had on Finland's economy, people and psyche.

I still remember when Microsoft bought my favourite todo-list app, WunderList:

Microsoft just bought Wunderlist :( How sad, it has been my favourite to-do list app for a long time. Alternatives?

— Gerrit Niezen (@gendor) June 1, 2015

It took them fewer than two years to get to step 3:

I knew it was only time before Microsoft would do this, the bastards! https://t.co/b1UnJfJWtd

— Gerrit Niezen (@gendor) April 20, 2017


  1. the platform this blog runs on ↩︎

  2. Smart Objects For Intelligent Applications ↩︎

Comment on this post

Feature image

I joined Facebook more than a decade ago. The beginning was great – a social website that was actually well designed was taking on the big MySpace. The more people joined the more fun it was.

I remember even developing a Facebook app at one stage, something that would show a series of photos of your choosing on your profile. (I think Facebook includes that feature by default now?) It started going downhill after the news feed launched, when Facebook profiles were not the main focus anymore.

Facebook was great to stay in touch with friends when I moved overseas. But I started using it less and less. The whole Cambridge Analytics scandal felt like the last straw, and I downloaded all my account data with the intention to close my account. But I still didn’t have the impetus I needed to click the delete account button.

Yesterday my wife sent me a message asking “why are we not friends on Facebook anymore”? I haven’t logged in in a week, but when I took a look at my account all my friends were unfriended. It looked like a Facebook glitch, but finally gave me that push I needed to click that button. #DeleteFacebook

Comment on this post

Feature image

I've made many attempts at starting a blog. Let's see if it sticks this time ...

Comment on this post

Enter your email to subscribe to updates.