Raspsberry Pi serial prj

Linux specific questions.
dkl
Site Admin
Posts: 3217
Joined: Jul 28, 2005 14:45
Location: Germany

Re: Raspsberry Pi serial prj

Postby dkl » Oct 20, 2014 11:43

dasyar wrote:Next question, I noticed when I have #include once "wirringpi.bi", I have to have wirringpi.bi file in the directory that I am working in, but when I added #include "vbcompat.bi", I did not have to have the file in my working directory?

vbcompat.bi is in FB's "standard" header directory, on Linux this is include/freebasic/ relative to the location where fbc is installed, for example /usr/local/include/freebasic. If an #included file doesn't exist in the same directory as the file that #includes it then fbc will search it in the standard include/freebasic/ location.
dasyar
Posts: 369
Joined: Dec 04, 2008 15:31

Re: Raspsberry Pi serial prj

Postby dasyar » Oct 20, 2014 12:01

OK, so is there an easy way to have "wirringpi.bi" accessible automatically when used with other programs, or do I have to create its own directory and then use a full path name to get to it from the working directory? It seems like it is heavy duplication if I have to copy it to every working directory.
dkl
Site Admin
Posts: 3217
Joined: Jul 28, 2005 14:45
Location: Germany

Re: Raspsberry Pi serial prj

Postby dkl » Oct 20, 2014 13:02

Yea, you can put it into the standard include directory (/usr/local/include/freebasic/ if you have installed fbc at /usr/local/bin/fbc). This makes sense for "common" headers that should be used by many programs (i.e. aren't specific to one project only).
dasyar
Posts: 369
Joined: Dec 04, 2008 15:31

Re: Raspsberry Pi serial prj

Postby dasyar » Oct 20, 2014 13:33

I just checked the /usr/local/include/freebasic directory and I also checked one of the .bi files for permissions. The typical for the .bi files are:
owner: root
group: staff
- it is marked as executable
- read and write to owner, everybody else gets read only.
Now my concern is what kind of permissions should be given to wirringpi.bi, or any other new .bi that would be manually added? I know if I screw this up, it could be a very difficult process to undo that mistake. What would be the least pain full permissions selection to implement, so FBC can work with it?
dkl
Site Admin
Posts: 3217
Joined: Jul 28, 2005 14:45
Location: Germany

Re: Raspsberry Pi serial prj

Postby dkl » Oct 20, 2014 14:07

They should be the same as the .h files in /usr/include, for example. They shouldn't be executable since they're just text files, not binaries or scripts, but it doesn't really matter. Generally the default should be ok:
sudo cp my.bi /usr/local/include/freebasic/my.bi
dasyar
Posts: 369
Joined: Dec 04, 2008 15:31

Re: Raspsberry Pi serial prj

Postby dasyar » Oct 20, 2014 15:48

Thanks dkl, that worked, just a straight forward cp into the directory.
dasyar
Posts: 369
Joined: Dec 04, 2008 15:31

Re: Raspsberry Pi serial prj

Postby dasyar » Oct 20, 2014 20:54

This is my rendition to check for available port, there must be a neater way of doing this, I think? Open for suggestions.

Code: Select all

.
.
.
open com "/dev/ttyUSB0:9600,n,8,1,cs0,ds0,cd0,rs" as #1
sleep 1000,1
' Check if port is available
if Err <> 0 then
   print "error: Can't open port!"
'   sleep 2000,1
else
   print "ttyUSB0"
   goto startp
end if
open com "/dev/ttyUSB1:9600,n,8,1,cs0,ds0,cd0,rs" as #1
sleep 1000,1
if Err <> 0 then
   print "error: Can't open port!"
else
   print "ttyUSB1"
   goto startp
end if
open com "/dev/ttyUSB2:9600,n,8,1,cs0,ds0,cd0,rs" as #1
sleep 1000,1
if Err <> 0 then
   print "error: Can't open port!"
else
   print "ttyUSB2"
   goto startp
end if
open com "/dev/ttyUSB2:9600,n,8,1,cs0,ds0,cd0,rs" as #1
sleep 1000,1
if Err <> 0 then
   print "error: no ports available"
   cls
   goto Cend
else
   print "ttyUSB3"
   goto startp
end if
.
.
.
dkl
Site Admin
Posts: 3217
Joined: Jul 28, 2005 14:45
Location: Germany

Re: Raspsberry Pi serial prj

Postby dkl » Oct 21, 2014 0:00

Maybe like this:

Code: Select all

dim portsToTry(0 to ...) as string => _
{ _
   "/dev/ttyUSB0", _
   "/dev/ttyUSB1", _
   "/dev/ttyUSB2"  _
}

var settings = ":9600,n,8,1,cs0,ds0,cd0,rs"

dim f as integer

for i as integer = 0 to ubound(portsToTry)
   var port = portsToTry(i)
   print "trying '" + port + "'... ";
   f = freefile()
   if open com(port + settings, as #f) = 0 then
      print "ok"
      exit for
   end if
   print "failed"
   f = -1
next

if f < 0 then
   print "could not find any working ports"
   end 1
end if

print "reading..."
close #f
D.J.Peters
Posts: 8203
Joined: May 28, 2005 3:28
Contact:

Re: Raspsberry Pi serial prj

Postby D.J.Peters » Oct 21, 2014 10:14

dkl wrote:They should be the same as the .h files in /usr/include, for example. They shouldn't be executable since they're just text files, not binaries or scripts, but it doesn't really matter. Generally the default should be ok:
sudo cp my.bi /usr/local/include/freebasic/my.bi

FreeBASIC on ARM devices
D.J.Peters wrote:I self use the '/usr' prefix ./install.sh -i /usr
many distros for small devices don't have a /usr/local folder at all
or /usr/local/bin isn't in the PATH and you have to add /usr/local/lib/freebasic/linux to the linker path also.

For short prefix /usr is safe on all distros I tested in the past.
dasyar
Posts: 369
Joined: Dec 04, 2008 15:31

Re: Raspsberry Pi serial prj

Postby dasyar » Oct 21, 2014 11:55

Thanks dkl for the auto port selection snippet. I was trying to create a command 'remoteport', but since you have 'port' declared locally, I will have to give it some more thought as to how to make it a global variable, without disrupting the for loop, so I can do a 'print #1, "Remote port in use: " + port' within my command structure. So far everything is working as expected.


Code: Select all

'XBsun.bas
' Version 1.0
' Oct 20, 2014
'
' UI program for Raspbery Pi using an XBee on COM port
'
' Oct 21, 2014
' Version 1.0 rev .01
'  - Added the dkl auto port search
'  - Added time and date commands, displays actual time and date
'

#include once "wirringpi.bi"  ' wiringPi
#include "vbcompat.bi"        ' Date function

' Check to see if wiringPi is setup
if wiringPiSetup()=-1 then
   print "error: Can't setup GPIO!"
   sleep : end
end if

' Declare Subs
declare sub onled()
declare sub offled()
declare sub menu()


' Declare functions


' Declare variables
dim as string Key,buffer,buffer1


' Check for open com port.
dim portsToTry(0 to ...) as string => _
{ _
    "/dev/ttyUSB0", _   
    "/dev/ttyUSB1", _
    "/dev/ttyUSB2", _
   "/dev/ttyUSB3" _
}

var settings = ":9600,n,8,1,cs0,ds0,cd0,rs"  ' Settings for port

dim f as integer

for i as integer = 0 to ubound(portsToTry)
    var port = portsToTry(i)
    print "trying '" + port + "'... ";
    f = freefile()
    if open com(port + settings, as #1) = 0 then
        print "ok"
        exit for
    end if
    print "failed"
    f = -1
next

if f < 0 then
    print "Could not find any working ports!"
    end 1
    goto Cend  ' Added this just in case
end if



'**********
' Main
startp:
print "Started program.";
print #1, " XBsun"

do
' Check locally for Esc key press
   Key = inkey$
   if Key = chr$(27) then
      exit do
   end if

' Check for incoming on com port   
   while Loc(1) > 0
      buffer = input$(loc(1),#1)
      buffer1 = buffer1 + buffer
      if buffer = chr$(27) then   ' Check for Esc key press, remote
         goto Cend
      end if
      if buffer = chr$(13) then
         print buffer1;
         buffer1 = rtrim(buffer1, chr$(13))  ' End of str CR
         if buffer1 = "quit" then  ' Check for 'quit' command
            goto Cend
         'end if
         elseif buffer1 = "onled" then  ' Check for 'onled' command
            onled()
         'end if
         elseif buffer1 = "offled" then  ' Check for 'offled' command
            offled()
         'end if
         elseif buffer1 = "help" then
            menu()
         'end if
         elseif buffer1 = "date" then            
            print #1, Date
         elseif buffer1 = "time" then
            print #1, Time
         else
            print #1, "Unknown Command"
         end if
         buffer1 = ""
      end if
   wend
''''''''''''''''''''
   
sleep 1,0
loop
'**********

Cend:
print #1, "Program stop"
close  ' Close open com port
end

' Functions and subRoutines
' Turn on LED on pin 6
sub onled()
   pinMode(6, PIN_OUTPUT)
   digitalWrite(6,PIN_HIGH)
end sub

' Turn off LED on pin 6
sub offled()
   pinMode(6, PIN_OUTPUT)
   digitalWrite(6,PIN_LOW)
end sub

sub menu()
   print #1, "Menu - help, quit, onled, offled, date "
   print #1, "       time, "
   print #1, "quit stops the program"
end sub
dasyar
Posts: 369
Joined: Dec 04, 2008 15:31

Re: Raspsberry Pi serial prj

Postby dasyar » Oct 25, 2014 12:39

Just an update with this project. I got the sht11 module to work in FB, as soon as I get the sht11 hooked into RPi GPIO, I will be adding the code so I can get remote access to temperature and humidity data. This will be the first real functioning gadget on this RPi platform.

The other gadget that I have is the RPi camera module, after checking for available code, their was no C code to be found. But their is bash code that is available, so I guess I will have to implement the use of 'shell' command to get some camera functioning code to work. I did notice that their is Python code available for the RPi camera, does FreeBasic work with Python?

The other thing that I was thinking about is to make the camera movable, possibly attaching a picamera bracket to a stepper motor and then maybe have some FB code that could drive the stepper motor that would position the camera module. Not sure whether FB can do this kind of thing, driving stepper motors that is.
dasyar
Posts: 369
Joined: Dec 04, 2008 15:31

Re: Raspsberry Pi serial prj

Postby dasyar » Oct 29, 2014 12:58

Below is rev 2 of the XBsun.bas program, I am now using the bcm2835 lib instead of wiringPi. So far everything is working as expected, I am getting good temperature readings, and the newly attached LEDs are turning on and off. Because of where the LEDs are attached on the GPIO, you have to use the Raspberry Pi B+ model.

As a reminder this is setup to be used with an XBee module plugged in. I was running into some problems with the WiFi module, so I removed that, and now it has a LAN line plugged in, I guess this unit is becoming less mobile.

Now I will be experimenting with the picamera, at first it will be in a stationary position, then I might try mounting it on a movable device.


Code: Select all

'XBsun.bas
' Version 2.0
' Oct 20, 2014
'
' UI program for Raspbery Pi using an XBee on COM port
'
' Oct 21, 2014
' Version 1.0 rev .01
'  - Added the dkl auto port search
'  - Added time and date commands, displays actual time and date
'
' Oct 29, 2014
' Version 2.0 rev .01
'  - Now using the bcm2835 lib
'  - Added LEDs to the B+ GPIO on pins 40, 38, 36

#inclib "bcm2835"
#inclib "RPi_sht1x"
#include "bcm2835.bi"
#include "RPi_SHT1x.bi"
'#include once "wirringpi.bi"  ' Not using wiringPi
#include "vbcompat.bi"        ' Date function

' Define the GPIO pins
#define rPIN RPI_BPLUS_GPIO_J8_40
#define gPIN RPI_BPLUS_GPIO_J8_38
#define yPIN RPI_BPLUS_GPIO_J8_36

' Check to see if bcm2835 is setup
if bcm2835_init()=-1 then
   print "error: init() fault!"
   sleep : end
end if
' Init pins for   
bcm2835_gpio_fsel(rPIN, BCM2835_GPIO_FSEL_OUTP)
bcm2835_gpio_fsel(gPIN, BCM2835_GPIO_FSEL_OUTP)
bcm2835_gpio_fsel(yPIN, BCM2835_GPIO_FSEL_OUTP)
' Check to see if wiringPi is setup
'if wiringPiSetup()=-1 then
'   print "error: Can't setup GPIO!"
'   sleep : end
'end if

' Declare Subs
declare sub ronled()
declare sub roffled()
declare sub gonled()
declare sub goffled()
declare sub yonled()
declare sub yoffled()
declare sub tempmod()
declare sub menu()


' Declare functions


' Declare global variables
dim as string Key,buffer,buffer1


' Check for open com port.
dim portsToTry(0 to ...) as string => _
{ _
    "/dev/ttyUSB0", _   
    "/dev/ttyUSB1", _
    "/dev/ttyUSB2", _
   "/dev/ttyUSB3" _
}

var settings = ":9600,n,8,1,cs0,ds0,cd0,rs"  ' Settings for port

dim f as integer

for i as integer = 0 to ubound(portsToTry)
    var port = portsToTry(i)
    print "trying '" + port + "'... ";
    f = freefile()
    if open com(port + settings, as #1) = 0 then
        print "ok"
        exit for
    end if
    print "failed"
    f = -1
next

if f < 0 then
    print "Could not find any working ports!"
    end 1
    goto Cend  ' Added this just in case
end if



'**********
' Main
startp:
print "Started program."
print #1, " XBsun"

do
' Check locally for Esc key press
   Key = inkey$
   if Key = chr$(27) then
      exit do
   end if

' Check for incoming on com port   
   while Loc(1) > 0
      buffer = input$(loc(1),#1)
      buffer1 = buffer1 + buffer
      if buffer = chr$(27) then   ' Check for Esc key press, remote
         goto Cend
      end if
      if buffer = chr$(13) then
         print buffer1  ' Log a session to a file at this point ?
         buffer1 = rtrim(buffer1, chr$(13))  ' End of str CR trim
         if buffer1 = "quit" then  ' Check for 'quit' command
            goto Cend
         elseif buffer1 = "ronled" then  ' Check for 'ronled' command
            ronled()
         elseif buffer1 = "roffled" then  ' Check for 'roffled' command
            roffled()
         elseif buffer1 = "gonled" then  ' Check for 'gonled' command
            gonled()
         elseif buffer1 = "goffled" then  ' Check for 'goffled' command
            goffled()
         elseif buffer1 = "yonled" then  ' Check for 'yonled' command
            yonled()
         elseif buffer1 = "yoffled" then  ' Check for 'yoffled' command
            yoffled()
         elseif buffer1 = "help" or buffer1 = "?" then
            menu()
         elseif buffer1 = "date" then            
            print #1, Date
         elseif buffer1 = "time" then
            print #1, Time
         elseif buffer1 = "temp" then
            tempmod()            
         else
            print #1, "Unknown Command"
         end if
         buffer1 = ""
      end if
   wend
''''''''''''''''''''
   
sleep 1,0
loop
'**********

Cend:
print #1, "Program stop"
close  ' Close open com port
end

' Functions and subRoutines
' Turn on rLED on pin GPIO21
sub ronled()
'   pinMode(6, PIN_OUTPUT)   ' Not using wiringPi
'   digitalWrite(6,PIN_HIGH) ' Not using wiringPi

   bcm2835_gpio_write(rPIN,HIGH)
end sub

' Turn off LED on pin GPIO21
sub roffled()
'   pinMode(6, PIN_OUTPUT)  ' Not using wiringPi
'   digitalWrite(6,PIN_LOW) ' Not using wiringPi

   bcm2835_gpio_write(rPIN,LOW)
end sub

sub gonled()
   bcm2835_gpio_write(gPIN,HIGH)
end sub

sub goffled()
   bcm2835_gpio_write(gPIN,LOW)
end sub

sub yonled()
   bcm2835_gpio_write(yPIN,HIGH)
end sub

sub yoffled()
   bcm2835_gpio_write(yPIN,LOW)
end sub

sub tempmod()
dim humi_val as ushort
dim temp_val as ushort
dim realh as single  ' Float
dim realt as single  ' Float
dim realf as single  ' Float

SHT1x_InitPins()  ' Initialise pins, hard coded
SHT1x_Reset()
SHT1x_Measure_Start(SHT1xMeaT)  ' Temp
SHT1x_Get_Measure_Value(@temp_val) ' Temp
SHT1x_Measure_Start(SHT1xMeaRh)  ' Humidity
SHT1x_Get_Measure_Value(@humi_val)  ' Humidity

realt = CSng(temp_val)  ' Convert integer temp_val to float
realh = CSng(humi_val)  ' Convert integer humi_val to float

'          humidity, temperature
SHt1x_Calc(@realh, @realt)  ' Calculate float values

realf = ((realt*1.8)+30.60)  ' Calculate for F plus adjustment
print #1, "Temp C: " & realt & "C"
print #1, "Temp F: " & realf & "F"
print #1, "Humid: " & realh

end sub

sub menu()
   print #1, "Menu - help, quit, time, temp, date "
   print #1, "       ronled, roffled, "
   print #1, "quit stops the program"
end sub

Return to “Linux”

Who is online

Users browsing this forum: No registered users and 6 guests