1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
| import random import struct import socket import time from datetime import datetime
DEVICE_IDS = [ "333804318B10", "3338158B14FD", "3338978BC34C", "333826AD44CE", "3338CA967271", "3338AB12D24F", "3338C8A47C4B", "33387AED3585", "3338DA08B0CC", "333825246E4A" ]
previous_ph_value = 7.0 previous_conductivity_value = 30.0 previous_dissolved_oxygen_value = 10.0 previous_turbidity_value = 150.0 previous_water_temperature_value = 10.0
def generate_random_data(): global previous_ph_value, previous_conductivity_value, previous_dissolved_oxygen_value, previous_turbidity_value, previous_water_temperature_value
now = datetime.now() current_hour = now.hour current_minute = now.minute
if current_hour == 12 and current_minute < 10: ph_value = previous_ph_value + random.uniform(-0.1, 0.1) ph_value = max(6.5, min(8.5, ph_value)) previous_ph_value = ph_value
conductivity_value = previous_conductivity_value + random.uniform(-0.5, 0.5) conductivity_value = max(20, min(40, conductivity_value)) previous_conductivity_value = conductivity_value
dissolved_oxygen_value = previous_dissolved_oxygen_value + random.uniform(-0.5, 0.5) dissolved_oxygen_value = max(0, min(20, dissolved_oxygen_value)) previous_dissolved_oxygen_value = dissolved_oxygen_value
turbidity_value = previous_turbidity_value + random.uniform(-5, 5) turbidity_value = max(0, min(300, turbidity_value)) previous_turbidity_value = turbidity_value
water_temperature_value = previous_water_temperature_value + random.uniform(-0.5, 0.5) water_temperature_value = max(2, min(20, water_temperature_value)) previous_water_temperature_value = water_temperature_value else: ph_value = 0.0 conductivity_value = 0.0 dissolved_oxygen_value = 0.0 turbidity_value = 0.0 water_temperature_value = 0.0
ph = struct.pack('>I', int(ph_value * 100)) conductivity = struct.pack('>I', int(conductivity_value * 10)) dissolved_oxygen = struct.pack('>I', int(dissolved_oxygen_value * 10)) turbidity = struct.pack('>I', int(turbidity_value * 1)) water_temperature = struct.pack('>I', int(water_temperature_value * 10))
frame_header = b'\xFE\xDC' fixed_value = b'\x01' unused_bytes = b'\x00\x00\x00\x06\x03' content_length = b'\x00\x20' signal_strength = b'\x00\x00\x00\x55' error_code = b'\x00\x00\x00\x00' version = b'\x00\x00\x00\x75' checksum = b'\x00'
device_id = random.choice(DEVICE_IDS)
data = (frame_header + fixed_value + bytes.fromhex(device_id) + unused_bytes + content_length + ph + conductivity + dissolved_oxygen + turbidity + water_temperature + signal_strength + error_code + version + checksum)
return data
def generate_and_send_data(server_ip, server_port): data = generate_random_data()
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as client_socket: try: client_socket.connect((server_ip, server_port)) client_socket.sendall(data) print(f'{data.hex().upper()}') except ConnectionRefusedError: print("Connection refused. Make sure the server is running.")
if __name__ == "__main__": SERVER_IP = 'ch.jitux.com' SERVER_PORT = 8001
while True: generate_and_send_data(SERVER_IP, SERVER_PORT) time.sleep(5)
|