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.

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python script to signalk
#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


Messages In This Thread
python script to signalk - by jamos.tan@gmail.com - 2020-01-31, 06:47 PM
RE: python script to signalk - by PaddyB - 2020-02-01, 11:33 AM
RE: python script to signalk - by jamos.tan@gmail.com - 2020-02-02, 01:49 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)