How to get value from rpi into Igntion

I have an rpi and I’ve written a class and python code that will calculate instantaneous flow rate and log flow volumes to a sqlite database, upon getting a rising edge trigger from two flowmeters, where a pulse = 1L or 10L (i have two different flowmeters). The class subscribes to a pin change interrupt and waits for the rising edge to do its thing.

I’m trying to get the instantaneous flow rate value into igntion maker edition running on the Pi, but I’m not sure the best approach. I’m more or less a noob with pi’s/Linux, node red, web tech in general.

Few questions:

  1. how should I run my flowmeter script? At the moment, i manually execute the script in the debian terminal… All my attempts to run it automatically on boot have failed… Linux is friggin hard :roll_eyes:
    I assume it’s not possible to run in igntion as the rpi libraries are most likely C-based? Also, I want the code running even when igntion isn’t

  2. how do I get the value into igntion? Via node-red? If so, how do I write the value from my terminal running script into node-red?

1 Like

To run your script on boot up you can put your command in a shell script

#! /bin/bash
sleep 10 (optional if you want start delay)
python /path/to/my/script.py

you can save this file something like flowrate.sh

Then you need to give this file executable permissions

chmod +x flowrate.sh

To run this on boot up add flowrate.sh to /etc/xdg/lxsession/LXDE-pi/autostart
add this line @/path/to/flowrate.sh

4 Likes

You said that all these values are being written to a SQLite database, correct? You should be able to make a SQLite database connection in the Ignition gateway. Then you can query through that connection.

1 Like

I store each pulse and the volume of the pulse in the database, yes, but to extract the instantaneous flowrate from that is a bit more involved which is what my class is doing. I also want to pull in the temperature and load of the CPU, RAM, etc. so I’d rather not have to store into the database to get into Ignition. Is it possible using node-red?

I haven’t used node red, but Im pretty sure that is how @nader.chinichian did his home automation project

Python support opc ua lib so you can push value to ignition but if I want to do that I write my code in javascript in nodered and then send the value either by opc ua or ignition-nodered module.

1 Like

I have a python mqtt project I have been working on. It is pretty easy to extend, might fit your use case perfectly. Just have to finish it…

2 Likes

Sounds good! I’m not in any rush, just another home project to do on my endless list :laughing:

I use node red and the sparkplug library. I love that the values get quality code and source timestamp. If ur code hangs or ur power goes off, you see it directly. Also you can buffer data with store and forward via an additional edge node if needed.

How did you solve it? Curious :nerd_face:

I haven’t solved it yet, too busy at work at the moment and haven’t had a chance. I haven’t used node red much at all, but how would I send the instantaneous flow calculated by my python code running in the background (not in ignition) to node red?

Maybe better to use the python code instead of node red then. There are python code and examples in the sparkplug git repo.

I have a fan with a web interface. Someone made a python script to control the fan. Then I just merged that code with the sparkplug code. One has to spent a little time to understand the sparkplug examples of course.

1 Like

I had a look again at this last night and found the paho-mqtt library. I got my python script publishing topics / data to my Maker Ignition MQTT Distributor, however the MQTT Engine isn’t picking it up yet… I connected to the Dist. with another MQTT client and could see the topics. Not sure what’s up with the Ignition Engine :thinking: This is all I see:
image

First time using MQTT

I followed Steve (or is it Alan? :grin:) in order to code the python, if it’s of anyone’s interest:

If you want to use normal mqtt like that, you have to subscribe to the same topics with the mqtt engine. You do this on the config section under mqtt engine.

Basically you give ur subscription group a name (will give you a tag folder in mqtt engine tag provider) then just add wildcard # or #/ to that group then ALL topics will populate once they are published.

When you get that to work, you should try the sparkplug example. If ur running python 3.5 I can send you an 3.5 modified sparkplug library.

1 Like

Cool, i’ll give that a go over the weekend!

Pretty sure i’m using python 3.7. What does sparkplug add to the mix?

Cheers

You don’t have to worry about a lot of stuff. Like

  • birth/death messages.
  • If you reboot ignition, ignition will ask your sparkplug node to rebirth.
  • automatic population of sparkplug nodes, no need to subscribe.
  • quality codes
  • timestamps
  • online status for both sparkplug nodes and devices.
  • keep alive setting (I’m actually a bit struggling with the python code for this)
  • data compression
  • ++

For all my home devices I put timeouts on them, killing the respective node or device. Giving me disconnection alarms in ignition.

It might be overkill for home automation. But you can take it to work later;)

1 Like

Sounds much better! I’ll look into it, thanks for the tip

Try sparkplug in nodered first, Its easier to get a quick overview this way. Here is an example can get you going:

2 Likes

I’m just getting back to this. When running a script like this on startup:

  1. can I run it in a terminal? I print stuff to the terminal so that I know what’s going on. Maybe not the best practice, but it’s all I know
  2. how do I know if it’s running? My python script runs indefinitely

Make it a service (Google “systemd.service” and “systemd.unit”).

Script output can be sent to your logs, or you can explicitly call logging functions.

3 Likes

I'm trying to use sparkplug, looking at the example on git here and here, but they seem to have errors in them (all the print statements use print "hello" instead of print("hello") - still not sure why Ignition's Jython uses the former and any Python I write outside of Ignition needs brackets) and they always complain that the library sparkplug_b is missing. I don't know how to install it though. There's a 'sparkplug' library included in pip, but it doesn't seem to have the B spec. I have no idea how to install B...

This is what the official Cirrus Link guide says:
image

Clearly they're not the only dependencies though if these are the imports:

import sys
import paho.mqtt.client as mqtt
import pibrella
import sparkplug_b as sparkplug
import time
import random
import subprocess

The second link guide also has no mention of which version of Python they wrote the example in :confused: