# pir2main.py A58 pir2ver="A58" #as of 2022CMar06 # added 7 more ioDevices (25 to 31) # import the whole PiR2 software module import pir2A # import the piR2 data module import pir2Data from os import system pir2A.useArray(pir2A.g_ioDeviceArray) #make g_ioDeviceArray[] global # g_ioDeviceArray defines the ioDevice characteristics # g_ioDeviceArray is home(located) in pir2A library module # so those functions don't need to use the pir2A prefix # g_ioDeviceArray is assigned values in pir2Data # g_ioDeviceArray is made global in useArray() in module pir2A.py pir2A.g_isAddingNewLine=True pir2A.g_lastRecId=-1 # room where PiR2 is located pir2A.g_piAreaStr="This Room" #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=32 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 or Ctrl-C?") 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") print("Kno 11: main turns ledR_pin off and controls heat(g_do0_pin)") print("Kno 12: Never save py software using Ctrl-S, creates tab issues") print("Kno 13: First real pi2r board pin #s are correct in v54.") print("Bug 14: wrong IP in log title") print("Bug 15: pi20 not yet fully in use") print("Bug 16: last 5 or 6 ioDevices not yet programmed") print("Bug 17: need log to limit itself to 3 digits after the decimal pt.") print("Bug 18: need log to always report pi20 value as 2nd record.") #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 ################################################################ #temp_SetPoint= # SET.POINT targTempL0= 25.8 # new Set.Pointg_targTempL0=11.1 # set global version #testIt=pir2A.g_ioDeviceArray[24] #print("testIt:"+testIt) #targTempL0 ?????????????? print("targTempL0:",targTempL0) print(pir2A.access_global_list_definition() ) #define the globalAccessList pir2A.set_global("g_targTempL0",targTempL0) # set global pi20 = "EST/180B/lanai" pir2A.set_global("g_pi20",pi20) # set global test_pi20=pir2A.get_global("g_pi20") # get global print("pi20:"+test_pi20) # display pi20 location if False: # these tests worked just fine D@CC on 2022CMar03 print("any :"+pir2A.get_global("g_any") ) #fail print("set :"+"pir2A.g_gALtest ",pir2A.set_global("g_gALtest","newTestValue") ) print("get :"+"pir2A.g_gALtest ",pir2A.get_global("g_gALtest") ) exit ################################################################ # 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) # if any previous program (even this one) # did not run GPIO.cleanup() before exiting # this program will say: RuntimeWarning: This channel is already in use, # continuing anyway. Use GPIO.setwarnings(False) to disable warnings. # Source: gives the solution # https://raspberrypi.stackexchange.com/questions/55143/gpio-warning-channel-already-in-use # turn diC_pin off (in case GPIO is currently on) #GPIO.cleanup() # this doesn't fix it GPIO.setmode(GPIO.BCM) ledR_pin=27 # was 18 print("caution:ledR_pin (27) is the channel that might be in use") GPIO.setwarnings(False) #solution GPIO.setup(ledR_pin,GPIO.OUT) GPIO.setwarnings(True) print ("after GPIO.setup()") GPIO.output(ledR_pin,GPIO.LOW) print ("after using ledR_pin") 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,pir2ver) # 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=4. # control range: 5. means 5 % #targTempL0=19.0004 # SET.POINT #set at top of program ################################################################## Ttype=type(targTempL0) Ttype=str(type(targTempL0)) #print("before convert, Ttype:"+Ttype) #if Ttype=="": if pir2A.isTypeStr(targTempL0): #print("in convert, targTempL0 is str:"+targTempL0+":") # convert str to float # testStr="555.5" # convert string to float if targTempL0.find('.')==-1 : # "." not found targTempL0=int(targTempL0) else: # "." found targTempL0=float(targTempL0) #if end else: #print("targTempL0 is not type ") #print("type:",type(targTempL0) ) continueDoh=1 #if end #print("after convert, targTempL0:",targTempL0) Ttype=type(targTempL0) #Ttype=str(type(targTempL0)) #print("after convert, Ttype:",Ttype) #T=targTempL0 + 0.0 #causes error T=targTempL0 #print("T:",T,"****************************************************") temp_SetPointLow=targTempL0 * (100.-rangePC/2)/100. #print("set.low :",temp_SetPointLow) temp_SetPointHigh=targTempL0 * (100.+rangePC/2)/100. #print("set.high:",temp_SetPointHigh) #print("tempId00 targTempL0:",targTempL0,"{",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 : # TEST HERE ******************************** targTempL0_returned=pir2A.get_targTempL0("targTempL0") targTempL0=targTempL0_returned #targTempL0=value(targTempL0_str) #print("targTempL0_str:"+targTempL0_str) #print("tempId00 SetPointg:",getIt,"{",temp_SetPointLow,"-", \ #temp_SetPointHigh,"}") #print("tempId00 SetPointg:",g_targTempL0,"{",temp_SetPointLow,"-", \ #temp_SetPointHigh,"}") print("tempId00 SetPoint :",targTempL0,"{",temp_SetPointLow,"-", \ temp_SetPointHigh,"}") print(" PiR2 CONTROL PANEL") print(" HOLDING WITHIN RANGE BY USING") print("[tempId00] [targTempL0] % [digOut0]") print(" 21.420") print(" 20.437 C LO [ 21.000+-4] [+] 1:ON") print(" 20.580") print("SENSING [procTemp]: 45.100 C") print(" [displayInterval]: 10.000 s") #print("") # end if #print("A58 g_displayInterval:",pir2A.g_displayInterval) sleep(pir2A.g_displayInterval) #end while #queue up the first task pir2A.queuehelloTask() # start the task manager pir2A.manageTasks() #/FD42/PiR2/python/mainprogram.py _A58 #/r/PiR2/python/mainprogram.py # end of PiR2 mainprogram software module