<commit_before><commit_msg>added the freezer monitor<commit_after>

import esp
import dht
import time
import json
import machine
import network
from umqtt.robust import MQTTClient


d = dht.DHT11(machine.Pin(2))
p2 = machine.Pin(4, machine.Pin.PULL_UP)

BLINK_DELAY=0.1
MEASR_DELAY=60

def blink():
    p2.on()
    time.sleep(BLINK_DELAY)
    p2.off()


def get_rssi():
    nets = sta.scan()
    for n in nets:
        if n[0].decode() == settings["network"]:
            return n[3]
    return None

def ctof(t):
    return t * (9 / 5) + 32

f = open("settings.json")
raw_settings = "".join(f.readlines())
f.close()

settings = json.loads(raw_settings)

sta = network.WLAN(network.STA_IF)
sta.active(True)
sta.connect(settings["network"], settings["password"])

#randomNum = int.from_bytes(uos.urandom(3), 'little')
#myMqttClient = bytes("client_"+str(randomNum), 'utf-8')

c = MQTTClient(
    client_id = "client_1",
    server = settings["broker"],
    user = settings["userid"],
    password = settings["mqttapikey"],
    port = 1883
    )

while True:
    print("connecting to wifi")
    if sta.isconnected():
        print("connected!")
        c.connect()
        print("mqtt connected!")
        break
    time.sleep(1)

counter = 0
while True:
    d.measure()
    blink()
    credentials = bytes("channels/{:s}/publish/{:s}".format(settings["channel"], settings["apikey"]), 'utf-8')
    payload = bytes("field1={:.1f}&field2={:d}&field3={:d}&field4={:d}\n".format(ctof(d.temperature()), get_rssi(), esp.freemem(), d.humidity()), 'utf-8')
    c.publish(credentials, payload)
    print("Blinked! Free mem: {}, Count: {}, Temp: {}, RH: {}, RSSI: {}".format(esp.freemem(), counter, ctof(d.temperature()), d.humidity(), get_rssi()))
    counter += 1
    time.sleep(MEASR_DELAY)
{'fraction_non_alphanumeric': 0.18079730488489612, 'fraction_numerical': 0.01909039865244245, 'mean_word_length': 3.842391304347826, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6028717', 'n_tokens_mistral': 665, 'n_tokens_neox': 644, 'n_words': 130}