def ligarCafeteira(self): if (self.cafeteiraLigada == True): print('--- Cafeteira já está ligada.') else: self.cafeteiraLigada = True GPIO.output(2, GPIO.HIGH) print('--- Cafeteira ligada.')
async def handler(self): while 1: if not self.cmd.empty(): cmd = self.get_new_cmd() if str(cmd) == "ENGINESTART": GPIO.output(GPIOVALVES["lox_dump"]["pin"], GPIO.LOW) self.update_states(GPIOVALVES["lox_dump"]["pin"], 0) self.logger.info("DUMPING LOX") sleep(GPIOVALVES["engine_start_delay"]["time_ms"]) GPIO.output(GPIOVALVES["kerosene_dump"]["pin"], GPIO.LOW) self.update_states(GPIOVALVES["kerosene_dump"]["pin"], 0) self.logger.info("DUMPING KEROSENE") GPIO.output(GPIOVALVES["igniter"]["pin"], GPIO.HIGH) elif str(cmd) == "ENGINESTOP": GPIO.output(GPIOVALVES["lox_dump"]["pin"], GPIO.HIGH) self.update_states(GPIOVALVES["lox_dump"]["pin"], 1) self.logger.info("STOPPING LOX") GPIO.output(GPIOVALVES["kerosene_dump"]["pin"], GPIO.HIGH) self.update_states(GPIOVALVES["kerosene_dump"]["pin"], 1) self.logger.info("STOPPING KEROSENE") else: #general non-mission critical gpio commands self.execute(cmd) else: await asyncio.sleep(0.1)
def MedirAgua(cafeteira): print('--- [SENSOR] Medindo quantidade de água.') GPIO.output(17, GPIO.HIGH) tempoTotal = cafeteira.TempoSensorAgua() time.sleep(tempoTotal) GPIO.output(17, GPIO.LOW) distancia = (tempoTotal * VEL_SOM) / 2 return distancia
def MedirCafe(cafeteira): print('--- [SENSOR] Medindo quantidade de pó de café.') GPIO.output(22, GPIO.HIGH) tempoTotal = cafeteira.TempoSensorCafe() time.sleep(tempoTotal) GPIO.output(22, GPIO.LOW) distancia = (tempoTotal * VEL_SOM) / 2 return distancia
def vent(self, valve, time_ms: float): GPIO.output(valve, GPIO.LOW) super().states[f"pin{valve}"] = 0 self.logger.info(f"VENTING {valve}, time:{time_ms}ms") sleep(time_ms / 1000) GPIO.output(valve, GPIO.HIGH) self.logger.info(f"FINISHED VENTING {valve}") super().states[f"pin{valve}"] = 1
def fazerCafe(self): if (self.cafeteiraPronta == True): self.porcentAgua -= 10 self.porcentCafe -= 15 time.sleep(3) print('--- [CAFETEIRA] Café pronto.') GPIO.output(4, GPIO.HIGH) self.cafeteiraPronta = False GPIO.output(3, GPIO.LOW)
def execute(self, cmd: CMD): newStates = cmd.get_new_states() pins = cmd.get_pins() GPIO.output( pins, list([GPIO.LOW if i == 0 else GPIO.HIGH for i in newStates])) for pin_states in self.states: for idx, pin in enumerate(pins): if pin_states == str(pin): self.states[pin_states] = newStates[idx]
def verify_password(): waiting = input() #waiting "s" key if waiting == "s": file = open('password.txt', 'r') password = file.readline().strip() safe_password = file.readline().strip() change_password = file.readline().strip() file.close() print(password, safe_password, change_password) a = input("Insert password:"******"c": return if a == password: gpio.output(17, gpio.HIGH) time.sleep(1) gpio.output(17, gpio.LOW) print("Password") data_exchange.msg('user1', password, pass_type="Password") return if a == safe_password: gpio.output(17, gpio.HIGH) time.sleep(1) gpio.output(17, gpio.LOW) print("Safe_Password") data_exchange.msg('user1', safe_password, pass_type="Safe_password") return if a == change_password: new_password() print("Change_Password") return else: print("Wrong password")
def desligarCafeteira(self): if (self.cafeteiraLigada == True): self.cafeteiraLigada = False GPIO.output(2, GPIO.LOW) GPIO.output(3, GPIO.LOW) GPIO.output(4, GPIO.LOW) print('--- Cafeteira desligada.')
def checarCafeteiraPronta(self): GPIO.output(4, GPIO.LOW) distSensorAgua = MedirAgua(self) distSensorCafe = MedirCafe(self) print('--- {}% de água disponível.'.format(self.porcentAgua)) print('--- {}% de pó de café disponível.'.format(self.porcentCafe)) if (distSensorAgua > 0.154 or distSensorCafe > 0.145): self.cafeteiraPronta = False GPIO.output(3, GPIO.LOW) if (distSensorAgua > 0.154): print('--- [CAFETEIRA] Pausa no processo - água insuficiente.') if (distSensorCafe > 0.145): print( '--- [CAFETEIRA] Pausa no processo - pó de café insuficiente.' ) else: self.cafeteiraPronta = True GPIO.output(3, GPIO.HIGH) print('--- [CAFETEIRA] Cafeteira pronta para preparar o café.')
else: #real hardware import RPi.GPIO as GPIO global CONFIGURATION global GPIOCFG global GPIOVALVES global GPIOPRESSURE global GPIOLOADCELL global DUMP CONFIGURATION = load_config()["rpi"] GPIOCFG = CONFIGURATION["GPIO"] GPIOVALVES = GPIOCFG["valves"] GPIOPRESSURE = GPIOCFG["pressure"] GPIOLOADCELL = CONFIGURATION["loadcell"] GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) DUMP = pickle.dumps #initialize gpio to either GPIO.HIGH (valves) # the NO side of the 3-way solendoids defaults to a closed # position when given a GPIO.HIGH voltage. Changing, # the RPI gpio pin to low will signal the NC side # of the 3-way solendoid valve to it's NC position # opening the fluid valve for system in GPIOVALVES.keys(): print(system) GPIO.setup(int(GPIOVALVES[system]["pin"]), GPIO.OUT) GPIO.output(int(GPIOVALVES[system]["pin"]), GPIO.HIGH)
def control(pin, action): #Actuator configuration if pin == 'fourteen': actuator = 14 if pin == 'fifteen': actuator = 15 if pin == 'eighteen': actuator = 18 if pin == 'twentythree': actuator = 23 if pin == 'twentyfour': actuator = 24 if pin == 'twentyfive': actuator = 25 if pin == 'eight': actuator = 8 if pin == 'seven': actuator = 7 if pin == 'twelve': actuator = 12 if pin == 'sixteen': actuator = 16 if pin == 'twenty': actuator = 20 if pin == 'twentyone': actuator = 21 if pin == 'two': actuator = 2 if pin == 'three': actuator = 3 if pin == 'four': actuator = 4 if pin == 'seventeen': actuator = 17 if pin == 'twentyseven': actuator = 27 if pin == 'twentytwo': actuator = 22 if pin == 'ten': actuator = 10 if pin == 'nine': actuator = 9 if pin == 'eleven': actuator = 11 if pin == 'five': actuator = 5 if pin == 'six': actuator = 6 if pin == 'thirteen': actuator = 13 if pin == 'nineteen': actuator = 19 if pin == 'twentysix': actuator = 26 #Control interface if action == 'on': config = 1 if action == 'off': config = 0 tx_hash = contract_instance.functions.control(actuator, config).transact( {'from': w3.eth.accounts[0]}) print('Transaction submitted:', tx_hash.hex()) y = format(contract_instance.functions.pinStatus(actuator).call()) print('Pin Status Changed: ', y) if y == "1": GPIO.output(actuator, GPIO.HIGH) else: GPIO.output(actuator, GPIO.LOW) return render_template('index.html')
# * SPDX-License-Identifier: MIT # * Licensed to @Author: Salman Dabbakuti(https://github.com/Salmandabbakuti) # * See the License for the specific language governing permissions and limitations under the License. import json from web3 import Web3, HTTPProvider # import RPi.GPIO as GPIO # for real rasp-pi from RPiSim.GPIO import GPIO from flask import Flask, render_template GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) pinList = [ 14, 15, 18, 23, 24, 25, 8, 7, 12, 16, 20, 21, 2, 3, 4, 17, 27, 22, 10, 9, 11, 5, 6, 13, 19, 26 ] for i in pinList: GPIO.setup(i, GPIO.OUT) # compile your smart contract with truffle first truffleFile = json.load(open('./build/contracts/homeAutomation.json')) abi = truffleFile['abi'] bytecode = truffleFile['bytecode'] # web3.py instance w3 = Web3(HTTPProvider("http://localhost:8545/")) # Instantiate and deploy contract contract = w3.eth.contract(abi=abi, bytecode=bytecode) # Get transaction hash from deployed contract tx_hash = contract.constructor().transact({
environment = PC_OFFLINE if environment == PC_OFFLINE: bt = SAMPLING_BASE_TIME.total_seconds() sleep_time = bt lag_correction = 0.0 full_connect = False from RPiSim.GPIO import GPIO elif environment == RPI_PRODUCTION: bt = SAMPLING_BASE_TIME.total_seconds() sleep_time = bt lag_correction = 0.0 full_connect = False GPIO.setmode(GPIO.BCM) KEY_ON = True KEY_OFF = False LED_ON = GPIO.HIGH LED_OFF = GPIO.LOW GPIO.setup(3, GPIO.IN, initial=GPIO.HIGH, pull_up_down=GPIO.PUD_UP) # this is the key sense input GPIO.setup(5, GPIO.OUT, initial=GPIO.HIGH, pull_up_down=GPIO.PUD_DOWN) # this the power latch enable output logging_led_status = GPIO.LOW GPIO.setup( 7, GPIO.OUT, initial=logging_led_status, pull_up_down=GPIO.PUD_DOWN) # this is the Datalogging running blinking led GPIO.setup(9, GPIO.OUT, initial=GPIO.LOW, pull_up_down=GPIO.PUD_DOWN) # this is the power state led output
class Cafeteira: # pino 2 - out - cafeteira ligada? GPIO.setup(2, GPIO.OUT, initial=GPIO.LOW) # pino 3 - out - cafeteira apta? GPIO.setup(3, GPIO.OUT, initial=GPIO.LOW) # pino 4 - out - café pronto? GPIO.setup(4, GPIO.OUT, initial=GPIO.LOW) def __init__(self, porcentAgua, porcentCafe): self.cafeteiraLigada = False self.cafeteiraPronta = False self.porcentAgua = porcentAgua self.porcentCafe = porcentCafe def ligarCafeteira(self): if (self.cafeteiraLigada == True): print('--- Cafeteira já está ligada.') else: self.cafeteiraLigada = True GPIO.output(2, GPIO.HIGH) print('--- Cafeteira ligada.') def desligarCafeteira(self): if (self.cafeteiraLigada == True): self.cafeteiraLigada = False GPIO.output(2, GPIO.LOW) GPIO.output(3, GPIO.LOW) GPIO.output(4, GPIO.LOW) print('--- Cafeteira desligada.') def TempoSensorAgua(self): return 0.001 * (100 - self.porcentAgua) / 100 def TempoSensorCafe(self): return 0.001 * (100 - self.porcentCafe) / 100 def checarCafeteiraPronta(self): GPIO.output(4, GPIO.LOW) distSensorAgua = MedirAgua(self) distSensorCafe = MedirCafe(self) print('--- {}% de água disponível.'.format(self.porcentAgua)) print('--- {}% de pó de café disponível.'.format(self.porcentCafe)) if (distSensorAgua > 0.154 or distSensorCafe > 0.145): self.cafeteiraPronta = False GPIO.output(3, GPIO.LOW) if (distSensorAgua > 0.154): print('--- [CAFETEIRA] Pausa no processo - água insuficiente.') if (distSensorCafe > 0.145): print( '--- [CAFETEIRA] Pausa no processo - pó de café insuficiente.' ) else: self.cafeteiraPronta = True GPIO.output(3, GPIO.HIGH) print('--- [CAFETEIRA] Cafeteira pronta para preparar o café.') def adicionarAgua(self, qtd): self.porcentAgua += qtd def adicionarCafe(self, qtd): self.porcentCafe += qtd def fazerCafe(self): if (self.cafeteiraPronta == True): self.porcentAgua -= 10 self.porcentCafe -= 15 time.sleep(3) print('--- [CAFETEIRA] Café pronto.') GPIO.output(4, GPIO.HIGH) self.cafeteiraPronta = False GPIO.output(3, GPIO.LOW)
def SensorAgua(): # pinos 17 e 18 - sensor de distância - água GPIO.setup(17, GPIO.OUT, initial=GPIO.LOW) # TRIGGER GPIO.setup(18, GPIO.IN) # ECHO
def SensorCafe(): # pinos 22 e 23 - sensor de distância - pó de café GPIO.setup(22, GPIO.OUT, initial=GPIO.LOW) # TRIGGER GPIO.setup(23, GPIO.IN) # ECHO
import time import data_exchange import os if os.name == 'nt': from RPiSim.GPIO import GPIO as gpio else: import RPi.GPIO as gpio #setting GPIO gpio.setwarnings(False) gpio.setmode(gpio.BCM) gpio.setup(17, gpio.OUT) #reading passwords file = open('password.txt', 'r') password = file.readline().strip() safe_password = file.readline().strip() change_password = file.readline().strip() #print(password,safe_password,change_password) #define new password def new_password(): new_pass = input("Insert new password") file = open('password.txt', 'r') password = file.readline().strip() safe_password = file.readline().strip() change_password = file.readline().strip() file.close() #writing new password
def control(pin, action): #Actuator configuration if pin == 'fourteen': actuator = 14 if pin == 'fifteen': actuator = 15 if pin == 'eighteen': actuator = 18 if pin == 'twentythree': actuator = 23 if pin == 'twentyfour': actuator = 24 if pin == 'twentyfive': actuator = 25 if pin == 'eight': actuator = 8 if pin == 'seven': actuator = 7 if pin == 'twelve': actuator = 12 if pin == 'sixteen': actuator = 16 if pin == 'twenty': actuator = 20 if pin == 'twentyone': actuator = 21 if pin == 'two': actuator = 2 if pin == 'three': actuator = 3 if pin == 'four': actuator = 4 if pin == 'seventeen': actuator = 17 if pin == 'twentyseven': actuator = 27 if pin == 'twentytwo': actuator = 22 if pin == 'ten': actuator = 10 if pin == 'nine': actuator = 9 if pin == 'eleven': actuator = 11 if pin == 'five': actuator = 5 if pin == 'six': actuator = 6 if pin == 'thirteen': actuator = 13 if pin == 'nineteen': actuator = 19 if pin == 'twentysix': actuator = 26 #Control interface if action == 'on': config = 1 if action == 'off': config = 0 tx = contract_instance.functions.control( actuator, config).buildTransaction({ 'nonce': w3.eth.get_transaction_count(account_address), 'gas': 3000000, 'gasPrice': Web3.toWei('2000', 'gwei') }) signed_tx = w3.eth.account.sign_transaction(tx, private_key=key) tx_hash = w3.eth.send_raw_transaction(signed_tx.rawTransaction) w3.eth.wait_for_transaction_receipt(tx_hash) print('Transaction submitted:', tx_hash.hex()) y = format(contract_instance.functions.pinStatus(actuator).call()) print('Pin Status Changed: ', y) if y == "1": GPIO.output(actuator, GPIO.HIGH) else: GPIO.output(actuator, GPIO.LOW) return render_template('index.html')
from RPiSim.GPIO import GPIO GPIO.setmode(GPIO.BCM) GPIO.setup(3, GPIO.IN, initial=GPIO.HIGH, pull_up_down=GPIO.PUD_DOWN) GPIO.setup(5, GPIO.IN, initial=GPIO.LOW, pull_up_down=GPIO.PUD_DOWN)
from RPiSim.GPIO import GPIO import time import traceback import random GPIO.setmode(GPIO.BCM) VEL_SOM = 340 # em m/s # Sobre a cafeteira, definiu-se: # Recipiente cheio -> distância superfície sensor: 0m # Recipiente vazio -> distância superfície sensor: 0.17m # porcentagem de água necessária para preparo de 1 café -> 10% => equivale a distância superfície sensor = 0.153m # porcentagem de pó necessário para preparo de 1 café -> 15% => equivale a distância superfície sensor = 0.1445m class Cafeteira: # pino 2 - out - cafeteira ligada? GPIO.setup(2, GPIO.OUT, initial=GPIO.LOW) # pino 3 - out - cafeteira apta? GPIO.setup(3, GPIO.OUT, initial=GPIO.LOW) # pino 4 - out - café pronto? GPIO.setup(4, GPIO.OUT, initial=GPIO.LOW) def __init__(self, porcentAgua, porcentCafe): self.cafeteiraLigada = False self.cafeteiraPronta = False self.porcentAgua = porcentAgua self.porcentCafe = porcentCafe