Skip to content

Commit

Permalink
Add basic UDP communication for sensor support
Browse files Browse the repository at this point in the history
  • Loading branch information
Finchiedev committed Apr 20, 2019
1 parent 1e0c674 commit 1523c2c
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
33 changes: 33 additions & 0 deletions Server/Server-Code-2018/sensors.py
Original file line number Diff line number Diff line change
@@ -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']]
# output += sensors['value']
clientSock.sendto(bytes('hey', 'utf-8'), (UDP_IP_ADDRESS, UDP_PORT_NO))
19 changes: 19 additions & 0 deletions Server/sensors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const { StringDecoder } = require('string_decoder');
const decoder = new StringDecoder('utf-8');

var dgram = require('dgram');
var server = dgram.createSocket('udp4');

var PORT = 6789;
var HOST = '127.0.0.1';

server.on('listening', function () {
var address = server.address();
console.log('UDP Server listening on ' + address.address + ":" + address.port);
});

server.on('message', function (message, remote) {
console.log(decoder.end(Buffer.from(message)));
});

server.bind(PORT, HOST);

0 comments on commit 1523c2c

Please sign in to comment.