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 (Adjust as needed)
R_NOMINAL = 10000 # 10kΩ thermistor
B_COEFFICIENT = 3950 # Beta value
SERIES_RESISTOR = 10000 # 10kΩ pull-up resistor
VCC = 3.3 # Supply voltage
# Simulate Wi-Fi Connection
wifi_info = ("192.168.4.1", "255.255.255.0", "192.168.4.1", "8.8.8.8")
print(f" Connected to Wi-Fi! {wifi_info}\n")
# Simulate Attacker Interception
print(" Attacker Intercepting on 192.168.4.1:8080...")
print(" Server Listening on 192.168.4.1:8081...\n")
def read_temperature():
"""Reads ADC value and converts it to temperature using thermistor formula."""
raw_value = adc.read() # Read ADC value (0-4095)
voltage = raw_value * (VCC / 4095) # Convert to voltage (0-3.3V)
# Convert voltage to resistance
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 temperature
while True:
try:
# Read temperature from NTC sensor
temperature = read_temperature()
print(f" Client Sending: Temperature={temperature:.2f}°C")
time.sleep(1)
# Simulate MITM Attack
intercepted_temp = temperature
print(f" Intercepted: Temperature={intercepted_temp:.2f}°C")
# Modify the intercepted data
modified_temp = temperature + 40 # Example: increasing temp by 40°C
print(f" Modified Data: Temperature={modified_temp:.2f}°C")
time.sleep(1)
# Simulate Server Receiving Data
print(f" Server Received: Temperature={modified_temp:.2f}°C")
time.sleep(3)
except Exception as e:
print("Error reading temperature sensor:", e)
time.sleep(2)