Einfache Veränderung der ID über den 1-Wire Bus

Für viele Greäte mag zwar eine absolut eindeutige ID wichtig sein, im Bereich der Home-Sensorik ist es aber eher lästig für die ID den Quelltext der des Programms für den Mikrocontroller immer neu zu übersetzen.

Vorgehensweise

Die einzelnen Befehle für die Änderung der ID wurden schon im Beitrag "Erklärungen zum Sourcecode" beschrieben. Hier ist nocheinmal die generelle Vorgehensweise mit den entsprechenden CODES:

MasterDatenBeschreibung
Tx Reset Master sendet Resetimpuls
Rx Presence Slaves antworten mit Presence
Tx 0x55 Master sendet Match-Rom
Tx 64-bit 1-Wire ID Master sendet aktuelle ID
Tx 0x75 Master sendet Befehl Write-New-ID
Tx 64-bit New ID Master sendet neue ID
Tx Reset Master sendet Resetimpuls
Rx Presence Slaves antworten mit Presence
Tx 0x55 Master sendet Match-Rom
Tx 64-bit 1-Wire Alte-ID Master sendet alte ID
Tx 0xA7 Master sendet Befehl Read-New-ID
Rx 64-bit New ID Master empfängt neue ID und vergleicht
Tx Reset Master sendet Resetimpuls
Rx Presence Slaves antworten mit Presence
Tx 0x55 Master sendet Match-Rom
Tx 64-bit 1-Wire Alte-ID Master sendet alte ID
Tx 0x79 Master sendet Befehl Read-New-ID
Tx Byte 2 der alten ID Master sendet das zweite Byte der alten ID
Tx Byte 6 der alten ID Master sendet das 6. Byte der alten ID
Tx Byte 7 der alten ID Master sendet das 7. Byte der alten ID
Wait 30 ms   Slave speichert neue ID

Um ein versehendliches Verändern der ID zu verhindern (es könnte ja sein das der AVR zufälltig den Code 0x79 empfängt), müssen noch drei Byte aus der alten ID als Code an den AVR übertragen werden. Erst dann kommt es zu einer Veränderung der ID.

Der AVR speichert die ID in den letzten 8 Byte des EEPROMs. Bei jedem Start des AVR wird im EEPROM geschaut, ob das erst Byte der ID verschieden von 0xFF ist. Dann werden die Bytes als ID gelesen. Alternativ kommt die ID im Quellcode zum Einsatz.

Programme

Python auf dem Raspberry PI

Auf dem Raspberry PI (vielleicht auch auf einigen anderen Linuxsystemen) kann auf den 1-Wire Bus über das Verzeichniss /sys/bus/w1/devices/ zugegriffen werden. Damit kann relativ einfach über die Datei rw der Zugriff direkt auf den 1-Wire Bus erfolgen. Somit ist ein kleines Python-Script entstanden, womit sich relativ einfach die ID ändern lässt:
#!/usr/bin/python
# Copyright (c) 2015, Tobias Mueller tm(at)tm3d.de
# All rights reserved. 
# 
# Redistribution and use in source and binary forms, with or without 
# modification, are permitted provided that the following conditions are 
# met: 
# 
#  * Redistributions of source code must retain the above copyright 
#    notice, this list of conditions and the following disclaimer. 
#  * Redistributions in binary form must reproduce the above copyright 
#    notice, this list of conditions and the following disclaimer in the 
#    documentation and/or other materials provided with the 
#    distribution. 
#  * All advertising materials mentioning features or use of this 
#    software must display the following acknowledgement: This product 
#    includes software developed by tm3d.de and its contributors. 
#  * Neither the name of tm3d.de nor the names of its contributors may 
#    be used to endorse or promote products derived from this software 
#    without specific prior written permission. 
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 




import time
import subprocess

def owcom(dev,send,rc):
	res=[]
	f=open("/sys/bus/w1/devices/%s/rw" %(dev),"r+b",0)
	f.write("".join(map(chr, send)))
	if (rc!=0):
		res=map(ord,f.read(rc))
	f.close()
	return res
		

def crc8(arr):
	lscrc=0x0;
	for v in arr:
		bit=1;
		while bit<256:
			if (v&bit)==bit:
				lb=1
			else:
				lb=0
			if (lscrc&1)!=lb:
				lscrc=(lscrc>>1)^0x8c 
			else:
				lscrc=(lscrc>>1)
			bit=bit*2
	return lscrc
	
def testnr(s):
	try:
		i=int(s)
		return True
	except ValueError:
		return False

def addid(id,val):
	for i in range(7):
		id[i+1]=id[i+1]+val
		if id[i+1]>254:
			id[i+1]=id[i+1]-254
			val=1
		else:
			return id
	return id
#

l=subprocess.check_output("ls /sys/bus/w1/devices/", shell=True)
dc=0
dl=[]
for g in  (l.split("\n")):
	if len(g)>2:
		if testnr(g[0:2]):
			dl.append(g)
			dc=dc+1
			print dc,") ",g
if dc==0:
	print "No 1-Wire Device found"
	exit(0)
n=int(raw_input("No. of Device: "))
n=n-1
if (n>dc):
	exit(0)
print dl[n], "will be changed"
s=dl[n]
nr=[int(s[0:2],16),int(s[13:15],16),int(s[11:13],16),int(s[9:11],16),int(s[7:9],16),int(s[5:7],16),int(s[3:5],16)]
nr.append(crc8(nr))
onr=nr[:]
re=1
while re:
	re=0
	print "Number: %02X %02X %02X %02X %02X %02X %02X %02X" % tuple(nr)
	print "1) Increment the old ID"
	print "2) Add a value to the old ID (value < 255)"
	print "3) Input 6 hex numbers like AA,B5,00,32,12,F1"
	print "4) Exit"
	cho=int(raw_input("Press the number of your choice: "))
	if cho==1:
		nr=addid(nr,1)
	elif cho==2:
		val=int(raw_input("Add value <255: "))
		nr=addid(nr,val)
	elif cho==3:
		hn=raw_input("Input six numbers in hex (like AA,B5,00,32,12,F1): ")
		i=1
		for hnt in hn.split(","):
			nr[i]=int(hnt,16)
			i=i+1
	elif cho==4:
		exit()
	else:
		re=1
nr[7]=crc8(nr[:-1])
print "New ID: %02X %02X %02X %02X %02X %02X %02X %02X" % tuple(nr)
cho=raw_input("Set this ID? [Y/n): ")
if (cho!='Y'):
	exit()


owcom(s,[0x75]+nr,0)
nnr=owcom(s,[0xA7],8)
print "New ID: %02X %02X %02X %02X %02X %02X %02X %02X" % tuple(nnr)
if nr!=nnr:
	print "ERROR writing new ID\nMaybe it's no simulatet ow device from tm3d.de"
	exit(0)
print "New ID: %02X %02X %02X %02X %02X %02X %02X %02X" % tuple(onr)
owcom(s,[0x79,onr[1],onr[5],onr[6]],0)
print "Done.\nWait for a new Searchrom from Master\nSometimes the old nr keeps in the directory"

Programm für Windows

Zum Seitenanfang