Python Script into .JSON file for node red?

I can start the Skricpt with python and with python3, i get the same outprint


The reports are showing because i interrupted the program with ^C otherwise the program will keep running.

How do I find this out ? :smiley:

I set the exec node like so.

But still no reports in the debug window

You don't seem to have a space between python3 and /home/pi....

If I look at the result you show it seems the script is not connecting to your MQTT broker

I would expect the first line on top like this

image

Thanks ! I changed that.
But still no data in the debug window..

Do you have 127.0.0.1 for the broker? Is it running in the same RPi?

Yes it is running on the same RP.

where do i see that ?

Check setting for MQTT nodes and check the script (actually, it should be ok since the nodes are all indicating "Verbunden"

I will make a short test script just to verify the MQTT connection

If you see the text "Subscribed: None 1 (0,)" in the command prompt, it is working (like it does here for me)

Yes the adress here ist like you said.
Screenshot 2021-05-12 172455

Sorry but I still don't get it.:smiley: what do i have to type in the command box to check if this report is showing ?

It shall be shown when the script starts if it is able to connect to your MQTT broker. But I think it does not connect correctly or not at all

But try to run this script from the command prompt. It is just connecting to the broker and sends a simple message to NR (you can then kill it with ctrl+c)

mqtt_test.txt (727 Bytes)

For me it works, I see a message in debug with the test script

image

So i copied the script from mqtt_test.txt and paste it into a new data in /home/pi/mqtt_test.py and then i tried to run it but i get this report.

Screenshot 2021-05-12 174244

The script looks like this.

Added: with "sudo python /home/pi/mqtt_test.py" i get the same report

Added: Do i have to change the exec node also to mqtt_test.py ?

No, this is a separate test

When you do the copy & pasting you seem to lose the important spaces that is required as indents in python. Instead of doing that, download my script and rename it to .py

Oh yea sorry i tried so many things today, that I don't know a single thing about, that makes me confused :smiley:

i started it and this is what i'm getting

I also get the report in the debug node, so the test is working!

But still, when i start the script of the flow you send, only the script in the console starts and nothing appears in the debug window....

That is good,,,let me think a while about why the other script is not working, it does basically the same but there must be something I have missed

Could you try to add a delay, time.sleep, in the script code like below (I suspect I'm calling the usb port too fast)

    data = array.array('B',(0,)*4)
    while data[0] != 3 and th_abort == False:
        try:
            data = device.read(endpoint.bEndpointAddress, endpoint.wMaxPacketSize)
            print(data)
            result, mid = client.publish("sds011/data", str(data), 0)
            time.sleep(1)
        except usb.core.USBError as e:
            if e.args == ('Operation timed out',):
                print("timeout)")
                continue

I added it into the script:

But still nothing in the debug window..

maybe my script ins't correct ?

EDIT:

I forget to tell you that i get this report.

Screenshot 2021-05-12 191543

If that is the script you are runnning, it is not the script I did share

Let me think until tomorrow

Let's see if this works better. Try this new script. I have re-arranged it a bit, now running the MQTT and SDS011 data readings in separate so called threads. Download it and run it from the console and check what is written to the console and the debug in NR

In this first test I am interested to know

  1. does it connect and subscribe to the MQTT broker correctly
  2. does it start reading data from the SDS011 device correctly

The script is not yet sending SDS011 data via MQTT, it should just print it once per second to the console

If this works we can do the next steps and process the data you would like to send to NR via MQTT and try that. I guess you are interested in data[2] to data [5], the PM2.5 and PM10 values

sds011.txt (3.0 KB)

Wow ! Thank you very much, I appreciate the work you've done ! :slight_smile:

I copied the new script and ran it, i get the following.

Screenshot 2021-05-13 101000

Yes thats correct! I have already prepared to continue calculating with the values

Did you break the script or did it crash??

EDIT: You shall delete the following two lines in the script, they are wrong, should not be there

print ("sds011 script stopped...")
exit()