Skip to content

Instantly share code, notes, and snippets.

@hasumikin
Last active February 16, 2026 08:06
Show Gist options
  • Select an option

  • Save hasumikin/5e37162ff3481381a905e81729e02720 to your computer and use it in GitHub Desktop.

Select an option

Save hasumikin/5e37162ff3481381a905e81729e02720 to your computer and use it in GitHub Desktop.

※和文はページ下部にあります

PicoRuby Workshop 2025 - Internet of Things

Previous sections:

  1. Setup + LED: https://tinyurl.com/picoruby-2025
  2. Microphone: https://tinyurl.com/picoruby-mic
  3. Buzzer: https://tinyurl.com/picoruby-buzzer
  4. 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.

WiFi Setup

First, let's connect your R2P2 to WiFi using the nmcli (NetworkManager Command Line Interface) tool.

Step 1: Configure WiFi

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.

Step 1: Confirm Configuration

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.

Step 2: Verify Connection

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.

Making HTTP Requests

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!

Workshop IoT Service

For this workshop, we've created a special IoT data collection service. Let's send some data!

Basic Data Upload

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]}"
end

View Your Data Online

After 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.

Exercises

Now it's time to put everything together! Create your own IoT applications:

  1. 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
  2. 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
  3. 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!

Internet of Things

前のセクション:

  1. Setup + LED: https://tinyurl.com/picoruby-2025
  2. Microphone: https://tinyurl.com/picoruby-mic
  3. Buzzer: https://tinyurl.com/picoruby-buzzer
  4. Useful gems: https://tinyurl.com/picoruby-gems

ワークショップで最もワクワクするパートへようこそ! 今日の R2P2 はインターネットに接続し、マイコンを本当の IoT device に変えます。WiFi を設定し、HTTP リクエストを送り、センサデータをクラウドサービスへ送信します。

WiFi のセットアップ

まず、nmcli(NetworkManager Command Line Interface)ツールを使って、R2P2 を WiFi に接続します。

Step 1: WiFi を設定する

R2P2 シェルで次を実行します。

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

上の例では、my_ssidmy-password を、実際の WiFi ネットワーク名とパスワードに置き換えてください。 PT はポルトガルの国コードです。別の国にいる場合は変更してください。例:日本なら JP、ポーランドなら PL です。

Step 1: 設定を確認する

保存された設定を確認します。

$> 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 はマイコンのユニーク ID から作られるため、wifi.yml を別のデバイスへコピーしても動作しません。

Step 2: 接続を確認する

R2P2 を再起動します。

$> reboot

R2P2 は起動時に自動的に WiFi ネットワークへ接続します。 接続後、オンラインかどうかを確認します。

$> date

成功の目印:

  • ✅ 現在の UTC 時刻が表示されます(例:2025-09-19 13:30:45 0000
  • ❌ エポック時刻が表示されます(例:1970-01-01 00:00:XX 0000) - これはインターネットに接続できていないことを意味します

現在の日時が表示されれば、おめでとうございます! R2P2 がインターネットに接続できています。 表示されない場合は、ネットワーク情報が正しいか確認し、nmcli でもう一度 WiFi 設定を行ってください。

ヒント: Raspberry Pi Pico (2) W は 5GHz ではなく 2.4GHz の WiFi ネットワークにしか接続できません。また、IPv4 が必要です。IPv6 のみのネットワークでは動作しません。

HTTP リクエストを送る

Ruby を使ってインターネット接続を試してみましょう。IRB を起動してください。

irb> client = Net::HTTPSClient.new("catfact.ninja")
irb> client.get "/fact"

ランダムな猫の雑学が入った JSON レスポンスが表示されるはずです。次はパースしてみましょう。

irb> require 'json'
irb> JSON.parse(client.get "/fact")["fact"]

実行するたびに、インターネットから異なる猫の雑学が取得できます!

ワークショップ用 IoT サービス

このワークショップのために、特別な IoT データ収集サービスを用意しました。データを送ってみましょう!

基本のデータアップロード

これを 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]}"
end

オンラインでデータを見る

データの送信に成功したら、次の Web サイトへアクセスして、参加者全員のデータを確認してください。

🌐 http://iot-workshop-website-574134345704.s3-website-eu-west-1.amazonaws.com/

注: この Web サービスは、ワークショップ終了後の任意の時点で停止されます。

演習

ここからは総仕上げです! 自分の IoT アプリケーションを作ってください。

  1. 本格的な IoT データロガー: センサデータを継続的にアップロードする仕組みを作ってください。

    # 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
  2. OLED IoT ダッシュボード: OLED ディスプレイを使って視覚的なフィードバックを追加してください。

    # Show current sensor readings on OLED
    # Display upload status (success/failure)
    # Include timestamp or upload counter
    # Simple bar graphs for sensor values
  3. スマート環境モニタ: 次を満たす仕組みを作ってください。

    • すべてのセンサを継続的に監視する
    • 値が十分に変化したときだけアップロードする(threshold-based)
    • 値が安全上の上限を超えたときにアラートを送る
    • OLED に傾向を表示する(increasing/decreasing arrows)

🎉 おめでとうございます! GPIO、ADC、PWM、I2C、そしてインターネット接続を学び、ゼロから完全なIoTシステムを作り上げました。これによって自らのコネクティックデバイスを作り、Internet of Things に貢献するためのスキルが身に付きました!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment