def test_is_connected_is_false(): ''' Tests is connected. Make sure counterfit is running until you see a message telling you to close it ''' CounterFitConnection.init('127.0.0.1', 5000) print("Please close counterfit") time.sleep(10) assert not CounterFitConnection.is_connected()
def test_camera_image(): ''' Tests reading an image from a camera sensor. The image is saved locally ''' CounterFitConnection.init('127.0.0.1', 5000) image_data = CounterFitConnection.read_binary_sensor('Picamera') with open('test_image.png', 'wb') as image_file: image_file.write(image_data.read())
def read(self, retries=15): ''' Read the humidity and temperature from the sensor ''' humidity = CounterFitConnection.get_sensor_float_value( self.__humidity_pin) temperature = CounterFitConnection.get_sensor_float_value( self.__temperature_pin) return humidity, temperature
def wait_ready(self): ''' return None -- Error status False -- Timeout True -- Ready ''' return CounterFitConnection.is_connected()
def read(self, channel): ''' Read the ratio between channel input voltage and power voltage (most time it's 3.3V). Args: channel (int): 0 - 7, specify the channel to read Returns: (int): the ratio, in 0.1% ''' return CounterFitConnection.get_sensor_int_value(channel)
def capture(self, output, image_format=None): ''' Capture an image from the camera, storing it in *output*. ''' # read the image from Counterfit raw_image = CounterFitConnection.read_binary_sensor('Picamera') raw_image.seek(0) image = Image.open(raw_image) if self.__rotation > 0: image = image.rotate(self.__rotation, expand=True) if self.__width > 0 and self.__height > 0: image = self.__resize_and_crop(image) image.save(output, format=image_format)
import time from counterfit_connection import CounterFitConnection from counterfit_shims_grove.grove_light_sensor_v1_2 import GroveLightSensor from counterfit_shims_grove.grove_led import GroveLed CounterFitConnection.init('127.0.0.1', 5000) light_sensor = GroveLightSensor(1) led = GroveLed(2) while True: sensor_value = light_sensor.light print(f'Light sensor reading: {sensor_value}') if sensor_value > 200: print('Turning LED off') led.off() else: print('Turning LED on') led.on() time.sleep(2)
def init_counterfit_device(): ''' Test fixture to initialise the connection to the CounterFit Virtual IoT device running on localhost on port 5000 ''' CounterFitConnection.init('127.0.0.1', 5000)
def off(self) -> None: ''' light off the led ''' CounterFitConnection.set_actuator_boolean_value(self.__pin, False)
def on(self) -> None: ''' light on the led ''' CounterFitConnection.set_actuator_boolean_value(self.__pin, True)
# In this example: # We will create a circuit where, when a button is pressed, we will check the current temperature # If the recorded temperature is under 38 celcius, the relay will be activated # PIN 1: Button # PIN 2: Temperature sensor # PIN 3: Relay import time from counterfit_connection import CounterFitConnection from counterfit_shims_grove.grove_relay import GroveRelay CounterFitConnection.init('127.0.0.1', 5000) relay = GroveRelay(3) while True: button = CounterFitConnection.get_sensor_boolean_value( 1 ) # The button returns boolean values, True for pressed and False for released if button: print("The button has been pressed") temperature = CounterFitConnection.get_sensor_float_value( 2) # Get temperature value print(f"Recorded temperature is {temperature}") if temperature < 38:
def get_distance(self): return CounterFitConnection.get_sensor_int_value(self.__address)
def light(self) -> int: ''' Get the light strength value, maximum value is 1023 ''' return CounterFitConnection.get_sensor_int_value(self.__pin)
def ReadVisible(self): ''' Read the visible light from the sensor ''' return CounterFitConnection.get_sensor_int_value(self.__light_pin)
def readline(self, **kwargs) -> bytes: '''Reads a line from the serial port ''' return CounterFitConnection.read_serial_sensor_line( self.__port).encode('utf-8')
def ReadUV(self): ''' Read the IR light from the sensor ''' return CounterFitConnection.get_sensor_int_value(self.__uv_pin)
def test_is_connected(): ''' Tests is connected. Make sure counterfit is running ''' CounterFitConnection.init('127.0.0.1', 5000) assert CounterFitConnection.is_connected()
def test_init_counterfit_device(): ''' Tests the connection. You should see the CounterFit app status change to connected ''' CounterFitConnection.init('127.0.0.1', 5000)