This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm whether you accept or reject these cookies being set.

A cookie will be stored in your browser regardless of choice to prevent you being asked this question again. You will be able to change your cookie settings at any time using the link in the footer.

  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python script to signalk
#1
Hi Guys,

I have a python script which reads voltage values through a serial connection.
I'd like to have these values available as a signalk path.

Is such a thing possible? If so, I can provide the details of the script.

Kind regards,

Jamos
  Reply
#2
(01-31-2020, 01:47 PM)jamos.tan@gmail.com Wrote: Hi Guys,

I have a python script which reads voltage values through a serial connection.
I'd like to have these values available as a signalk path.

Is such a thing possible? If so, I can provide the details of the script.

Kind regards,

Jamos

This works on an ESP32 sending signalk over wifi, you might be able to adapt so it sends over serial>

   
Quote:def insertIntoSigKdata(self, path, value):        
        _sigKdata = {
        "updates": [
                {"values":[]
                }]}
        _sigKdata["updates"][0]["values"].append( {"path":path,
                    "value": value
                    })
        self.sendToUDP(ujson.dumps(_sigKdata))
  Reply
#3
Thanks!

I must admit, I do need some assistance as in how to implement it, do you have some steps to this?
Also, here is some more information about the contents of the script that I use:


Code:
import serial
from time import sleep
import datetime
import os
import time
from time import sleep
wide_range_volt_min = 4.8
battery_volt_min = 0.5
mUSB_volt_min = 4.1
breakS = 0.1
breakL = 0.5
serial_port = serial.Serial()
serial_port.baudrate = 38400
serial_port.port = '/dev/serial0'
serial_port.timeout = 1
serial_port.bytesize = 8
serial_port.stopbits = 1
serial_port.parity = serial.PARITY_NONE
if serial_port.isOpen(): serial_port.close()
serial_port.open()
#######################################################################################################################
def enabled_disabled_converter(argument):
    switcher = {
        0: 'Disabled',
        1: 'Enabled',
    }
    return switcher.get(argument, 'nothing')
def weekday_converter(argument):
    switcher = {
        1: 'Monday',
        2: 'Tuesday',
        3: 'Wednesday',
        4: 'Thursday',
        5: 'Friday',
        6: 'Saturday',
        7: 'Sunday',
    }
    return switcher.get(argument, 'nothing')
def strompi_mode_converter(argument):
    switcher = {
        1: 'mUSB -> Wide',
        2: 'Wide -> mUSB',
        3: 'mUSB -> Battery',
        4: 'Wide -> Battery',
        5: "mUSB -> Wide -> Battery",
        6: "Wide -> mUSB -> Battery",
    }
    return switcher.get(argument, 'nothing')
def alarm_mode_converter(argument):
    switcher = {
        1: 'Time-Alarm',
        2: 'Date-Alarm',
        3: 'Weekday-Alarm',
    }
    return switcher.get(argument, 'nothing')
def batterylevel_shutdown_converter(argument):
    switcher = {
        0: 'Disabled',
        1: '10%',
        2: '25%',
        3: '50%',
    }
    return switcher.get(argument, 'nothing')
def output_status_converter(argument):
    switcher = {
        0: 'Power-Off', #only for Debugging-Purposes
        1: 'mUSB',
        2: 'Wide',
        3: 'Battery',
    }
    return switcher.get(argument, 'nothing')

def batterylevel_converter(batterylevel,charging):
    if charging:
        switcher = {
            1: ' [10%] [charging]',
            2: ' [25%] [charging]',
            3: ' [50%] [charging]',
            4: ' [100%] [charging]',
        }
        return switcher.get(batterylevel, 'nothing')
    else:
        switcher = {
            1: ' [10%]',
            2: ' [25%]',
            3: ' [50%]',
            4: ' [100%]',
        }
        return switcher.get(batterylevel, 'nothing')
#######################################################################################################################
serial_port.write(str.encode('quit'))
sleep(breakS)
serial_port.write(str.encode('\x0D'))
sleep(breakL)
serial_port.write(str.encode('status-rpi'))
sleep(1)
serial_port.write(str.encode('\x0D'))
sp3_time = serial_port.readline(9999);
sp3_date = serial_port.readline(9999);
sp3_weekday = serial_port.readline(9999);
sp3_modus = serial_port.readline(9999);
sp3_alarm_enable = serial_port.readline(9999);
sp3_alarm_mode = serial_port.readline(9999);
sp3_alarm_hour = serial_port.readline(9999);
sp3_alarm_min = serial_port.readline(9999);
sp3_alarm_day = serial_port.readline(9999);
sp3_alarm_month = serial_port.readline(9999);
sp3_alarm_weekday = serial_port.readline(9999);
sp3_alarmPoweroff = serial_port.readline(9999);
sp3_alarm_hour_off = serial_port.readline(9999);
sp3_alarm_min_off = serial_port.readline(9999);
sp3_shutdown_enable = serial_port.readline(9999);
sp3_shutdown_time = serial_port.readline(9999);
sp3_warning_enable = serial_port.readline(9999);
sp3_serialLessMode = serial_port.readline(9999);
sp3_intervalAlarm = serial_port.readline(9999);
sp3_intervalAlarmOnTime = serial_port.readline(9999);
sp3_intervalAlarmOffTime = serial_port.readline(9999);
sp3_batLevel_shutdown = serial_port.readline(9999);
sp3_batLevel = serial_port.readline(9999);
sp3_charging = serial_port.readline(9999);
sp3_powerOnButton_enable = serial_port.readline(9999);
sp3_powerOnButton_time = serial_port.readline(9999);
sp3_powersave_enable = serial_port.readline(9999);
sp3_poweroffMode = serial_port.readline(9999);
sp3_poweroff_time_enable = serial_port.readline(9999);
sp3_poweroff_time = serial_port.readline(9999);
sp3_wakeupweekend_enable = serial_port.readline(9999);
sp3_ADC_Wide = float(serial_port.readline(9999))/1000;
sp3_ADC_BAT = float(serial_port.readline(9999))/1000;
sp3_ADC_USB = float(serial_port.readline(9999))/1000;
sp3_ADC_OUTPUT = float(serial_port.readline(9999))/1000;
sp3_output_status = serial_port.readline(9999);
sp3_powerfailure_counter = serial_port.readline(9999);
sp3_firmwareVersion = serial_port.readline(9999);
date = int(sp3_date)
strompi_year = int(sp3_date) // 10000
strompi_month = int(sp3_date) % 10000 // 100
strompi_day = int(sp3_date) % 100
strompi_hour = int(sp3_time) // 10000
strompi_min = int(sp3_time) % 10000 // 100
strompi_sec = int(sp3_time) % 100
try:
    if sp3_ADC_Wide > wide_range_volt_min:
        wide_range_volt = str(sp3_ADC_Wide) + 'V'
    else:
        wide_range_volt = ' not connected'
    if sp3_ADC_BAT > battery_volt_min:
        battery_volt = str(sp3_ADC_BAT) + 'V' + batterylevel_converter(int(sp3_batLevel),int(sp3_charging))
    else:
        battery_volt = ' not connected'
    if sp3_ADC_USB > mUSB_volt_min:
        microUSB_volt = str(sp3_ADC_USB) + 'V'
    else:
        microUSB_volt = ' not connected'
    output_volt = str(sp3_ADC_OUTPUT) + 'V'
    print(' ')
    print('---------------------------------')
    print('StromPi-Status:')
    print('---------------------------------')
    print('Time: ' + str(strompi_hour).zfill(2) + ':' + str(strompi_min).zfill(2) + ':' + str(strompi_sec).zfill(2))
    print('Date: ' + weekday_converter(int(sp3_weekday)) + ' ' + str(strompi_day).zfill(2) + '.' + str(strompi_month).zfill(2) + '.' + str(strompi_year).zfill(2))
    print(' ')
    print('StromPi-Output: ' + output_status_converter((int(sp3_output_status))))
    print(' ')
    print('StromPi-Mode: ' + strompi_mode_converter((int(sp3_modus))))
    print(' ')
    print('Raspberry Pi Shutdown: ' + enabled_disabled_converter(int(sp3_shutdown_enable)))
    print(' Shutdown-Timer: ' + str(sp3_shutdown_time, 'utf-8').rstrip('\n').zfill(2) + ' seconds')
    print(' ')
    print('Powerfail Warning: ' + enabled_disabled_converter(int(sp3_warning_enable)))
    print(' ')
    print('Serial-Less Mode: ' + enabled_disabled_converter(int(sp3_serialLessMode)))
    print(' ')
    print('Power Save Mode: ' + enabled_disabled_converter(int(sp3_powersave_enable)))
    print(' ')
    print('PowerOn-Button: ' + enabled_disabled_converter(int(sp3_powerOnButton_enable)))
    print(' ')
    print(' PowerOn-Button-Timer: ' + str(sp3_powerOnButton_time, 'utf-8').rstrip('\n').zfill(2) + ' seconds')
    print(' ')
    print('Battery-Level Shutdown: ' + batterylevel_shutdown_converter(int(sp3_batLevel_shutdown)))
    print(' ')
    print('Powerfail-Counter: ' + str(sp3_powerfailure_counter, 'utf-8').rstrip('\n'))
    print(' ')
    print('PowerOff Mode: ' + enabled_disabled_converter(int(sp3_poweroffMode)))
    print('---------------------------------')
    print('Alarm-Configuration:')
    print('---------------------------------')
    print('WakeUp-Alarm: ' + enabled_disabled_converter(int(sp3_alarm_enable)))
    if int(sp3_poweroff_time_enable) == 1:
        print (' Alarm-Mode: Minute WakeUp-Alarm')
    elif int(sp3_alarm_mode) > 0 and int(sp3_alarm_mode) < 4:
        print(' Alarm-Mode: ' + alarm_mode_converter(int(sp3_alarm_mode)))
    print(' Alarm-Time: ' + str(sp3_alarm_hour, 'utf-8').rstrip('\n').zfill(2) + ':' + str(sp3_alarm_min, 'utf-8').rstrip('\n').zfill(2))
    print(' Alarm-Date: ' + str(sp3_alarm_day, 'utf-8').rstrip('\n').zfill(2) + '.' + str(sp3_alarm_month, 'utf-8').rstrip('\n').zfill(2))
    print(' WakeUp-Alarm: ' + weekday_converter(int(sp3_alarm_weekday)))
    print(' Weekend Wakeup: ' + enabled_disabled_converter(int(sp3_wakeupweekend_enable)))
    print(' Minute Wakeup Timer: ' + str(sp3_poweroff_time, 'utf-8').rstrip('\n').zfill(2) + ' minutes ')
    print(' ')
    print('PowerOff-Alarm: ' + enabled_disabled_converter(int(sp3_alarmPoweroff)))
    print(' PowerOff-Alarm-Time: ' + str(sp3_alarm_hour_off, 'utf-8').rstrip('\n').zfill(2) + ':' + str(sp3_alarm_min_off, 'utf-8').rstrip('\n').zfill(2))
    print(' ')
    print('Interval-Alarm: ' + enabled_disabled_converter(int(sp3_intervalAlarm)))
    print(' Interval-On-Time: ' + str(sp3_intervalAlarmOnTime, 'utf-8').rstrip('\n').zfill(2) + ' minutes')
    print(' Interval-Off-Time: ' + str(sp3_intervalAlarmOffTime, 'utf-8').rstrip('\n').zfill(2) + ' minutes')
    print(' ')
    print('---------------------------------')
    print('Voltage-Levels:')
    print('---------------------------------')
    print('Wide-Range-Inputvoltage: ' + wide_range_volt)
    print('LifePo4-Batteryvoltage: ' + battery_volt)
    print('microUSB-Inputvoltage: ' + microUSB_volt)
    print('Output-Voltage: ' + output_volt)
    print(' ')
except KeyboardInterrupt:
    print('interrupted!')
serial_port.close()


The value that I need is this one: sp3_ADC_OUTPUT

Hope you can help!
  Reply
#4
Found this example in openplotter example tools:


Code:
#!/usr/bin/python
'''
//Arduino code
int i=0;
// the setup routine runs once when you press reset:
void setup() {
 Serial.begin(9600);
}

void loop() {
 if (i>100) i=0;
 i++;
 Serial.print("ArduinoValue:");
 Serial.println(i);
 delay(300);        // delay in between reads for stability
}
'''

import signal, sys, time, socket, datetime, subprocess, math, serial

# Control-C signal handler to suppress exceptions if user presses Control C
# This is totally optional.
def signal_handler(sig, frame):
    print('You pressed Ctrl+C!!!!')
    sys.exit(0)
    
# init
signal.signal(signal.SIGINT, signal_handler)
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
ser = serial.Serial('/dev/ttyOP_LEONARDO', 9600, timeout=1)
ser.flush()

Value = 0            
# forever loop until user presses Control-C
while 1:    
    serLine = ser.readline()
    if 'ArduinoValue:' in serLine:
        Value = serLine[13:-1]
        #print Value
        
    SignalK = '{"updates": [{"$source":"OPserial.BAT.V1","values":[{"path": "electrical.batteries.main.voltage","value":'+str(Value)+'}]}]}\n'
    #print SignalK
    sock.sendto(SignalK, ('localhost', 55559))
    time.sleep(0.300)





This code seems to include a serial connection with an arduino, and maps it to the signalk path that I want.
I have changed the code to include the details of my original script:

Code:
#!/usr/bin/python
'''
//Arduino code
int i=0;
// the setup routine runs once when you press reset:
void setup() {
 Serial.begin(38400);
}

void loop() {
 if (i>100) i=0;
 i++;
 Serial.print("sp3_ADC_OUTPUT");
 Serial.println(i);
 delay(300);        // delay in between reads for stability
}
'''

import signal, sys, time, socket, datetime, subprocess, math, serial

# Control-C signal handler to suppress exceptions if user presses Control C
# This is totally optional.
def signal_handler(sig, frame):
    print('You pressed Ctrl+C!!!!')
    sys.exit(0)
    
# init
signal.signal(signal.SIGINT, signal_handler)
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
ser = serial.Serial('/dev/serial0', 38400, timeout=1)
ser.flush()

Value = 0            
# forever loop until user presses Control-C
while 1:    
    serLine = ser.readline()
    if 'sp3_ADC_OUTPUT' in serLine:
        Value = serLine[13:-1]
        #print Value
        
    SignalK = '{"updates": [{"$source":"OPserial.BAT.V1","values":[{"path": "electrical.batteries.main.voltage","value":'+str(Value)+'}]}]}\n'
    #print SignalK
    sock.sendto(SignalK, ('localhost', 55559))
    time.sleep(0.300)


The trick is, I don't use an arduino, so I need to play with the baudrate I think.
I'll test it and report back.

Ok, started the script from the tools tab in openplotter.
SignalK sentence is now added, but reads as 0V.
Not sure what goes wrong here.
  Reply
#5
After trying for the weekend, I am afraid to say I am stuck. I am at the point where I get the signalk sentence into signalk, but with no value.
I am not sure if this is because the serial connection wont open (unlikely, because the connection method works in my original script) or if there is something wrong with the second example I found.

What I suspect is:

- Serial connection opens OK
- Signalk senstence is added OK
- value cannot be read in script

I was hoping someone can take a look at the two scripts in my previous example and help me find out.

Regards,

Jamos
  Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)