def task(): global lastT global lastR global lastSW global swMode global GC adata = DAQC.getADCall(3) val = round(DAQC.getADC(0, 8), 1) Power5Text.set(val) val = round((adata[1] - 2.73) * 100, 2) val = 0.2 * val + 0.8 * lastT lastT = val TempText.set(str("{:>3.1f}".format(val)) + 'C') val = round(adata[2] * 12.21 / 1.636, 1) Power12Text.set(val) val = round(adata[0] / 4.096 * 100, 1) FanText.set(str(val) + '%') DAQC.setPWM(0, 0, int(val * 1024 / 100)) val = DAQC.getRANGE(0, 6, 'c') if isinstance(val, float): if val > 100: val = 99.9 else: val = 0 val = round(val, 1) val = 0.8 * lastR + 0.2 * val lastR = val RangeText.set(str("{:>2.1f}".format(val)) + 'cm') lamp = 0 if (DAQC.getDINbit(0, 2) == 1): lamp = 32 sw = DAQC.getSWstate(0) if (sw == 1) and (lastSW == 0): lastSW = 1 swMode = not swMode if swMode: GCmode.set('Binary Code :') else: GCmode.set('Grey Code :') if (sw == 0) and (lastSW == 1): lastSW = 0 val = (int(adata[7] * 32 / 4.096)) if swMode == 0: GCText.set(GC[val]) num = GC[val] else: GCText.set(val) num = val DAQC.setDOUTall(0, (num + lamp)) root.after(100, task)
def send_data(self): # create the sensor data message if self.verbose: #convert to string and keep order by naming keys data = json.dumps( { "00-Board address": self.address, "01-Input Channel": self.channel, "02-Description": self.name, "03-Readout": DAQC.getTEMP(self.address, self.channel, "f"), "04-Unit": "F", "05-Switch status": bool(DAQC.getSWstate(0)), "06-Sensor1 status": bool(MOTOR.getSENSORS(0) & 0x01), "07-Sensor2 status": bool(MOTOR.getSENSORS(0) & 0x02), "08-Sensor3 status": bool(MOTOR.getSENSORS(0) & 0x04), "09-Sensor4 status": bool(MOTOR.getSENSORS(0) & 0x08), "10-Timestamp": time.strftime("%Y-%m-%d %H:%M:%S") }, sort_keys=True) states = json.dumps( { "00-FSM Name": 'test FSM', "01-FSM state": self.fsm.state, "02-Timestamp": time.strftime("%Y-%m-%d %H:%M:%S") }, sort_keys=True) relay_state = json.dumps( { "Relay 1": bool(RELAY.relaySTATE(0) & 0x01), # just a int right now 1,2,4 etc "Relay 2": bool(RELAY.relaySTATE(0) & 0x02), "Relay 3": bool(RELAY.relaySTATE(0) & 0x04), "Relay 4": bool(RELAY.relaySTATE(0) & 0x08), "Relay 5": bool(RELAY.relaySTATE(0) & 0x10), "Relay 6": bool(RELAY.relaySTATE(0) & 0x20), "Relay 7": bool(RELAY.relaySTATE(0) & 0x40), "Relay 8": bool(RELAY.relaySTATE(1) & 0x01), "Relay 9": bool(RELAY.relaySTATE(1) & 0x02), "Relay 10": bool(RELAY.relaySTATE(1) & 0x04), "Relay 11": bool(RELAY.relaySTATE(1) & 0x08), "Relay 12": bool(RELAY.relaySTATE(1) & 0x10), "Relay 13": bool(RELAY.relaySTATE(1) & 0x20), "Relay 14": bool(RELAY.relaySTATE(1) & 0x40) }, sort_keys=True) else: #short version # takes a while to get the sensor reading data = DAQC.getTEMP(self.address, self.channel, "f") states = self.fsm.state #TODO big combersome testing led message ''' dict=json.loads(data) if dict['Switch status']: DAQC.setDOUTbit(0, 0) else : DAQC.clrDOUTbit(0, 0) ''' #publish.single("sensors/data", data, hostname=broker) self.mqttc.publish(hostname + "/sensors/data", data, 0) # publish #report state self.mqttc.publish("sys/state", states, 0) # publish # report relay state self.mqttc.publish(topic=hostname + "/relays/state", payload=relay_state, qos=0, retain=True) # publish