Search code examples
pythonserial-portarduinopyserialarchlinux

How to open serial port programatically?


I have an Arduino connected to Raspberry Pi running Arch.

The arduino has not much to do, but to constantly print some sensor data:

#include<Arduino.h>

void setup() {
    Serial.begin(9600);
}

float temp = .0;
int lumen = 0;

void loop() {
    if(Serial.available()) {        
        temp = analogRead(4) * .4882812 - 273.15;
        lumen = analogRead(0);
        Serial.print(temp);
        Serial.print("|");
        Serial.print(lumen);
        Serial.println();
    }
    delay(10);
}

On the Raspberry, following script will run:

# run.py
#!/usr/bin/env python2
from arduino import Arduino
from poster import Poster
import time
import subprocess
import json

if __name__ == '__main__':
    with open("config.json") as config_fh:
        config = json.load(config_fh)
        print(config)

    base_url = config["URL"]
    security_token = config["KEY"]
    port = config["TTY"]

    arduino = Arduino(port=port)
    arduino.start()
    time.sleep(1)

    while True:
        is_door_open = arduino.is_door_open()
        temperature = arduino.get_temperature()

        print arduino.is_door_open()
        print arduino.get_temperature()

#       nmap = subprocess.Popen("./networkClientsInNetwork.sh", stdout = subprocess.PIPE, stderr = subprocess.PIPE)
#       network_clients_count = int(nmap.stdout.readlines()[0])
#       print network_clients_count

#       poster = Poster()
#       poster.post_door_state(base_url, arduino.is_door_open(), security_token)
#       poster.post_temperature(base_url, str(arduino.get_temperature()), security_token)
#       poster.post_clients(base_url, str(network_clients_count), security_token)

        time.sleep(3)


from threading import Thread
import serial
import random
import time

This is the thread which constantly should get the values from the serial port:

class Arduino(Thread):
    def __init__(self, port):
        Thread.__init__(self, target=self.recieve)
        self.daemon = True
        self.last_recieved = None

        self.serial = self.configure_port(port)
        self.serial.open()

    def configure_port(self, port_id):
        ser = serial.Serial(port=port_id, timeout=1)
        ser.rtscts = True
        ser.dsrdtr = True

        return ser

    def recieve(self):
        while True:
            self.serial.flushInput()
            self.serial.flushOutput()
            if self.serial.isOpen():
                self.last_recieved = self.serial.readline().replace("\r\n", "").split("|")

    def getLastRecieved(self):
        return self.last_recieved

    def is_door_open(self):
        try:
            lumen = int(self.getLastRecieved()[1])
            return_val = lumen > 150
        except Exception:
            return_val = None
            pass

        return return_val

    def get_temperature(self):
        try:
            temperature = int(float(self.getLastRecieved()[0]))
            temperature_offset = -5
            return_val = int(temperature) + temperature_offset
        except Exception:
            return_val = None
            pass

        return return_val

Now, when I start run.py for the first time, it never will get the data from the serial port. They always will be None.

When I run

ino serial (which starts picocom)

and hit Enter, the Arduino sends all its data in the stream. If I exit picocom via Q and then start run.py, the python script can read all the data from the serial port.

So I am wondering: What am I missing in the initalization? How can I automate the serial port initalization with picocom via python?


Solution

  • I have worked around the issue by begging the question. I changed my Arduino loop method to:

    void loop() {
        temp = analogRead(4) * .4882812 - 273.15;
        lumen = analogRead(0);
        Serial.print(temp);
        Serial.print("|");
        Serial.print(lumen);
        Serial.println();
        delay(10);
    }
    

    This will make the run.py without the magic initialization via picocom. Though I still would like to know what Serial.available() expects as input.