import piplates.TINKERplate as TINK import time for i in range(100): #Repeat this loop 100 times TINK.clrLED(0,0) #LED off time.sleep(0.05) #sleep for 50msec TINK.setLED(0,0) #LED on time.sleep(0.05) #sleep for 50msec print ("I'm all blinked out!")
def ledChange(): #Callback function for on board LED if(ledCntl.value=='ON'): TINK.setLED(0,0) else: TINK.clrLED(0,0)
def main(): # log start up message logging.info("***************************************************************") logging.info("Data Collector has started") logging.info("Running %s", __file__) logging.info("Working directory is %s", os.getcwd()) logging.info("SQLITE Database file is %s", dbfilename); localipaddress = "IP: Unknown" try: hostname = socket.gethostname() externalip = get('https://api.ipify.org').text s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.connect(('8.8.8.8', 1)) # connect() for UDP doesn't send packets localipaddress = s.getsockname()[0] logging.info("Hostname is %s", hostname) logging.info("Local IP is %s and external IP is %s", localipaddress, externalip) except Exception as e: logging.exception("Exception occurred") logging.error("Unable to get network information") # close any open db connections # create connection to our database mydb = createConnection(dbfilename) if mydb is not None: # create table createTable(mydb) mydb.commit() # insert some values lastRowId = countRows(mydb) logging.info("Data points in table: %d", lastRowId) rowcount = lastRowId # create a temperature service instance temperatureService = None try: temperatureService = TemperatureService() except Error as e: logging.error("Unable to create temperature service") # create a voltage service instance voltageService = None try: voltageService = ADCService() except Exception as e: logging.error("Unable to create ADC service") # try to access TinkerPlate tinkerplate = None try: tinkerplate = tink tinkerplate.setDEFAULTS(0) logging.info("TinkerPlate found") except Exception as e: logging.error("Unable to get Tinker Plate") tinkerplate = None # counter for measurement iterations iteration = 1 # keep running until ctrl+C while True: # increase iteration count iteration = iteration + 1 # toggle LED to indicate action if tinkerplate != None: try: tink.setLED(0, 0) except Exception as e: pass # read temperature values dataSetIndex = 0 if (temperatureService != None): tempsString = "" temperatureService.readSensors() now = datetime.datetime.now() nowDateTime = str(now) nowDate = now.strftime("%Y-%m-%d") nowTime = now.strftime("%H:%M:%S") try: values = temperatureService.getValues() sensorId = 1 for value in values: row = (lastRowId + 1, sensorId, nowDate, nowTime, nowDateTime, value) lastRowId = insertRow(mydb, row) rowcount = rowcount + 1 tempsString = tempsString + str(value) + " " sensorId = sensorId + 1 except Exception as e: logging.error("Unable to read values and to add data to database") logging.info("Try to recreate DB file") mydb.close() # create table mydb = createConnection(dbfilename) createTable(mydb) mydb.commit() # get the last row id lastRowId = countRows(mydb) logging.info("Data points in table: %d", lastRowId) rowcount = lastRowId # toggle LED to indicate action if tinkerplate != None: try: tink.clrLED(0, 0) except Exception as e: pass # readvoltage values now = datetime.datetime.now() nowDateTime = str(now) nowDate = now.strftime("%Y-%m-%d") nowTime = now.strftime("%H:%M:%S") values = [] try: if (voltageService != None): values = voltageService.getValues() for value in values: row = (lastRowId + 1, sensorId, nowDate, nowTime, nowDateTime, value) lastRowId = insertRow(mydb, row) sensorId = sensorId + 1 rowcount = rowcount + 1 except Exception as e: logging.error("Unable to read voltage") # read voltage values from TinkerPlate try: if (tinkerplate != None): channelid = 1 #voltagefactors = [1220 / 220, 1220 / 220, 1333, 1] voltagefactors = [1220 / 220, 1220 / 220, 1, 1] values = tinkerplate.getADCall(0) for value in values: row = (lastRowId + 1, sensorId, nowDate, nowTime, nowDateTime, value * voltagefactors[channelid - 1]) lastRowId = insertRow(mydb, row) sensorId = sensorId + 1 rowcount = rowcount + 1 channelid = channelid + 1 except Exception as e: logging.error("Unable to read from TinkerPlate") try: # commit the DB write mydb.commit() except Exception as e: logging.exception("Exception occurred while trying to commit to DB") time.sleep(15) mydb.close() if einkDisplay != None: einkDisplay.turnOff() logging.info("Data Collector main loop has terminated, database is closed")