Issue uploading data using FTP and Python

8 views (last 30 days)
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

Hassaan
Hassaan on 9 Jan 2024
Edited: Hassaan on 9 Jan 2024
  1. Rate Limits: ThingSpeak limits data updates to every 15 seconds for free accounts. Exceeding this rate will cause only the initial records to be accepted, with subsequent updates being blocked until the rate limit interval has passed.
  2. Batch Updates: To efficiently handle large datasets, ThingSpeak allows sending multiple data points in one POST request, which is a preferred method over individual updates for each record.
  3. Network Latency and Response Time: Continuously sending data without pauses can lead to problems due to network delays and server response times. Introducing a delay between consecutive data transmissions can help ensure that each piece of data is properly processed.
import time
import pandas as pd
import requests
import ftplib
import io
# ThingSpeak API endpoint
update_url = 'https://api.thingspeak.com/update'
# Rate limit in seconds (15 for a free account)
rate_limit = 15
# FTP server credentials
ftp_server = 'your_ftp_server.com'
username = 'your_username'
password = 'your_password'
ftp_file_path = 'path_to_your_file.csv'
# Your ThingSpeak API key
api_key = 'your_api_key'
# Connect to FTP and download the file into memory
try:
ftp = ftplib.FTP(ftp_server)
ftp.login(username, password)
memory_file = io.BytesIO()
ftp.retrbinary('RETR ' + ftp_file_path, memory_file.write)
ftp.quit()
memory_file.seek(0)
data = pd.read_csv(memory_file)
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}')
# Wait for the rate limit to refresh before sending the next request
time.sleep(rate_limit)
except ftplib.all_errors as e:
print(f"FTP error: {e}")
except Exception as e:
print(f"An error occurred: {e}")
Please replace 'your_ftp_server.com', 'your_username', 'your_password', 'path_to_your_file.csv', and 'your_api_key' with your actual FTP server details and ThingSpeak API key.
Remember, the rate_limit variable should be set according to the rate limit of your ThingSpeak account. If you have a different rate limit, adjust the value of rate_limit accordingly. If you are sending data too frequently, ThingSpeak may ignore the data sent after the rate limit is exceeded.
---------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
  2 Comments
James
James on 9 Jan 2024
Thank you Muhammad Hassaan Shah! I understand, it is now transmitting each with a 15 sec delay. It is processing now.
So I assume ThingSpeak stores uploaded data. I now need to figure out how to only get recent data from FTP and only upload that is small manageable batches.
Thank you
Christopher Stapels
Christopher Stapels on 10 Jan 2024
Edited: Christopher Stapels on 10 Jan 2024
The bulk updatee endpoint is different than the /up[date endpoint shown above. You can see the details on the JSON bulk update page. There is also a similar page for .csv bulk updates if you are interested.

Sign in to comment.

More Answers (0)

Communities

More Answers in the  ThingSpeak Community

Categories

Find more on Write Data to Channel in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!