Difference between revisions of "Internet of Things Lighting python"

From csn
Jump to navigation Jump to search
 
(21 intermediate revisions by the same user not shown)
Line 1: Line 1:
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, as well as some of the potential for misuse if security is not handled correctly. We will learn about this while playing with a little python scripting on Windows.
+
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.
  
 
== Turning the lights on and off ==
 
== Turning the lights on and off ==
Line 7: Line 7:
 
import requests
 
import requests
 
url = 'http://10.50.41.230/api/override'
 
url = 'http://10.50.41.230/api/override'
fixtureNo = FIXTURENUMBER
+
fixtureNo = #this is the number on the LED Above your desk
 
json = {"target": "fixture","num": fixtureNo,"intensity": 255,"red": 255,"green": 0,"blue": 0,"temperature": 255,"fade": 1.0,"path": "Default"}
 
json = {"target": "fixture","num": fixtureNo,"intensity": 255,"red": 255,"green": 0,"blue": 0,"temperature": 255,"fade": 1.0,"path": "Default"}
 +
#try changing the red, green and blue values and see what happens
 
requests.put(url=url,  data=json)
 
requests.put(url=url,  data=json)
 
print("Check your LED!")
 
print("Check your LED!")
 
</pre>
 
</pre>
Copy the lines above into notepad++, and then change the '''FIXTURENUMBER''' to the light found on your computer overhead.
+
Copy the lines above into notepad++, and then change the '''FixtureNo''' to the light found on your computer overhead.
 
save the file to your desktop named IOT-LED.py
 
save the file to your desktop named IOT-LED.py
  
 +
open command prompt
 +
<pre>
 +
cd Desktop
 +
python IOT-LED.py
 +
</pre>
 +
====Tasks====
 +
*Can you make the LED Purple?
 +
*Can you make the LED Orange?
 +
*Can you turn the LED off?
 +
For help with colors check out https://www.w3schools.com/colors/colors_rgb.asp
 +
 +
TIP: remember to save your code before running it
  
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 ==  
 
== Some Basic Python Programming ==  
  
The internet of things is all about communicating with other devices and services, this section will continue with python requests and work with response data.
+
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.
 
<pre>
 
<pre>
import requests
+
import requests, json
url = 'http://api.openweathermap.org/data/2.5/weather?q={city name}&appid={API key}'
+
APIkey = 'b0aa5d74a76a853c83a1210b27236f3c' #this will be provided by your instructor
response = requests.get(url=url)
+
CityName = 'Perth'
print(response)
+
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']))
 
</pre>
 
</pre>
  
Save the file as hello.py  
+
Save the file as weather.py  
  
 
open command prompt
 
open command prompt
 
<pre>
 
<pre>
 
cd Desktop
 
cd Desktop
python hello.py
+
python weather.py
 
</pre>
 
</pre>
 
Press Ctrl+C to stop the program when you have seen enough hello worlds.
 
  
 
====Tasks====
 
====Tasks====
*Can you modify the program to print ten Hello worlds?
+
*Can you modify the program to get the weather for Melbourne?
*Can you modify the program to see if you can turn the light on and off every second?  
+
*Can you modify the program to get the temp_min?
*After you have done this, can you modify the program to change the colour every second?
+
*Can you modify the program to see if you can print the wind speed?
  
== Measuring the CPU temperature ==
+
a JSON validator may help visualize the data https://jsonformatter.curiousconcept.com/
 +
== python IF statements ==
  
We would like the operation of the fan to turn on to cool the CPU on your mini computer. The following command will show you the current temperature of your CPU
+
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'.
 +
<pre>
 +
import requests, json
 +
APIkey = '' #this will be provided by your instructor
 +
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())
  
cat /sys/class/thermal/thermal_zone2/temp
+
print('allData:' + str(response))
 +
print('current:' + str(response['main']['temp']))
 +
print('Temp Max:' + str(response['main']['temp_max']))
  
Write down the CPU temperature at idle time.
+
temp = int(response['main']['temp'])
  
Read the following code and see if you can work out what is going on. Open a new file, paste the code below and save it as temperature_visualisation.sh
+
if temp > 30:
 
+
  print('hot')
<pre>
+
elif temp > 24:
#!/bin/bash
+
  print('warm')
 
+
else:
temp=$(cat /sys/class/thermal/thermal_zone2/temp)
+
  print('cool')
echo $temp
 
 
</pre>
 
</pre>
  
 
====Tasks====
 
====Tasks====
*Use your knowledge of looping from the previous section to continually check and print the temperature for 2 minutes.
+
*Add an if statement to print 'Freezing!' when below 5 degrees
*See if you can integrate the line below to print a more meaningful temperature:
+
*Test this out by changing city name to Antarctica
n=1000
 
temp=$((temp/n))
 
  
== Generating CPU load ==
+
== Change the LED base on the current temperature ==
  
Open a second terminal window, while your previous code is running and paste in the following command to generate some load on your CPU
+
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
  
stress --cpu 8
 
  
How hot does your CPU get? Hit ctrl+c to close this window.
 
  
== If statements ==
+
<pre>
 +
import requests, json
 +
APIkey = 'b0aa5d74a76a853c83a1210b27236f3c' #this will be provided by your instructor
 +
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())
  
Computer programs can take different actions/paths based on circumstances. Run the following code:
+
print('allData:' + str(response))
 +
print('current:' + str(response['main']['temp']))
 +
print('Temp Max:' + str(response['main']['temp_max']))
  
<pre>
+
temp = int(response['main']['temp'])
#!/bin/bash
+
fixtureNo =  
 
+
url = 'http://10.50.41.230/api/override'
temp=$(cat /sys/class/thermal/thermal_zone2/temp)
 
n=1000
 
temp=$((temp/n))
 
echo $temp
 
  
if [ $temp -gt 60 ]
+
if temp > 30:
then
+
  json = {"target": "fixture","num": fixtureNo,"intensity": 255,"red": 255,"green": 0,"blue": 0,"temperature": 255,"fade": 1.0,"path": "Default"}
  echo "Its really hot"
+
  requests.put(url=url, data=json)
elif [ $temp -gt 50 ]
+
elif temp > 24:
then
+
  json = {"target": "fixture","num": fixtureNo,"intensity": 255,"red": 0,"green": 255,"blue": 0,"temperature": 255,"fade": 1.0,"path": "Default"}
echo "Its sorta hot"
+
  requests.put(url=url, data=json)
elif [ $temp -gt 40 ]
+
else:
then
+
  print('cool')
echo "Its coolish "
 
elif [ $temp -gt 30 ]
 
then
 
  echo "Its cool"
 
else
 
echo "Dunno"
 
fi
 
 
</pre>
 
</pre>
 
You can change/test the temperature of the CPU using the load command used earlier.
 
  
 
==== Challenging Tasks====
 
==== Challenging Tasks====
*Can you modify the code to change to a different colour based on the temperature, you can also play with the fade?
+
*Can you modify the script to turn the LED blue if it is cold?
*Can you then loop over this code block for 10 minutes or 600 seconds, it may be helpful to use the sleep command again and retest the temperature every second?
+
*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)

Latest revision as of 01:16, 7 April 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.

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 = #this is the number on the LED Above your desk
json = {"target": "fixture","num": fixtureNo,"intensity": 255,"red": 255,"green": 0,"blue": 0,"temperature": 255,"fade": 1.0,"path": "Default"}
#try changing the red, green and blue values and see what happens
requests.put(url=url,  data=json)
print("Check your LED!")

Copy the lines above into notepad++, and then change the FixtureNo 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

Tasks

  • Can you make the LED Purple?
  • Can you make the LED Orange?
  • Can you turn the LED off?

For help with colors check out https://www.w3schools.com/colors/colors_rgb.asp

TIP: remember to save your code before running it


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 = 'b0aa5d74a76a853c83a1210b27236f3c' #this will be provided by your instructor
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?

a JSON validator may help visualize the data https://jsonformatter.curiousconcept.com/

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 = '' #this will be provided by your instructor
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 = 'b0aa5d74a76a853c83a1210b27236f3c' #this will be provided by your instructor
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'])
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)