ik wil zeker wat code geven ,
tis mss niet het beste en meest elegante , maar het doet voor mij wat het moet doen , en tis ook voor mij , mijn 1e stappen in python.
resume : python leest adc uit alover i2c , en maakt een file op de sdkaart in php code.
de file staat in een map die webserveert

een andere php , leest de file met waarden uit , en zet het in mooi html om <meter>-tag
op een pc-client connecteert naar de server
pythoncode ,
Code:
#!/usr/bin/env python3
# read abelectronics ADC Pi board inputs
# uses quick2wire from http://quick2wire.com/
# github: https://github.com/quick2wire/quick2wire-python-api
# Requries Python 3
# I2C API depends on I2C support in the kernel
#
# This is a slightly modified version of the example code on http://www.abelectronics.co.uk/products/3/Raspberry-Pi/7/ADC-Pi---Raspberry-Pi-Analogue-to-Digital-converter
# See http://elinux.org/RPi_ADC_I2C_Python for full setup instructions
#programmanaam = analoog.py
#aanvulbestand = /var/www/analoog.inc met rechten op 777
#
# de file die we generen is een php , zodat we dit makkelijk kunnnen inlezen in de phpwebserver zelf
# op die manier moeten we niet teveel python doen voor edit vd sourcecode.
# we doen in python de omrekeing van 5volt naar 100% , zo hebben we percentwaarden in php
#python3 anloog.py
from time import gmtime, strftime
print(strftime("%Y-%m-%d %H:%M:%S", gmtime()))
currenttime = (strftime("%Y-%m-%d %H:%M:%S", gmtime()))
import quick2wire.i2c as i2c
import re
import time
adc_address1 = 0x68
adc_address2 = 0x69
adc_channel1 = 0x98
adc_channel2 = 0xB8
adc_channel3 = 0xD8
adc_channel4 = 0xF8
teller = 0
wacht = 5*60 #refreshtime in sec
commentaarinfo = "python rekent uit van 5v naar 100%, parameters allemaal anal1tot8 , round op 2decimalen , 4.4v komt er uit bboxadsl gemeten met voltmeter"
for line in open('/proc/cpuinfo').readlines():
m = re.match('(.*?)\s*:\s*(.*)', line)
if m:
(name, value) = (m.group(1), m.group(2))
if name == "Revision":
if value [-4:] in ('0002', '0003'):
i2c_bus = 0
else:
i2c_bus = 1
break
with i2c.I2CMaster(i2c_bus) as bus:
def getadcreading(address, channel):
bus.transaction(i2c.writing_bytes(address, channel))
time.sleep(0.05)
h, l, r = bus.transaction(i2c.reading(address,3))[0]
time.sleep(0.05)
h, l, r = bus.transaction(i2c.reading(address,3))[0]
t = (h << 8) | l
v = t * 0.000154
if v < 5.5:
v=v*20 #vith aanpassing
v=round(v,2)#vith afronden tot 2decimalen
return v
else: # must be a floating input
return 0.00
while True:
begintag = "<?php\n"
eindtag= "?>\n"
volgnummer = "$volgnummer=" + str(teller) + ";\n"
commentaar = "//commentaar" + commentaarinfo + "\n"
currenttime = (strftime("%Y-%m-%d %H:%M:%S", gmtime()))
timestamp= "$timestamp=\"" + currenttime + "\";\n"
tijd = "$refreshtime=" +str(wacht) + ";\n"
anal1 = "$anal1=" + str(getadcreading(adc_address1, adc_channel1)) + ";\n"
anal2 = "$anal2=" + str(getadcreading(adc_address1, adc_channel2)) +";\n"
anal3 = "$anal3=" + str(getadcreading(adc_address1, adc_channel3)) +";\n"
anal4 = "$anal4=" + str(getadcreading(adc_address1, adc_channel4)) +";\n"
anal5 = "$anal5=" + str(getadcreading(adc_address2, adc_channel1)) + ";\n"
anal6 = "$anal6=" + str(getadcreading(adc_address2, adc_channel2)) + ";\n"
anal7 = "$anal7=" + str(getadcreading(adc_address2, adc_channel3)) + ";\n"
anal8 = "$anal8=" + str(getadcreading(adc_address2, adc_channel4)) +";\n"
teller = teller +1
correctiefactor = "$correctiefactor=" +str(100/getadcreading(adc_address2, adc_channel1))+";\n"
#puurtext = anal1 + anal2 + anal3 + anal4 + anal5 + anal6 + anal7 + anal8 + volgnummer + tijd
print(str(teller))
totaalphp = begintag +correctiefactor +anal1 + anal2 + anal3 + anal4 + anal5 + anal6 + anal7 + anal8 + volgnummer +tijd+ timestamp + commentaar + eindtag
print(totaalphp)
# puurtext = anal1 + anal2 + anal3 + anal4 + anal5 + anal6 + anal7 + anal8 + volgnummer
## print("1: %f" % getadcreading(adc_address1, adc_channel1))
## print("2: %f" % getadcreading(adc_address1, adc_channel2))
## print("3: %f" % getadcreading(adc_address1, adc_channel3))
## print("4: %f" % getadcreading(adc_address1, adc_channel4))
##
## print("5: %f" % getadcreading(adc_address2, adc_channel1))
## print("6: %f" % getadcreading(adc_address2, adc_channel2))
## print("7: %f" % getadcreading(adc_address2, adc_channel3))
## print("8: %f" % getadcreading(adc_address2, adc_channel4))
file = open("/var/www/analoog.inc", "w")
file.write(totaalphp + "\n" )
# file.write(puurtext + "\n" + "txt made in raspi" )
file.close()
## file = open("/var/www/analoog.txt", "w")
## file.write(puurtext + "\n" + "txt made in raspi" )
## file.close()
time.sleep(wacht)
php bestand die gemaakt word
Code:
<?php
$correctiefactor=1.1336583153837434;
$anal1=0.0;
$anal2=0.0;
$anal3=0.0;
$anal4=0.0;
$anal5=88.22;
$anal6=3.64;
$anal7=59.24;
$anal8=38.19;
$volgnummer=0;
$refreshtime=300;
$timestamp="2013-11-02 20:26:29";
//commentaarpython rekent uit van 5v naar 100%, parameters allemaal anal1tot8 , round op 2decimalen , 4.4v komt er uit bboxadsl gemeten met voltmeter
?>
htmlcode op een client in browser
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>autorefresh 7sec html5</title>
<script>
window.setTimeout(function(){ document.location.reload(true); }, 7000);
//alert("g");
</script>
</head>
<body >
1.1583458820804
<a href="cssbars.php" title="css">cssbars</a><br />
<a href="html5.php" title="css">html5</a><br />
<a href="http://192.168.1.3/html5.php" title="css">html5 raspi</a><br />
<a href="http://192.168.1.3/analoog.inc" title="css">192.168.1.3/analoog.inc</a><br />
<a href="http://192.168.1.3/priezen.html" title="css">bediening via wan lukt het niet ! ge zou er op duwen gulder!</a><br />
HTML5code ( niet in android , ...<br>
<progress value=" 0" max="100"></progress> testmeter <br>
<hr>
<progress value=" 0" max="100"></progress> potmeter : 0 <br>
<progress value=" 0" max="100"></progress> potmeter : 0 <br>
<progress value=" 0" max="100"></progress> potmeter : 0 <br>
<progress value=" 0" max="100"></progress> potmeter : 0 <br>
<progress value=" 86" max="100"></progress> potmeter : 4.32 <br>
<progress value=" 1" max="100"></progress> ldr : 1.12 <br>
<progress value=" 100" max="100"></progress> regenput : 100.11 <br>
<progress value=" 43" max="100"></progress> potmeter : 43.29 <br>
The meter tag is supported in Firefox, Opera, Chrome, and Safari 6.<br>
<meter value="0" min="0" low="35" max="100"></meter>0 vrij temp<br>
<meter value="0" min="0" low="35" max="100"></meter>0 vrij vat1000liter<br>
<meter value="0" min="0" low="35" max="100"></meter>0 vrij TEMP<br>
<meter value="0" min="0" low="35" max="100"></meter>0 vrij AMP<br>
<meter value="86" min="0" low="35" max="100"></meter>4.32 V effectief<br>
<meter value="1" min="0" low="35" max="100"></meter>1.12 ldr<br>
<meter value="100" min="0" low="35" max="100"></meter>100.11 regenput<br>
<meter value="43" min="0" low="35" max="100"></meter>43.29 potmeter<br>
live update , every 1200 ; alive = 64<br>
update van 2013-11-05 19:19:57<br>
correctiefactor spanningsverlies voeding raspi , in theorie Vcc = 5V , praktijk = 0.2 V effectief<br>
correctiefactor word berekend door raspi via .inc = 5/4.32 = 1.1574074074074<br>
gecorigeerde voedingspanning = 5.0011583458821<br>
tijd uit raspi zonder winter/zomeruur gelet = 2013-11-05 19:19:57<br>
</body>
</html>
php die bovenstaande genereerd
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>autorefresh 7sec html5</title>
<script>
window.setTimeout(function(){ document.location.reload(true); }, 7000);
//alert("g");
</script>
</head>
<body >
<?php
include('analoog.inc');//file gemaakt door raspberry pi door pythoncode die analoog over i2c omzet naar analoog.inc
include('labels.inc');//file gemaakt door lokaal php ofwel manueel instellen
//ijken spanningsverlies en weerstandtolerantie spanningsdeling van 10v naar5v
$meetdespanningopdeultrasoonsensor = 7.760;
$meetdespanningopdeanalogeingang5 = 2.999;
$x=$meetdespanningopdeultrasoonsensor;
$y=$meetdespanningopdeanalogeingang5;
$spanningsverlies = ($x*10)/($y*20);//werkelijkheid in pct / gemeten analoog in pct
echo $correctiefactor ;//berekend door raspi hardcoded via anal5- //verwacht rond de 1.13
$gecorrigeerdevoeding = $anal5/20*$correctiefactor ;//vrij
$anal1 = round($anal1 * $correctiefactor,2) ;//
$anal2 = round($anal2 * $correctiefactor,2) ;
$anal3 = round($anal3 * $correctiefactor,2) ;
$anal4 = round($anal4 * $correctiefactor,2) ;//
$anal5 = round($anal5/20 , 2);//werkelijke spanning is ook referentiewarop correctie word berekent
$anal6 = round($anal6 * $correctiefactor,2) ;//ldr
$anal7 = round($anal7 * $spanningsverlies ,2) ;//60.01adcregenput 7.76op sensor = 2.99 na spanningsdeling
$anal8 = round($anal8 * $correctiefactor ,2);//potmeter als tester
//echo $timestamp;//geen rekening met winter/zomeruur uit raspi
?>
<a href="cssbars.php" title="css">cssbars</a><br />
<a href="html5.php" title="css">html5</a><br />
<a href="http://192.168.1.3/html5.php" title="css">html5 raspi</a><br />
<a href="http://192.168.1.3/analoog.inc" title="css">192.168.1.3/analoog.inc</a><br />
<a href="http://192.168.1.3/priezen.html" title="css">bediening via wan lukt het niet ! ge zou er op duwen gulder!</a><br />
HTML5code ( niet in android , ...<br>
<progress value=" <?php echo round($potmeter100) ;?>" max="100"></progress> testmeter <br>
<hr>
<progress value=" <?php echo round($anal1) ;?>" max="100"></progress> potmeter : <?php echo ($anal1) ;?> <br>
<progress value=" <?php echo round($anal2) ;?>" max="100"></progress> potmeter : <?php echo ($anal2) ;?> <br>
<progress value=" <?php echo round($anal3) ;?>" max="100"></progress> potmeter : <?php echo ($anal3) ;?> <br>
<progress value=" <?php echo round($anal4) ;?>" max="100"></progress> potmeter : <?php echo ($anal4) ;?> <br>
<progress value=" <?php echo round($anal5*20) ;?>" max="100"></progress> potmeter : <?php echo ($anal5) ;?> <br>
<progress value=" <?php echo round($anal6) ;?>" max="100"></progress> ldr : <?php echo ($anal6) ;?> <br>
<progress value=" <?php echo round($anal7) ;?>" max="100"></progress> regenput : <?php echo ($anal7) ;?> <br>
<progress value=" <?php echo round($anal8) ;?>" max="100"></progress> potmeter : <?php echo ($anal8) ;?> <br>
The meter tag is supported in Firefox, Opera, Chrome, and Safari 6.<br>
<meter value="<?php echo round($anal1) ;?>" min="0" low="35" max="100"></meter><?php echo ($anal1) ;?> <?php echo ($label1) ;?><br>
<meter value="<?php echo round($anal2) ;?>" min="0" low="35" max="100"></meter><?php echo ($anal2) ;?> <?php echo ($label2) ;?><br>
<meter value="<?php echo round($anal3) ;?>" min="0" low="35" max="100"></meter><?php echo ($anal3) ;?> <?php echo ($label3) ;?><br>
<meter value="<?php echo round($anal4) ;?>" min="0" low="35" max="100"></meter><?php echo ($anal4) ;?> <?php echo ($label4) ;?><br>
<meter value="<?php echo round($anal5*20) ;?>" min="0" low="35" max="100"></meter><?php echo ($anal5) ;?> <?php echo ($label5) ;?><br>
<meter value="<?php echo round($anal6) ;?>" min="0" low="35" max="100"></meter><?php echo ($anal6) ;?> <?php echo ($label6) ;?><br>
<meter value="<?php echo round($anal7) ;?>" min="0" low="35" max="100"></meter><?php echo ($anal7) ;?> <?php echo ($label7) ;?><br>
<meter value="<?php echo round($anal8) ;?>" min="0" low="35" max="100"></meter><?php echo ($anal8) ;?> <?php echo ($label8) ;?><br>
live update , every <?php echo $refreshtime ;?> ; alive = <?php echo $volgnummer ;?><br>
update van <?php echo $timestamp ;?><br>
correctiefactor spanningsverlies voeding raspi , in theorie Vcc = 5V , praktijk = <?php echo round($anal5)/20 ;?> <?php echo ($label5) ;?><br>
correctiefactor word berekend door raspi via .inc = 5/<?php echo ($anal5) ;?> = <?php echo 5/$anal5 ;?><br>
gecorigeerde voedingspanning = <?php echo $gecorrigeerdevoeding ;?><br>
tijd uit raspi zonder winter/zomeruur gelet = <?php echo $timestamp ;?><br>
</body>
</html>