stavrosdee
Posts: 3
Joined: Sun Jun 01, 2014 12:29 am

Upload data to server when online

Sun Jun 01, 2014 12:34 am

Hi.
I am using a Raspberry Pi to log some sensor activity. However, I would like to do the following:

- Store the captured data locally in a file (whenever a new entry appears)
- Check to see if network is available
- If network is available, upload the new data on the server

However, I am not sure how I can proceed with this project. Any suggestions?

User avatar
DeeJay
Posts: 2027
Joined: Tue Jan 01, 2013 9:33 pm
Location: East Midlands, UK

Re: Upload data to server when online

Sun Jun 01, 2014 7:36 am

What have you tried? Where have you got stuck?
How To Ask Questions The Smart Way: http://www.catb.org/~esr/faqs/smart-questions.html
How to Report Bugs Effectively: http://www.chiark.greenend.org.uk/~sgtatham/bugs.html

User avatar
DougieLawson
Posts: 36178
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Upload data to server when online

Sun Jun 01, 2014 8:01 am

Use SQLite3 to hold the data.

Use MQTT to send when the network comes active.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

stavrosdee
Posts: 3
Joined: Sun Jun 01, 2014 12:29 am

Re: Upload data to server when online

Mon Jun 02, 2014 8:03 pm

At the moment I am using this code to save the data and send them to Xively. I don't understand how to upload them ONLY when the system is online. Any examples I could look at?
Thanks

Code: Select all

import os
import xively
import subprocess
import time
import datetime
import requests

FEED_ID = "xxx"
API_KEY = "xxx"

# initialize api client
api = xively.XivelyAPIClient(API_KEY)

# function to read the temperature from ds18b20 temperature sensor on i2c def read_temperature():
   tempfile = open("/sys/bus/w1/devices/YOURTEMPERATURESENSORID/w1_slave")
   thetext = tempfile.read()
   tempfile.close()
   tempdata = thetext.split("\n")[1].split(" ")[9]
   temperature = float(tempdata[2:])
   temperature = temperature / 1000
   return temperature

# function to return a datastream object. This either creates a new datastream,
# or returns an existing one
def get_datastream(feed):
  try:
    datastream = feed.datastreams.get("TemperatureSensor")
    return datastream
  except:
    datastream = feed.datastreams.create("TemperatureSensor", tags="temperature")
    return datastream

# main program entry point - runs continuously updating our datastream with the
# latest temperature reading
def run():
  feed = api.feeds.get(FEED_ID)

  datastream = get_datastream(feed)
  datastream.max_value = None
  datastream.min_value = None

  while True:
    degreesCelcius = read_temperature()
    datastream.current_value = degreesCelcius
    datastream.at = datetime.datetime.utcnow()
    try:
      datastream.update()
    except requests.HTTPError as e:
      print "HTTPError({0}): {1}".format(e.errno, e.strerror)

    time.sleep(10)

run()

User avatar
DeeJay
Posts: 2027
Joined: Tue Jan 01, 2013 9:33 pm
Location: East Midlands, UK

Re: Upload data to server when online

Tue Jun 03, 2014 5:25 am

Isn't Xively always online? In any case, you are checking for the success of the upload - if it fails, can't you retry the transfer later?
How To Ask Questions The Smart Way: http://www.catb.org/~esr/faqs/smart-questions.html
How to Report Bugs Effectively: http://www.chiark.greenend.org.uk/~sgtatham/bugs.html

stavrosdee
Posts: 3
Joined: Sun Jun 01, 2014 12:29 am

Re: Upload data to server when online

Tue Jun 03, 2014 9:49 am

Sometimes the network may not be available, but I don't want to loose data then. Therefore I am thinking of saving the data locally, and then check if the system is online. When online, to send the data again.
But I am not sure HOW to check if the system is online. Thanks

Return to “General discussion”