Read BH1750 Sensor

#1

Hi

You know a way how to read the sensor data from the BH1750 sensor?

0 Likes

Using function() within a function node
#2

One simple way is to use an ESP8266:

https://www.letscontrolit.com/wiki/index.php/BH1750

2 Likes

#3

I have a working BH1750 on a raspberry pi using the following:
Node node-red-contrib-i2c

Screenshot_2018-09-23_17-33-24
35 is the address of the sensor, 16 is the command to get the lux value.

Followed by a change node:
Screenshot_2018-09-23_17-34-40

The JSONata expression is:
Screenshot_2018-09-23_17-35-39

Which is converting the two byte value returned by the i2c node to a decimal value and dividing the result by 1.2 as described in the datasheet (the sensor returns values that are typically 20% too high), and rounding to 2 decimal places.

msg.lux contains the result.

2 Likes

#4

Thanks for sharing my output is sadly : [9,213]
i did exactly like you said. what did i do wrong ?

image
JSONata:
$round((payload."1" + (265 * payload."0"))/1.2,2)

my output:
image

0 Likes

#5

you are setting msg.lux to be the value, but your debug is set to display msg.payload

Double click on the debug to change it to display the correct part of the message object or the entire message

1 Like

#6

oh lol i am just getting started with node-red and overlooked that part :sweat_smile:

0 Likes

#7

Worth reading
https://nodered.org/docs/user-guide/messages for some tips on how to use debug nodes to help navigate the message object

0 Likes

#8

Hello,
I hope anybody can help me.
I use node-Red over ioBroker and I want to use the BH1750 sensor connected on my raspberry. I've followed the instructions from ghayne but unfortunately I don't get any message from the sensor. Not from "I2C in" and not from "I2C Scan". The sensor works fine with a phyton script.
I connected the sensor like the description on the following site www.raspberrypi-spy.co.uk
Here is also a picture from the flow. Any tips?

0 Likes

#9

You need an inject mode before the i2c node!

[{"id":"6e0dfdec.2f6c74","type":"inject","z":"9dcf23c2.6c9ca","name":"","topic":"","payload":"","payloadType":"date","repeat":"60","crontab":"","once":false,"x":110,"y":60,"wires":[["f9655f1b.ef3c1"]]},{"id":"f9655f1b.ef3c1","type":"i2c in","z":"9dcf23c2.6c9ca","name":"Read BH1750","address":"35","command":"16","count":"2","x":300,"y":20,"wires":[["69f7be89.9e122"]]},{"id":"69f7be89.9e122","type":"change","z":"9dcf23c2.6c9ca","name":"Lux","rules":[{"t":"set","p":"lux","pt":"msg","to":"$round((payload.\"1\" + (256 * payload.\"0\"))/1.2,2)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":450,"y":60,"wires":[["5714a6d3.d06238","a7aae33a.77a05"]]}]
1 Like

#10

Hello ghayne, thank you so much, now all is working fine.Have a nice day!

1 Like