commit 152abcb4ea352509421eb3bcf55fe4cef32b63ed
Author: rbckman <rbckman@reikaleipa.reikaleipa.fi>
Date: Mon, 15 Aug 2022 14:17:36 +0300
first
Diffstat:
4 files changed, 98 insertions(+), 0 deletions(-)
diff --git a/client.py b/client.py
@@ -0,0 +1,16 @@
+import asyncio
+import sys
+
+async def tcp_echo_client(message, loop):
+ reader, writer = await asyncio.open_connection('127.0.0.1', 8888, loop=loop)
+ print('Send: %r' % message)
+ writer.write(message.encode())
+ data = await reader.read(1000)
+ print('Received: %r' % data.decode())
+ print('Close the socket')
+ writer.close()
+
+message = sys.argv[1]
+loop = asyncio.get_event_loop()
+loop.run_until_complete(tcp_echo_client(message, loop))
+loop.close()
diff --git a/readfridge.py b/readfridge.py
@@ -0,0 +1,19 @@
+import serial
+import os
+import time
+
+arduino = serial.Serial(port='/dev/ttyACM0', baudrate=115200, timeout=.1)
+
+temp = 0
+oldtemp = 1
+
+def read():
+ temp = arduino.readline().decode('utf-8').rstrip()
+ return temp
+
+while True:
+ temp = read()
+ if oldtemp != temp and temp != '':
+ os.system('python3 client.py' + ' "Kylskåpe: ' + str(temp) + '"')
+ oldtemp = temp
+ time.sleep(10)
diff --git a/recv.py b/recv.py
@@ -0,0 +1,20 @@
+import asyncio
+from nio import (AsyncClient, RoomMessageText)
+import config
+
+open('msg.txt', 'w').close()
+
+async def message_cb(room, event):
+ if room.display_name == config.display_name:
+ print("{}: {}".format(room.user_name(event.sender), event.body))
+ msg_file = open('msg.txt', 'a')
+ msg_file.write(room.user_name(event.sender) + ': ' + event.body + '\n')
+
+async def main():
+ client = AsyncClient(config.matrixserver, config.username)
+ client.add_event_callback(message_cb, RoomMessageText)
+
+ await client.login(config.password)
+ await client.sync_forever(timeout=30000)
+
+asyncio.get_event_loop().run_until_complete(main())
diff --git a/server.py b/server.py
@@ -0,0 +1,43 @@
+import asyncio
+from nio import AsyncClient
+import config
+
+async def handle_echo(reader, writer):
+ client = AsyncClient(config.matrixserver, config.username)
+ await client.login(config.password)
+
+ data = await reader.read(512)
+ message = data.decode()
+ addr = writer.get_extra_info('peername')
+ print("Received %r from %r" % (message, addr))
+ writer.write(data)
+ await writer.drain()
+ print("Send: %r" % message)
+ print("Close the client socket")
+ writer.close()
+ print('sending msg to matrix')
+ await client.room_send(
+ room_id=config.room_id,
+ message_type="m.room.message",
+ content={
+ "msgtype": "m.text",
+ "body": message
+ }
+ )
+ await client.close()
+
+loop = asyncio.get_event_loop()
+coro = asyncio.start_server(handle_echo, '127.0.0.1', 8888, loop=loop)
+server = loop.run_until_complete(coro)
+
+# Serve requests until Ctrl+C is pressed
+print('Serving on {}'.format(server.sockets[0].getsockname()))
+try:
+ loop.run_forever()
+except KeyboardInterrupt:
+ pass
+
+# Close the server
+server.close()
+loop.run_until_complete(server.wait_closed())
+loop.close()