Reading arduino data

Ok so I am having some issues reading date sent from the Arduino via the serial port to a Linux (ubuntu) platform. I am using python3 on Linux to receive and display the data. Here is the python 3 code:

import serial
import sys

ser = serial.Serial(
        # Serial Port to read the data from
        port='/dev/ttyACM0',
 
        #Rate at which the information is shared to the communication channel
        baudrate = 9600,
   
        #Applying Parity Checking (none in this case)
        parity=serial.PARITY_NONE,
 
       # Pattern of Bits to be read
        stopbits=serial.STOPBITS_ONE,
     
        # Total number of bits to be read
        bytesize=serial.EIGHTBITS,
 
        # Number of serial commands to accept before timing out
        timeout=1
)
# Pause the program for 1 second to avoid overworking the serial port
while 1:
        x=ser.readline()
        data=ser.inWaiting
        print(x)

Here's the output. I don't understand the b. The \r\n I get, but not sure why I am getting that. I was expecting 23.4. I think there is a basic something I am not getting on serial readline(), like its reading bytes as an array and just displaying them as a string. But the b is still a mystery unless I have a serial parameter wrong.

b''
b''
b''
b'23.40\r\n'
b''
b''
b'23.40\r\n'
b''
b''
b'23.40\r\n'
b''

Here is the Arduino code. Basically the DHT22 example but I don't plug the sensor in so the only part of the code that executes is after the if data check.

/*
 * Created by ArduinoGetStarted.com
 *
 * This example code is in the public domain
 *
 * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-dht22
 */

#include "DHT.h"
#define DHT22_PIN 2

DHT dht22(DHT22_PIN, DHT22);

void setup() {
  Serial.begin(9600);
  dht22.begin(); // initialize the DHT22 sensor
}

void loop() {
  // wait a few seconds between measurements.
  delay(2000);

  // read humidity
  float humi  = dht22.readHumidity();
  // read temperature as Celsius
  float tempC = dht22.readTemperature();
  float tempCI =23.4;
  // read temperature as Fahrenheit
  float tempF = dht22.readTemperature(true);

  // check if any reads failed
  if (isnan(humi) || isnan(tempC) || isnan(tempF)) {
    // Serial.println("Failed to read from DHT22 sensor!");
    //Serial.write("\n");
    Serial.print(tempCI);
    Serial.print("\r\n");
  } else {
    Serial.print("DHT22# Humidity: ");
    Serial.print(humi);
    Serial.print("%");

    Serial.print("  |  "); 

    Serial.print("Temperature: ");
    Serial.print(tempC);
    Serial.print("°C ~ ");
    Serial.print(tempF);
    Serial.println("°F");
  }
}

When I capture the data in miniterm its fine so must be something in the python code.

Any help will be greatly appreciated. Thanks.

The b is Python telling you that is a bytes object and not a string. You wont be able to co.pare that with another string.

Try:

x=ser.readline().decode("utf_8")

If you don't need the "\r\n", why do you still sending it?

To put each tempCI on a new line. Otherwise they are all printed on one line. Just for show right not, not necessary at future implementations of the project.

Many thanks. Been banging my head against that for hours. I thought is was something like that, just not that familiar with serial coding.
Thanks again!!

1 Like