This is to document my learning to use a ds18b20 temps sensor and displaying it to a OLED display using just node red installed in a Raspberry Pi 3. This may help other beginners like myself with the node red journey.
While this "temp and display thing" has been done many times before with python etc in the pi or in combination with esp8266 to drive the oled, i could not find any such implementation using just the PI and node red alone on the net.
In the process I learnt a bit about nodered. I have to acknowledge the nodered forum and in particular @knolleary for the advise given on the project.
Here is a very brief outline of the project:
Aim: To connect ds18b20 temp sensor and display to an Oled display using just a pi and node red.
Initial SETUP:
Install ds18b20: From palatte, find and Install ds18b20 node
Install Oled: Same for oled Node-red-contrib-oled
Enable 1 wire in pi with config
Enable 12c in pi with config
==============
HARDWARE:
hook up 18b20
=================
Hook up the oled
Data: (GPIO2); Clock (GPIO3)
===================
NODE-RED FLOW
- Set up the ds18b20 node to read temp every few sec and store it into a global variable. This is done with a function node that set the global variable" t1" to the payload.
declare global variable
Result: Now the temperature is in a global variable t1 for retrieval
=====================
Displaying the global variable on the oled:
The description of the oled node is a bit vague. The oled needs an i2c address, usually hex 3c in the node setup. Note: Just enter 3c and not 0x3c. To find the address: i2cdetect -y 1.
To display data to oled, the oled node needs a json object payload of:
{"size":3,"x":1,"y":1,"text":"text string to display"}.
So to display the global variable (t1), we need to:
- "get" the global variable and pass it tthe payload with a function node
var t1= global.get("t1");
msg.payload=t1
- Because the payload is numeric, we have to convert this numeric data to string, so another function node to convert number to string:
msg.payload=String(msg.payload). Note case sensitive, I was caught out using string.
- Another function node to add *C
msg.payload +=" *C";
- Finally a function to set up the json payload for the oled with the payload from the flow. refer to topic where Nick OLeary, @knolleary show me how. How to use oled node
- Just add an inject node at the beginning to update the temp every few secs.
The result.
It may not be the most elegant approach, but I learnt a lot and was fun.
PS: I also got the temperature to display on various remote oleds connected to esp8266(espeasy) via mqtt.