BreakingExpress

Use your Raspberry Pi as an information logger

Data logging might be completed for varied causes. In a earlier article, I wrote about how I monitor the electrical energy consumption of my family. The Raspberry Pi platform is an ideal match for such purposes because it permits communication with many sorts of analog and digital sensors. This article reveals the right way to log the CPU temperature of a Raspberry Pi and create a spreadsheet-based report on demand. Logging the CPU temperature will not require any further boards or sensors.

Even with no Raspberry Pi, you possibly can comply with the steps described right here when you exchange the particular elements of the code.

Setup

The code relies on Pythonic, a graphical Python programming fronted. The best technique to get began with Pythonic is to obtain and flash the Raspberry Pi image. If you do not have a Raspberry Pi, use one of many different set up strategies talked about on the GitHub web page (e.g., Docker or Pip).

Once put in, join the Raspberry Pi to the native community. Next, open the web-based GUI in a browser by navigating to http://pythonicrpi:7000/.

You ought to now see the next display:

Download and unzip the example obtainable on GitHub. The archive consists of a number of file varieties.

Use the green-marked button to add the current_config.json, with the yellow-marked button add the XLSX file and the remaining *.py recordsdata.

You ought to have this configuration in entrance of you after you add the recordsdata:

Implementation

The software might be separated into two logical elements: Logging and report technology. Both elements run independently from one another.

Logging

The high a part of the configuration might be summarized because the logging setup:

Involved parts:

  • GuideScheduler – 0x0412dbdc: Triggers linked parts on startup (or manually).
  • CreateDesk – 0x6ce104a4: Assembles an SQL question which creates the working desk (if not already existent).
  • Scheduler – 0x557616c2: Triggers subsequent aspect each 5 seconds.
  • DataAcquisition – 0x0e7b8360: Here we gather the CPU temperature and assemble an SQL question.
  • SQLite – 0x196f9a6e: Represents an SQLite database, accepts the SQL queries.

I’ll take a better take a look at DataAcquisition – 0x0e7b8360. Open the built-in net editor (code-server) by navigating to http://pythonicrpi:8000/. You can see all of the element-related *.py recordsdata within the left pane. The DataAcquisition aspect relies on the kind Generic Pipe. Open the file with the associated id:

generic_pipe_0x0e7b8360.py

In this aspect, accountable for studying the CPU temperature, you possibly can uncomment the traces of code relying on whether or not you are working this on a Raspberry Pi or not.

The above code produces an SQL question that inserts a row within the desk my_table containing the Unix timestamp in seconds and the precise CPU temperature (or a random quantity). The code is triggered each 5 seconds by the earlier aspect (Scheduler – 0x557616c2). The SQL question string is forwarded to the linked SQLite – 0x196f9a6e aspect, which applies the question to the associated SQLite database on the file system. The course of logs the CPU temperature within the database with a sampling fee of 1/5 samples per second.

Report technology

The backside community generates a report on request:

Involved parts:

  • GuideScheduler – 0x7c840ba9: Activates the linked Telegram bot on startup (or manually).
  • Telegram – 0x2e4148e2: Telegram bot which serves an interface for requesting and offering of stories.
  • GenericPipe- 0x2f78d74c: Assembles an SQL question comprising the info of the report.
  • SQLite – 0x5617d487:
  • ReportGenerator- 0x13ad992a: Create a XLSX-based report primarily based on the info.

The instance code comprises a spreadsheet template (report_template.xlsx) which additionally belongs to this configuration.

Note: To get the Telegram bot working, present a Telegram bot token to speak with the server. core.telegram.org describes the method of making a bot token.

The Telegram aspect outputs a request as a Python string when a person requests a report. The GenericPipe- 0x2f78d74c aspect that receives the request assembles a SQL question which is forwarded to the SQLite – 0x5617d487 aspect. The precise knowledge, which is learn primarily based on the SQL question, is now despatched to the ReportGenerator- 0x13ad992a, which I’ll take a better take a look at:

generic_pipe_13ad992a.py

def execute(self):

        path = Path.dwelling() / 'Pythonic' / 'executables' / 'report_template.xlsx'

        attempt:
                wb = load_workbook(path)
        besides FileNotFoundError as e:
                recordDone = Record(PythonicError(e), 'Template not discovered')
                self.return_queue.put(recordDone)
                con.shut()
                return
        besides Exception as e:
                recordDone = Record(PythonicError(e), 'Open log for particulars')
                self.return_queue.put(recordDone)
                con.shut()
                return

        datasheet = wb['Data']

        # create an iterator over the rows within the datasheet
        rows = datasheet.iter_rows(min_row=2, max_row=999, min_col=0, max_col=2)

In the primary half, I take advantage of the load_workbook() of the openpyxl library to load the spreadsheet template. If efficiently loaded, I purchase a reference to the precise sheet within the datasheet variable. Afterward, I create an iterator over the rows within the datasheet, which is saved within the variable rows.

        # Convert unix time [s] again right into a datetime object, returns an iterator
        reportdata_dt = map(lambda rec: (datetime.datetime.fromtimestamp(rec[0]), rec[1]), self.enterData)
       
        # iterate until the primary iterator is exhausted
        for (dt, val), (row_dt, row_val) in zip(reportdata_dt, rows):
                row_dt.worth = dt
                row_val.worth = val

        reportDate = datetime.datetime.now().strftime('%d_percentb_percentY_percentH_percentM_percentS')
        filename = 'report_{}.xlsx'.format(reportDate)
        filepath = Path.dwelling() / 'Pythonic' / 'log' / filename
        wb.save(filepath)
        wb.shut()
       
        recordDone = Record(filepath, 'Report saved beneath: {}'.format(filename))
        self.return_queue.put(recordDone)

The final half begins with the variable reportdata_dt: The variable holds an iterator which, when used, converts the uncooked Unix timestamp of the enter knowledge from the SQLite database (self.inputdata) again to a Python datetime object. Next, I zip the reportdata_dt iterator with the beforehand created rows iterator and iterate until the primary of them is exhausted, which ought to be reportdata_dt. During iteration, I fill the columns of every row with the timestamp and the worth. In the final step, I save the spreadsheet with a filename consisting of the particular date and time and ahead the filename to the Telegram – 0x2e4148e2 aspect.

The Telegram – 0x2e4148e2 then hundreds the file from disk again into reminiscence and sends it to the person who requested the report. This video reveals the entire process:

The report the person receives seem like this:

Wrap up

This article reveals the right way to simply convert the Raspberry Pi into an information logger. The Raspberry Pi platform means that you can work together with sensors of any type, enabling you to watch bodily values in addition to computed values. Using spreadsheets as the premise on your stories provides you a variety of flexibility and makes these stories very customizable. The openpyxl library together with Pythonic makes it easy to automate this course of.

Exit mobile version