# import network
# wlan = network.WLAN(network.STA_IF)
# wlan.active(True)
# available_networks = wlan.scan()
# print("Available Networks:")
# for network in available_networks:
# print("SSID:", network[0].decode("utf-8"))
# ssid = "Wokwi-GUEST"
# password = ""
# wlan.connect(ssid, password)
# import time
# max_retries = 10
# retries = 0
# while not wlan.isconnected():
# retries += 1
# if retries > max_retries:
# print("Failed to connect to Wi-Fi.")
# break
# print("Connecting to Wi-Fi...")
# time.sleep(2)
# if wlan.isconnected():
# print("Connected to Wi-Fi:", wlan.config("essid"))
# # Compare the lengths
# if length_of_encoded_data < length_of_normal_packet:
# print("CBOR-Encoded Data is shorter.")
# elif length_of_encoded_data > length_of_normal_packet:
# print("Normal Packet is shorter.")
# else:
# print("Both have the same length.")
from cbor import dumps, loads
import json
import uprotobuf
import a_upb2
# Define your data to be encoded in CBOR format
data_to_encode = {
"temperature": 25,
"location": "Living Room",
}
# Create a sample normal packet (e.g., JSON)
normal_packet = json.dumps(data_to_encode)
# Calculate the length of the normal packet
length_of_normal_packet = len(normal_packet)
print("original data of cbor format",data_to_encode)
# Encode the data to CBOR format
encoded_data = dumps(data_to_encode)
# Calculate the length of the CBOR-encoded data
length_of_encoded_data_cbor = len(encoded_data)
# Decode the CBOR-encoded data
decoded_data = loads(encoded_data)
# Create an instance of the SensorData message
sensor_data = a_upb2.SensorData()
sensor_data.temperature = 25
sensor_data.location = "Living Room"
print("original data of protobuff",sensor_data)
# Encode the data to PROTOBUFF format
encoded_message = sensor_data.encode()
length_of_encoded_data = len(encoded_message)
print("encoded_message",encoded_message)
#decode the protobuff-encoded data
decoded_sensor_data = a_upb2.SensorData()
new = decoded_sensor_data.decode(encoded_message)
# Print the length of the normal packet and encoded data
print("Length of Normal Packet:", length_of_normal_packet)
print("Length of CBOR-Encoded Data:", length_of_encoded_data_cbor)
print("Length of proto-Encoded Data:", length_of_encoded_data)
print("decoder messege: ",new)
# print("Decoded Sensor Data from Protobuf:")
# print("Temperature:", new.temperature)
# print("Location:", new.location)