Socket connect to server, enter number and get reply test.
server.py
import socket
import threading
# Define the host and port
HOST = '0.0.0.0' # Localhost (change as needed)
PORT = 65432 # Port to listen on (non-privileged ports are > 1023)
# Function to handle each client connection
def handle_client(conn, addr):
print(f"Connected by {addr}")
# Send a thank you message to the client upon connection
thank_you_message = "Thank you for connecting! Please enter a number:\n"
conn.sendall(thank_you_message.encode('utf-8'))
while True:
try:
data = conn.recv(1024)
if not data:
break
# Decode the received data
received_number = data.decode('utf-8').strip()
print(f"Received from {addr}: {received_number}")
# Try to convert the received data to an integer
try:
number = int(received_number)
response = f"The double of {number} is {number * 2}\n"
except ValueError:
response = "Please enter a valid number.\n"
# Send the response back to the client
conn.sendall(response.encode('utf-8'))
except ConnectionResetError:
print(f"Connection with {addr} lost.")
break
conn.close()
print(f"Connection with {addr} closed.")
# Function to start the server and listen for connections
def start_server():
# Create a socket object
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Bind the socket to the host and port
server.bind((HOST, PORT))
# Start listening with a maximum backlog of 5 connections
server.listen(5)
print(f"Server listening on {HOST}:{PORT}")
while True:
# Accept a new connection
conn, addr = server.accept()
# Create a new thread to handle the client connection
client_thread = threading.Thread(target=handle_client, args=(conn, addr))
client_thread.start()
# Run the server
if __name__ == "__main__":
start_server()
python-client.py
import socket
# Define the server host and port
HOST = 'IPNUMBERSERVER' # The server's hostname or IP address
PORT = 65432 # The port used by the server
def start_client():
# Create a socket object
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Connect to the server
client.connect((HOST, PORT))
# Receive and print the welcome message from the server
welcome_message = client.recv(1024).decode('utf-8')
print(welcome_message)
while True:
# Enter a number and send it to the server
number = input("Enter a number (or type 'exit' to quit): ")
if number.lower() == 'exit':
print("Closing connection...")
break
client.sendall(number.encode('utf-8'))
# Receive the response from the server and print it
response = client.recv(1024).decode('utf-8')
print(response)
# Close the connection after the loop ends
client.close()
# Run the client
if __name__ == "__main__":
start_client()
arduino-client.ino
#include <ESP8266WiFi.h> // For ESP8266
//#include <WiFi.h> // For ESP32
// Replace with your network credentials
const char* ssid = "your_SSID"; // Replace with your network SSID (name)
const char* password = "your_PASSWORD"; // Replace with your network password
// Define the server's IP address and port
const char* host = "192.168.1.100"; // Replace with your server's IP address
const int port = 65432; // Server port
WiFiClient client;
void setup() {
Serial.begin(115200);
delay(10);
// Connect to WiFi
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println();
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
// Connect to the server
Serial.print("Connecting to server at ");
Serial.print(host);
Serial.print(":");
Serial.println(port);
if (client.connect(host, port)) {
Serial.println("Connected to server!");
// Wait for the welcome message from the server
while (client.available() == 0);
// Read and print the welcome message
while (client.available()) {
char c = client.read();
Serial.print(c);
}
} else {
Serial.println("Connection failed.");
}
}
void loop() {
// Check if connected to the server
if (client.connected()) {
// Check if there is any serial input from the user
if (Serial.available() > 0) {
String input = Serial.readStringUntil('\n');
input.trim();
if (input.equalsIgnoreCase("exit")) {
Serial.println("Closing connection...");
client.stop(); // Disconnect from the server
while (true); // Stop the loop
}
// Send the number to the server
client.println(input);
// Wait for the server's response
while (client.available() == 0);
// Read and print the server's response
while (client.available()) {
char c = client.read();
Serial.print(c);
}
}
} else {
Serial.println("Disconnected from server.");
while (true); // Stop the loop
}
}
My C64 had a problem with previous attached speaker. It drew too much current to drive. And random characters where printed. Choosing another speaker and a minimal amplifier solved the issue. (Thanks to Bigred finding the problem at Bornhack 2024)
Today I made a Linux version of Tyrone’s QuickPath tool.
My friend Tyrone came up with a great idea.
A directory switching tool, to move around in often used directories. You can use a keyword to move around. These keywords are configured in a config file. Even starting Total Commander with preset directories. Work/Private/Programming environments. His version uses PowerShell, but he wanted a multiplatform version, so we have chosen to use Python on both environments.
My version uses Python and Bash. (Bash is used for a change directory workaround on Linux and bash completion.)
Source will be in Tyrone’s git when finished.
Options:
qp – lists config items with number and short key
qp 1 or qp c64demo – changes directory to below example
qp add c64demo /data/store/git/projects/c64code2024 – add a entry
qp del 1 – removes entry
qp mc tmp c64demo – starts midnight commander with left and right directories
Tomorrow some laser cutting, so let’s design some things to cut.
Jigsaw test – using engraving and cutting
Make a front for my bus manipulator
Make some cases for the game controllers (These are beta, and will be 3D printed at a later stage. My old 3D printer is slow)
Saw a cool game a while ago, and found some old code. There was no schematic, so I had to reverse engineer it using the Arduino code. This one uses a Micro Pro.
Build a working version, now I can use this as base to create other games. But first i’m going to rebuild it so it can use Wifi and uses a Lipo Battery. Making it usable without wires.
Rotary – set angle/speed (Press resets)
Blue – toggle angle or speed ( was rotary press )
Green – select digit to change
Red – Fire
Led – not completely working yet, shows color of player Wil be changed to addressable leds with more functions (Player color, energy warning and more)
Last week I bought an old Bornhack Badge. I thought it needed a display.
Using a SSD1306 display, and Circuitpython I made this.
( Wooded thingy contains an RFID chip ( Part of my player ))
Library and files needed:
font5x8.bin in root of filesystem ( just google for this file )
copy of adafruit_framebuf.mpy in /lib
copy of adafruit_ssd1306.mpy in /lib
Code: (midway some pixel examples, just uncomment)
import board
from time import sleep
import busio
from PN7150 import PN7150
import adafruit_ssd1306
import math
import adafruit_framebuf
if True:
# Fast 400KHz I2C
i2c = busio.I2C(board.SCL, board.SDA, frequency = 400000)
else:
# Regular 100kHz I2C
i2c = board.I2C()
WIDTH = 32
HEIGHT = 8
buffer = bytearray(round(WIDTH * math.ceil(HEIGHT / 8)))
fb = adafruit_framebuf.FrameBuffer(
buffer, WIDTH, HEIGHT, buf_format=adafruit_framebuf.MVLSB
)
nfc = PN7150(i2c, board.IRQ, board.VEN)
display = adafruit_ssd1306.SSD1306_I2C(128, 32, i2c,addr=0x3c)
assert nfc.connect()
print("Connected.")
assert nfc.modeRW()
print("Switched to read/write mode.")
display.fill(0)
display.show()
#display.fill(0)
#display.text('Hello', 0, 0, 1 )
#display.text('World', 0, 10, 1)
#display.show()
# Set a pixel in the origin 0,0 position.
#display.pixel(0, 0, 1)
# Set a pixel in the middle 64, 16 position.
#display.pixel(64, 16, 1)
# Set a pixel in the opposite 127, 31 position.
#display.pixel(127, 31, 1)
#display.show()
while True:
display.fill(0)
display.text('Waiting for card', 0, 0, 1 )
display.show()
assert nfc.startDiscoveryRW()
print("Waiting for card..")
card = nfc.waitForCard()
assert nfc.stopDiscovery()
print("ID: {}".format(card.nfcid1()))
id = card.nfcid1()
display.text(id, 0, 10, 1 )
display.show()
sleep(0.5)
Not sure about display i2c address? Use below code
import time
import board
import busio
# List of potential I2C busses
ALL_I2C = ("board.I2C()",)
# Determine which busses are valid
found_i2c = []
for name in ALL_I2C:
try:
print("Checking {}...".format(name), end="")
bus = eval(name)
bus.unlock()
found_i2c.append((name, bus))
print("ADDED.")
except Exception as e:
print("SKIPPED:", e)
# Scan valid busses
if len(found_i2c):
print("-" * 40)
print("I2C SCAN")
print("-" * 40)
while True:
for bus_info in found_i2c:
name = bus_info[0]
bus = bus_info[1]
while not bus.try_lock():
pass
print(
name,
"addresses found:",
[hex(device_address) for device_address in bus.scan()],
)
bus.unlock()
time.sleep(2)
else:
print("No valid I2C bus found.")