Gregory O. Voronin
Created September 3, 2019 © GPL3+

M2M for M&M's!

Earn some cryptocandy with Iota and a bit of sweat equity.

IntermediateFull instructions provided6 hours23
M2M for M&M's!

Things used in this project

Hardware components

Raspberry Pi 3 Model B
Raspberry Pi 3 Model B
×1
Raspberry Pi Zero Wireless
Raspberry Pi Zero Wireless
×1
pi-topCeed
×1
Adafruit VCNL4010 Proximity/Light sensor
×1
Servo Module (Generic)
×1
LED (generic)
LED (generic)
×4
Resistor 221 ohm
Resistor 221 ohm
×4
Breadboard (generic)
Breadboard (generic)
×1
Rechargeable Battery, Lithium Ion
Rechargeable Battery, Lithium Ion
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

IOTA Tangle
IOTA Tangle
IOTA Trinty Wallet

Hand tools and fabrication machines

https://www.lulzbot.com/store/printers/lulzbot-mini

Story

Read more

Custom parts and enclosures

Body 1-6 Are Components of the Candy Dispenser

Body 1-6 Are Components of the Candy Dispenser

Loosely inspired by:
Candy dispenser :) by iPadNanito
Published on October 16, 2013
www.thingiverse.com/thing:166380

Body 1-6 Are Components of the Candy Dispenser

Body 1-6 Are Components of the Candy Dispenser

Body 1-6 Are Components of the Candy Dispenser

Body 1-6 Are Components of the Candy Dispenser

Schematics

Machine 01 - Activity Counter

Counts pedal revolutions by proximity of pedal to VCNL4010 proximity sensor; Power provided through USB port connection.

Machine 02 - Candy Dispenser

The pi-topCEED blocks direct access to pinouts on the Rasp Pi B+ board. You can purchase a piProto board or just plug pins into the pi-top Hub, using this guide to pin footprint/identity:
https://github.com/DesignSparkrs/pi-top-resources/blob/master/pi-Top-hub-pinout.md

Code

machine01v02.py

Python
The code running on the activity counter
# ### Hackster.io Contest: Machine Money: Empowering Devices with Wallets
# ### with IOTA
# ### machine01(actvity counter)

# iota libraries
from iota import Iota
from iota import ProposedTransaction
from iota import Address
from iota import Tag
from iota import TryteString

# RPI Interface libraries
import board
import busio
import digitalio
import adafruit_vcnl4010

# Python utility libraries
import time

# RPI Interface libraries
import board
import busio
import digitalio
import adafruit_vcnl4010

# Python utility libraries
import time

# iota variables

# seed of this device, MACHINE01(counter)
seed = ''
# Phase 01, permanent address of MACHINE02(dispenser)
machine02Address = 'KJJCIEYNUUDPOWRCIZTTXXEPFBNMIMUCAFRNRLWHOIXYGALBUVWQHEDGNLMBV9B9VAYIJEYPZJTNURWTD'


api = Iota('https://nodes.devnet.thetangle.org:443', seed)


# initiaization code
redLED = digitalio.DigitalInOut(board.D23)
redLED.direction = digitalio.Direction.OUTPUT

greenLED = digitalio.DigitalInOut(board.D24)
greenLED.direction = digitalio.Direction.OUTPUT

i2c = busio.I2C(board.SCL, board.SDA)
sensor = adafruit_vcnl4010.VCNL4010(i2c)

waveCount = 0
redLED.value = False
greenLED.value = True

while True:
   print('Proximity: {0} Activity Count: {1}'.format(sensor.proximity,waveCount$
   time.sleep(0.30)

   if (sensor.proximity > 6000): waveCount = waveCount + 1

   if (waveCount>9):
      greenLED.value = False

      redLED.value = True
      
      tx = ProposedTransaction(
           address=Address(machine02Address),
           message=TryteString.from_unicode('3i for some M&Ms!'),
           tag=Tag('MANDM'),
           value=3
      )
        

      tx = api.prepare_transfer(transfers=[tx])
      result = api.send_trytes(tx['trytes'],depth=3, min_weight_magnitude=9)

      waveCount = 0
      time.sleep(1.5)

      print('Transaction sent to the tangle!')
      print('https://devnet.thetangle.org/address/%s' % machine02Address)

     else:
        greenLED.value = True
        redLED.value = False

machine02v04.py

Python
This is the code that runs on Machine 2, the Candy Dispenser.
# ### Hackster.io Contest: Machine Money: Empowering Devices with Wallets
# ### with IOTA
# ### machine02(dispenser)

# iota libraries
from iota import Iota
from iota import ProposedTransaction
from iota import Address
from iota import Tag
from iota import TryteString

# QR related libraries
import pyqrcode

# RPi Interface libraries
import RPi.GPIO as GPIO
import board
import busio
import digitalio

# Python utility libraries
import os
import time
import pprint


# ## LED Setup
redLED = digitalio.DigitalInOut(board.D18)
redLED.direction = digitalio.Direction.OUTPUT
redLED.value = False

greenLED = digitalio.DigitalInOut(board.D21)
greenLED.direction = digitalio.Direction.OUTPUT
greenLED.value = False


# ## Servo Setup
GPIO.setup(12, GPIO.OUT)
p = GPIO.PWM(12,50)
p.start(7.5)

# ## iota Setup
print("Setting up iota now...")
seed = ''
api = Iota('https://nodes.devnet.thetangle.org:443',seed)
# addresses = api.get_new_addresses(index=0, count=1, security_level=2,checksum=True)
# address = addresses['addresses'][0]
# addrStr = str(address[0:90])
addrStr = 'KJJCIEYNUUDPOWRCIZTTXXEPFBNMIMUCAFRNRLWHOIXYGALBUVWQHEDGNLMBV9B9VAYIJEYPZJTNURWTD'
chkSumStr = 'KJJCIEYNUUDPOWRCIZTTXXEPFBNMIMUCAFRNRLWHOIXYGALBUVWQHEDGNLMBV9B9VAYIJEYPZJTNURWTDKPFZRXRPX'
 

print("Begin, checking initial iota balance")

currentBalance = 0
oldBalance = api.get_account_data()['balance']

print("address: ", addrStr)
print("starting balance: ", oldBalance)
CANDYCOST = 3
time.sleep(3.5)

# ## Functions

def printMenu():
   print()
   print(30*'-','CANDY DISPENSER', 32*'-')
   print(79*'-')
   print(' 1. Get Candy')
   print(' 2. Cheat :)')
   print(' 3. Accept Trinty Tokens')
   print(' 4. Exit')
   print(79*'-')

def dispense():
   time.sleep(1.5)
   p.ChangeDutyCycle(7.5)
   time.sleep(1.0)
   p.ChangeDutyCycle(2.5)
   time.sleep(1.0)

def checkBalance():
   global oldBalance
   global CANDYCOST
   global redLED
   global greenLED
   currentBalance = api.get_account_data()['balance']
   if ((currentBalance-oldBalance) >= CANDYCOST):
      print("You earned some candy!")
      redLED.value = False
      greenLED.value = True
      oldBalance = currentBalance - 3
      dispense()
      time.sleep(3.0)
      greenLED.value = False
      redLED.value = True
   else:
     print("Sorry, not enough iota, exercise some more!")
     time.sleep(3.0)

def acceptTokens():
   qr = pyqrcode.create(chkSumStr)
   qr.png('./qr.png',scale=7)
   os.system('fim ./qr.png')

def leave():
   os.system('clear')   
   exit()

while True:
   os.system('clear')
   printMenu()
   choice = int(input('Enter your choice: '))

   if choice == 1:
      print('1 selected')
      checkBalance()
   elif choice == 2:
      print('2 selected')
      dispense()
   elif choice == 3:
      print('3 selected')
      acceptTokens()
   elif choice == 4:
      leave()

Credits

Gregory O. Voronin

Gregory O. Voronin

12 projects • 90 followers
biomedical researcher - hope & cures through research; making in my spare time - don't hesitate to reach out!
Thanks to joypixels.

Comments