beaglebone black接入ds18b20并通过网络上传数据

时间有限写的简单点主要是代码,不明白的留言

first

编辑dts文件如下

/dts-v1/;
/plugin/;

/ {
	compatible = "ti,beaglebone", "ti,beaglebone-black";

	part-number = "BB-W1";
	version = "00A0";

	/* state the resources this cape uses */
	exclusive-use =
		/* the pin header uses */
		"P9.22",
		/* the hardware IP uses */
		"gpio0_2";

	fragment@0 {
               target = <&am33xx_pinmux>;
               __overlay__ {
					dallas_w1_pins: pinmux_dallas_w1_pins {
						pinctrl-single,pins = < 							0x150 0x37 						>;
					};
               };
	};

	fragment@1 {
               target = <&ocp>;
               __overlay__ {
		       onewire@0 {
			       compatible      = "w1-gpio";
			       pinctrl-names   = "default";
			       pinctrl-0       = <&dallas_w1_pins>;
			       status          = "okay";

			       gpios = <&gpio1 2 0>;
		       };
         };
	};
};

编译之

dtc -O dtb -o BB-W1-00A0.dtbo -b 0 -@ BB-W1-00A0.dts

然后拷贝到firmware下加载

cp BB-W1-00A0.dtbo /lib/firmware/
echo BB-W1:00A0 > /sys/devices/bone_capemgr.*/slots
接着编写python应用程序
import time
import socket

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

address = ('192.168.7.1', 9000)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

while True:
    try:
        s.connect(address)
        break
    except Exception, e:
        time.sleep(0.1)
        print "ERROR"
        continue



w1="/sys/bus/w1/devices/28-000006601b9c/w1_slave"

while True:
    raw = open(w1, "r").read()
    data= "Temperature is "+str(float(raw.split("t=")[-1])/1000)+" degrees"
    print data
    s.send(data)
    time.sleep(1)


 而后运行之 
  

python ds.py

稍微解释下程序首先读取ds18b20的温度而后上传到192.168.7.2的9000端口

你需要在你电脑上运行一个tcp调试助手(青云)打开你的9000端口监听就能收到bbb上传上来的温度

稍后我会在android上做一个客户端来读取实时温度

运行信息


RX:Temperature is 29.437 degrees (22:09:01:400)
RX:Temperature is 29.5 degrees (22:09:03:179)
RX:Temperature is 29.5 degrees (22:09:04:964)
RX:Temperature is 29.5 degrees (22:09:06:749)
RX:Temperature is 29.437 degrees (22:09:08:533)
RX:Temperature is 29.5 degrees (22:09:10:311)
RX:Temperature is 29.437 degrees (22:09:12:090)
RX:Temperature is 29.5 degrees (22:09:13:878)
RX:Temperature is 29.5 degrees (22:09:15:659)
RX:Temperature is 29.5 degrees (22:09:17:440)
RX:Temperature is 29.437 degrees (22:09:19:222)

你可能感兴趣的:(beaglebone,black)