import machine
from machine import Pin, ADC, SoftI2C
from time import sleep
import ssd1306
import network
import binascii
def notify(text, x = 0, y = 0, wait = 0, clear = False):
if clear:
oled.fill(0)
oled.show()
oled.text(text, x, y)
oled.show()
sleep(wait)
def beep(duration = 100, pieces = 1, long = False):
for i in range(pieces):
buzzer.on()
sleep(duration/2000)
buzzer.off()
sleep(duration/2000)
def read_temp(register):
data = i2c.readfrom_mem(MLX90614_I2C_ADDR, register, 3)
temp = (data[1] << 8) | data[0]
temp = (temp * 0.02) - 273.15 # Convert from Kelvin to Celsius
return temp
def convert(x, in_min, in_max, out_min, out_max):
return (x - in_min) * (out_max - out_min) // (in_max - in_min) + out_min
def readBattLevel():
meas = []
summ = 0
for i in range(20):
meas.append( batt.read_uv() / 500000)
for i in meas:
summ += i
average = summ / 20
print("Batt Voltage: {:.2f}V".format(average))
battPercent = convert( average, 3.3, 4.25, 0, 100)
return [average, battPercent]
def configNetwork(ssid = "", key = ""):
# create station interface
wlan = network.WLAN(network.STA_IF)
wlan.active(True) # activate the interface
sleep(2)
wifiMevcut = False
while(not wifiMevcut):
networks = wlan.scan() # scan for access points
for i in networks:
wifiAdi = i[0].decode("utf-8")
if wifiAdi == ssid:
wifiMevcut = True
sleep(2)
if wifiMevcut:
print("Baðlanýlýyor")
state = wlan.isconnected() # check if the station is connected to an AP
if state == False:
print("{} agina baglaniliyor.".format(ssid))
wlan.connect(ssid, key) # connect to an AP
sleep(3)
else:
print("Wifi Bekleniyor.")
mac = binascii.hexlify(wlan.config('mac')) # get the interface's MAC address
conf = wlan.ifconfig() # get the interface's IP/netmask/gw/DNS addresses
ip, x, gateway, y = conf
return [mac, ip, gateway]
buzzer = Pin(3, Pin.OUT)
key1 = Pin(5, Pin.IN)
key2 = Pin(10, Pin.IN)
key3 = Pin(9, Pin.IN)
key4 = Pin(8, Pin.IN)
MLX90614_I2C_ADDR = 0x5A
MLX90614_TA = 0x06 # Ambient temperature register
MLX90614_TOBJ1 = 0x07 # Object temperature register
i2c = SoftI2C(sda=Pin(6), scl=Pin(7), freq=100000)
oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
ssid = "******" # Wifi ağ adı ile değiştirin
key = "******" # Wifi şifresi ile değiştirin
print("Wifi Bağlantısı deneniyor.")
notify("Wifi Connect", clear = True)
mac, ip, gateway = configNetwork(ssid, key)
print("Wifi Bağlantısı yapıldı.")
print("MAC:{}, ip:{}, Gateway: {}".format(mac, ip, gateway))
notify("Wifi Cnnctd to:", clear= True)
notify("SSID:{}".format(ssid), x= 0, y= 15)
notify("Ip:{}".format(ip), x= 0, y= 30)
notify("GW:{}".format(gateway), x= 0, y= 45, wait = 4)
beep(duration = 500)
batt = ADC(Pin(2))
batt.atten(ADC.ATTN_11DB)
print("batt analog:{}".format(batt.read_u16()))
print("batt uV:{}".format(batt.read_uv()/500000))
buzzer.on()
sleep(1)
buzzer.off()
try:
print("* MLX90614 Temperature Readings *")
while(True):
key1Val = key1.value()
key2Val = key2.value()
key3Val = key3.value()
key4Val = key4.value()
#print("key1:{}, key2:{}, key3:{}, key4:{}".format(key1Val, key2Val, key3Val, key4Val))
if (key4Val == 0):
object_temp = read_temp(MLX90614_TOBJ1)
ambient_temp = read_temp(MLX90614_TA)
notify("Amb Temp:{}".format(ambient_temp), clear= True)
notify("Obj Temp:{}".format(object_temp), x= 0, y= 15)
print("obj:{:>5.3f}°C, amb:{:>5.3f}°C ".format( object_temp, ambient_temp))
sleep(1)
if (key1Val == 0):
battV, battPerc = readBattLevel()
print("batt V:{}".format(battV))
print("batt %:{}".format(battPerc))
notify("Batt V: {}".format(battV), clear= True)
notify("batt %: {}".format(battPerc), x= 0, y= 15)
sleep(1)
sleep(1/20)
except KeyboardInterrupt:
# Gracefully handle script termination by the user
print('\nScript stopped by user')
finally:
# Any cleanup can be done here
print('Goodbye!')