Python Forum
Arduino Uno connected to PI 0 W - Sensor Data MQTT
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Arduino Uno connected to PI 0 W - Sensor Data MQTT
#1
Anybody has a reference to a good example of how to best transfer sensor data from Arduino Uno to Raspberry Pi over the USB Cable?

I can figure out how to send data back and fourth, but was wondering if there are some best practices for managing sensor data this way.

I already have the Raspberry Pi 0 W setup as a pool controller with various sensors, relays etc. It is controlled and sends data to Home Assistant via MQTT.
I just ran out of pins and wanted a more robust unit for my sensor inputs, so I'm adding an Arduino Uno to deal with various other sensors (Pressure, Temperature, Levels etc.)
Any help and examples would be greatly appreciated.
Reply
#2
is this command driven?
I'm thinking the way to go is to first establish the serial link through USB using putty,
the protocol is fussy enough that if this is not done, you may never establish a connection.
Once you have a clear idea of what is needed, you can then use something like: https://www.raspberrypi.org/forums/viewt...p?t=139813
to get it working with python.
Reply
#3
Why would you need Putty? Don't they communicate fine without?

Arduino Sketch:
const int ledPin = 13
void setup()
{
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}
void loop()
{
  Serial.println("Hello Pi");
  if (Serial.available())
  {
  flash(Serial.read() - '0');
  }
  delay(1000);
}
void flash(int n)
{
  for (int i = 0; i < n; i++)
  {
    digitalWrite(ledPin, HIGH);
    delay(100);
    digitalWrite(ledPin, LOW);
    delay(100)
Raspberry Code:
import serial
ser = serial.Serial('/dev/ttyACM0', 9600)
while True:
  print(ser.readline())
And to send from Raspberry to Arduino:
ser.write('5'.encode())
I would think that there must be tons of people out there who are pulling sensor data from an Arduino via a Pi, so I was just interested in their approach.
Reply
#4
Quote:Don't they communicate fine without?
Yes, they will communicate just fine if all variables are set properly.
There are obscure things about RS-232 that are hardly ever used, but once in a while you will run into them.
Some of these include use of secondary CTS, DTR or DSR being used, Cable reversing CTS and RTS and/or (usually and) CTS and RTS reversed in cable.

Use of putty (or some other terminal program) will show these immediately, and can be modified immediately just to get the proper setup.
As stated, this is rarely the case, but is does happen, and the first time it does, the value of using a terminal program becomes apparent.

When I first started using serial communications we didn't have putty, nor did we have USB. It was imperative back then to use a 'break-out' box to check connections. Old habits, it's sort of like checking your gasoline before takeoff.
Reply
#5
Ok, makes sense.

The data I'm collecting from the Arduino is just sensor data and it's not mission critical if a little bit is missed here and there, so I think I will stick to just reading it directly.
I ended up sending the data tab delimited from the Arduino and then just splitting it up upon reception in the Pi:

while True:
        ser.flushInput()
        data=ser.readline()
        pieces=data.decode().split("\t")
        client.publish("pool/heater/volt", pieces[0])
        client.publish("pool/pressure/volt", pieces[1])
        client.publish("pool/flow/status", pieces[2])
        client.publish("pool/chlorLevel/status", pieces[3])
        client.publish("pool/acidLevel/status", pieces[4])
        client.publish("pool/chlorOverflow/status", pieces[5])
        client.publish("pool/acidOverflow/status", pieces[6])
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to check if Skype call is connected or disconnected in Python? boral 1 357 Dec-28-2023, 08:31 AM
Last Post: buran
  add mqtt feature to existing code Positron79 0 567 Jan-31-2023, 05:56 PM
Last Post: Positron79
  Trying to Get Arduino sensor data over to excel using Python. eh5713 1 1,616 Dec-01-2022, 01:52 PM
Last Post: deanhystad
  When is stdin not connected to a tty, but can still be used interactively? stevendaprano 1 967 Sep-24-2022, 05:06 PM
Last Post: Gribouillis
  Mqtt Shiri 0 1,261 Dec-07-2021, 02:01 PM
Last Post: Shiri
  Paho-mqtt fully supports MQTT 5.0 protocol? nitinkothari17 0 1,528 Jan-12-2021, 11:14 AM
Last Post: nitinkothari17
  Printing to a printer connected to pi 4 alan 2 2,381 Oct-04-2020, 10:08 PM
Last Post: alan
  Save Arduino data in mysql server via raspberrypi rithikvg 1 3,345 Jun-24-2020, 10:59 PM
Last Post: Larz60+
  Help Graphing Arduino Data Real Time in Python nschulz 0 2,498 Mar-12-2020, 06:15 PM
Last Post: nschulz
  Create sensor data for a IoT project georgelza 9 5,735 Dec-20-2019, 06:19 PM
Last Post: georgelza

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020