"8th" - Embedded and more (part 1)

Overview

This is the first in a series of articles in which we'll explore using 8th with embedded as well as desktop and server systems, in an integrated project. It is not intended to be a tutorial on how to use 8th: for that, you can see the tutorials, the manual, or the forum.

If you would like an introduction to 8th, please view "8th - a Gentle Introduction to a Modern Forth", or "8th - Grokking the REPL", on the Arm Community.

The Project

What we want to accomplish in this project is to hook up some environmental sensors (temperature, etc) to a Raspberry Pi so we can take periodic readings. The readings will be periodically sent to a server, and the server will give web-access to the information via HTTPS.

All the code for the project will be done in 8th.

To be demonstrated:

  • I2C communication between the RPI and peripherals
  • debugging and ensuring proper functioning of code on a per-word basis
  • creation of standalone executables
  • network communications
  • writing server-side code
  • fail-safe techniques

Prerequisites

In order to follow along in this series, you'll need a copy of 8th. The minimum version which will work for this is the Hobbyist Edition (since it includes support for GPIO and I2C, which the Free version does not).

However, for a limited time you can purchase the current Professional Edition, and automatically receive the Embedded edition as soon as it's released. Details here.

In addition to 8th, you'll need the following hardware:

You'll also need wires, solder etc… some (hardware) assembly required!

For the server side, you'll need a laptop or a desktop computer (or a RPI) whose IP address is visible to the RPI on which the sensor code will be running. Any computer will do.

Raspberry Pi 2 board

First steps: program organization

We're going to have two distinct programs, one running on the RPI as a data-collector, and the other running on a server as the external (web) interface. So you'll start by creating a project folder with two sub-folders. I'm using Linux or macOS for development, so the commands I show will be different from what you would use on Windows; nevertheless, you can also use Windows for development purposes:

mkdir proj
mkdir proj/serv
mkdir proj/data

Create a single file proj/data/data.8th using whatever text-editor you like. I prefer vim, but anything which saves plain-text will work. The contents of the file should look like:


\ The data-collector

: collect-data
"collect" log ;

: save-data
"save" log ;

: send-data
"send" log ;

: app:main
repeat
collect-data
save-data
send-data
5 sleep
again ;


If you run this by typing 8th proj/data/data.8th you'll see this log output every five seconds:

collect
save
send

In subsequent posts, we'll expand the functionality by incremental improvement, testing as we go. That is, in my opinion, the preferred methodology when developing applications in general, and in 8th (and Forth) in particular.

Conclusion

In the next edition, we'll expand the data collector and start putting together the hardware, and testing the bits and pieces work together. Until then, make sure to get 8th

Get 8th: One Effort, Multiple Platforms

Anonymous