Datalogger with Graphical plotting

The place to share and discuss your Ultibo projects.
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.  }

  { Add additional units here };

 Logfile: Textfile;
 Hour,Min,Sec,HSec : word;

  { Add your program code here }

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

 {Initialize a variable so we can count how long we've been waiting}

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

   {Update our counter}

   {Check how long we have waited}
   if Counter > 90 then
     {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}

 {Now we can set the timezone to another place and see what the time is there}
   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));

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

 ConsoleWindowWriteLn(WindowHandle2,'Output goes here');

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


 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));


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

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

 //AssignFile(Logfile, Filename );
 //writeln(Logfile, 'Date,Time,Temp1,Temp2,Temp3,Temp4');

 ConsoleWindowWriteLn(WindowHandle,'Logfile found');

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

        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 );

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

 {We're not doing a loop this time so we better halt this thread before it exits}

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