import machine
import time
import math
# Setup ADC for NTC Sensor on GPIO 34
adc = machine.ADC(machine.Pin(34))
adc.atten(machine.ADC.ATTN_11DB) # 11dB attenuation (0-3.3V range)
# Thermistor Parameters
R_NOMINAL = 10000 # 10kΩ thermistor at 25°C
B_COEFFICIENT = 3950 # Beta value
SERIES_RESISTOR = 10000 # 10kΩ pull-up resistor
VCC = 3.3 # Supply voltage
# Wi-Fi Connection Simulation
wifi_ip = "192.168.1.100"
gateway = "192.168.1.1"
dns_server = "8.8.8.8"
print(f"Connected to Wi-Fi: IP={wifi_ip}, Gateway={gateway}, DNS={dns_server}")
def read_temperature():
"""Reads ADC value and converts it to temperature using the thermistor formula."""
raw_value = adc.read() # Read ADC value (0-4095)
if raw_value == 0:
print("Error: ADC read 0, check wiring!")
return None
voltage = raw_value * (VCC / 4095) # Convert to voltage
# Prevent division by zero error
if voltage == 0:
print("Error: Invalid voltage reading, check sensor!")
return None
resistance = SERIES_RESISTOR * (VCC / voltage - 1)
# Apply the Steinhart-Hart equation
temperature = 1 / (1 / (273.15 + 25) + (1 / B_COEFFICIENT) * math.log(resistance / R_NOMINAL))
temperature -= 273.15 # Convert Kelvin to Celsius
return round(temperature, 2) # Round for readability
while True:
try:
temperature = read_temperature()
if temperature is None:
continue
print(f"Client Sending: Temperature={temperature}°C")
# Simulate MITM Attack
print(f"[ATTACK] Intercepted Data: Temperature={temperature}°C")
# Modify intercepted data (example: increase by 10°C)
modified_temp = temperature + 10
print(f"[ATTACK] Modified Data Sent: Temperature={modified_temp}°C")
# Simulated Server Response
print(f"Server Received: Temperature={modified_temp}°C\n")
time.sleep(3) # Delay before next read
except Exception as e:
print("Error:", e)
time.sleep(2)