-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic UDP communication for sensor support
- Loading branch information
1 parent
1e0c674
commit 1523c2c
Showing
2 changed files
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |