Close

Temperature ​meter code added

A project log for IMTAIDKW - ESP12 4-digit display

I made this and I don't know why

aryaArya 07/23/2017 at 23:210 Comments

Our hackerspace had an old Pro Mini-based board with two DS18B20 sensors that used to measure temperature inside and outside our hackerspace's main room, mostly for shits&giggles. Now, I took the IMTAIDKW, attached those sensors to it and wrote some code: 

def run(sleep_time=0.0001, cycle_counter=50000, update_counter=10000, format_counter=10010, trigger_counter=5000):
    #Good luck understanding this lol
    global thermometers, current_thermometer, is_error, digit_bytes
    print("Hello!")
    prev_i = 3
    run_counter = 0
    while True:
        for i, digit_byte in enumerate(digit_bytes):
            isr = machine.disable_irq()
            shiftOut(digit_byte)
            columns[prev_i].on()
            latch.on()
            columns[i].off()
            machine.enable_irq(isr)
            run_counter += 1
            if run_counter >= cycle_counter:
               print("cycling through thermometers")
               current_thermometer += 1
               if current_thermometer >= len(thermometers):
                   current_thermometer = 0
               print("current thermometer: {}".format(current_thermometer))
               run_counter = 0
            elif run_counter % update_counter == 0:
               print("getting temperature")
               try:
                   temperature = ds.read_temp(thermometers[current_thermometer])
               except (OneWireError, IndexError):
                   print("sensor {} failed".format(current_thermometer))
                   is_error = True
               else:
                   temperatures[current_thermometer] = temperature
               if is_error:
                   thermometers = ds.scan()
                   if not thermometers:
                       print("no sensors found!")
                       digit_bytes = [mapping[char] for char in " err"]
                   else:
                       is_error = False
            elif run_counter % format_counter == 0 and not is_error:
               print("formatting temperatures for display")
               temperature = temperatures[current_thermometer]
               if type(temperature) != float:
                   print("wrong temperature {1} for sensor {0}!".format(current_thermometer, temp_str))
               else:
                   temp_str = "{:.1f}".format(temperature)
                   print("sensor {} has temperature {}".format(current_thermometer, temp_str))
                   digit_bytes = generate_digit_bytes(temp_str, thermometers[current_thermometer])
            elif run_counter % trigger_counter == 0:
               print("updating temperatures")
               try:
                   ds.convert_temp()
               except OneWireError:
                   is_error = True
                   thermometers = ds.scan()
                   if not thermometers:
                       print("no sensors found!")
                       digit_bytes = [mapping[char] for char in " err"]
                   else:
                       is_error = False
            sleep(sleep_time)
            prev_i = i

 .. Yeah, good luck reading that. The full code is here: https://github.com/CRImier/IMTAIDKW/blob/master/software/temperature_meter.py

I also attached a light sensor to the analog port, but I don't feel like I can be arsed to write the code that does something useful with it. That's going to be it on IMTAIDKW for now!

I'll add some photos of this setup later - though, frankly, I don't care enough, so it's unlikely I actually will.

Discussions