import urequests
import json
import network
import time
wifi_ssid = "Wokwi-GUEST"
wifi_password = ""
endpoint = "http://localhost:5000/ecg_arrhythmia_detection"
def read_csv():
with open('./simple_ecg_with_arrhythmia.csv', 'r') as file:
lines = file.readlines()
data = []
# Process each line in the CSV file
for line in lines:
line = line.strip()
values = line.split(',')
if len(values) >= 2:
data.append(float(values[1]))
return data
def connect_to_wifi(ssid, password):
wifi = network.WLAN(network.STA_IF)
wifi.active(True)
wifi.connect(ssid, password)
timeout=0
if not wifi.isconnected():
print("Connecting to WiFi...")
while (not wifi.isconnected() and timeout<10):
print(7-timeout)
timeout+=1
time.sleep(1)
if wifi.isconnected():
print("Connected to WiFi:", wifi.ifconfig())
return wifi.isconnected()
# Connect to Wi-Fi
if connect_to_wifi(wifi_ssid, wifi_password):
# Read CSV data
ecg = read_csv()
for i in ecg:
# Create a JSON payload
data_payload = {'ecg':i}
# Convert the payload to a JSON string
payload_json = json.dumps(data_payload)
# Set headers
headers = {'Content-Type': 'application/json'}
# Send the POST request
response = urequests.post(endpoint, data=payload_json, headers=headers)
# Print the response from the server
print(response)
# Close the response
response.close()
else: print("Not connected")