Lazarus, GPIO and 1-wire


10 posts
by feverish » Fri Apr 05, 2013 3:46 pm
Has anyone written a successful application using Lazarus, GPIO and 1-wire or OWFS direct control. please? I'd be grateful for any example code.
Posts: 251
Joined: Wed Jun 27, 2012 2:29 pm
by bobdee » Mon Apr 08, 2013 8:24 am
I haven't actually done this yet, but if you're getting a bandwagon going I'd like to join it. I've got a prototype heating controller working well via a bash script, but now want to make it a bit more grown up by porting it to FP. (And I mean FP, not Lazarus; it won't need any window-based front end).

Is this the sort of thing you want to work on?
Posts: 3
Joined: Mon Apr 08, 2013 8:16 am
by feverish » Mon Apr 08, 2013 10:32 am
I want to SSH into the remote Raspi and use the Raspi-based Lazarus GUI as a reporter and controller. The aim is to self-learn Raspi and its Linux, prior to adding such things to our existing wildlife school-demonstration activities. For that reason, I want to try to keep the cost down, the Raspi may seem cheap enough but adding third-party peripherals makes prices soar.

The only code I can turn up in Lazarus without using external ADC, or 1-wire master controller chips, which I want for reasonably consistent and accurate temperature measurement, is for ON/OFF operation, which I also want to use, and that latter bit seems easy enough - <http://elinux.org/RPi_Low-level_peripherals> .

WIRINGPI, written in C, seems popular around the forums. It has Arduino-like coding but there is an FPC wrapper and example LAZWIRINGPI, for that, for FPC/Lazarus users, which is self-explanatory.

There have also been a few tutorials on web-based Javascript applications.

I have Arduinos and know how to link them in, thanks to some excellent tutorials around these forums, if I use LM35 probes, but for this exercise I'd prefer to go the direct 1-wire route if ever I can find out how to do it. Meantime, I am using stand-alone Arduinos and broadcasting barbecue controllers, without the remote visuals.

Hope this rambling info may have been of some use to you.

A possible alternative might be to use a more sophisticated ARM-based MK802 version IV, with its built-in facilities, one of which is arriving soon. I propose to attempt to repeat the exercise using it, though the price might put it beyond the reach of local schoolkids.
Posts: 251
Joined: Wed Jun 27, 2012 2:29 pm
by bobdee » Mon Apr 08, 2013 11:05 am
OK, let me lay out where I am. Or at least where the plan says I am...

I'm installing a heating system, controlled by the Pi. I have DS18B20 temperature sensors connected to the Pi's GPIO4 (WiringPi 7). That pin is hardwired for the DS18B20, but you can daisy chain several (they are each addressable by their unique ID numbers). Enough DS18B20s will let the controller collect readings of room temp, solar panel temp, hot water cylinder etc.

Having collected the data the FP program does its logic, and uses a Sainsmart relay board to switch on various mains devices - mostly pumps for different bits of the heating circuit.

I have a prototype wired up on a breadboard (with 7 leds where the relay will go) and a little bash script to record the room temperature, switch on the circuits as required, and display a nice little text-only screen to show ambient temperature and what's currently on/off. I can SSH into the Pi to set it up and modify it, and I'm going to provide a webpage (served up by a Lighttpd server on the Pi) to give me a means of checking and setting the whole system from any device on the network - even my Android smartphone.

All very neat. Clearly bash is OK for proof-of-concept but a little too klunky for production use, so I want to switch to FP. And I say FP not Lazarus because I don't want the processing overhead of windows, buttons, graphic object event handlers etc for a program that just sits and gets on with its job quietly. FP is perfect for it (I used to code in Turbo Pascal back in the last century).

Total cost of the bits (apart from the Pi) is about £20 so far, including 10 DS18B20s from China. I don't plan on spending much more.

BTW I am also hoping to develop this into something I can use for teaching 11-year olds at school.

Does it look like we are treading a similar path...?
Posts: 3
Joined: Mon Apr 08, 2013 8:16 am
by feverish » Mon Apr 08, 2013 4:01 pm
bobdee wrote: . . .

I'm installing a heating system, controlled by the Pi. I have DS18B20 temperature sensors connected to the Pi's GPIO4 (WiringPi 7). That pin is hardwired for the DS18B20, but you can daisy chain several (they are each addressable by their unique ID numbers). Enough DS18B20s will let the controller collect readings of room temp, solar panel temp, hot water cylinder etc.

Fine.
I have a prototype wired up on a breadboard (with 7 leds where the relay will go) and a little bash script to record the room temperature, switch on the circuits as required, and display a nice little text-only screen to show ambient temperature and what's currently on/off. I can SSH into the Pi to set it up and modify it, and I'm going to provide a webpage (served up by a Lighttpd server on the Pi) to give me a means of checking and setting the whole system from any device on the network - even my Android smartphone.

Seems, like me, your hardware is sufficiently organised.

All very neat. Clearly bash is OK for proof-of-concept but a little too klunky for production use, so I want to switch to FP. And I say FP not Lazarus because I don't want the processing overhead of windows, buttons, graphic object event handlers etc for a program that just sits and gets on with its job quietly. FP is perfect for it (I used to code in Turbo Pascal back in the last century).

I started with Dartmouth Basic, then to Forth, via compiled Basic to Delphi. PDP8's, homebrew 8085 kits, then Sinclairs, now use other-peoples bright ideas for PCs and now Linux.

Your bash code might be a help.

BTW I am also hoping to develop this into something I can use for teaching 11-year olds at school. Does it look like we are treading a similar path...?

Very similar paths, my primary one is for egg incubators, for endangered-species rehab and release, hence the need to measure temperatures and control rotator motors and heaters. A second need is for humidity control (increasing it) and producing warm vapour.
The hardware bit seems simple enough, and the use of GPIO pin 4 a given. We're also agreed on the use of DS18B20s for the 1-wire temp measurement. I am using solid-state relays in the Arduino scenario, but expect no real problems using Raspi's 3.3v GPIO pin ON/OFFSs to drive them. What is currently holding me back is lack of detailed fpc/Lazarus GPIOpin4 coding for the 1-wire initiation, reads, writes, and clearance. The fpsystem.pas code from 'used' unix and baseunix units also seems necessary (unless someone knows better) to effect this.
Posts: 251
Joined: Wed Jun 27, 2012 2:29 pm
by feverish » Tue Apr 09, 2013 9:17 pm
From 2006-ish, I have stumbled across a 'tutorial' for using 1-wire devices to measure temperature using a DS1820, written in Delphi4, and for the Windows version (what other sort was there in those days?). The author refers repeatedly to programming errors he has made, and for corrective work needing to be done, but in the absence of anything demonstrably better, when combined with the elinux.org GPIO low-level peripherals pages, may prove to be a potential gem. <http://sheepdogguides.com/dst2a.htm>
Posts: 251
Joined: Wed Jun 27, 2012 2:29 pm
by feverish » Fri Apr 12, 2013 10:26 pm
Solved.

The good folks from Cambridge University have already been there and done that, with direct drive from the GPIO, no need for master-controller, using w1_gpio et al, for relatively short but adequate cable lengths between the Raspi and the probe tip.
An excellent tutorial from them at:-
<http://www.cl.cam.ac.uk/freshers/raspberrypi/tutorials/temperature/#step-appendix-three>
Q.E.D.

enjoy! I am!
Posts: 251
Joined: Wed Jun 27, 2012 2:29 pm
by bobdee » Sat Apr 13, 2013 12:00 am
Well, for the record, below is my very rough bash script that: shows one temperature probe; switches on/off up to 7 relay circuits in a hard-coded sequence (using Gordon Henderson's WiringPi pin addresses); and provides a neat colour-coded status display on the terminal.

Code: Select all
#!/bin/bash
NONE='\033[00m'
RED='\033[01;31m'
GREEN='\033[01;32m'
YELLOW='\033[01;33m'
PURPLE='\033[01;35m'
CYAN='\033[01;36m'
WHITE='\033[01;37m'
BOLD='\033[1m'
UNDERLINE='\033[4m'

AtYx ()
#Force Cursor Position   <ESC>[{ROW};{COLUMN}f  ...so 8,10 would be: echo '\033[8;12f'
{
   if test ${4+defined}; then echo -e "$4"; fi
   echo -e -n "\033[$1;$2f$3\033[20;30f${NONE}"
}

clear
echo -e "                ${UNDERLINE}The Heat Bank project${NONE}\n"
echo ""
echo -e "      ${RED}Wood burning stove${NONE}, ${CYAN}Gas boiler${NONE} and ${YELLOW}solar hot water${NONE} contol panel."
# NEED TO END THE ESCAPED STUFF WITH: tput sgr0
setup ()
{
 AtYx 20 30 "SETTING UP"  "${YELLOW}"
 S=1
  for i in 8 9 0 2 3 12 14 ; do
   AtYx $(( 5 + $S )) 10 "Pump $S status is [   ]" "${PURPLE}"
   ((S++))
   gpio mode  $i out ;
  done
}

setup

temp=`cat /sys/bus/w1/devices/28-00000492b470/w1_slave | tail -n 1 | cut -d= -f2`
mytemp=`echo "scale=2; $temp/1000" | bc`
AtYx 22 3 "Temp is: $mytemp deg. C"

while true; do
 S=1
  for i in 8 9 0 2 3 12 14 ; do
   AtYx $(( 5 + $S )) 28 "OFF" "${RED}"
   ((S++))
   gpio write $i   0
   sleep 0.3
  done
  AtYx 20 30 "ALL TURNED OFF"  "${RED}"
  sleep 1

 S=1
  for i in 8 9 0 2 3 12 14 ; do
   AtYx $(( 5 + $S )) 28 "ON " "${GREEN}"
   ((S++))
   gpio write $i   1
   sleep 0.3
  done
  AtYx 20 30 "ALL TURNED ON "  "${GREEN}"
  sleep 1

done


And a first step at converting it all to pascal - a little terminal program to read and display the temperature at one, named probe:

Code: Select all
Program ShowDS18B20_temp; //polls a named temperature sensor and displays the temp in centigrade
Uses Crt, Unix;
Var temp,MySensor:string;

Function DS18B20_temp(ID:string) : string;
Var f:text; i:integer; s:string; tempfound:boolean; //c:char; l:longint; temperature:string;
begin
 tempfound:=false; i:=0;
 repeat
        Assign (f,'/sys/bus/w1/devices/28-' + ID + '/w1_slave');
        Reset(f);       Readln(f,s);    tempfound:=pos ('YES',s)>0;
        if tempfound then begin
                Readln(f,s);       i:=pos('t=',s) + 2;
                DS18B20_temp:=Copy(s,i,2) + '.' + Copy(s,i+2,2);
        end;
        i:=succ(i);
  until tempfound or (i=30);
end;

begin
        MySensor:='00000492b470';
        temp:=DS18B20_temp(MySensor);
        Writeln('Temp at probe "' + MySensor + '" is: ' + temp + 'C');
end.
Posts: 3
Joined: Mon Apr 08, 2013 8:16 am
by feverish » Sat Apr 13, 2013 5:07 pm
Lang may your lum reek. I'll post my gui version i.d.c. It was good to have come across someone with similar interests.
Posts: 251
Joined: Wed Jun 27, 2012 2:29 pm
by jwdietrich » Tue Apr 30, 2013 6:23 pm
@bobdee:

Great. Your code also well demonstrates the advantages of Unix that treats everything as file.
User avatar
Posts: 13
Joined: Wed Nov 30, 2011 11:52 am