def set_start_time(): """ saves the time of the start of the program so that time can be used to calculate how many seconds the programs has been running """ from utime import mktime, localtime from miscellaneous import write_json, read_json from log import message json_dict = read_json( "data/config" ) # read config json file (so i can later save it to dump json file) json_dict["dumpJson"]["startTime"] = mktime( localtime()) # set starTime equal to current time write_json("data/dump", json_dict["dumpJson"]) # save dictionary to the json file dump message("Start time set")
def update_display(components, header: str = None, body: list = None): """ updates the display if there is a change in the body and header compared to the previous time :param components: the dictionary with al the components :param header: the top line of the display (the yellow part of the display) :param body: the main part of the display from line +-20 to +-50 (the blue part of the display) :return: returns True if the display updated """ from miscellaneous import read_json, write_json json_dict = read_json("data/dump") if not header == json_dict["display"]["header"] or not body == json_dict[ "display"]["body"]: # check if body and/or # header has chanced json_dict["display"][ "header"] = header # add header to json dictionary json_dict["display"]["body"] = body # add body to json dictionary components["OLEDDisplay"]["object"].fill(0) # clear display if header is not None: # check if there is a header components["OLEDDisplay"]["object"].text(str(header), 0, 5) # display header if body is not None: # check if there is a body for index, value in enumerate(body): # loop trough body if not value == "": # check if there is text in this line of the body components["OLEDDisplay"]["object"].text( str(value), 0, ((index + 1) * 10) + 10) # display body components["OLEDDisplay"]["object"].show() # update display write_json( "data/dump", json_dict) # write the body and header to the json file named dump from log import debugging debugging("Updated display") return True
def repeat_message(log_message: str, repeat: int, message_id: str = ""): """ print a message ones every [repeat] seconds and only one every second if you give the parameter repeat_message_previous_time :param log_message: the message :param repeat: how many seconds it takes to print the message again :param message_id: a id to get the previous time a repeat_message printed (so multiple repeat messages do not print in 1 second) :return: whether or not the message printed (printed = True) """ from miscellaneous import read_json, write_json, time json_dict = read_json("data/dump") if time() % repeat == 0 and not json_dict["repeatMessage"][ message_id] == time(): # check if there is a rest when # you divide repeat by time() and if you already send a message of a certain id this second print('\033[92m' + "(" + str(pretty_time()) + ") LOG: " + str(log_message) + '\033[0m') # print the message json_dict["repeatMessage"][message_id] = time( ) # save time of message to json dictionary for next message/run write_json("data/dump", json_dict) # save dictionary to the json file dump
def main(): message("Main function start") from miscellaneous import read_json from networkManager import network_handler from socket import socket, AF_INET, SOCK_STREAM, SOL_SOCKET, SO_REUSEADDR from hardware import setup_components, calibrate_sensor, DS04NFC, update_display from log import debugging components = setup_components(read_json( "data/config")["components"]) # initialize all the components components["button"]["previousState"] = False robbery_active = False # This is true if there is a robber components["led"]["object"].off() # turn led off components["motor"]["object"].stop() # turn motor off components["speaker"]["object"].stop() # turn speaker off components["laser"]["object"].off() # turn laser off calibrate_sensor(components) # calibrate the sensor s = socket(AF_INET, SOCK_STREAM ) # make a socket (=give access to the BSD socket interface) s.settimeout(1) # accept function will timeout after 1 second s.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) # check and filter outgoing packages s.bind( ('', 80) ) # bind socket to localhost and port 80 (=localhost:80 or http://localhost) s.listen(5) # listen to the bounded address while True: import gc gc.collect() # collect garbage try: connection_socket, address = s.accept( ) # wait for user to accept socket print('Got a connection from %s' % str(address)) network_handler( connection_socket, components, robbery_active) # a function that handles network usages except OSError: # a result of settimeout(1) see line 24 debugging("Socket timeout") if not robbery_active: # check if there is no robbery robbery_active = normal_activity( components) # execute the normal events if components["configButton"]["object"].value( ) == 1: # check config button from utime import sleep message("Config button pressed") sleep(3) # wait for 3 seconds calibrate_sensor(components) # calibrate sensor again update_display(components) # clear the display sleep(3) # wait for 3 seconds elif components["configButton"]["object"].value( ) == 1: # there is a robbery and the config button is pressed from log import warning from machine import Pin warning("Robbery state turned off by config button") update_display(components) # clear display components["led"]["object"].off() # turn led off DS04NFC(Pin(2, Pin.OUT)).stop() # turn motor off components["speaker"]["object"].stop() # turn speaker off robbery_active = False # set robbery variable to no robbery
def connect_to_network(): """ connect to a network and disable the access point """ from network import WLAN, STA_IF, AP_IF from miscellaneous import read_json sta_if = WLAN(STA_IF) # wifi station (connect to router) ap_if = WLAN(AP_IF) # wifi access point (like a router) if read_json( "data/config")["network"]["networkStation"]["enabled"] is True: warning("Network station will be enabled") if sta_if.isconnected(): # check if you're already connected message('Already connected to network') while not sta_if.isconnected(): message('Connecting to network...') sta_if.active(True) # enable the station if read_json("data/config")["network"]["networkStation"][ "homeNetwork"] is True: # check if you want to # connect to home wifi warning("Using home network") sta_if.connect('Hackercollective', 'w6HSB2S3bb042') # let station connect to wifi else: # connect to custom wifi warning("Using custom network config") ssid = read_json("data/config")["network"]["networkStation"][ "customSSID"] # read custom wifi ssid # from json password = read_json("data/config")["network"][ "networkStation"]["customNetworkPassword"] # read # custom password from json sta_if.connect(ssid, password) # let station connect to wifi from time import time # import time to make a timer start_time = time( ) + 120 # make timer (last value is how long the timer in ms) while not sta_if.isconnected(): # wait to be connected if time() == start_time: # check if timer is over break message('Network config:' + str(sta_if.ifconfig())) message('The ip is ' + str(sta_if.ifconfig()[0])) else: warning("Network station will be disabled") sta_if.active(False) # enable network station if read_json("data/config")["network"]["accessPoint"][ "enabled"] is True: # check if you want an access point in # config warning("Access point will be enabled") ap_if.active(True) # disable access point ap_if.config( essid=read_json("data/config")["network"]["accessPoint"] ["SSID"], # set password and ssid of password=read_json("data/config")["network"]["accessPoint"] ["password"]) # the access point else: warning("Access point will be disabled") ap_if.active(False) # enable access point