Program work only when started twice? lazarus / 21-General ---------------------------------------------------------------------- Ramses · 2021-02-02 15:08 Hi!     I have a small program to turn on a relay on my raspberry pi, but when i start or restart my raspberry board, the program dosent work the first time... Exemple: if i start my raspberry and run the program in lazarus, the relay is NOT working, but if i stop the program and restart it, then my relay work great, and continue to work each time i restart this program in lazarus... So when my raspberry board is turned on, the relay work only after i run the program twice Is there something i do wrong in my code? Thanks! Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, Unix; type   { TForm1 }   TForm1 = class(TForm)    ToggleBox1: TToggleBox;    procedure FormActivate(Sender: TObject);    procedure ToggleBox1Change(Sender: TObject);  private   public   end; var  Form1: TForm1;  gReturnCode: longint;  implementation {$R *.lfm} { TForm1 } procedure TForm1.FormActivate(Sender: TObject);begin  gReturnCode := fpsystem('echo "17" > /sys/class/gpio/export');  gReturnCode := fpsystem('echo "out" > /sys/class/gpio/gpio17/direction');end; procedure TForm1.ToggleBox1Change(Sender: TObject);begin   if ToggleBox1.Checked then  begin    gReturnCode := fpsystem('echo "1" > /sys/class/gpio/gpio17/value');  end  else  begin    gReturnCode := fpsystem('echo "0" > /sys/class/gpio/gpio17/value');  end;end; end. ---------------------------------------------------------------------- rvk · 2021-02-02 15:39 Why not use FormCreate instead of FormActivate? What's the result of gReturnCode in FormActivate when it doesn't work? (log it to a file of screen) BTW, it could also be that you need to put in a delay before you can really access gpio17 after setting the direction (but I'm not sure about that). But if that's the case you can just wait while your program is running and then press the button. ---------------------------------------------------------------------- Ramses · 2021-02-02 16:57 Quote from: rvk on February 02, 2021, 04:39:54 pmWhy not use FormCreate instead of FormActivate? What's the result of gReturnCode in FormActivate when it doesn't work? (log it to a file of screen) BTW, it could also be that you need to put in a delay before you can really access gpio17 after setting the direction (but I'm not sure about that). But if that's the case you can just wait while your program is running and then press the button. Thanks for your reply! After reading your reply. i tried to use FormCreate, but it does the exact same thing: relay work only after program is started twice for the delay, i tried to wait like 10 second after the program start, then i press the button: same problem you asked me to log the result of gReturnCode in FormActivate. but i dont know how to log (im a beginner with lazarus) Can you please explain me how to log? thanks again! ---------------------------------------------------------------------- rvk · 2021-02-02 17:19 https://www.freepascal.org/docs-html/rtl/unix/fpsystem.html So gReturnCode  should have the exit status. Try adding Showmessage(IntToStr(gReturnCode)); after both fpsystem calls. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i begin  gReturnCode := fpsystem('echo "17" > /sys/class/gpio/export');  Showmessage(IntToStr(gReturnCode));  gReturnCode := fpsystem('echo "out" > /sys/class/gpio/gpio17/direction');  Showmessage(IntToStr(gReturnCode));end; You could also add two labels to your form and do Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i begin  gReturnCode := fpsystem('echo "17" > /sys/class/gpio/export');  Label1.Caption := 'echo 17 status = ' + IntToStr(gReturnCode);  gReturnCode := fpsystem('echo "out" > /sys/class/gpio/gpio17/direction');  Label2.Caption := 'echo out status = ' + IntToStr(gReturnCode);end; ---------------------------------------------------------------------- Ramses · 2021-02-02 18:30 Thanks for your help! I have do it with the two label, so here is the result: First time i run the program after raspberry is started relay is NOT working and on the label i can read: QuoteLabel1 = echo 17 status = 0 Label2 = echo out status = 512 Then each time i close the program and run it again, the relay work and i can always read same thing on the label: QuoteLabel1 = echo 17 status= 256 Label2 = echo out status 0 ---------------------------------------------------------------------- rvk · 2021-02-02 19:01 Mmm, we need the wexitStatus. Can you do the following? (I havent got a machine handy) Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i begin  gReturnCode := fpsystem('echo "17" > /sys/class/gpio/export');  Label1.Caption := 'echo 17 status = ' + IntToStr(gwexitStatus(ReturnCode));  gReturnCode := fpsystem('echo "out" > /sys/class/gpio/gpio17/direction');  Label2.Caption := 'echo out status = ' + IntToStr(wexitStatus(gReturnCode));end; Maybe 512 is directory not found. Could you place a delay between the echo 17 and echo out. It could be that directory /sys/class/gpio/gpio17/direction doesn't exists yet when doing echo 17. It will be created in a second. After that second the directory /sys/class/gpio/gpio17/direction exists and you can write to it. So Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i begin  gReturnCode := fpsystem('echo "17" > /sys/class/gpio/export');  Label1.Caption := 'echo 17 status = ' + IntToStr(gwexitStatus(ReturnCode));  Sleep(1000); // wait for directory creation  gReturnCode := fpsystem('echo "out" > /sys/class/gpio/gpio17/direction');  Label2.Caption := 'echo out status = ' + IntToStr(wexitStatus(gReturnCode));end; If that works you could also build in a while loop until the directory is created instead of a sleep. ---------------------------------------------------------------------- Ramses · 2021-02-02 19:52 Quote from: rvk on February 02, 2021, 08:01:01 pmMaybe 512 is directory not found. Could you place a delay between the echo 17 and echo out. You found the solution!! i have placed a delay and now it always work at the first try now, when i start the program for the first time, the two label say: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Label1 = echo 17 status = 0Label2 = echo out status = 0  Thanks a lot for your help, it is working perfectly fine now! ---------------------------------------------------------------------- MarkMLl · 2021-02-02 22:11 That's interesting... and a bit worrying. I've not seen anything documented about delays and my personal policy is to handle GPIO configuration outside the program since in the general case it's a privileged operation. But I also find myself thinking of the way in which the RPi uses a kernel-moderated backdoor to the proprietary part of the chip for some operations, and if those included GPIO configuration it could explain the lag. MarkMLl ---------------------------------------------------------------------- rvk · 2021-02-02 22:29 Quote from: MarkMLl on February 02, 2021, 11:11:33 pmThat's interesting... and a bit worrying. I've not seen anything documented about delays and my personal policy is to handle GPIO configuration outside the program since in the general case it's a privileged operation. But I also find myself thinking of the way in which the RPi uses a kernel-moderated backdoor to the proprietary part of the chip for some operations, and if those included GPIO configuration it could explain the lag. The lag is indeed because of the fact there is a file-system "simulated" by the kernel. Echo 17 into /sys/class/gpio/export triggers the creation of the directory /sys/class/gpio/gpio17/ which otherwise doesn't exists. That trigger and creation takes a few milliseconds. That's why you need a delay or loop to check for the existence of that directory before you can write to it.