Skip to content
Miloš Viktorović
  • Home
  • Coaching
    • Coaching
    • Introduction to Agile
    • Practical (Remote) Agile
  • Posts & Publications
    • Publications
    • Flying
    • Blog
  • Projects
    • Research Project
    • Student IoT kit
    • CODES
  • About
  • Contact

[CODE] iot_simulator.py

## ##########################################################
## @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')
  • [CODE] GrovePi_plus-Example.py
  • [CODE] iot_simulator.py
  • [CODE] GrovePi_plus-startup
  • [CODE] mqtt_and_csv.py
  • [CODE] mqtt_messenger_example.py
  • [CODE] test_IoT.py

Recent Posts

  • Schnitzel flight! EHEH – EDRK
    Schnitzel flight! EHEH – EDRK
    03-Sep-2021
  • Private Pilot License – My journey to PPL!
    Private Pilot License – My journey to PPL!
    22-Apr-2021
  • Solo Cross-country Navigation flight (Big Triangle) EHEH ⇒ EHMZ ⇒ EHSE ⇒ EHEH
    Solo Cross-country Navigation flight (Big Triangle) EHEH ⇒ EHMZ ⇒ EHSE ⇒ EHEH
    13-Dec-2020
  • Spin & acrobatics on Super Decathlon
    Spin & acrobatics on Super Decathlon
    19-Oct-2020
  • Flight log EHEH-EHSE (via Rotterdam)
    Flight log EHEH-EHSE (via Rotterdam)
    19-Oct-2020

Recent Posts

  • Schnitzel flight! EHEH – EDRK
  • Private Pilot License – My journey to PPL!
  • Solo Cross-country Navigation flight (Big Triangle) EHEH ⇒ EHMZ ⇒ EHSE ⇒ EHEH

Archives

  • September 2021
  • April 2021
  • December 2020
  • October 2020
  • August 2020
  • June 2020
  • May 2020
  • January 2020
  • May 2019
Milos Viktorovic © 2025
By Posterity Theme
  • Home
  • Coaching
    • Coaching
    • Introduction to Agile
    • Practical (Remote) Agile
  • Posts & Publications
    • Publications
    • Flying
    • Blog
  • Projects
    • Research Project
    • Student IoT kit
    • CODES
  • About
  • Contact