Datalogger with Graphical plotting

The place to share and discuss your Ultibo projects.
Gavinmc42
Posts: 1662
Joined: Sun Jun 05, 2016 12:38 pm
Location: Brisbane, Australia

Datalogger with Graphical plotting

Postby Gavinmc42 » Wed Jul 13, 2016 7:52 am

This is a temperature data logger example, using the new graphical console code.
It has two text consoles and a graphical console window.

Temperature sensor is a TMP112, these can have 4 addresses, so you can have 4 sensors.
TMP175 will give you 27 sensors on i2c or bit-bang a 1 Wire sensor?

Basic functions of logging to a csv file on the SD card and plotting the temperature in a Graphics console window.
Logging every 5 seconds is overkill for this application, but good for testing other apps.
No idea how fast it could go. Digital CRO, logic analyser, i2/SPI/Uart debugger?
Would be best to save to RAM disk then write to card later.
Could make some small interesting gadgets, loggers with a Zero.
Zero based Tricorder? Anyone want to do a Startrek font?

Scaling is pretty basic, 0-40 degree C and 24 hour plotting.
Only plotting points, drawing a line plot will take a bit more effort as the lines can only be vertical and horizontal at the moment.
Actually it is plotting a 2x2 pixel block, a single pixel is very small, hard to see.

Auto scaling would be ideal, with compiled pascal it would be fast.
This was a only a few hours work, more to do yet.
Has a bit of scaling debug info outputting in the top left console.
Still need to make a new log file every day at 00:00:00 hr and to format it correctly for the IoT webserver example that needs to be added.

Using the Graphical console code to make a basic GUI should be easy as long as you want rectangle buttons ;)
Did a little bit of testing, need to play around with text on buttons, to get font color and background color to match the button color etc.
Bitmap images would be one way to do fancy buttons.

Mouse over plot value reading?

Code: Select all

program Templogger;

{$mode objfpc}{$H+}

{ Raspberry Pi Application                                                     }
{  Add your program code below, add additional units to the "uses" section if  }
{  required and create new units by selecting File, New Unit from the menu.    }
{                                                                              }
{  To compile your program select Run, Compile (or Run, Build) from the menu.  }

uses
  RaspberryPi,
  GlobalConfig,
  GlobalConst,
  GlobalTypes,
  Platform,
  Threads,
  Console,
  GraphicsConsole,
  Framebuffer,
  Devices,
  I2C,
  SysUtils,
  DateUtils,
  Classes,
  Ultibo
  { Add additional units here };

var
 Logfile: Textfile;
 WindowHandle:TWindowHandle;
 WindowHandle2:TWindowHandle;
 Handle:TWindowHandle;
 I2CDevice:PI2CDevice;
 Temp:Word;
 hsb:Byte;
 lsb:Word;
 tmp:Byte;
 Gwidth:Word;
 Gheight:Word;
 Tickmark:Word;
 Timex:Word;
 Degreey:Word;
 Plotx:Word;
 Ploty:Word;
 Xmin:Word;
 Xmax:Word;
 Ymin:Word;
 Ymax:Word;
 Ytic:Word;
 Xtic:Word;
 Xlabel:Word;
 Ylabel:Word;
 Data:LongWord;
 Filename:String;
 Startext:String;
 Counter:LongWord;
 Hour,Min,Sec,HSec : word;

begin
  { Add your program code here }
 FRAMEBUFFER_CONSOLE_DEFAULT_DESKTOPCOLOR:=COLOR_BLUE;
 WindowHandle:=ConsoleWindowCreate(ConsoleDeviceGetDefault,CONSOLE_POSITION_TOPLEFT,True);
 WindowHandle2:=ConsoleWindowCreate(ConsoleDeviceGetDefault,CONSOLE_POSITION_TOPRIGHT,True);
 Handle:=GraphicsWindowCreate(ConsoleDeviceGetDefault,CONSOLE_POSITION_BOTTOM);

 I2CDevice:=PI2CDevice(DeviceFindByDescription('BCM2835 BSC1 Master I2C'));

 {Initialize a variable so we can count how long we've been waiting}
 Counter:=0;

 {Let's wait for a while for the time to be updated}
 while YearOf(Now) < 2000 do
  begin
   {Sleep for a second}
   Sleep(1000);

   {Update our counter}
   Inc(Counter);

   {Check how long we have waited}
   if Counter > 90 then
    begin
     {Print a failure message on the console}
     ConsoleWindowWriteLn(WindowHandle,'Sorry, failed to get the time after 90 seconds. Is the network connected?');

     {Break out of the loop and continue}
     Break;
    end;
  end;

 {Now we can set the timezone to another place and see what the time is there}
   {Australia}
   ConsoleWindowWriteLn(WindowHandle,'Setting Timezone to "AUS Eastern Standard Time"');
   SetCurrentTimezone('AUS Eastern Standard Time');
   ConsoleWindowWriteLn(WindowHandle,'The date and time is now ' + FormatDateTime(DefaultFormatSettings.LongDateFormat + ' ' + DefaultFormatSettings.LongTimeFormat,Now));
   ConsoleWindowWriteLn(WindowHandle,'');

 while not DirectoryExists('C:\') do
  begin
   {Sleep for a second}
   Sleep(1000);
  end;
 ConsoleWindowWriteLn(WindowHandle,'C:\ drive is ready');
 ConsoleWindowWriteLn(WindowHandle,'');

 ConsoleWindowWriteLn(WindowHandle2,'Output goes here');

 DecodeTime(Time,Hour,Min,Sec,HSec);
 ConsoleWindowWriteLn(WindowHandle,'Current time');
 ConsoleWindowWriteLn(WindowHandle, IntToStr(Hour) +  ':' + IntToStr(Min) + ':' + IntToStr(Sec));



 Gwidth:=GraphicsWindowGetWidth(Handle);
 Gheight:=GraphicsWindowGetHeight(Handle);

 Xmin:=5 * Gwidth div 100;
 Xmax:=95 * Gwidth div 100;
 Ymin:=10 * Gheight div 100;
 Ymax:=90 * Gheight div 100;

 ConsoleWindowWriteLn(WindowHandle, 'Width = ' + IntToStr(Gwidth));
 ConsoleWindowWriteLn(WindowHandle, 'Height = ' + IntToStr(Gheight));

 Startext:='Start';

 for Tickmark := 0 to 8 do
     begin
          Ytic:=Ymin + Tickmark * 10 * Gheight div 100;
          Ylabel:=40 - Tickmark * 5;
          GraphicsWindowDrawLine(Handle,Xmin,Ytic,Xmax,Ytic,COLOR_SILVER,1);
          GraphicsWindowDrawText(Handle,IntToStr(Ylabel),Xmin - 20,Ytic - 8 );
     end;

 for Tickmark := 0 to 24 do
     begin
          Xtic:=Xmin + Tickmark * 15 * Gwidth div 400;
          GraphicsWindowDrawLine(Handle,Xtic,Ymin,Xtic,Ymax,COLOR_SILVER,1);
          GraphicsWindowDrawText(Handle,IntToStr(Tickmark),Xtic - 8,Ymax );
     end;

 Filename:='C:\www\logs\data.csv';
 //AssignFile(Logfile, Filename );
 //Rewrite(Logfile);
 //writeln(Logfile, 'Date,Time,Temp1,Temp2,Temp3,Temp4');
 //Close(Logfile);

 ConsoleWindowWriteLn(WindowHandle,'Logfile found');

 while True do
 begin
 if I2CDeviceStart(I2CDevice,100000) <> ERROR_SUCCESS then
  begin
   //Error Occurred
       ConsoleWindowWriteLn(WindowHandle,'i2c error!!');
  end
 else
  begin
   //No  error, ready to use I2C
   Data:=2;
   if I2CDeviceRead(I2CDevice,$49,@Temp,SizeOf(Word),Data) = ERROR_SUCCESS then

   begin
        DecodeTime(Time,Hour,Min,Sec,HSec);
        Timex:=Hour * 60 + Min;
        Plotx:= Timex * 9 * Gwidth div 14400 + Xmin;

        hsb:= Temp MOD 256;
        tmp:= Temp DIV 256;
        lsb:= tmp * 625 DIV 16;

        Degreey:=hsb * 16 + tmp div 16;
        Ploty:= Ymax - (Degreey * Gheight div 800);


        ConsoleWindowWriteLn(WindowHandle, 'Plotx : ' +IntToStr(Plotx) + ' Ploty : ' + IntToStr(Ploty));

        // open file to append data
        AssignFile(Logfile, Filename );
        Append(Logfile);

        if lsb = 625 then
           begin
                ConsoleWindowWriteLn(WindowHandle2, 'Time is : '+ DateTimeToStr(Now) + ', Temperature is:'+IntToStr(hsb) +'.0' +IntToStr(lsb));
                writeln(Logfile, DateTimeToStr(Now) + ',' + IntToStr(hsb) + '.0' +IntToStr(lsb));
           end
        else
           begin
                ConsoleWindowWriteLn(WindowHandle2, 'Time is : '+ DateTimeToStr(Now) + ', Temperature is:'+IntToStr(hsb) +'.' +IntToStr(lsb));
                writeln(Logfile, DateTimeToStr(Now) + ',' + IntToStr(hsb) + '.' +IntToStr(lsb));
           end;
        Close(Logfile);
        GraphicsWindowDrawBlock(Handle, Plotx, Ploty, Plotx + 1, Ploty + 1, COLOR_RED);
        //GraphicsWindowDrawPixel(Handle, Plotx, Ploty, COLOR_RED);
    end;

  end;
 MillisecondDelay(5000);
 end;
 {We're not doing a loop this time so we better halt this thread before it exits}
 ThreadHalt(0);

end.
                     
Gavinmc42
Posts: 1662
Joined: Sun Jun 05, 2016 12:38 pm
Location: Brisbane, Australia

Re: Datalogger with Graphical plotting

Postby Gavinmc42 » Mon Aug 15, 2016 3:22 am

Also look in the Advanced example code for the Sensormatic300.
4 console windows, one with plotting plus web server etc.

Return to “Projects”

Who is online

Users browsing this forum: No registered users and 1 guest