elpa02
Posts: 7
Joined: Thu Mar 22, 2018 9:00 pm

My 1st project: send sensor data to influxdb

Mon Nov 21, 2022 3:01 pm

hi everyone

i post here cause i am a beginner. however since i have no previous serious experience in working with raspberry if this is not the right place to post please accept my apologies in advance.

project target: check temperature of 4 boilers by 4 ds1820b sensors and enviroment data by a dht22 over internet

i have read many articles and seen many youtube videos but it seems that there are many ways to implement it. having a lack of theoritical knowledge might needed i cannot keep up. so this is where i need your help to put everything in order.

up to know i have figure out that :
1. i can connect 4 sensors on the same cable creating a sensor chain
2. it is better to use a microcontroller like raspberry pico w (so to save resources) to receive data from sensors and send them to a database like influxdb either running in the raspberry pi 3b or to influxdb cloud service or google spreasheet
3. use grafana to visualize data

up until now :
i have connected a ds18b20 sensor on the raspberry pi 3b
1 wire interface is enabled
on /sys/bus/w1/devices there is a folder named 28-00000a082896
and in this folder there is a file named w1_slave
typing " cat w1_slave" i can read the temp

the difficult part for me, hoping to your help, starts now. trying to split the whole thing i will try to make use of the sensor connected on the raspberry pi 3b.
So :
1. how to send sensor data to a database ? is influxdb what i have to set up next?
2. is it better to have influxdb running on raspberry pi or there is a cloud service (preferable) available?

thank you in advance
elpa02

User avatar
neilgl
Posts: 5317
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near The National Museum of Computing

Re: My 1st project: send sensor data to influxdb

Mon Nov 21, 2022 4:12 pm

In my case I had several microcontrollers (esp8266 based) sending temperature data from DS18B20 sensors via MQTT to a pi3 running influxdb and grafana and acting as a MQTT server.
Rough outline:
Install on the pi influxdb and telegraf
Create sensor database in influxdb
Configure telegraf to use an mqtt_consumer input, using the pi mqtt server , with topic “temps/#”
Add a data source in Grafana that uses Influxdb database that we created earlier.
So,
1. The sensor data gets into the database using telegraf.
2. I used influxdb on the pi, no cloud to fail…

elpa02
Posts: 7
Joined: Thu Mar 22, 2018 9:00 pm

Re: My 1st project: send sensor data to influxdb

Thu Nov 24, 2022 6:06 am

I wonder which is the easiest way to achieve this. For example would it be easier if skip db ? Could I make a simple dashboard using only node red? I have tried many things but it seems I am not capable to write 10lines of code correctly

MiscBits
Posts: 1098
Joined: Wed Jan 27, 2021 12:48 pm

Re: My 1st project: send sensor data to influxdb

Thu Nov 24, 2022 4:22 pm

You could use a node execute the 'cat' command

Then use a function node to tidy up the text to turn it to a number as needed and then use the graph function to create a dashboard

The dashboard nodes will need to be loaded using the pallet function

You may find executing a short script to do the text to number tidy up easier - I do not have a ds18b20 handy to see what the output is so I'm a bit blind to help much here.

One drawback - you will loose any historic data every time Node-Red restarts this way.

elpa02
Posts: 7
Joined: Thu Mar 22, 2018 9:00 pm

Re: My 1st project: send sensor data to influxdb

Thu Nov 24, 2022 4:34 pm

i ll give a try with node red only. it seems to be easier for the beginning.
i wish i could provide you the data needed of the ds18b20 but i don't even know what you expect :oops: :roll:
maybe i should take more seriously python learning

MiscBits
Posts: 1098
Joined: Wed Jan 27, 2021 12:48 pm

Re: My 1st project: send sensor data to influxdb

Fri Nov 25, 2022 1:37 am

You can do it in Bash.

I use the following to get the temperature into Node-Red from an execute statement:

Code: Select all

sudo vcgencmd measure_temp | awk -F "[=\']" '{printf "%.1f",$2/NR}'
The command returns temp=33.6'C followed by a new line so I pass this to awk and split the line at the equals sign and the single quote - this sets $2 as the bit in between or 33.6.

This is then printed with one decimal place and the key is the /NR - this takes out the new line at the end of the data and just the number us passed back as the result if the execution node.

If you can show the output of the cat command I'll try and create you the execution node.

Key thing is to show the line layout - if you run the command and copy and paste the whole output in code tags like this:

Code: Select all

pi@pi-server:~ $ sudo vcgencmd measure_temp
temp=35.5'C
pi@pi-server:~ $
then I may be able to see if it has a new line and the output.

If it does not have a new line then I would expect the data to be like

Code: Select all

pi@pi-server:~ $ sudo vcgencmd measure_temp
temp=35.5'Cpi@pi-server:~ $

User avatar
rpdom
Posts: 20858
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: My 1st project: send sensor data to influxdb

Fri Nov 25, 2022 6:55 am

MiscBits wrote:
Fri Nov 25, 2022 1:37 am
You can do it in Bash.

I use the following to get the temperature into Node-Red from an execute statement:

Code: Select all

sudo vcgencmd measure_temp | awk -F "[=\']" '{printf "%.1f",$2/NR}'
Why use sudo?

Code: Select all

notpi@raspi15:~ $ vcgencmd measure_temp 
temp=39.7'C
Unreadable squiggle

elpa02
Posts: 7
Joined: Thu Mar 22, 2018 9:00 pm

Re: My 1st project: send sensor data to influxdb

Fri Nov 25, 2022 7:20 am

Thank you for your reply and your effort to help me.
Playing around with node red I found that I can install ready to run nodes for ds18b20. I had only to put between the sensor node and the gauge node (so to visualize sensor data) a third function node where I had to (copy paste) put into it a few lines of code :

var o = msg.payload
msg.payload = o.temperature;
return msg;

Finally it worked! Don't asked how and why I only had to put in order 3 nodes.

MiscBits
Posts: 1098
Joined: Wed Jan 27, 2021 12:48 pm

Re: My 1st project: send sensor data to influxdb

Fri Nov 25, 2022 5:36 pm

rpdom wrote:
Fri Nov 25, 2022 6:55 am
...
Why use sudo?
...
Because I copied the command from a script that runs over ssh to a fair number of Pi boards as different users and on a couple of them the user was really limited so I added vcgencmd in the sudoers file ages ago (three OS version from memory) and it's never been changed / revisited :oops:

Return to “Beginners”