def http_get(): send_message(Command.START, 'http_get') url = '{}/{}'.format(API_ENDPOINT, '12') response = requests.get(url=url) response_json = response.json() send_message(Command.STOP, 'http_get')
def query_records(): send_message(Command.START, 'query_record_sqlite3') connection = sqlite3.connect(DB_PATH) cursor = connection.cursor() command = 'select * from Measurements order by ID desc limit 5' cursor.execute(command) rows = cursor.fetchall() connection.close() send_message(Command.STOP, 'query_record_sqlite3')
def insert_record(): timestamp = datetime.datetime.now().strftime('%H:%M:%S.%f') temperature = 25.072 humidity = 56.19 pressure = 998.646 send_message(Command.START, 'save_record_sqlite3') connection = sqlite3.connect(DB_PATH) cursor = connection.cursor() command = 'insert into Measurements (TimeStamp, Temperature, Humidity, Pressure) ' \ + 'values (?, ?, ?, ?)' cursor.execute(command, (timestamp, temperature, humidity, pressure)) connection.commit() connection.close() send_message(Command.STOP, 'save_record_sqlite3')
def http_post(): data = [{ 'TimeStamp': '23:33:14.695226', 'Temperature': 24.893, 'Humidity': 56.576, 'Pressure': 998.634 }, { 'TimeStamp': '23:33:33.805037', 'Temperature': 24.963, 'Humidity': 56.488, 'Pressure': 998.640 }, { 'TimeStamp': '23:34:02.239622', 'Temperature': 25.177, 'Humidity': 55.715, 'Pressure': 998.687 }, { 'TimeStamp': '23:34:54.227033', 'Temperature': 24.939, 'Humidity': 55.843, 'Pressure': 998.686 }, { 'TimeStamp': '23:39:44.063628', 'Temperature': 24.980, 'Humidity': 55.676, 'Pressure': 998.705 }] headers = { 'Content-Type': 'application/json', 'Accept': 'application/json' } send_message(Command.START, 'http_post') try: response = requests.post(url=API_ENDPOINT, json=data, headers=headers) except Exception as e: print(e) send_message(Command.STOP, 'http_post')
#!/usr/bin/env python3 import os import sys import argparse from scripts.communication_helpers.communication_helper import Command, send_message if __name__ == '__main__': parser = argparse.ArgumentParser( formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument('command', choices=['start', 'stop'], help='Start / stop the energy measurement.') parser.add_argument( 'message', help='Start / stop the measurement with the given message.') args = parser.parse_args() command = Command.START if args.command == 'start' else Command.STOP send_message(command, args.message)
import os import sys import time from scripts.communication_helpers.communication_helper import Command, send_message # change this value for a different result nterms = 10000 # first two terms n1 = 0 n2 = 1 count = 0 send_message(Command.START, 'fibonacci') time.sleep(1) fibonacci_sequence = [n2] while count < nterms: nth = n1 + n2 fibonacci_sequence.append(nth) # update values n1 = n2 n2 = nth count += 1 time.sleep(1)
#!/usr/bin/env python3 import os import sys import time from scripts.communication_helpers.communication_helper import Command, send_message send_message(Command.START, 'test_sender.py:27') time.sleep(4) send_message(Command.STOP, 'test_sender.py:27') send_message(Command.START, 'test_sender.py:28') time.sleep(2) send_message(Command.STOP, 'test_sender.py:28')
from scripts.communication_helpers.communication_helper import Command, send_message import sys import signal from sense_hat import SenseHat from evdev import InputDevice, list_devices, ecodes sense = SenseHat() def signal_handler(): raise Exception('timeout') if __name__ == "__main__": for dev in [InputDevice(fn) for fn in list_devices()]: if dev.name == 'Raspberry Pi Sense HAT Joystick': break signal.signal(signal.SIGALRM, signal_handler) signal.alarm(10) try: send_message(Command.START, 'joystick_event_loop') for event in dev.read_loop(): if event.type == ecodes.EV_KEY: pass except: send_message(Command.STOP, 'joystick_event_loop')
from scripts.communication_helpers.communication_helper import Command, send_message import time from sense_hat import SenseHat sense = SenseHat() W = (255, 255, 255) B = (0, 0, 0) for _ in range(10): # Show string message sense.clear() send_message(Command.START, 'LED_show_message') message = 'TEST' for i in range(len(message)): sense.show_letter(message[i]) time.sleep(1) send_message(Command.STOP, 'LED_show_message') # Show an 'A' letter sense.clear() send_message(Command.START, 'LED_show_letter') sense.show_letter('A') time.sleep(3) send_message(Command.STOP, 'LED_show_letter') # Light up pixels for more accurate measurement. sense.clear()
import os import sys import time from scripts.communication_helpers.communication_helper import Command, send_message # change this value for a different result nterms = 10000 # first two terms n1 = 0 n2 = 1 count = 0 send_message(Command.START, 'figure_3_2_1_part_1') fibonacci_sequence = [n2] while count < nterms: nth = n1 + n2 fibonacci_sequence.append(nth) # update values n1 = n2 n2 = nth count += 1 send_message(Command.STOP, 'figure_3_2_1_part_1') send_message(Command.START, 'figure_3_2_1_part_2') with open('fibonacci_sequence.txt', 'w') as f:
#!/usr/bin/env python3 from scripts.communication_helpers.communication_helper import Command, send_message import time import sched import datetime scheduler = sched.scheduler(time.time, time.sleep) start_time = '' def function_1(): if datetime.datetime.now() - start_time < datetime.timedelta(seconds=10): scheduler.enter(1, 0, function_1, ()) if __name__ == "__main__": send_message(Command.START, 'scheduling') start_time = datetime.datetime.now() scheduler.enter(0, 0, function_1, ()) scheduler.run() send_message(Command.STOP, 'scheduling')
key = arr[i] j = i - 1 while j >= 0 and key < arr[j] : arr[j + 1] = arr[j] j -= 1 arr[j + 1] = key return arr nterms = int(sys.argv[1]) # first two terms n1 = 0 n2 = 1 count = 0 send_message(Command.START, 'fibonacci_alg_python') fibonacci_sequence = [ n2 ] while count < nterms: nth = n1 + n2 fibonacci_sequence.append(nth) # update values n1 = n2 n2 = nth count += 1 fibonacci_sequence.reverse() sorted_list = insertionSort(fibonacci_sequence)
#!/usr/bin/env python3 from scripts.communication_helpers.communication_helper import Command, send_message from sense_hat import SenseHat sense = SenseHat() for _ in range(10): send_message(Command.START, 'temperature_from_pressure') temperature_from_pressure = sense.get_temperature_from_pressure() send_message(Command.STOP, 'temperature_from_pressure') send_message(Command.START, 'temperature_from_humidity') temperature_from_humidity = sense.get_temperature_from_humidity() send_message(Command.STOP, 'temperature_from_humidity') send_message(Command.START, 'pressure') pressure = sense.get_pressure() send_message(Command.STOP, 'pressure') send_message(Command.START, 'humidity') humidity = sense.get_humidity() send_message(Command.STOP, 'humidity')