Difference between revisions of "Internet of Things Lighting python"

From csn
Jump to navigation Jump to search
Line 47: Line 47:
 
*Can you modify the program to see if you can print the wind speed?  
 
*Can you modify the program to see if you can print the wind speed?  
  
== Measuring the CPU temperature ==
+
== 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 print out how warm it is base on the information we got.
  
cat /sys/class/thermal/thermal_zone2/temp
+
<pre>
 +
import requests, json
 +
APIkey = 'b0aa5d74a76a853c83a1210b27236f3c'
 +
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())
  
Write down the CPU temperature at idle time.
+
print('allData: + response')
 +
print('current:' + str(response['main']['temp']))
 +
print('Temp Max:' + str(response['main']['temp_max']))
  
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
+
temp = int(response['main']['temp'])
 
 
<pre>
 
#!/bin/bash
 
  
temp=$(cat /sys/class/thermal/thermal_zone2/temp)
+
if temp > 30:
echo $temp
+
  print('hot')
 +
elif temp > 24:
 +
  print('warm')
 +
else:
 +
  print('cool')
 
</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 ==
 
== Generating CPU load ==

Revision as of 03:28, 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, 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.

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


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 will continue with python requests and work with response data.

import requests, json
APIkey = 'b0aa5d74a76a853c83a1210b27236f3c'
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: + 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?

python IF statements

We are going to print out how warm it is base on the information we got.

import requests, json
APIkey = 'b0aa5d74a76a853c83a1210b27236f3c'
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: + 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

Generating CPU load

Open a second terminal window, while your previous code is running and paste in the following command to generate some load on your CPU

stress --cpu 8

How hot does your CPU get? Hit ctrl+c to close this window.

If statements

Computer programs can take different actions/paths based on circumstances. Run the following code:

#!/bin/bash

temp=$(cat /sys/class/thermal/thermal_zone2/temp)
n=1000
temp=$((temp/n))
echo $temp

if [ $temp -gt 60 ]
then
 echo "Its really hot"
elif [ $temp -gt 50 ]
then
 echo "Its sorta hot"
elif [ $temp -gt 40 ]
then
 echo "Its coolish "
elif [ $temp -gt 30 ]
then
 echo "Its cool"
else
 echo "Dunno"
fi

You can change/test the temperature of the CPU using the load command used earlier.

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 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?