Difference between revisions of "Yocto Light"

From csn
Jump to navigation Jump to search
 
(One intermediate revision by the same user not shown)
Line 28: Line 28:
  
 
  sudo touch /usr/bin/light_level
 
  sudo touch /usr/bin/light_level
  chmod 777 /usr/bin/light_level
+
  sudo chmod 777 /usr/bin/light_level
 
  sudo vim /usr/bin/light_level
 
  sudo vim /usr/bin/light_level
  
Line 37: Line 37:
 
output=$(sudo YLightSensor any get_currentValue)
 
output=$(sudo YLightSensor any get_currentValue)
  
# Extract the whole number using pattern matching
+
# Extract the whole number using awk
[[ $output =~ [0-9]+ ]] && whole_number=${BASH_REMATCH[0]}
+
whole_number=$(awk -F' ' '{print int($NF)}' <<< "$output")
  
 
# Print the whole number
 
# Print the whole number
echo "Light level: $whole_number"
+
echo "$whole_number"
 
</pre>
 
</pre>

Latest revision as of 01:09, 18 May 2023

Install the GPG key of our server with the following command:

wget -q -O - https://www.yoctopuce.com/apt/KEY.gpg | gpg --dearmor  | sudo tee -a /usr/share/keyrings/yoctopuce.gpg > /dev/null

Add the repo:

echo "deb [signed-by=/usr/share/keyrings/yoctopuce.gpg] https://www.yoctopuce.com/ apt/stable/" | sudo tee /etc/apt/sources.list.d/yoctopuce.list

Install the VirtualHub

Thanks to this repository, it is possible to install the VirtualHub more easily. Indeed you just need execute the following commands:

sudo apt-get update
sudo apt-get install virtualhub

Install the command line library

sudo apt-get update
sudo apt-get install yoctolib-cmdlines

Test

You can now test the module with:

sudo YLightSensor any get_currentValue

Ok, so lets package that up into a bash script:

sudo touch /usr/bin/light_level
sudo chmod 777 /usr/bin/light_level
sudo vim /usr/bin/light_level
#!/bin/bash

# Execute the command and capture the output
output=$(sudo YLightSensor any get_currentValue)

# Extract the whole number using awk
whole_number=$(awk -F' ' '{print int($NF)}' <<< "$output")

# Print the whole number
echo "$whole_number"