Difference between revisions of "Internet of Things Lighting python"
Line 24: | Line 24: | ||
== Some Basic Python Programming == | == Some Basic Python Programming == | ||
− | The internet of things is all about communicating with other devices and services, this section will | + | The internet of things is all about communicating with other devices and services, this section we will get weather information and work with response data. |
Your instructor will give you your APIKey. | Your instructor will give you your APIKey. |
Revision as of 03:49, 16 March 2021
In this lab, we will learn how to control the lab lighting using a computer. This should reveal some of the amazing possibilities of the IoT. We will learn about this while playing with a little python scripting on Windows.
Contents
Turning the lights on and off
The lab you are sitting in contains network-accessible lighting. Look directly above you and make a note of the FIXTURE NUMBER noted one your light. Note the command provided below.
import requests url = 'http://10.50.41.230/api/override' fixtureNo = FIXTURENUMBER json = {"target": "fixture","num": fixtureNo,"intensity": 255,"red": 255,"green": 0,"blue": 0,"temperature": 255,"fade": 1.0,"path": "Default"} requests.put(url=url, data=json) print("Check your LED!")
Copy the lines above into notepad++, and then change the FIXTURENUMBER to the light found on your computer overhead. save the file to your desktop named IOT-LED.py
open command prompt
cd Desktop python IOT-LED.py
How do you think you could switch the light off? Play with the parameters and ask for help if you need some guidance.
Some Basic Python Programming
The internet of things is all about communicating with other devices and services, this section we will get weather information and work with response data.
Your instructor will give you your APIKey.
import requests, json APIkey = '' CityName = 'Perth' url = 'http://api.openweathermap.org/data/2.5/weather?q=' + CityName +'&appid=' + APIkey + '&units=metric' print(url) response = (requests.get(url=url).json()) print('allData:' + str(response)) print('current:' + str(response['main']['temp'])) print('Temp Max:' + str(response['main']['temp_max']))
Save the file as weather.py
open command prompt
cd Desktop python weather.py
Tasks
- Can you modify the program to get the weather for Melbourne?
- Can you modify the program to get the temp_min?
- Can you modify the program to see if you can print the wind speed?
TIP: remember to save your code before running it
python IF statements
We are going to use if statements to test the data we got back. If the temp is over 30 degrees we will print 'hot', over 24 'warm' and finally 'cool'.
import requests, json APIkey = '' CityName = 'Perth' url = 'http://api.openweathermap.org/data/2.5/weather?q=' + CityName +'&appid=' + APIkey + '&units=metric' print(url) response = (requests.get(url=url).json()) print('allData:' + str(response)) print('current:' + str(response['main']['temp'])) print('Temp Max:' + str(response['main']['temp_max'])) temp = int(response['main']['temp']) if temp > 30: print('hot') elif temp > 24: print('warm') else: print('cool')
Tasks
- Add an if statement to print 'Freezing!' when below 5 degrees
- Test this out by changing city name to Antarctica
Change the LED base on the current temperature
Now we need to add the two scripts together, one to get the weather and another to change the colour, blue for cold, greed for good, red for hot
import requests, json APIkey = '' CityName = 'Antarctica' url = 'http://api.openweathermap.org/data/2.5/weather?q=' + CityName +'&appid=' + APIkey + '&units=metric' print(url) response = (requests.get(url=url).json()) print('allData:' + str(response)) print('current:' + str(response['main']['temp'])) print('Temp Max:' + str(response['main']['temp_max'])) temp = int(response['main']['temp']) fixtureNo = url = 'http://10.50.41.230/api/override' if temp > 30: json = {"target": "fixture","num": fixtureNo,"intensity": 255,"red": 255,"green": 0,"blue": 0,"temperature": 255,"fade": 1.0,"path": "Default"} requests.put(url=url, data=json) elif temp > 24: json = {"target": "fixture","num": fixtureNo,"intensity": 255,"red": 0,"green": 255,"blue": 0,"temperature": 255,"fade": 1.0,"path": "Default"} requests.put(url=url, data=json) else: print('cool')
Challenging Tasks
- Can you modify the script to turn the LED blue if it is cold?
- Can you modify the script to fade for a longer time if it is windy? (Hard)
- Can you modify the script so that if the sky is cloudy have the intensity (brightness) lower? (Hard)