Problem in showing results in python

Hi friends,
I have a problem in showing my sensor results after using pythonshell node.
my output result in sys-python is :

29.00

But the output after node-pythonshell is as below :

msg.payload : string[46]
string[46]
55, 255, 0, 0, 50, 57, 46, 48, 48, 0, 217, 16

Whats the problem ,and how can I solve it ?

You have to share the python code you have in the node and an example input message that is sent to the node. Nobody is (yet) able to read what you are thinking

OK
In previous post, I describe the main problem shortly ...
Here is the python code that works correctly in raspbian and show me sensor results :

import time
from time import sleep
from SX127x.LoRa import *
from SX127x.board_config import BOARD
BOARD.setup()
BOARD.reset()
class mylora(LoRa):
    def __init__(self, verbose=False):
        super(mylora, self).__init__(verbose)
        self.set_mode(MODE.SLEEP)
        self.set_dio_mapping([0] * 6)
    def on_rx_done(self):
        BOARD.led_on()
        #print("\nReceived: ")
        self.clear_irq_flags(RxDone=1)
        payload = self.read_payload(nocheck=True)
        #print ("Receive: ")
        mens=bytes(payload).decode("utf-8",'ignore')
        mens=mens[2:-2] #to discard \x00\x00 and \x00 at the end
        print(mens)
        BOARD.led_off()
    def start(self):
        self.reset_ptr_rx()
        self.set_mode(MODE.RXCONT)
        while True:
            sleep(.5)
            rssi_value = self.get_rssi_value()
            status = self.get_modem_status()
            sys.stdout.flush()
lora = mylora(verbose=False)
lora.set_mode(MODE.STDBY)
lora.set_pa_config(pa_select=1)
assert(lora.get_agc_auto_on() == 1)
try:
    print("START")
    lora.start()
except KeyboardInterrupt:
    sys.stdout.flush()
    print("Exit")
    sys.stderr.write("KeyboardInterrupt\n")
finally:
    sys.stdout.flush()
    print("Exit")
    lora.set_mode(MODE.SLEEP)
    BOARD.teardown()

And this is the arduino code that sends the sensor data :

#include <SPI.h> //Import SPI librarey
#include <RH_RF95.h> // RF95 from RadioHead Librarey
#include "DHT.h"
#define RFM95_CS 10 //CS if Lora connected to pin 10
#define RFM95_RST 9 //RST of Lora connected to pin 9
#define RFM95_INT 2 //INT of Lora connected to pin 2
#define DHTPIN 4
#define DHTTYPE DHT11   // DHT 11
#define LED 13
#define RF95_FREQ 434.0
RH_RF95 rf95(RFM95_CS, RFM95_INT);
DHT dht(DHTPIN, DHTTYPE);//make class
void setup()
{
  pinMode(RFM95_RST, OUTPUT);
  digitalWrite(RFM95_RST, HIGH);
  while (!Serial);
  Serial.begin(9600);
  delay(100);
  dht.begin();
  // manual reset
  digitalWrite(RFM95_RST, LOW);
  delay(10);
  digitalWrite(RFM95_RST, HIGH);
  delay(10);
//Initialize LoRa Module
  while (!rf95.init()) {
    Serial.println("LoRa radio init failed");
    while (1);
  }
  //Set the default frequency 434.0MHz
  if (!rf95.setFrequency(RF95_FREQ)) {
    Serial.println("setFrequency failed");
    while (1);
  }
  rf95.setTxPower(18); //Transmission power of the Lora Module
  Serial.println("START");
}
void loop()
{  
  delay(2000);
  float temp = dht.readTemperature();
  char radiopacket[8];
  dtostrf(temp,2,2,radiopacket);
  Serial.print("Temperature : ");
  Serial.println(radiopacket);
  rf95.send((uint8_t *)radiopacket,8);
  delay(5000);
}

I send sensor data from arduino with lora module and receive it in raspberry pi.I can see the results after open it in raspbian OS.So now I want to see the result in debuge node for example. I used pythonshell, but the out put was what I show in the first post !
Maybe it shows the correct result but I should put some nodes to see !
Any idea ?

1 Like

Fine, and now show the code you have in the python node. My experience is that you could get problems with things like while loops etc

But also show an exact example of the message you are feeding into the python node so I can make simulation

It might be that you anyway have to keep the python outside of NR and instead send the result to NR via mqtt. That is anyway very easy to fix, I do it for many similar python services I have in my system

EDIT: If you are comfortable with programming the arduino, it might be even easier for you to send the sensor data to NR directly via mqtt, then you could skip the whole python thing

Node RED flow is very simple :

[{"id":"679b950f.e5adcc","type":"tab","label":"Flow 9","disabled":false,"info":""},{"id":"26ac952b.0e5e2a","type":"pythonshell in","z":"679b950f.e5adcc","name":"Lora","pyfile":"/home/pi/Documents/LORA_CLIENT.py","virtualenv":"","continuous":true,"stdInData":false,"x":170,"y":100,"wires":[["48083e13.3635"]]},{"id":"48083e13.3635","type":"debug","z":"679b950f.e5adcc","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":410,"y":140,"wires":[]}]

Also MQTT is a good idea. I receive some sensor data through mqtt, and if you guide me how to use it, the problem will be solved ...

No problem,,,just let me look at your flow

1 Like

OK, I now understand your setup. Obviously the LORA node should work somehow using the underlying python script but I have no experience in using that node. I can help you adding mqtt support to your python script and you will get the sensor data in NR very easy if you want? If you prefer to add mqtt directly to arduino, I cannot help. You want me to modify your python script with mqtt?

Of course Lora uses the Python script,
I thought its easier to edit the node in node red , but if you can send the data in mqtt ,ok , go on ... :wink:

Just show a print screen how the messages looks like when they are correct when running the python script. EDIT: or is it shown like 29.00?

Only I want such a data in node red ...

Yes, I understand you want is what is printed with "print(mens)" sent to NR?

Some data as you said in mqtt, as string or if posible float variable ...
of course if you Publish the data, I can subscribe it in node red ...
thanks

You can try this script & flow, it should if it is working correctly, give you the data in NR

To note, you have to:

  • install paho mqtt client in python on yor RPI if you don't have it already (https://pypi.org/project/paho-mqtt/)
  • edit the script and flow if your mqtt broker is running somewhere else, I'm assuming 127.0.0.1
  • edilt the mqtt topics in the script and in NR if you want a different naming convention
  • always stop the script (abort) correctly, use the command "abort" and send it to the topic as in the flow

image

[{"id":"65e3a7a6.456798","type":"inject","z":"a4d7c0e0.63723","name":"","topic":"","payload":"abort","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":220,"y":490,"wires":[["f9adfe3f.f2a3f"]]},{"id":"5d1a1708.13c028","type":"mqtt in","z":"a4d7c0e0.63723","name":"","topic":"loramessage","qos":"2","datatype":"auto","broker":"75eba16c.094f9","x":240,"y":370,"wires":[["a314d60f.a3c5c8"]]},{"id":"f9adfe3f.f2a3f","type":"mqtt out","z":"a4d7c0e0.63723","name":"","topic":"cmnds","qos":"","retain":"","broker":"75eba16c.094f9","x":460,"y":490,"wires":[]},{"id":"a314d60f.a3c5c8","type":"debug","z":"a4d7c0e0.63723","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":470,"y":370,"wires":[]},{"id":"4a84e79b.10bbf8","type":"comment","z":"a4d7c0e0.63723","name":"Terminate the script correctly","info":"","x":290,"y":450,"wires":[]},{"id":"75eba16c.094f9","type":"mqtt-broker","z":"","name":"","broker":"127.0.0.1","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

import time
import paho.mqtt.client as mqtt
from time import sleep
from SX127x.LoRa import *
from SX127x.board_config import BOARD
BOARD.setup()
BOARD.reset()
class mylora(LoRa):
    def __init__(self, verbose=False):
        super(mylora, self).__init__(verbose)
        self.set_mode(MODE.SLEEP)
        self.set_dio_mapping([0] * 6)
    def on_rx_done(self):
        BOARD.led_on()
        #print("\nReceived: ")
        self.clear_irq_flags(RxDone=1)
        payload = self.read_payload(nocheck=True)
        
        data = payload
        #data = bytearray( [0x37, 0xFF, 0x0, 0x0, 0x32, 0x39, 0x2E, 0x30, 0x30, 0x0, 0xD9, 0x10] )
        tmp = float(str(data).split('\\x')[3])
        send_mqtt_message('loramessage', str(tmp))

        #print ("Receive: ")
        mens=bytes(payload).decode("utf-8",'ignore')
        mens=mens[2:-2] #to discard \x00\x00 and \x00 at the end
        print(mens)
        BOARD.led_off()
    def start(self):
        self.reset_ptr_rx()
        self.set_mode(MODE.RXCONT)
        while True:
            sleep(.5)
            rssi_value = self.get_rssi_value()
            status = self.get_modem_status()
            sys.stdout.flush()


def send_mqtt_message(topic, msg):
    for i in range(5):
        try:
            result, mid = client.publish(topic, msg, 0)
            #print ('result', result)
            if result == 0:
                break
        except:
            print ("retrying...")


def on_connect(client, obj, flags, rc):
    client.subscribe("cmnds", 0)
    

def on_subscribe(client, userdata, mid, granted_qos):
    print ('Subscribed:', userdata, mid, granted_qos)
    print ('Lora script running...')


def on_message(mosq, obj, msg):
    global abort
    event = msg.payload.decode("utf-8")
    print (event)
    if event == 'abort':
        abort = True


abort = False
client = mqtt.Mosquitto()
client.on_connect = on_connect
client.on_message = on_message
client.on_subscribe = on_subscribe
client.connect("127.0.0.1", 1883, 60)
client.loop_start()

lora = mylora(verbose=False)
lora.set_mode(MODE.STDBY)
lora.set_pa_config(pa_select=1)
assert(lora.get_agc_auto_on() == 1)

try:
    print("START")
    lora.start()
except KeyboardInterrupt:
    sys.stdout.flush()
    print("Exit")
    abort = True
    sys.stderr.write("KeyboardInterrupt\n")
finally:
    sys.stdout.flush()
    print("Exit")
    abort = True
    lora.set_mode(MODE.SLEEP)
    BOARD.teardown()

while not abort:
    time.sleep(1)
    
client.loop_stop()
client.disconnect()
del client
lora.set_mode(MODE.SLEEP)
BOARD.teardown()
exit(0)


1 Like

I have already Mosquito broker run in the same port, installing paho doesnt have any interfering ???

paho is the python mqtt client. it is actually what will communicate with your mosquito broker. So yes no interference as they actually are two different things. One is a client the other a broker.
Think of paho for python as the same thing that the mqtt node is in nodered.
best regards Johannes

1 Like

Have you tried running your script in a nodered node daemon instead of pythonshell? It works quite good for me to get values from long running python scripts that print their output while running. Would be an alternative to using a whole other abstraction layer like mqtt.
Best regards Johannes

1 Like

As @JGKK says, you need the mosquitto clients and the paho package provides that. Begin running the script in a terminal in the RPI, that should be the easiest start. If you would like to try @JGKK suggestion, you should run the original python script, not my modified. Anyway, using mqtt also gives you the other type of benefits specific to mqtt

1 Like

Daemon node's output is exactly like pythonshell output,
I think there is some parser error.
python2

have you tried doing a string conversion in python before printing? str(mens). Sorry I cant be really helpful here as i cant simulate your python script as I dont have your hardware.

Yes of course
I explained later the codes ,and that I recieve python results as I want outside node red.
But when I want to get the result in node red ,I can't ...insted of a simple output,node red give me a Irrelevant string (maybe of course) !!!

1 Like