diff --git a/Server/Server-Code-2018/sensors.py b/Server/Server-Code-2018/sensors.py deleted file mode 100644 index c367116..0000000 --- a/Server/Server-Code-2018/sensors.py +++ /dev/null @@ -1,33 +0,0 @@ -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -# from py_read_serial import * -import socket - -UDP_IP_ADDRESS = '127.0.0.1' -UDP_PORT_NO = 6789 -clientSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) - -# messages = { -# 0: 'CO2: ', -# 1: 'Temperature: ' -# } - -# numOfSensors = len(messages) - -while 1: - # for x in range(numOfSensors): - # while 1: - # try: - # sensor = readPins() # sensor = {'num': sensorID, 'value': some number} - # break - # except: - # pass - # if sensor['num'] == 1: - # sensor['value'] = round((57*(sensor['value']-20))/100,1) - # # print(messages[sensor['num']][0], sensor['value'], messages[sensor['num']][1]) - # output = "" - # output += messages[sensor['num']] - # output += sensors['value'] - clientSock.sendto(bytes('hey', 'utf-8'), (UDP_IP_ADDRESS, UDP_PORT_NO)) diff --git a/Server/Server-Sensor-Code/__pycache__/py_read_serial.cpython-36.pyc b/Server/Server-Sensor-Code/__pycache__/py_read_serial.cpython-36.pyc new file mode 100644 index 0000000..70ff275 Binary files /dev/null and b/Server/Server-Sensor-Code/__pycache__/py_read_serial.cpython-36.pyc differ diff --git a/Server/Server-Sensor-Code/arduino_robot_read_sensors/arduino_robot_read_sensors.ino b/Server/Server-Sensor-Code/arduino_robot_read_sensors/arduino_robot_read_sensors.ino new file mode 100644 index 0000000..ed63229 --- /dev/null +++ b/Server/Server-Sensor-Code/arduino_robot_read_sensors/arduino_robot_read_sensors.ino @@ -0,0 +1,53 @@ +#include + +SoftwareSerial co2Serial(10, 11); +unsigned char hexdata[9] = {0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79}; +char CO2 = true; +char temperature = false; + +// Reads the value of a pin, and outputs it +void readPin(int pin) { + Serial.print(pin); + Serial.println(analogRead(pin)); +} + +// Read the amount of CO2 in the surrounding air +void readCO2() { + co2Serial.write(hexdata, 9); + int ch; + long high; + long low; + long CO2; + + for (int i = 0; i < 9; ++i) { + if (co2Serial.available() > 0) { + ch = co2Serial.read(); + + if (i == 2) + high = ch; + else if (i == 3) + low = ch; + else if (i == 8) { + CO2 = high*256 + low; + Serial.print("C "); + Serial.println(CO2); + } + } + } +} + +// Setup serial communication on channel 9600 +void setup() { + Serial.begin(9600); + co2Serial.begin(9600); +} + +// The .ino equivalent of a main function +void loop() { + if (temperature) + readPin(1); + if (CO2) + readCO2(); + + delay(500); +} diff --git a/Server/Server-Sensor-Code/py_read_serial.py b/Server/Server-Sensor-Code/py_read_serial.py new file mode 100644 index 0000000..4ba557a --- /dev/null +++ b/Server/Server-Sensor-Code/py_read_serial.py @@ -0,0 +1,25 @@ +import serial as s +''' "serialdata" is the reading of serial data + from the arduino,plugged in by USB ''' +serialdata = s.Serial('/dev/ttyUSB0',9600) + +def readPins(): + ''' "string" is the line of data read from the ardiuno. ''' + string = str(serialdata.readline()).replace("b'","") + ''' sensor_num is the pin number the sensor is plugged into. + sensor_value is the analog value read from the sensor. ''' + sensor_num = string[0] + sensor_value = '' + ''' This loop goes though each character of "string" + and retrives the analog value from the string ''' + for x in range(1,len(string)): + if string[x] != '\\': + sensor_value = sensor_value + string[x] + else: + break + ''' "sensor" is a dictionary that has two attributes, "value" and "num" ''' + sensor = {} + sensor['value'] = int(sensor_value) + sensor['num'] = int(sensor_num) + ''' This returns the sensor dictionay so it can be used outside the function. ''' + return sensor diff --git a/Server/Server-Sensor-Code/sensors.py b/Server/Server-Sensor-Code/sensors.py new file mode 100644 index 0000000..7863f2a --- /dev/null +++ b/Server/Server-Sensor-Code/sensors.py @@ -0,0 +1,33 @@ +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function + +from py_read_serial import * +import socket + +UDP_IP_ADDRESS = '127.0.0.1' +UDP_PORT_NO = 6789 +clientSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + +messages = { + 0: 'CO2: ', + 1: 'Temperature: ' +} + +numOfSensors = len(messages) + +while 1: + for x in range(numOfSensors): + while 1: + try: + sensor = readPins() # sensor = {'num': sensorID, 'value': some number} + break + except: + pass + if sensor['num'] == 1: + sensor['value'] = round((57*(sensor['value']-20))/100,1) + print(messages[sensor['num']][0], sensor['value'], messages[sensor['num']][1]) + # output = "" + # output += messages[sensor['num']][0] + # output += sensors['value'] + # clientSock.sendto(bytes(, 'utf-8'), (UDP_IP_ADDRESS, UDP_PORT_NO))