Network Data Logging Suite

This suite of programs takes data from a sensor and saves it to a text file on a network. Each sensor reading is time stamped. The suite illustrates the basic principles involved in sending sensor data to a networked file or database.
The first program involved is a microcontroller program, written in PicBasic Pro, tested on a PIC18F258. It waits for serial input from an external program. Then it reads its analog sensor, and sends the result out in two bytes.
The second program is the same microcontroller code in Wiring/Arduino, thanks to Jamie Allen for the cleanup.
The third program involved is a desktop computer program, written in Processing. It requests data via its serial port from the microprocessor and sends that data to a CGI program on a web server. It passes the data into the CGI using an HTTP GET request. This program only sends every three seconds, so as not to overwhelm the server with hits.
The fourth program is a CGI (common gareway interface) program, written in PHP. It takes in data from an HTTP GET request and appends it to a text file, along with the time the request was received. Note that this program does not check to see how big the file is, or whether the incoming data is properly formatted, so it isn’t terribly secure.
The fifth program is another PHP script that logs the data to a mySQL database. Running this doesn’t require any change in the microcontroller code, but it does require a slight change in the Processing code. The change is in the sentToNet() method, and is noted below.

Technorati Tags: ,


Continue reading “Network Data Logging Suite”

Weighted averaging of an analog input (Smoothing)

Here’s an example that filters an analog sensor reading by taking a weighted average of samples of the sensor. It’s based on this algorithm:

 filteredValue = x * rawValue + (1-x)*lastFilteredValue;

Where X is a value between 0 and 1 that indicates how reliable the new raw value is. If it’s 100% reliable, X = 1, and no filtering is done. If it’s totally unreliable, x = 0, and the raw result is filtered out. Examples for Wiring and PicBasic Pro follow:

Written in C for Arduino

Continue reading “Weighted averaging of an analog input (Smoothing)”

Peak Finding

updated 9 Sept 2014

This example finds the peak value of an analog sensor over time. It assumes the sensor is moving in a simple curve.

The peak value is initially set at zero. The program checks to see that the current value is greater than the current peak value. If so, then it saves the current value as a peak.  If the current value is below a given threshold and the current peak value is above it, then the current peak is taken as the final peak. This example also includes a noise value, that allows you to adjust for sensor fluctuation.

The graph below gives you a visual sense of how the program operates. At readings 1, 2, and 3, the latest reading becomes the new peak. At reading 4, the peak remains unchanged because reading 4 is less than the last recorded peak. But the peak isn’t reported until reading 5, when the current reading goes below the threshold. This is the one disadvantage to this peak finding method: you don’t know you’ve had a peak until after it’s over.

peak_explained

Continue reading “Peak Finding”

Edge Detection

Most of the time, you don’t need to know whether a switch is on or off so much as you need to know when it changed. You want to know when it turned on or when it turned off. In other words, you want to find the edge of the transition from on to off, or vice versa. This is often called edge detection.

The basic idea is that you check not only what the state of the switch is, but what the state was the last time you checked, and compare the two. Here’s an example in PicBasic Pro:

Continue reading “Edge Detection”

Debouncing a digital input

Any switch is made of two conductive mechanical contacts that are either touching each other (closed circuit) or not (open circuit). When they make contact, the contact is not always perfect. For the fraction of s second before the contact is complete, the circuit may make contact and be broken several times. When you read this input on a microcontroller, you’ll see a rapid switching from 0 to 1 over a few milliseconds until the contact is final.

Continue reading “Debouncing a digital input”

Random Numbers and Physical Computing

Most microcontrollers don’t have a random function. Random functions are not truly random, they’re actually a complex mathematical formula that results in a number that “seems” random. That can take up lots of processing time, so it’s usually the first function to go when writing a microprocessor language.

In fact, most of what you do in programming physical computing projects is to figure out how to deal with the world’s natural randomness and make it look smooth. A photoresistor read through an analog-to-digital converter, for example, will never give you a nice steady number, it always fluctuates with tiny changes in lighting that your eye can’t see. Your consciousness is a great leveller for the sensors that are your eyes, ears, skin, nose, and taste buds When you move a photoresistor from one room to another, your readings will be totally different, and all of a sudden, you have to re-calculate what is “average” and what constitutes the lighting change that you want. And that’s just one of many examples. The fact is, data from sensors is filled with the noise of the real world. Plan for it in advance.

Technorati Tags: , ,


Continue reading “Random Numbers and Physical Computing”