Skip to main content

Solana DePIN Programs

DePIN Code Example of Solana.

Example Source Code: A DePIN example with Solana for "Selling Air".

Solana IoT

Required Hardware:

  • 1 Raspberry Pi (Pi 5 recommended, Pi 4 works with different GPIO mapping)
  • 1 SD card
  • 1 SD card reader
  • 1 USB-C cable for power
  • 1 NPN transistor (with "AG" marking on back)
  • 1 LED (for testing)
  • 2-3 resistors
  • 1 small motor
  • Several jumper wires/cables
  • 1 fan attachment (optional, for visual effect)

Software:

  • Raspberry Pi OS
  • Node.js (version 18.19)
  • Solana CLI tools

Preparation

  1. Prepare your Raspberry Pi workspace:

    • Insert SD card into reader and connect to your computer
    • Download and install Raspberry Pi OS using the Raspberry Pi Imager
    • During setup, enable SSH and configure Wi-Fi credentials
    • Insert configured SD card into Raspberry Pi and power it on
  2. Season your Raspberry Pi with essential software:

    • SSH into your Raspberry Pi using: ssh username@raspberrypi.local
    • Update package repositories: sudo apt update
    • Install Node.js: sudo apt install nodejs
    • Install npm: sudo apt install npm
    • Install NVM (Node Version Manager): curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
    • Set Node.js version: nvm use 18.19.0

Base Circuit: LED Test

  1. Assemble your basic testing circuit:

    • Connect a white jumper wire from ground pin (3rd from top, right row) to the ground rail on your breadboard
    • Place an LED on the breadboard
    • Connect one side of the LED to ground through a resistor
    • Connect the other side to GPIO 18 (or GPIO 589 if using Raspberry Pi 5)
  2. Create your test program:

    • Navigate to Documents folder: cd Documents
    • Create a Python script: nano LED.py
    • Add this code to turn LED on for 3 seconds:
      import RPi.GPIO as GPIO
      import time
      GPIO.setmode(GPIO.BCM)
      GPIO.setwarnings(False)
      GPIO.setup(18, GPIO.OUT)
      GPIO.output(18, GPIO.HIGH)
      time.sleep(3)
      GPIO.output(18, GPIO.LOW)
    • Save (Ctrl+X, then Y)
    • Run with: sudo python LED.py
  3. Create a JavaScript version:

    • Create a new directory: mkdir led2
    • Navigate to it: cd led2
    • Install the onoff package: npm install onoff
    • Create a JavaScript file: nano blink.js
    • Add code to blink the LED for 5 seconds
    • Run with: sudo node blink.js

Main Course: Motor Circuit

  1. Enhance your circuit for the motor:

    • Remove the LED from your circuit
    • Connect a wire from the 5V power pin to one side of the motor
    • Connect the NPN transistor with a resistor to the GPIO pin
    • Connect the other side of the transistor to the other side of the motor
    • Connect the last pin of the transistor to ground
  2. Test your motor circuit:

    • Run your LED script to check if the motor spins
    • If it spins continuously, you'll need to modify the code

Connecting to Solana Blockchain

  1. Prepare your Solana development environment:

    • Clone the Solana DePIN examples repository
    • Navigate to the LED switch example
  2. Set up the web application:

    • Go to the app folder within the example
    • Run yarn dev to start the local server
    • Use ngrok to make your local server accessible: ngrok http 3000
  3. Deploy script to Raspberry Pi:

    • Connect to Raspberry Pi using VS Code remote SSH
    • Copy the entire raspberry folder from the examples to your Raspberry Pi
    • Navigate to the folder and install dependencies: npm install
    • Make sure you're using Node 18.19: nvm use 18.19.0
    • Run the script: npx tsx src/led.ts
  4. Modify the script for motor control:

    • Edit the led.ts file to turn on the motor for only 5 seconds when triggered
    • Restart the script after changes

Serving

  1. Test your creation:

    • Open the web application showing the QR code
    • Scan the QR code with a Phantom wallet
    • Confirm the transaction
    • Watch as your motor spins for 5 seconds, "selling air"
  2. Garnish with creativity:

    • Add a fan attachment to the motor for visual effect
    • Consider mounting your project in a case with holes for cables
    • Deploy your web app to a service like Vercel for permanent access

Variations

Other projects you could build with this setup:

  • Decentralized Vending Machine: Use a solenoid to unlock items when payment is received
  • Blockchain Door Lock: Create a door that unlocks when the owner of a specific NFT scans the code
  • Geocache with Payment: Hide treasures that can only be accessed after scanning and paying
  • Interactive Livestream: Set up a system where viewers can pay to feed animals on stream
  • Air Quality Sensor: Connect sensors to record environmental data on the blockchain

Context