John Exterkamp
John Exterkamp

Reputation: 23

Microbit not receiving serial data sent form computer

I am trying to send data from my MacBook via python to the USB port that my microbit is connected to. my python program transfers the data then by looking on the back of my microbit I see that right next to the USB port a little light is blinking when information is sent so the micro bit is receiving information but the program that I wrote for the microbit won't display the information that has been sent. I have followed a tutorial on how to do this too. something is going wrong I need help!

import serial
import Stock_Web as SW
import time

ser = serial.Serial()
ser.baudrate = 115200
ser.port = "/dev/cu.usbmodem14102"
ser.open()

while True:
    i =+ 1
    i = str(i)
    print('this is time ' + i)
    DowPerBytes = str(SW.DowPercent())
    DowPerBytes = DowPerBytes.encode()
    ser.write(DowPerBytes)
    time.sleep(.5)
    # data = microbitdata[2:]
    # data = data.replace('')

this is my custom module SW:

import requests
from bs4 import BeautifulSoup as soup

def DowPercent():
    url = 'https://money.cnn.com/data/markets/'
    result = requests.get(url)
    src = result.content
    Soup = soup(src, 'html.parser')

    stock_per_raw = Soup.find('span', class_="ticker-name-change", attrs={"stream": "changePct_599362", "data-ticker-name": "Dow"})

    return soup.get_text(stock_per_raw)

and here is the micro bit code:

code

Upvotes: 1

Views: 924

Answers (1)

Oppy
Oppy

Reputation: 2887

Please find a working solution below.

This is a screenshot of the blocks that I used:

enter image description here

I got the following Python v3.7 code to work on Debian Linux using a microbit running firmware v 253. As the port on which the microbit mounts can change each time it is connected, I wrote the find_comport method to identify the port using the VID and PID of the micro:bit. Stock_Web.py was not changed from your example code. I can see e.g. 0.1% scroll across the LEDs.

import logging
import serial
import serial.tools.list_ports as list_ports
import Stock_Web as SW
import time

logging.basicConfig(level=logging.DEBUG, format='%(message)s')

PID_MICROBIT = 516
VID_MICROBIT = 3368
TIMEOUT = 0.1


def find_comport(pid, vid, baud):
    ''' return a serial port '''
    ser_port = serial.Serial(timeout=TIMEOUT)
    ser_port.baudrate = baud
    ports = list(list_ports.comports())
    print('scanning ports')
    for p in ports:
        logging.debug('port: {}'.format(p))
        try:
            logging.debug('pid: {} vid: {}'.format(p.pid, p.vid))
        except AttributeError:
            continue
        if (p.pid == pid) and (p.vid == vid):
            logging.info('found target device pid: {} vid: {} port: {}'.format(
                p.pid, p.vid, p.device))
            ser_port.port = str(p.device)
            return ser_port
    return None


i=0
logging.debug('looking for microbit')
ser_micro = find_comport(PID_MICROBIT, VID_MICROBIT, 115200)
if not ser_micro:
    logging.info('microbit not found')
    
logging.debug('opening and monitoring microbit port')
ser_micro.open()

while True:
    i += 1
    print
    logging.debug('this is time {}'.format(i))
    DowPerBytes = str(SW.DowPercent())
    logging.debug('{}'.format(DowPerBytes))
    DowPerBytes = DowPerBytes.encode()
    logging.debug('{}'.format(DowPerBytes))
    ser_micro.write(DowPerBytes)
    time.sleep(5)

screen output:

looking for microbit
scanning ports
port: /dev/ttyACM3 - "BBC micro:bit CMSIS-DAP" - mbed Serial Port
pid: 516 vid: 3368
found target device pid: 516 vid: 3368 port: /dev/ttyACM3
opening and monitoring microbit port
this is time 1
Starting new HTTPS connection (1): money.cnn.com:443
https://money.cnn.com:443 "GET /data/markets/ HTTP/1.1" 200 29158
0.10%
b'0.10%'
this is time 2
Starting new HTTPS connection (1): money.cnn.com:443
https://money.cnn.com:443 "GET /data/markets/ HTTP/1.1" 200 29158
0.10%
b'0.10%'
this is time 3

Upvotes: 1

Related Questions