Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 8015

MicroPython • Re: Print the contents in Thonny shell

$
0
0
Thanks again for more support. I will look into these recent ways to get the shell output with the formatting. I know there are other ways to get weather, but this is a challenge for me since I am new to micropython, plus I choose what content I want and format to my taste.

The current program is as follows. Note: where ever you see 'your', you need to specify parameters unique to you, e.g., your latitude and longitude.

Code:

import networkimport timeimport urequestswlan=network.WLAN(network.STA_IF)wlan.active(True)wlan.connect('yourssid', 'yourpassword')while wlan.isconnected==False:    print('... Connecting!')print ('Congrats, connected!!')red = '\033[91m'green = '\033[92m'blue = '\033[94m'magenta = '\033[0;35;38m'brightcyan = '\033[0;36;38m'bold = '\033[1m'italics = '\033[3m'underline = '\033[4m'end = '\033[0m'def degrees_to_cardinal(d):    '''    note: this is highly approximate...    '''    dirs = ["N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE",            "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW"]    ix = int((d + 11.25)/22.5 - 0.02)    return dirs[ix % 16]def convert_to_12_hour(hour, minute):      if hour < 12 : xm = "AM"  else       : xm = "PM"  hour = hour % 12  if hour == 0 : return 12, minute, xm  else       : return hour, minute, xm  weather=urequests.get("https://api.openweathermap.org/data/2.5/weather?lat=yourlat&lon=yourlon1&appid=yourid&units=imperial").json()tm=time.localtime(weather['dt']+weather['timezone'])print()print('The current weather for:')print(weather['name']+',' + ' yourstate')print()print('The current Time and Date:')hour = tm[3]minute = tm[4]t = "{:01}:{:02} {}".format(*convert_to_12_hour(hour, minute))print(t+'  '+str(tm[1])+'/'+str(tm[2])+'/'+str(tm[0]))print()print('Current Temp: '+str(weather['main']['temp'])+'ºF')print('Current Humidity: '+str(weather['main']['humidity'])+'%')pressure = weather['main']['pressure']* 0.02952998print('Current Barometric Pressure: '+ ("{:.2f}".format(pressure))+' inches of Mercury')for i in range(len(weather['weather'])):    print('Current Conditions: '+weather['weather'][i]['description'])  weather1=urequests.get("https://api.openweathermap.org/data/2.5/forecast?lat=yourlat&lon=yourlon&appid=9e7cdacc9e231c70980b7b749cfcf582&units=imperial").json()#print(weather1)tm=time.localtime(weather1['list'][0]['dt'])d = weather1['list'][0]['wind']['deg']print('Current Wind Conditions: '+'Wind speed: '+str(weather1['list'][0]['wind']['speed'])+' MPH''  Wind Gust speed: '+str(weather1['list'][0]['wind']['gust'])+' MPH '+ 'Direction: '+degrees_to_cardinal(d))print()sunrisetm=time.localtime(weather['sys']['sunrise']+weather['timezone'])hour = sunrisetm[3]minute = sunrisetm[4]t = "{:01}:{:02} {}".format(*convert_to_12_hour(hour, minute))print('Sunrise is: '+ t)sunsettm=time.localtime(weather['sys']['sunset']+weather['timezone'])hour = sunsettm[3]minute = sunsettm[4]t = "{:01}:{:02} {}".format(*convert_to_12_hour(hour, minute))print('Sunset is: '+ t)print()print('The Forecast for the next 5 days:')print()for i in range(len(weather1['list'])):    hour = int(weather1['list'][i]['dt_txt'][11]+weather1['list'][i]['dt_txt'][12])    minute = int(weather1['list'][i]['dt_txt'][14]+weather1['list'][i]['dt_txt'][15])    t = "{:01}:{:02} {}".format(*convert_to_12_hour(hour, minute))    date = weather1['list'][i]['dt_txt'][5]+weather1['list'][i]['dt_txt'][6]+weather1['list'][i]['dt_txt'][7]+weather1['list'][i]['dt_txt'][8]+weather1['list'][i]['dt_txt'][9]+weather1['list'][i]['dt_txt'][4]+weather1['list'][i]['dt_txt'][0]+weather1['list'][i]['dt_txt'][1]+weather1['list'][i]['dt_txt'][2]+weather1['list'][i]['dt_txt'][3]    print( red + bold +"Forecast Time: " +date+" "+t+end)    print(bold + '\033[1;37;44mForecast Temperature: ' +'\t' + 'Forecast Cloud Conditions: '+ '\t' + 'Forecast Humidity Conditions: '+ '\t' + 'Forecast Pressure Conditions: '+ '\t' + 'Forecast Wind Conditions:\033[0m')    pressure = weather1['list'][i]['main']['pressure']* 0.02952998    d = weather1['list'][i]['wind']['deg']    windspeed = str(weather1['list'][i]['wind']['speed'])        print(bold + italics +str(weather1['list'][i]['main']['temp'])+'ºF' +'\t' +'\t' +'\t' + weather1['list'][i]['weather'][0]['description'] +'\t' +'\t' +'\t'+ str(weather1['list'][i]['main']['humidity']) +'%' +'\t' +'\t' +'\t'+'\t'+("{:.2f}".format(pressure)) +' inches of Mercury'+'\t' +'\t' + windspeed + ' MPH '+ 'Direction '+degrees_to_cardinal(d) +end)    print()print()

Statistics: Posted by ajax12 — Tue Feb 04, 2025 12:51 am



Viewing all articles
Browse latest Browse all 8015

Trending Articles