I want to interface gsm A6 module to raspberry pi by software uart. I am looking for a python library which will convert any gpio pin to TX and RX pin. I was trying pigpio library. there I got the serial read but whats about serial write? is it possible to implement serial write also? or is there any other library?
import sys
import time
import difflib
import pigpio
RX=18
try:
pi = pigpio.pi()
pi.set_mode(RX, pigpio.INPUT)
pi.bb_serial_read_open(RX, 9600, 8)
while 1:
(count, data) = pi.bb_serial_read(RX)
if count:
print data
time.sleep(1)
except:
pi.bb_serial_read_close(RX)
pi.stop()
I want a software uart transmite function.
Related
I want to connect a pressure sensor that use a RS485 communication to raspberry pi,
when I connected it to RS485 to UART converter to the raspberry
but when I ran the code the output is b'' with no numbers from the sensor
can anybody know how I can read from the sensor ?
'KITA' is the sensor I use.
import serial
import time
ser = serial.Serial('/dev/serial0', 9600, timeout=0.1)'
ser.write(b'$PF,200\r\n')
ser.write(b'$PO,20\r\n')
x = ser.read()
print (x)
At the moment I am trying to let two MCP3008's communicate through SPI with my raspberry pi and a Python script. A potentiometer should send a certain analog value to the MCP3008 input channel.
Here is my setup in Fritzing:
Breadboard Schematic
and here is the schematic overview:
Schematic Overview
The SPI wiring is based upon a standard daisychain schematic as shown in:
SPI Daisy Chain
The Python Code I am using is:
import spidev
import time
spi = spidev.SpiDev()
spi.open(0,0)
spi.max_speed_hz = 1000000
def read_spi(channel):
spidata = spi.xfer2([0,(8+channel)<<4,0])
return ((spidata[1] & 3) << 8) + spidata[2]
try:
while True:
channeldata = read_spi(0)
print (channeldata)
time.sleep(.1)
except KeyboardInterrupt:
spi.close()
I am getting values, but they fluctuate a lot with every value possible between 0 and 1023.
I've tried it with one MCP3008 without daisychaining of course and it worked fine, so my guess is that it has something to do with either the daisychain being incorrect, or the addressing of the MCP3008.
Can you guys help me out? Thanks alot!!
Cheers,
Devatu
Might be a late response, but according to the data sheet this chip can't be daisy-chained at all.
I am also looking for an ADC which can be daisy-chained.
The Raspberry Pi has a Broadcom BCM 2835 chip allowing it to interface with SPI devices on its GPIO pins. I encountered a problem adressing LEDs in a 8x8 Matrix via SPI.
I want to adress single LEDs on the matrix, but I haven't figured out, which bytes adresses which LED(s).
SPIdev seems to be the right library for this purpose und I stumbled upon this snippet of code:
import spidev
import time
spi = spidev.SpiDev()
spi.open(0, 0)
spi.max_speed_hz = 7629
# Split an integer input into a two byte array to send via SPI
def write_pot(input):
msb = input >> 8
lsb = input & 0xFF
spi.xfer([msb, lsb])
# Repeatedly switch a MCP4151 digital pot off then on
while True:
write_pot(0x1FF)
time.sleep(0.5)
write_pot(0x00)
time.sleep(0.5)
Does anyone know which values adresses (a) specific LED(s)?
In C/C++ adressing works very well via the wiringpi-library, a useful documentation can be found here:
http://wiki.52pi.com/index.php/RPI-RGB-LED-Matrix_SKU:EP-0075
I FT232 module connected to the Raspberry Pi (with USB cable). I want to receive 8bytes (1 byte). On Windows, Terminal draws me correctly beats (eg. 00001011). In the following python script:
#!/usr/bin/python
import serial
import binascii
ser = serial.Serial('/dev/ttyUSB0', 115200, timeout=1)
while True:
str1 = ser.read(1)
if str1 == '':
print ''
else:
print bin(int(binascii.hexlify(str1),16))
returns only 2 bits at the end:
0b11111100 ,
0b11111101 ,
0b11111110 ,
0b11111111
What am I doing wrong that draws me just 2bits at the end?
Below please find screen how to receive data on Windows:
http://mateok.ovh/windows.jpg
And on Raspberry Pi:
http://mateok.ovh/linux.jpg
I would like to get on the Raspberry Pi received data were the same.
Edit:
I was able to find a solution! It turned out that the reason is ... that the device had been connected before starting the Raspberry Pi! If they Disconnect and reconnect only when the Raspberry Pi starts - values are returned well! Maybe someone knows the cause of this? And how can I fix it? I do not want every time you re-connect the converter ..
I hooked up a 16X2 LCD to the pi via a MCP23017 GPIO expander. The problem that I am facing is that the LCD code below works inside normal functions or when I call it individually. But it does not work inside any of the threads in my program.
#!/usr/bin/python
# Example script to show usage of MCP230xx GPIO extender to drive character LCD.
from Adafruit_CharLCD import Adafruit_CharLCD
from Adafruit_MCP230xx import MCP230XX_GPIO
bus = 1 # Note you need to change the bus number to 0 if running on a revision 1 Raspberry Pi.
address = 0x20 # I2C address of the MCP230xx chip.
gpio_count = 8 # Number of GPIOs exposed by the MCP230xx chip, should be 8 or 16 depending on chip.
# Create MCP230xx GPIO adapter.
mcp = MCP230XX_GPIO(bus, address, gpio_count)
# Create LCD, passing in MCP GPIO adapter.
lcd = Adafruit_CharLCD(pin_rs=1, pin_e=2, pins_db=[3,4,5,6], GPIO=mcp)
lcd.clear()
lcd.message(" Adafruit 16x2\n Standard LCD")
links to library programs,
https://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code/blob/master/Adafruit_CharLCD/Adafruit_CharLCD.py
https://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code/blob/master/Adafruit_MCP230xx/Adafruit_MCP230xx.py
part of my actual program:
def switch():
mcp.config(0, mcp.INPUT)
mcp.pullup(1,1)
if ((mcp.input(1)>>1))
lcd.clear()
lcd.message("Switch open")
switchthread=threading.thread(target=switch)
switchthread.start()
Can anyone please tell me how to get the LCD commands working in the threads ?