Using the Raspberry Pi to get weather from the internet
Created a semicircle marked with High/Medium/Low humidity ranges and converted the servo motor into a dial that moves according to the location the user inputs. Made some adjustments to the code so the dial will stay on the high/medium/low instead of resetting to zero at the end of the program (now it resets to zero at the beginning of the program). Below the GIF shows the humidity when I set the location to Hawaii.
Since the code to move the servo motor is lengthy, I created a function to move the servo by simply calling move_servo(num). The num is the amount of degrees the servo will move by.
I decided to try to move the servo based on the humidity percentage outside. If the humidity was less than 50%, the servo would move to 3 degrees (num=3) indicating a low humidity. If the humidity was greater than 50 but less than 70, the servo would move to 7.5 degrees (num=7.5) indicating a normal humidity. If the humidity was greater than 70 but less than 120 (random high number), the servo would move to 11 degrees (num=11) indicating a high humidity.
def move_servo(num): #function to move servo
p = GPIO.PWM(19,50) #19 is the pin
p.start(2.5) #Starting at 0
p.ChangeDutyCycle(num) #Changing degree based on humidity
p = GPIO.PWM(19,50) #After calling p.stop() must put this information again
p.start(num) #Starting at the degree we left off at we will begin to go back to zero
p.ChangeDutyCycle(2.5) #Going back to 0
I learned how to control a servo motor using Raspberry Pi. This servo motor will most likely be used as a dial that will turn depending on the humidity (or temperature) of the weather outside. The servo did initially shake a lot when going to different angles. The solution to reduce this shaking was that after going to a certain angle, stop the servo for half a second, then start it again; below is the code of how I did this.
p = GPIO.PWM(19,50)
First I start the servo motor at a duty cycle of 7.5 which corresponds to 90 degrees. I give it a third of a second to get there before stopping the servo motor for half a second. I then must put which pin I am using again (p = GPIO.PWM(19,50)) because it isn't saved after the servo is stopped. I then start the power again making sure that I am starting it at the same angle that I stopped at. Then I go to duty cycle of 12.5 which is 180 degrees (duty cycle of 2.5 is 0 degrees).
The entire code that can be found on my GitHub page is in the Scratch Pad folder, the file is called LED.py. This code moves the servo motor from 0 to 90 to 180 as can be seen in the attached video.
Create an Open Weather Map account to receive a key. This key will allow your code to access the weather of different locations available on the Open Weather Map website.
(My key is 9276659d9dc88d95bfbd5db39938c052)
To be able to use Open Weather Map, their library must first be downloaded.
Steps to downloading the pyowm library:
Once the library has been downloaded it is time to write the code!
First we must import the library we just downloaded so we type:
Next we insert the key into the code which you either created an account to get or are using mine:
owm = pyowm.OWM('9276659d9dc88d95bfbd5db39938c052')
*Make sure the key is written in between apostrophes
To find the weather at a specific location, we use the weather_at_place function that is in the pyowm library. The city and the country must be written in the format (“City, Country”). The code below is an example of trying to find the weather at the city of Pasadena which is in the United States. If there are cities with the same name, be sure to specify the location. For example, typing in (“Glendale, US”) gives the weather of the Glendale in Arizona state. To get the weather information of the Glendale city that is in California, the city’s direction is specified so it is written as (“North Glendale, US”)
The following code shows the weather in Pasadena, United States.
observation= owm.weather_at_place("Pasadena, US")
Fetch the weather information in the specified location and set the data found to a variable
There are many different types of information we can choose to receive.
We can get the wind speed by adding a function to the variable we are storing the weather information into; you take the variable you stored the weather information into which in my case was w, and add the function after it to get the wind speed which is .get_wind(). You store this information into a variable (I chose to name this variable wind).
To get the temperature in fahrenheit you follow the same steps as above but this time the function to get the temperature is get_temperature() and inside the parentheses specify whether you want the temperature in fahrenheit or celsius.
When you run this function, the maximum and minimum temperature is also printed out. Let’s say you just want the temperature in fahrenheit and nothing else. Create a new variable and put temperature.get('temp'). The .get() function returns to you a value for the given key. So when we put .get('temp'), this will return to us only the value of the temperature.
To print out what we want, we use the print() function. Inside the parenthesis we write the variable name that we want information from.
print(w) prints out the reference time
print(wind) prints the speed and degree of the wind
print(temperature) prints out the temperature as well as the highest and lowest temperatures of the day.
Created a basic circuit to turn an LED on and off. Next week I will try to turn it on and off based on the temperature outside.