def run(my_mqtt_config_yaml): try: thread_opcua = threading.Thread(target=run_opcua) thread_opcua.start() mqtt = Mqtt(my_mqtt_config_yaml) lamp_status_before = "" status_changed = False while True: mqtt.start() previous_sensors = copy.deepcopy(sensors) check_for_blinking() update_sensors_status() for s in sensors: if sensors[s]["status"] is not previous_sensors[s]["status"]: status_changed = True if status_changed: print("in if status_changed", end=" ", flush=True) status_changed = False update_mqtt_message() message_str = json.dumps(message) print("going to send", end=" ", flush=True) mqtt.publish(message_str) print("have sent", end=" ", flush=True) mqtt.disconnect() finally: #close connection, remove subcsriptions, etc opcua_server.server.stop() mqtt.disconnect()
def run(my_mqtt_config_yaml): mqtt = Mqtt(my_mqtt_config_yaml) mqtt.start() sensor_snr = mqtt.get_sensor_snr() time_interval = mqtt.get_time_interval() last_ten_temps = [] while True: datapoint = get_temperature(sensor_snr) if isNaN(datapoint): print("temperature value is NaN") pass else: temp = datapoint if len(last_ten_temps) < 10: last_ten_temps = [temp] * 10 if temp_is_valid(temp, last_ten_temps): msg = {"temperature": temp} msg_out = json.dumps(msg) mqtt.publish(msg_out=msg_out) else: print("last reading was an outlier") last_ten_temps.insert(0, temp) last_ten_temps.pop(10) time.sleep(time_interval)
def run(my_mqtt_config_yaml): mqtt = Mqtt(my_mqtt_config_yaml) mqtt.start() while True: lamp_status = get_lamp_status() mqtt.publish(lamp_status) time.sleep(mqtt.get_time_interval())
def run(my_mqtt_config_yaml): mqtt = Mqtt(my_mqtt_config_yaml) sensor_snr = mqtt.get_sensor_snr() time_interval = mqtt.get_time_interval() mqtt.start() yocto_mV_rx.initialize(sensor_snr) while True: datapoint = yocto_mV_rx.get_millivolts() msg = {"milliVolt": datapoint} msg_out = json.dumps(msg) mqtt.publish(msg_out=msg_out) time.sleep(time_interval)
def run(my_mqtt_config_yaml): mqtt = Mqtt(my_mqtt_config_yaml) time_interval = mqtt.get_time_interval() max_delta_temp_per_sec = 0.1 max_delta_temp_per_interval = time_interval * max_delta_temp_per_sec mqtt_reconnect_counter = 0 while True: if mqtt_reconnect_counter == 10: reconnect_mqtt(mqtt) for sensor in sensors.values(): # check for stable reading by comparing two readings temp = get_temp_reading(sensor) last_ten_temps = sensor["last_ten_temps"] # check whether new reading makes sense by comparing with last ten readings # within range of +/- 3*stdev of last ten readings. If outside this range don't send to TB for now but # add it to the list of last ten readings --> increases stdev for evaluation of next reading if len(last_ten_temps) < 10: last_ten_temps = [temp] * 10 if temp_is_valid(temp, last_ten_temps, max_delta_temp_per_interval): sensor["temp"] = temp else: print("last reading was an outlier") last_ten_temps.insert(0, temp) last_ten_temps.pop(10) sensor["last_ten_temps"] = last_ten_temps msg = {} for sensor in sensors: key = sensor + "_temp" value = sensors[sensor]["temp"] if isNaN(value): value = "" msg.update({key: value}) msg_out = json.dumps(msg) mqtt.publish(msg_out=msg_out) mqtt_reconnect_counter += 1 time.sleep(time_interval)
def run(my_mqtt_config_yaml): mqtt = Mqtt(my_mqtt_config_yaml) mqtt.start() sine_dataflow_enabled = False x_axis_steps = 10 sine_amplitude = 100 sine_displacement = 100 last_ten_temps = [] x_value=0 while True: if sine_dataflow_enabled: datapoint = calculate_sine_datapoint(x_value) x_value += x_axis_steps # check for stable reading by comparing two readings else: first_reading = sensor.readTempC() second_reading = sensor.readTempC() # check whether both readings are numbers if (isNaN(first_reading)) or (isNaN(second_reading)): print("temperature value is NaN") pass else: if abs(first_reading - second_reading) > 2: print("temperature reading is not stable") print("first_reading is {}, and second_reading is {}".format(first_reading, second_reading)) pass # check whether new reading makes sense by comparing with last ten readings # within range of +/- 3*stdev of last ten readings. If outside this range don't send to TB for now but # add it to the list of last ten readings --> increases stdev for evaluation of next reading else: temp = first_reading if len(last_ten_temps) < 10: last_ten_temps = [temp]*10 if temp_is_valid(temp, last_ten_temps): msg = {"temperature": temp} msg_out = json.dumps(msg) mqtt.publish(msg_out = msg_out) else: print("last reading was an outlier") last_ten_temps.insert(0, temp) last_ten_temps.pop(10) time.sleep(mqtt.get_time_interval())
# # You should have received a copy of the GNU Lesser General Public License # along with MFRC522-Python. If not, see <http://www.gnu.org/licenses/>. # import RPi.GPIO as GPIO import MFRC522 import signal import time, json from my_mqtt_module import Mqtt continue_reading = True card_dict = {} message = {"timestamp": "", "payload": ""} my_mqtt = Mqtt("/media/usb/MFRC522-python/rfid_to_mosquitto.yml") my_mqtt.start() mqtt_start_time = time.time() # Capture SIGINT for cleanup when the script is aborted def end_read(signal, frame): global continue_reading print "Ctrl+C captured, ending read." continue_reading = False GPIO.cleanup() # Hook the SIGINT signal.signal(signal.SIGINT, end_read)
#!/home/jan/paho_mqtt_client/venv/bin/python3 __author__ = 'Jan Kempeneers' import time, math, json from my_mqtt_module import Mqtt mqtt1 = Mqtt("my_mqtt_module.yml") mqtt1.start() sine_dataflow_enabled = True x_axis_steps = 10 sine_amplitude = 100 sine_displacement = 100 def calculate_sine_datapoint(x_value): sine_datapoint = int( round(math.sin(math.radians(x_value)) * sine_amplitude)) + sine_displacement return sine_datapoint def run(): x_value = 0 while True: if sine_dataflow_enabled: datapoint = calculate_sine_datapoint(x_value) x_value += x_axis_steps else: datapoint = x_value
def run(tb_demo_yaml, mosquitto_yaml): mqtt_tb_demo = Mqtt(tb_demo_yaml) mqtt_tb_demo.start() mqtt_mosquitto = Mqtt(mosquitto_yaml) mqtt_mosquitto.start() sensor_snr_mV = mqtt_mosquitto.get_yml_item("sensor_snr_mV") sensor_snr_mA = mqtt_mosquitto.get_yml_item("sensor_snr_mA") # current_set_value = mqtt_mosquitto.get_yml_item("current_set_value") current_set_value = get_new_current_set_value() time_interval = mqtt_mosquitto.get_time_interval() yocto_mV_rx.initialize(sensor_snr_mV) loop = yocto_mA_tx.initialize(sensor_snr_mA) while True: yocto_mA_tx.set_current(current_set_value[0], loop) milliamp = float(loop.get_advertisedValue()) time.sleep(0.25) millivolt = yocto_mV_rx.get_millivolts() ohm = round(millivolt / milliamp, 2) temperature = (ohm * 5.3739) - 972 msg = { "milliVolt": millivolt, "milliamp": milliamp, "ohm": ohm, "temperature": temperature } msg_out = json.dumps(msg) mqtt_tb_demo.publish(msg_out=msg_out) mqtt_mosquitto.publish(msg_out=msg_out) current_set_value = get_new_current_set_value(current_set_value[0], current_set_value[1]) time.sleep(time_interval)