Implementation of several simple protocols on Raspberry Pi

Source: Internet
Author: User

Http://conanwhf.gitcafe.io/2016/01/30/rpi-protocol/?hmsr=toutiao.io&utm_medium=toutiao.io&utm_source= Toutiao.io

implementation of several simple protocols on Raspberry Pi posted on 2016-01-30 |   Categories in Raspberrypi | No Comments

The procrastination of his own is really disciplining, this is a delay to abandon the article. Fast New Year, how can not be dragged into the years after the spirit, clear the list.

To create a small home environment monitoring system, a variety of sensor is indispensable. I've seen most of the people using Python on Raspberry Pi, and I'm here to get a buzz. Python is a very easy to get started with the language, I just a little bit of information, half a day, I took a few sensor, of course, but the simplest to take the data what, but in contrast, although I write these protocols with C very familiar, I'd rather see Python's syntax than change C ... if I have a problem. The advantages of Python are not all grammatical simplicity, the most important is that there is a huge amount of development library can be used. Like these simple protocols, all of the library is installed, directly to use it, and look at the code, it is almost unbearable to stare at the C code. For ensemble projects without the pursuit of speed, it is quite suitable.
This article first introduces several of the most commonly used basic protocol implementation, the usual, first on the source portal
GPIO

The Gpio port is simple, either input or output. Install the Development library first:
sudo apt-get install Python-rpi.gpio
And then quote in the code:

Import time
import Rpi.gpio as GPIO
# define PIN
gpio_pin=17
print ("Start GPIO test for pin=bcm%d"%pin)
Gpio.setmode (GPIO). BCM)
# Set to input, read Gpio
gpio.setup (PIN, Gpio. IN)
value = Gpio.input (PIN)
print ("Before set, pin=%d"%value)
# Change the Gpio output
gpio.setup (PIN, Gpio.
Gpio.output (PIN, value)
print ("Now pin=%d"%gpio.input (PIN))
gpio.cleanup ()

Here is just a demo, the actual application is not able to let you casually pull high pull low can also keep you set the situation. It's like a lamp or something, you pull it up and you pull it down, it's black, or some input device is like a button, or a more complex input with timing. Here are two points to note: The SetMode is in BCM mode, and the pin used is the same as the BCM number corresponding to the pin graph. You can also set the physical PIN number to use SetMode (GPIO). BOARD). The input/output mode is input when receiving data/status relative to host or Raspberry Pi, output i²c

I²c can be a bus to connect n devices, as long as the device's i²c address does not conflict, you can use the bread plate expansion, connected to several. The first is still to add the development library, note that this library looks like only in Python2, if I change Python3 will be an error can not find, maybe I am not familiar with it ...
sudo apt-get-y install Python-smbus
Then pick up the hardware, if you need to confirm the hardware, you can use the I²c tool to see:

sudo apt-get-y install i2c-tools
sudo i2cdetect-y 1

Parameter 1 refers to the device i2c-1, rpi2 on the default is 1,i2c0 reserved for e2prom use, did not toss open it. If you see a result similar to the following, it means that the hardware is OK.

The above figure shows that there are two i²c devices connected to the bus, and their addresses are 0x48 and 0x77 respectively. If you do not see a device, check the hardware connection, and remember to open the system config inside the i²c.
Then still the sample code:

Import time
Import SMBus

addr=0x48
cmd=0xab
# open/dviev/i2c-1  
bus = SMBus. SMBus (1)
# Read and write data, Case 1 Data
= bus.read_byte (addr)
bus.write_byte (addr, data+1)
# Read and write data, Case 2
= Bus.read_byte_data (addr, cmd)
bus.write_byte_data (addr, cmd, data+1)

Most of the I²C device read and write, in addition to the need for the I²C devices address, there are some fixed cmd, so the read-write interface is two, specifically to see the requirements of the device spec, similar. UART

Serial UART Communication has two main, one is used to debug the command line, one is sensor data interface. Always feel that the serial debugging is very remote, especially in the Raspberry Pi, basically is the network SSH bar. See some other routines, are doing serial debugging, in fact, if just take a data is not so complex.
First of all, of course it's Cool (library):
sudo apt-get-y install python-serial
Then do one more step: Raspberry Pi default console is the UART output, get rid of (remember to change with sudo).
Open /boot/cmdline.txt, inside the content "dwc_otg.lpm_enable=0 console=ttyama0,115200 console=tty1 root=/dev/ MMCBLK0P2 ROOTFSTYPE=EXT4 Elevator=deadline
Fsck.repair=yes rootwait ", the bold part is deleted, because ttyAMA0 is the device we want to use.
In some other articles have seen also need to change a place, may be the last generation of RPI, if your system is up-to-date, do not need to do.
Test the Code section:

Import serial
import time

def showdata (ARG):
    result = ' for
    i in arg:
        st = '%02x '%ord (i)
        result + = st+ '
    print (result)

ser = serial. Serial ('/dev/ttyama0 ', 9600, timeout=1)
# Gets the receive buffer character
#count = ser.inwaiting ()
# reads the content
data = Ser.read (5)
showdata (data)
# Clears the receive buffer
#ser. Flushinput ()
ser.close ()

In the code above, the function ShowData () is the one I added to print the 16 binary data that I read. Because the device I'm picking up is a sensor, not a character device, so I need to convert it. Similarly, the annotated Inwaiting () and Flushinput () functions are for character input, and I have not tested them on my machine, because sensor will keep on reporting data, simply read it.
Another important aspect of hardware connectivity:

The tx of the Raspberry Pi to be connected from the Rx of the device, the Rx of the Raspberry Pi to connect the TX from the device!!!

Don't make a mistake, or you won't get the data. spi-temporarily not

Originally was bought an SPI screen, who knows the courier problem did not send to, finally received the seller to the information also always forget, so had to owe Bong. Chinese New Year, it is estimated that some days to talk about it.
In other words, the home NAS system after upgrading, incredibly admin is not root permissions, a lot of hidden folders are not open. My bitsync a bit of a problem, want to look at the configuration file, is also helpless. It seems that QNAP is not going to give people play, seriously do business machine. Fortunately there are Raspberry Pi, later play a bit of a mess of things, it can only rely on it.
Another: I github on the source code, there are a few sensors of the demo, including bmp180,dht11,pcf8591, and so on, if necessary to take. #Python

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.