2020-02-02, 01:49 PM
(This post was last modified: 2020-02-02, 02:40 PM by jamos.tan@gmail.com.)
Found this example in openplotter example tools:
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:
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.
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.