import time
import machine
from machine import Pin, adc
from machine import I2C
from lcd_api import LcdApi
from pico_i2c_lcd import I2cLcd
button = Pin(1, Pin.IN, Pin.PULL_DOWN)
# 16 x 2 LCD I2C Sample Code
# Library from https://github.com/T-622/RPI-PICO-I2C-LCD
# See library GitHub repository for full list of functions and their use
I2C_ADDR = 0x27
I2C_NUM_ROWS = 2
I2C_NUM_COLS = 16
sda = machine.Pin(26)
scl = machine.Pin(27) # NOTE: It is important you change this to match the SDA and SCL pins you are using.
i2c_controller = 1 # Also change this to match the controller you are using (Listed on the Raspberry Pi Pico W Pinout as "I2C0" or "I2C1")
# You will need to wire the LCD to your Pi Pico, ensuring that each pin goes to the correct header. The pinout should be written on the LCDs PCB.
# You can use either 5V power via VBUS or 3.3V power via either VSYS or 3V(OUT).
i2c = I2C(i2c_controller, sda=sda, scl=scl, freq=400000)
lcd = I2cLcd(i2c, I2C_ADDR, I2C_NUM_ROWS, I2C_NUM_COLS)
### PUT EVERYTHING UNDERNEATH INTO LOOP ###
testString = "Air quality cityscape: Press the button to choose your desired location."
print("hello")
for i in range(len(testString) - 15):
lcd.putstr(testString[i:i+16])
time.sleep(0.6)
lcd.move_to(0,0)
print('HELLO')
while True:
lcd.clear()
analog_value1 = adc.read_u16()
mapped_lat = int(((analog_value1/65535.0)*180)-90)
lcd.print(f'Latitude: {mapped_lat}')
time.sleep(0.1)
if button.value():
lat = mapped_lat
lcd.print(f'Latitude: {lat}')
break
while True:
analog_value2 = adc.read_u16()
mapped_long = int(((analog_value2/65535.0)*360)-180)
lcd.print(f'Longitude: {mapped_long}')
time.sleep(0.1)
if button.value():
long = mapped_long
lcd.print(f'Longitude: {long}')
break
lcd.clear()
### CALLING API ###
r = urequests.get(f"http://api.openweathermap.org/data/2.5/air_pollution?lat={lat}&lon={lon}&appid=76b1f9c8c838c3c1445e72359b16f798")
print(r.json()) # use to read what the api shoots out
pollutant_concentration = r.json()
### CODING LED LIGHTS ###
pixels = Neopixel(30, 0, 0, "GRB") # 30 leds lit up, (unknown, just leave as 0), led output pin 0
if pollutant_concentration["list"][0]["main"]["aqi"] == 1:
pixels.fill((100, 100, 100)) # white
pixels.show()
if pollutant_concentration["list"][0]["main"]["aqi"] == 2:
pixels.fill((0, 0, 100)) # blue
pixels.show()
if pollutant_concentration["list"][0]["main"]["aqi"] == 3:
pixels.fill((0, 100, 0)) # green
pixels.show()
if pollutant_concentration["list"][0]["main"]["aqi"] == 4:
pixels.fill((70, 50, 0)) # yellow
pixels.show()
if pollutant_concentration["list"][0]["main"]["aqi"] == 5:
pixels.fill((100, 0, 0)) # red
pixels.show()
r.close()