Issue uploading data using FTP and Python
Show older comments
I have weather data that goes to an FTP site (only way to store it) I use Python to transfer from FTP to Thingspeak. If I upload all the data at once using import all my data comes into thingspeak. When I use Python I only get one maybe two records. Do I need to upload every minute?
sample code (manual push)
# Connect to FTP and download the file into memory
try:
ftp = ftplib.FTP(ftp_server)
ftp.login(username, password)
# Create an in-memory binary stream for the file
memory_file = io.BytesIO()
ftp.retrbinary('RETR ' + ftp_file_path, memory_file.write)
ftp.quit()
# Go to the start of the memory file
memory_file.seek(0)
# Read CSV file from memory
data = pd.read_csv(memory_file)
# Process and send each row of the CSV to ThingSpeak
for index, row in data.iterrows():
payload = {'api_key': api_key}
for i, value in enumerate(row):
payload[f'field{i+1}'] = value
response = requests.post(update_url, params=payload)
if response.status_code == 200:
print(f'Data sent successfully: {row}')
else:
print(f'Error sending data: {row}, Status Code: {response.status_code}')
except ftplib.all_errors as e:
print(f"FTP error: {e}")
except Exception as e:
print(f"An error occurred: {e}")
Accepted Answer
More Answers (0)
Communities
More Answers in the ThingSpeak Community
Categories
Find more on Write Data to Channel in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!