※和文はページ下部にあります
Previous sections:
- Setup + LED: https://tinyurl.com/picoruby-2025
- Microphone: https://tinyurl.com/picoruby-mic
- Buzzer: https://tinyurl.com/picoruby-buzzer
- Useful gems: https://tinyurl.com/picoruby-gems
Welcome to the most exciting part of our workshop! Today's R2P2 connects to the internet, transforming your microcontroller into a true IoT device. We'll set up WiFi, make HTTP requests, and send sensor data to a cloud service.
First, let's connect your R2P2 to WiFi using the nmcli (NetworkManager Command Line Interface) tool.
In the R2P2 shell, run:
$> nmcli
Ctrd-D to exit
Country Code? [JP] > PT
WiFi SSID? > my_ssid
WiFi Password? (leave blank if no password required) > my-password
Auto Connect? (y/n) [y] > y
Retry if failed? (y/n) [n] > n
Use Watchdog? (y/n) [n] > n
Successfully saved to /etc/network/wifi.yml
In the above, replace my_ssid and my-password with your actual WiFi network name and password.
PT is the country code for Portugal; change it if you're in a different country; eg. JP for Japan, PL for Poland.
See the saved configuration:
$> cat /etc/network/wifi.yml
wifi:
ssid: my_ssid
encoded_password: 4qylnqohymXYgB04TkGG4g==
auto_connect: true
retry_if_failed: false
watchdog: false
country_code: PT
encoded_password is created from the microcontroller's unique ID, so copying wifi.yml to another device won't work.
Let's reboot the R2P2:
$> reboot
R2P2 will automatically connect to the WiFi network on startup. After connecting, check if you're online:
$> date
Success indicators:
- ✅ Shows current UTC time (like
2025-09-19 13:30:45 0000) - ❌ Shows epoch time (like
1970-01-01 00:00:XX 0000) - means no internet
If you see the current date and time, congratulations! Your R2P2 is connected to the internet.
If not, ensure your network details are correct and try configuring WiFi again with nmcli.
Tip: Raspberry Pi Pico (2) W can only connect to 2.4GHz WiFi networks, not 5GHz. Also, IPv4 is required; IPv6-only networks won't work.
Now let's explore internet connectivity using Ruby! Start with IRB:
irb> client = Net::HTTPSClient.new("catfact.ninja")
irb> client.get "/fact"You should see a JSON response with a random cat fact! Let's parse it:
irb> require 'json'
irb> JSON.parse(client.get "/fact")["fact"]Each time you run this, you'll get a different cat fact from the internet!
For this workshop, we've created a special IoT data collection service. Let's send some data!
Save this as simple_upload.rb:
require 'json'
require 'net'
client = Net::HTTPSClient.new('bguykyjs4c.execute-api.eu-west-1.amazonaws.com')
obj = {
name: 'YOUR NAME',
distance: 50.5,
sound: 2000,
temperature: 25.1
}
data = JSON.generate(obj)
headers = {
'Content-Type' => 'application/json',
'Content-Length' => data.length,
'X-Api-Key' => 'euruko2025',
}
res = client.post('/prod/data', headers, data)
if res[:status] == 200
puts "Success!"
else
puts "Failure!: #{res[:body]}"
endAfter successfully sending data, visit this website to see all workshop participants' data:
🌐 http://iot-workshop-website-574134345704.s3-website-eu-west-1.amazonaws.com/
Note: This web service will be discontinued at an arbitrary time after the workshop ends.
Now it's time to put everything together! Create your own IoT applications:
-
Real IoT Data Logger: Create a system that continuously uploads sensor data:
# Combine all sensors (distance, sound, temperature) # Upload to the cloud service every 30 seconds # Include error handling for network issues # Display status messages in console
-
OLED IoT Dashboard: Add visual feedback with the OLED display:
# Show current sensor readings on OLED # Display upload status (success/failure) # Include timestamp or upload counter # Simple bar graphs for sensor values
-
Smart Environmental Monitor: Create a system that:
- Monitors all sensors continuously
- Only uploads when values change significantly (threshold-based)
- Sends alerts when values exceed safety limits
- Displays trends on OLED (increasing/decreasing arrows)
🎉 Congratulations! You've built a complete IoT system from scratch, learning GPIO, ADC, PWM, I2C, and internet connectivity. You now have the skills to create your own connected devices and contribute to the Internet of Things!