# pir2main.py A70 pir2ver="A70" # import the whole PiR2 software module import pir2A # hardware used ################################### # name pin_name GPIO pin # pinC pinC 18 # TMP102 I2C temperature reader # redLED g_ledR_pin =18 # digOut g_do0_pin =22 # digIn g_di0_pin =23 # pinC g_diC_pin =17 # pushButton g_btn_pin =27 # import the piR2 data module import pir2Data from os import system pir2A.g_isAddingNewLine=True pir2A.g_lastRecId=-1 # room where PiR2 is located pir2A.g_piAreaStr="eShop" #default # SerialNumber of Raspberry Pi processor pir2A.g_piSN_Str="SN" # suppress for now # refresh interval (in seconds) pir2A.g_displayInterval=10 # 10 seconds # Simulate is for non-Raspberry python pir2A.g_isSimulating=False # True for non-Raspberry python systems # False for Raspberry Pi python systems # with a PiR2 controller # write most recent values to Log pir2A.g_nextTopOfHour=0 # at top of each hour pir2A.g_numPrevValuesToDisplay=24 pir2A.g_prevValueArray=[] pir2A.g_prevValueArray.extend("noValue" for _ in range(pir2A.g_numPrevValuesToDisplay)) # now test if this is python version 3 pir2A.g_isPythonVersion3Str="True" #is this python3? (ie version 3) resultStr=pir2A.osCommand("python3 --version") if (resultStr.find("Python 3") == -1): print("ERROR001: Python 3 is needed") print("try typing python3 python/pir2main.py") #exit() pir2A.g_isPythonVersion3Str="False" else: print("This program is running under python3.") pir2A.g_isPythonVersion3Str="True" #end if # now put a looks-like temperature into the "result" file # so that the file is not missing tempStr="11.1" resultStr=pir2A.osCommand("echo "+'"'+tempStr+'"') print(resultStr) print("b4 id g_piAreaStr:"+pir2A.g_piAreaStr+":") print("************************************************************") print("pir2main.py "+pir2ver) print("Kno 1: clear does not work") print("Bug 2: doesn't display log records correctly") print("Vr2 3: input() does not work in pir2A.py") print("Vr2 4: input() needs quotes for the text being input in pir2A.py") print("Bug 5: python3 will not run programs with _ in name") print("Kno 6: python3 will not start if you type merely python") print("Kno 7: calling an imported function needs trailing pair of ()") print(" eg pir2A.displayHeader()") print("Kno 8: cancel python pgm using Ctrl-Z") print("Bug 9: a line feed is missing after the first log record") print("Bug 10: memory card 35 Python3 won't import GPIO correctly") #invoke by # Start Terminal # cd Desktop/PiR2 # python3 python/pir2main.py # or by # sh pir2.sh testStr=pir2A.piR2_findStrInArray(pir2A.g_ioDeviceArray,"|id|0000|") #print("testStr:"+testStr) #other imports from subprocess import call import os from os import system, name from time import sleep import RPi.GPIO as GPIO # identify software name and version pir2A.osCommand("clear") pir2A.identifyTask() # initialize all variables etc ################################################################ # isQuerying=True means "ask the user if verbose info is wanted" # This can be set False to Never ask the user ################################################################ #moved from isLogAvailable() isQuerying=True # ask user who will probably # hit return for non-verbose isVerbose = pir2A.askVerbose(isQuerying) # turn diC_pin off (in case GPIO is currently on) GPIO.setmode(GPIO.BCM) diC_pin=18 GPIO.setup(diC_pin,GPIO.OUT) GPIO.output(diC_pin,GPIO.LOW) logFileName="/home/pi/Desktop/PiR2/files/PiR2_Log.txt" if pir2A.isLogAvailable(isVerbose,logFileName)==False : print("ERROR010--Another instance is running. . . exiting:") exit() else: print("ok to run log. . .") #end if #begin by initializing the log file etc pir2A.initializeTask(isVerbose, pir2A.g_fileArray) #run the first task pir2A.helloTask() # TO DELETE # display the log file pir2A.osCommand("clear") # accept a command from the Owner or 'Log' pir2A.getControl_fromOwner(pir2A.g_ioDeviceArray,pir2A.g_prevValueArray, \ pir2A.g_isAddingNewLine,pir2A.g_fileArray) # getControl_fromOwner will request a command from the # Raspberry Pi user # then it will invoke the command by calling control_fromOwner() # the following section will #pir2.displayHeader #pir2.displayLog() # define the global array of ioDevices #global g_ioDeviceArray # done previously (above) while True: # display the log file #but clear the screen first pir2A.osCommand("clear") # calculate top of next hour import time timeNowFloat=time.time() timeNowStr=str(timeNowFloat) #1559547168.169622 #print("timeNowFloat:",timeNowFloat) timeFormat='%Y-%m-%d %H:%M:%S' now=time.strftime(timeFormat) #print("now:",now) # TO FIX ****************** timeAndDate="2019-06-03 04:00:00" timeStamp=time.mktime(time.strptime(timeAndDate,timeFormat)) #print("timeStamp:",timeStamp,":") #get time in my PiR2 format dateTimeStr=pir2A.sortableDateTime(timeNowStr) # eg 2019FJun03 21:46:11.740 EDT dateStr=dateTimeStr[0:10] #print("dateTimeStr:"+dateTimeStr+":") waitTime=timeStamp-timeNowFloat print("waitTime:",waitTime," seconds") pir2A.displayHeader(dateStr) # acquire the processor Temperature valueStr=pir2A.acquire_procTemp(pir2A.g_isSimulating) unitsStr="" pir2A.appendToLog(pir2A.g_piSN_Str,"A",pir2A.unixTime(),"procTemp", \ valueStr,unitsStr,pir2A.g_ioDeviceArray,pir2A.g_prevValueArray, \ pir2A.g_isAddingNewLine,pir2A.g_fileArray) # acquire the I2C TMP102 temperature tempId00 unitsStr=" C" busA=pir2A.init_I2C_Bus(pir2A.g_tempId00bus) valueStr=pir2A.acquire_tempId00(busA,pir2A.g_tempId00addr, \ unitsStr) pir2A.appendToLog(pir2A.g_piSN_Str,"A",pir2A.unixTime(),"tempId00", \ valueStr,unitsStr,pir2A.g_ioDeviceArray,pir2A.g_prevValueArray, \ pir2A.g_isAddingNewLine,pir2A.g_fileArray) # my first control loop 2019HAug02 value=float(valueStr) #print("tempId00:",value," C") ################################################################## rangePC=3. # control range: 5 means 5 % temp_SetPoint=20.3 # SET.POINT ################################################################## temp_SetPointLow=temp_SetPoint * (100.-rangePC/2)/100. #print("set.low :",temp_SetPointLow) temp_SetPointHigh=temp_SetPoint * (100.+rangePC/2)/100. #print("set.high:",temp_SetPointHigh) #print("tempId00 SetPoint :",temp_SetPoint,"{",temp_SetPointLow,"-", \ #temp_SetPointHigh,"}") isChange=False if value <= temp_SetPointLow : print("set.low :",temp_SetPointLow) print(" below temp_SetPointLow",temp_SetPointLow,"turning on the heat.") onOff="1" isChange=True # end if if value >= temp_SetPointHigh : print("set.high:",temp_SetPointHigh) print(" above temp_SetPointHigh",temp_SetPointHigh,"turning off the heat.") onOff="0" isChange=True # end if if isChange : pir2A.control_digOut0(pir2A.g_do0_pin,onOff) #control the heat # append to log pir2A.appendToLog(pir2A.g_piSN_Str,"C",pir2A.unixTime(),"digOut0", \ onOff,unitsStr,pir2A.g_ioDeviceArray,pir2A.g_prevValueArray, \ pir2A.g_isAddingNewLine,pir2A.g_fileArray) # end if isShowLog=True if isShowLog : pir2A.displayLog() ############################################################### # must add g_piSN_Str here ********************** pir2A.pir2_convertMIC_ControlTask(pir2A.g_ioDeviceArray, \ pir2A.g_prevValueArray,pir2A.g_isAddingNewLine, \ pir2A.g_fileArray) # end if isShowControlPanel=True if isShowControlPanel : print("tempId00 SetPoint :",temp_SetPoint,"{",temp_SetPointLow,"-", \ temp_SetPointHigh,"}") print(" PiR2 CONTROL PANEL") print("HOLDING WITHIN RANGE BY USING") print("[tempId00] SetPoint % digOut0") print(" 21.42") print(" 21.0123 C LO [ 21.0+-4] [+] ON") print(" 20.58") print("SENSING procTemp: 37.9 C") #print("") # end if sleep(pir2A.g_displayInterval) #end while #queue up the first task pir2A.queuehelloTask() # start the task manager pir2A.manageTasks() #/FD42/PiR2/python/mainprogram.py _A70 #/r/PiR2/python/mainprogram.py # end of PiR2 mainprogram software module