Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create mock wireless module test script #30

Merged
merged 37 commits into from Jan 14, 2020
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
23adb53
Mock wireless sensor test script
Blake-Haydon Jan 9, 2020
a370262
Removed header documentation
Blake-Haydon Jan 10, 2020
13505a0
Changed "==" to "is" for if statements
Blake-Haydon Jan 10, 2020
3992966
Remove empty lines
Blake-Haydon Jan 10, 2020
6bf18e9
Remove run function
Blake-Haydon Jan 10, 2020
ae26a68
Move Sensor class to MockSensor.py
Blake-Haydon Jan 10, 2020
15dfa4b
Refactor parser for readability
Blake-Haydon Jan 10, 2020
a050e18
Add init to MockSensor Class
Blake-Haydon Jan 10, 2020
bbd6a68
Add get_value method
Blake-Haydon Jan 10, 2020
afa5594
Add random library
Blake-Haydon Jan 10, 2020
221c50a
Split random value generation to other func
Blake-Haydon Jan 10, 2020
a4d70b5
Remove random library
Blake-Haydon Jan 10, 2020
cc42730
Refactor parser by removing newlines
Blake-Haydon Jan 10, 2020
7b21e81
Fix indexing error with *ave_val
Blake-Haydon Jan 10, 2020
d26e89f
Import MockSensor class
Blake-Haydon Jan 10, 2020
2c648ed
Add new "Mock sensors"
Blake-Haydon Jan 10, 2020
1cf11d5
Fix incorrect snake_case
Blake-Haydon Jan 10, 2020
f4ae830
Refactor test code with the new MockSensor Class
Blake-Haydon Jan 10, 2020
7d18a41
Fix incorrect sensor ID for sensor 3
Blake-Haydon Jan 10, 2020
5bf63d6
Swap type check for "isinstance()"
Blake-Haydon Jan 10, 2020
bef1a43
Convert confusing if statement to easier while loop
Blake-Haydon Jan 10, 2020
4e84ef2
Remove 'my library' comment
Blake-Haydon Jan 13, 2020
915beef
add TODO for outputting random sensor data
Blake-Haydon Jan 13, 2020
08ad25a
Change if state to else for clarity
Blake-Haydon Jan 13, 2020
f3ba943
Replace 'sensor_ID' with 'sensor_id'
Blake-Haydon Jan 13, 2020
2e267a3
Replace 'JSON_data' with 'json_data'
Blake-Haydon Jan 13, 2020
aa33131
Replace 'ValueError' with 'TypeError'
Blake-Haydon Jan 13, 2020
746e599
Fix documentation by changing 'and' to 'an'
Blake-Haydon Jan 13, 2020
1edce99
Fix spelling of 'outputted'
Blake-Haydon Jan 13, 2020
72570f1
Change 'ave_val' to 'average_value'
Blake-Haydon Jan 13, 2020
0854bfe
Change 'sub_val' to 'sub_value'
Blake-Haydon Jan 13, 2020
25a176e
Add todo to update message structure
Blake-Haydon Jan 13, 2020
cf8b253
Fix spelling of 'containing'
Blake-Haydon Jan 13, 2020
29579cf
Capitalise comments
Blake-Haydon Jan 13, 2020
8f19924
Add -i alternative for --id
Blake-Haydon Jan 14, 2020
ee262e7
Change host from '192.168.100.100' to 'localhost'
Blake-Haydon Jan 14, 2020
b991f10
Fix proper data structure and correct variable naming
Blake-Haydon Jan 14, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
55 changes: 55 additions & 0 deletions Raspi/Test/MockSensor.py
@@ -0,0 +1,55 @@
class Sensors:

# number of decimal places for the random generated val
decimals = 2
# percent range for the rangomly generated value
percent_range = 0.05

def CO2(val=325):
# value in ppm (normal background conentration 250-400ppm)
return round(val + val*random.uniform(-Sensors.percent_range, Sensors.percent_range), Sensors.decimals)

def humidity(val=75):
# value as a percentage of humidity
return round(val + val*random.uniform(-Sensors.percent_range, Sensors.percent_range), Sensors.decimals)

def temperature(val=20):
# value in degrees celcius
return round(val + val*random.uniform(-Sensors.percent_range, Sensors.percent_range), Sensors.decimals)

def accelerometer(val=5):
# value in m/s^2 for all axis stored in a dictionary
return {
"x": round(val + val*random.uniform(-Sensors.percent_range, Sensors.percent_range), Sensors.decimals),
"y": round(val + val*random.uniform(-Sensors.percent_range, Sensors.percent_range), Sensors.decimals),
"z": round(val + val*random.uniform(-Sensors.percent_range, Sensors.percent_range), Sensors.decimals)}

def gyroscope(val=90):
# value in degrees for all axis stored in a dictionary
return {
"x": round(val + val*random.uniform(-Sensors.percent_range, Sensors.percent_range), Sensors.decimals),
"y": round(val + val*random.uniform(-Sensors.percent_range, Sensors.percent_range), Sensors.decimals),
"z": round(val + val*random.uniform(-Sensors.percent_range, Sensors.percent_range), Sensors.decimals)}

def battery(val=80):
# value as a percentage of the battery
return round(val + val*random.uniform(-Sensors.percent_range, Sensors.percent_range), Sensors.decimals)

def reedSwitch(val=True):
Blake-Haydon marked this conversation as resolved.
Show resolved Hide resolved
# value as either true or false
return val

def GPS(latLngVal=20, speedVal=50):
Blake-Haydon marked this conversation as resolved.
Show resolved Hide resolved
# returns the latatude, longatude and the GPS speed of the bike stored in a dictionary
return {
"lat": round(latLngVal + latLngVal*random.uniform(-Sensors.percent_range, Sensors.percent_range), Sensors.decimals),
"lng": round(latLngVal + latLngVal*random.uniform(-Sensors.percent_range, Sensors.percent_range), Sensors.decimals),
"speed": round(speedVal + speedVal*random.uniform(-Sensors.percent_range, Sensors.percent_range), Sensors.decimals)}

def steering(val=0):
# value in degrees for the position of the steering stem
return round(val + random.uniform(-90, 90), Sensors.decimals)

def time():
# return the current time for the sensor as Unix Epoch time
return time.time()
135 changes: 135 additions & 0 deletions Raspi/Test/mock_wireless_module.py
@@ -0,0 +1,135 @@
import time
import random
import argparse
import json
import paho.mqtt.client as mqtt


parser = argparse.ArgumentParser(
description='MQTT wireless sensor test script that sends fake data',
add_help=True)

parser.add_argument(
'-t', '--time', action='store', type=int,
default=float('Inf'), help="""Length of time to send data in seconds
(duration). If nothing is selected it will continuously send out data.""")

parser.add_argument(
'-r', '--rate', action='store', type=float, default=1,
help="""Rate of data in data sent per second. Default is 1 data pack sent
per second.""")

parser.add_argument(
'--host', action='store', type=str, default="192.168.100.100",
Blake-Haydon marked this conversation as resolved.
Show resolved Hide resolved
help="""Address of the MQTT broker. If nothing is selected it will
default to 192.168.100.100.""")

parser.add_argument(
'--id', action='store', type=int, default=None,
Blake-Haydon marked this conversation as resolved.
Show resolved Hide resolved
help="""Specify the sensor to produce fake data. eg. --id 1 specifies that
sensor 1 only produces data. If nothing is given all sensors will be
active.""")


def send_fake_data(client, duration, rate, sensorID):
Blake-Haydon marked this conversation as resolved.
Show resolved Hide resolved
""" Send artificial data over MQTT for each sensor chanel. Sends [rate] per
second for [duration] seconds"""

start_time = round(time.time(), 2)

while True:
current_time = round(time.time(), 2)
total_time = round(current_time - start_time, 2)

print() # Newline for clarity

# Wireless Sensor 1 (Middle)
if sensorID == 1 or sensorID is None:

# generate JSON data and publish it over MQTT
data = {
Blake-Haydon marked this conversation as resolved.
Show resolved Hide resolved
"CO2": Sensors.CO2(),
"temperature": Sensors.temperature(),
"humidity": Sensors.humidity(),
"accelerometer": Sensors.accelerometer(),
"gyroscope": Sensors.gyroscope(),
"battery": Sensors.battery(),
"time": Sensors.time()}
Blake-Haydon marked this conversation as resolved.
Show resolved Hide resolved

JSON_data = json.dumps(data)
Blake-Haydon marked this conversation as resolved.
Show resolved Hide resolved
pub_path = "v3/wireless-sensor/1/data"

client.publish(pub_path, JSON_data)
print(pub_path, "--> ", JSON_data)

# Wireless Sensor 2 (client, Back)
if sensorID == 2 or sensorID is None:

# generate JSON data and publish it over MQTT
data = {
"CO2": Sensors.CO2(),
"reedSwitch": Sensors.reedSwitch(),
"GPS": Sensors.GPS(),
"temperature": Sensors.temperature(),
"battery": Sensors.battery(),
"time": Sensors.time()}

JSON_data = json.dumps(data)
pub_path = "v3/wireless-sensor/2/data"

client.publish(pub_path, JSON_data)
print(pub_path, "--> ", JSON_data)

# Wireless Sensor 3 (client, Front)
if sensorID == 3 or sensorID is None:

# generate JSON data and publish it over MQTT
data = {
"steering": Sensors.steering(),
"temperature": Sensors.temperature(),
"humidity": Sensors.humidity(),
"battery": Sensors.battery(),
"time": Sensors.time()}

JSON_data = json.dumps(data)
pub_path = "v3/wireless-sensor/3/data"

client.publish(pub_path, JSON_data)
Blake-Haydon marked this conversation as resolved.
Show resolved Hide resolved
print(pub_path, "--> ", JSON_data)

time.sleep(1/rate)
if total_time >= duration:
Blake-Haydon marked this conversation as resolved.
Show resolved Hide resolved
break


def start_publishing(client, args):
print("publishing started...")
Blake-Haydon marked this conversation as resolved.
Show resolved Hide resolved

send_fake_data(client, args.time, args.rate, args.id)

print("publishing finished")


def on_connect(client, userdata, flags, rc):
if rc == 0:
print("Connected Sucessfully! Result code: " + str(rc))
else:
print("Something went wrong! Result code: " + str(rc))


def on_message(client, userdata, msg):
print(msg.topic+" "+str(msg.payload.decode("utf-8")))


if __name__ == "__main__":
args = parser.parse_args()
broker_address = args.host
client = mqtt.Client()

client.on_connect = on_connect
client.on_message = on_message

client.connect(broker_address)
client.loop_start()
start_publishing(client, args)
client.loop_stop()