how would i replace one string values with another without breaking it

83 Views Asked by At

I've been trying to learn the microbit and Raspberry Pi but I can't figure out how to increase the amount of things that are measured by it,

import time
import serial
from datetime import datetime
from csv import writer
import http.server

ser = serial.Serial(
    port='/dev/ttyACM0',
    baudrate = 115200,
    parity=serial.PARITY_NONE,
    stopbits=serial.STOPBITS_ONE,
    bytesize=serial.EIGHTBITS,
    timeout=1
)

while True:
    x=ser.readline()
    if x:
        dt = datetime.now()
        datestamp = str(dt)[:16]
        temp, light = x.decode().split(':')
        newData = [datestamp,temp,light]
        print(newData)
        with open('test.csv', 'a', newline='') as f_object:
            writer_object = writer(f_object)
            writer_object.writerow(newData)

I tried replacing

temp, light = x.decode().split(':')          
newData = [datestamp,temp,light]

with longer and other values, however nothing works, I was expecting more outputs to be produced that just weren't however no error was given out.

Link to original code I am basing my work on: https://github.com/blogmywiki/microbit-pi-data

This is how I have modified that example code on micro:bit to send more data: microbit code example

no errors outputted.

when i next have access to pi and whatnot ill add the code from the pi

1

There are 1 best solutions below

0
SiHa On

In the example you linked, the two values are being joined with a : and written in a single operation. The code then reads this line and splits it by the : to get the two values.

In your microbit code, you send each value separately, with no :, so not surprisingly, the line in the Python code which splits the received data, fails, because there is no delimiter.

So, you either need to change the microbit code to join the values with the colon, again, or change the Python code to parse the data in the new format. Which you choose to do is up to you.