Introduction: IoT - Portable Mood Reporter

Things we are going to need:

  • Raspberry Pi with Raspbian
  • Touch Sensor from adafruit
  • Power Source (Battery/DC)
  • Ethernet or WiFi for Raspberry Pi
  • Another computer

Step 1: Setting Up Our Raspberry Pi Hardware

First, connect our touch sensor to GPIO pins on our Raspberry Pi. Use some flexible cables

Identify 3 pins on our sensor:

  • GND - connect to ground pin
  • VCC - connct to 5V pin
  • SIG - connect to a signel pin

We'll use the 5V, ground and GPIO 18 on our raspberry pi in this example.

Step 2: Setting Up Our Environment on the Raspberry Pi

Obtain the IP address of our raspberry Pi.

Then connect to our raspberry pi using ssh:

ssh username@ipaddress

then type in our password.

Once we are in, then install apache2 as our webserver by typing in:

sudo apt-get install apache2

Step 3: Design a Piece of Python Code to Read Input From Our Sensor


In our example code, we use a couple of different commands.

To indicate that we're happy, we do 2 long presses.

To indicate that we're sad, we do one short tap, and one long press.

To indicate that we're confused, we do one short tap, wait a beat, then 2 short taps. To indicate that we're happy, we do one short tap, one short gap, and one long press.

To indicate that we're bored, we 3 short taps.

Translating these to string commands:(t for short tap, T for long press, g for short gap, G for long gap)

happy: TgT

sad: tgT

confused: tGtgt

bored: tgtgt

Then we'll have our code output our current mood into a browser-friendly html file for serving.


import RPi.GPIO as GPIO
import time
touch_count = 0
touch_state = 0
touch_duration = 0
gap_duration = 0
current_cmd = ""
def cmd():
	global current_cmd
	if current_cmd[-3:] == "TgT":
		current_cmd = ""
	if current_cmd[-3:] == "tgT":
		current_cmd = ""
	if current_cmd[-5:] == "tGtgt":
		current_cmd = ""
	if current_cmd[-5:] == "tgtgt":
		current_cmd = ""
def mood(mood):
	file = open("index.html","w")
	html = """
Portable Mood Reporter
Hey, my most recent mood is
		if touch_state==0:
			if gap_duration > 2000:
				gap_duration = 0
			if gap_duration > 200:
				current_cmd = current_cmd + "G"
				current_cmd = current_cmd + "g"
			gap_duration = 0
			touch_state = 1
			touch_count = touch_count + 1
		if touch_state==1:
			touch_duration = touch_duration + 1
		if gap_duration < 10000:
			gap_duration = gap_duration + 1
		if touch_state==1:
			touch_state = 0
			if touch_duration > 200:
				current_cmd = current_cmd + "T"
				current_cmd = current_cmd + "t"
			touch_duration = 0

Setup to work with our webserver

type or upload the above python file into our webserver location,

which is defaulted to /var/www/html

cd /var/www/html
sudo nano

Then type in the code above


Step 4: Start Our Server to See It in Action!

cd /var/www/html 
sudo python

Then type in the IP address for our Raspberry Pi, then we should see our mood reporter working!

Try different types of touch commands, and the page should auto-refresh to reflect that!