Reading XBee data with Python not possible - python

I have chosen the following setup to read sensor data by Arduino and an XBee-connection:
List itemA TMP36-sensor is connected to an Arduino Uno
List itemA Sparkfun XBee-shield with an XBee S2C is mounted on the Arduino (Router, API-mode). The Arduino is connected to COM3.
List itemCOM4 is connected with a Sparkfun XBee-Explorer (USB-connection). Another XBee S2C is connected on the explorer. This XBee is the coordinator (API-mode).
I have written the code for reading data from Xbee-Explorer at COM4:
#! /usr/bin/python
# Import and init an XBee device
from xbee import XBee, ZigBee
import serial
ser = serial.Serial('COM4', 9600)
xbee = XBee(ser)
while True:
try:
enter response = xbee.wait_read_frame()
print response
except KeyboardInterrupt:
break
ser.close()
At the moment it is not possible to get any data received by the Xbee with the Python code, although it is possible to read the data by XCTU.
In Detail:
If I send sensor data (sensor reading and sending to Xbee is done by Arduino Software) from the router to the coordinator, I'm able to read the data frames by XCTU and the results make sense. If I use the Python-code above, I did not get any data frames, although the RSSI-diodes of router and coordinator are blinking independently from the software (XCTU or Python) I use.
For me it is not clear what is going wrong and I would be happy to get some help to solve the problem.
Thank you very much for your support.
Regards Daniel

I had the same problem, changing from API=2 to API=1 solved my problem

Related

cannot communicate Arduino with python

I am getting an error while trying to communicate Arduino with python, I am using Arduino module and I'm getting cannot open port error and I can communicate my Arduino from Arduino IDE.
from Arduino import Arduino
import time
board = Arduino(port="/dev/cu.usbmodem14201") # plugged in via USB, serial com at rate 115200
board.pinMode(13, "OUTPUT")
while True:
board.digitalWrite(13, "LOW")
time.sleep(1)
board.digitalWrite(13, "HIGH")
time.sleep(1)
This is my error
serial.serialutil.SerialException: [Errno 2] could not open port /dev/cu.usbmodem14201: [Errno 2] No such file or directory: '/dev/cu.usbmodem14201'
when I tried with pyfirmata I am getting an error
This is my code:
import pyfirmata
import time
board = pyfirmata.Arduino('/dev/cu.usbmodem14201')
led = board.get_pin('d:13:o')
while True:
led.write(1)
time.time(1)
led.write(0)
time.time(1)
my error for pyfirmata:
AttributeError: partially initialized module 'pyfirmata' has no attribute 'Arduino' (most likely due to a circular import)
To preface, I have done some Serial communication with Arduino, but have not worked with the Arduino library too extensively.
I suggest if you haven't done so already, considering the PySerial library. This may help with your initial issue with serial connection between your Mac and board. This does not entirely fix your need of directly writing to the LEDs, but can serve as a substitute in the meantime. You can use the incoming serial communication from your Mac to direct certain operations on your Arduino.
A great tutorial I have used can be found here:
https://create.arduino.cc/projecthub/ansh2919/serial-communication-between-python-and-arduino-e7cce0
Another issue may be that your Serial Monitor may be active which is blocking serial communication between devices over Python.

testing usb to serial to usb using pyserial has no output

I'm trying to test writing messages to serial port using pyserial and reading it again using two usb to serial adapters connected back to vack "USB-Serial --> Serial-USB" to verify it is writing to the serial port as it is meant for communication with hardware,
so I have open console witch is reading all the time
import serial
port = 'COM6'
read_ser = serial.Serial(port)
while True:
x=read_ser.read()
print(x)
and for writing I use
import serial
port = 'COM5'
ser = serial.Serial(port)
# ser.write(str.encode('$GPRMC,081117.24,A,5042.988302,N,1058.376556,E,14.7,,230813,0.0,E*74'))
ser.write('$GPRMC,081117.24,A,5042.988302,N,1058.376556,E,14.7,,230813,0.0,E*74'.encode())
And nothing is shown in the reading console
I tried both adapters and read from external serial hardware successfully.
I'm using python 3.7 on windows 10
I figured it out
The problem was with the pin layout as I used gender changer to connect the two serial adapters
using serial cable instead solved the problem.

receiving empty data while reading serial communication on usb port- Raspberry Pi 3 model B+

I have a smart camera sending telemetry data every one minute through wMbus (wireless Meter-Bus) and I have a usb stick (wMbus receiver) plugged in a raspberry pi 3 Model B+. I am trying to read the data sent by the camera. I used this:
import serial
ser = serial.Serial(port='/dev/ttyUSB0', baudrate=2400, parity=serial.PARITY_EVEN, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS, timeout=10)
while True:
data = ser.readline()
print(data)
the result is as follows:
b''
b''
b''
b''
b''
Do you know what is the problem and how I can fix it to be able to read the telemetry data sent by the camera?
This can be so many different things, almost every device has his own characteristics when it comes to serial communication, i recommend you to look for some library that's designed directly for your device, or maybe look the device documentation directly.
My opnion is based on my experience with IoT proofs of concept, i had to work with different modules and devices togheter with RPI and Arduino, and every single one of them had some peculiarity when it comes to serial pairing and data exchange.
Looking into google for "wMbus python3 lib" returned a few libraries on github that you may try out and check if it fits your needs.
Most likely it's because there is no data being received over the serial port, and the timeout (which is 10 seconds) has been hit. You can validate this with no device attached at all and see if you get the same behavior.

What is the exact syntax to use UART pin of nodemcu using micropython?

I am trying to send serial message from one nodemcu to two arduino boards. However, i am able to use only one uart pin of nodemcu which is TX pin. I can send message without any problem. But how can i send serial message from another uart. Node mcu documentation shows that it have two uart pins which are TX and GPIO15. Can someone share the syntax or example to use GPIO15. I have attached the program that i used to send message from TX pin but no idea for GPIO15.
import machine
from machine import UART
uart = UART(0) #For TX pin
uart.write("Hello \n")
from machine import UART
uart = UART(1, 9600) # init with given baudrate
uart.init(9600, bits=8, parity=None, stop=1) # init with given parameters
uart.write("Hello \n")

how to read data that is written from a communication Port?

I try to make communication between my device and python code via COM4 a communication port: pyserial communication.
So at first I try to send and write a hello then to read the output of my device,
But the problem for me is how to read the hello that is sent firstly. I mean that I want to read the string hello on COM4 from my device
import serial
ser = serial.Serial(
port='COM4',\
baudrate= 230400) # open serial port
print(ser.name) # check which port was really used
#ser.write(b'hello\n') # write a string
#str=ser.readline()
while True:
print(ser.read(30).decode())
ser.close() # close ports
The code of my device is written in C language.
I would be very grateful if you could guide me please.
This process differs based on the operating system. Here's a good library that supports Linux and Windows:
http://www.teuniz.net/RS-232/

Categories

Resources