## ##########################################################
## @title: IoT device simulator
## @author: Milos Viktorovic
## @email: m.viktorovic@tue.nl
## @version: 1.0b [13-11-19]
## ##########################################################
# ______________________________________________________________________________________
# In this example we are simulationg the IoT device with 3 analog ports A0,A1,A2
# To each port we have device attached
# A0 - Temperature Sensor (range 0-50Celsius, coresponding to 0-5VDC)
# A1 - Humidity sensor (range 0-100%, coresponding to 0-5VDC)
# A2 - LED (output 0-5VDC)
# Frequency of the sensor I/O is 5Hz
# ______________________________________________________________________________________
import random
import time
portmode = ['','','']
port = []
#temperature humidity pressure
values = [506,752,0]
lastCall = 0
frequency = 0.2 #200/1000 = 200ms = 5Hz
correct_read = 95 #percent
def pinMode(socket,mode):
global portmode, values, lastCall
if (time.time()-lastCall) > frequency:
lastCall = time.time()
if socket>=0 and socket<=2 and (isinstance(socket,int)):
portmode[socket] = mode
else:
raise IOError('Non-existing socket')
else:
raise BlockingIOError('Blocked socket')
def analogRead(socket):
global portmode, values, lastCall
if (time.time()-lastCall) > frequency:
lastCall = time.time()
if (not isinstance(socket,int)) or socket<0 or socket>2:
raise ValueError('Non-existing socket')
if portmode[socket] == 'INPUT':
val = random.randint(values[socket]-10, values[socket]+10)
if random.randint(0,100) > correct_read:
raise IOError('Non-existing socket')
else:
return val
else:
raise BlockingIOError('Permission wrong for socket')
else:
raise BlockingIOError('Blocked socket')
def analogWrite(socket, value):
global portmode, values, lastCall
if (time.time()-lastCall) > frequency:
lastCall = time.time()
if (not isinstance(socket,int)) or socket<0 or socket>2:
raise ValueError
if (not isinstance(value,int)) or value<0 or value>1023:
if portmode[socket] == 'OUTPUT':
values[socket] = value
if random.randint(0,100) > correct_read:
raise IOError('Non-existing socket')
else:
return True
else:
raise BlockingIOError('Blocked socket')
else:
raise ValueError('Non-existing socket')
else:
raise BlockingIOError('Blocked socket')