Raspberry Pi WiFi prj

Linux specific questions.
Post Reply
dasyar
Posts: 372
Joined: Dec 04, 2008 15:31

Raspberry Pi WiFi prj

Post by dasyar »

This is the start of the RPi WiFi program, I know you could do the below program in a resident bash format, but freeBasic will give you more choices if you decide to really expand the program.

I put together another RPi board with a sht11 module attached, placed it in a make shift box to keep it minimally weather proof and placed the project box outside which has overhead protection from rain, snow, and other things. The intent is to keep it outside during the winter months to see if the items included will withstand the freezing temperatures. Plus I will try to implement some temp data logging procedures, I can see this program growing already.

The next step is to implement the temperature readings which will be followed by implementing an RPi NoIR camera to see what kind of night time pictures I can take, and what they really look like. Then I will add a proximity sensor to automate a picture snap when something gets within a reasonable distance. This part might lead to using FB threads, so this should be interesting.

Code: Select all

'FBmobile.bas
'
' UI program for Raspberry Pi terminal window.
' Oct 31, 2014
' Version 1.0
'


#inclib "bcm2835"        ' Access RPi GPIO
#inclib "RPi_sht1x"      ' Access RPI sht11 module
#include "bcm2835.bi"
#include "RPi_SHT1x.bi"
#include "vbcompat.bi"   ' Time and date functions


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

' Declare Subs
declare sub menu()

' Declare Functions

'**********
' Main


print "FBmobile Program"

do
' Start collecting keypress
	buffer = input$(1)
	buffer1 = buffer1 + buffer
	print "" & buffer;  ' Print the keypress.
' If Esc then end program.
	if buffer = chr$(27) then
		goto Cend  ' A way to get out of do ... loop
	end if
' Check for EOL CR then strip away.
	if buffer = chr$(13) then
		buffer1 = rtrim(buffer1, chr$(13))
' Command IO selection
		if buffer1 = "quit" then
			exit do  ' Exit and run code after to do ... loop
		elseif buffer1 = "help" or buffer1 = "?" then
			menu()
		elseif buffer1 = "date" then
			print Date
		elseif buffer1 = "time" then
			print Time
		else 
			print "Unknown Command"
		end if
		buffer1 = ""
	end if
		
loop
'**********
Cend:
print "Program stop"

end
'**********

' Functions and Subroutines
sub menu()
	print "Menu - help, quit, time, date,  "
	print "quit ends program. "
end sub
dasyar
Posts: 372
Joined: Dec 04, 2008 15:31

Re: Raspberry Pi WiFi prj

Post by dasyar »

Added a couple of items like being able to get the temperature and humidity as individual requests. Added some more coding notes, I have to get into the habit of doing it more often.

Another request for the gurus, I would like to add data logging. The first simple idea, add a command for a hard coded duration of time to get the temp value, time stamp it, and add it to a file. I am not sure as to how you would open a file to write too, and how to store the data so maybe it could be opened by a graphics program to create some charts. So maybe the first request, best way to open and close a file for writing?

Code: Select all

'FBmobile.bas
'
' UI program for Raspberry Pi terminal window.
' Oct 31, 2014
' Version 1.0
'
' Oct 31, 2014
' Version 1.0 Rev .01
' - Added access to the sht11 module
' - Added temp and humid commands
'


#inclib "bcm2835"        ' Access RPi GPIO
#inclib "RPi_sht1x"      ' Access RPI sht11 module
#include "bcm2835.bi"
#include "RPi_SHT1x.bi"
#include "vbcompat.bi"   ' Time and date functions

' Check to see if bcm2835 is setup.
if bcm2835_init()=-1 then
	print "error: init() fault!"
	sleep : end
end if

' Declare global variables
dim as string Key,buffer,buffer1
' sht11
dim shared realh as single
dim shared realt as single
dim shared realf as single


' Declare Subs
declare sub menu()
declare sub sht11work()

' Declare Functions

'**********
' Main


print "FBmobile Program"

do
' Start collecting keypress
	buffer = input$(1)
	buffer1 = buffer1 + buffer
	print "" & buffer;  ' Print the keypress.
' If Esc then end program.
	if buffer = chr$(27) then
		goto Cend  ' A way to get out of do ... loop
	end if
' Check for EOL CR then strip away.
	if buffer = chr$(13) then
		buffer1 = rtrim(buffer1, chr$(13))
		print buffer1
' Command IO selection
		if buffer1 = "quit" then
			exit do  ' Exit and run code after to do ... loop
		elseif buffer1 = "help" or buffer1 = "?" then
			menu()
		elseif buffer1 = "date" then
			print Date
		elseif buffer1 = "time" then
			print Time
		elseif buffer1 = "temp" then
			sht11work()
			print "Temp F: " & realf & "F"
		elseif buffer1 = "humid" then
			sht11work()
			print "Humidity: " & realh
		else 
			print "Unknown Command"
		end if
		buffer1 = ""
	end if
		
loop
'**********
Cend:
print "Program stop"

end
'**********

' Functions and Subroutines
sub menu()
	print "Menu - help, quit, time, date,  "
	print "       temp, humid, "
	print "quit ends program. "
end sub

sub sht11work()
	dim humi_val as ushort
	dim temp_val as ushort
' These are now declared at the beggining of the program.
'	dim realh as single
'	dim realt as single
'	dim realf as single

' sht11 procedural steps for access.	
	SHT1x_InitPins()
	SHT1x_Reset()
	SHT1x_Measure_Start(SHT1xMeat)
	SHT1x_Get_Measure_Value(@temp_val)
	SHT1x_Measure_Start(SHT1xMeaRh)
	SHT1x_Get_Measure_Value(@humi_val)
	
	realt = CSng(temp_val)  ' Convert integer to float
	realh = CSng(humi_val)  ' Convert integer to float.
	
	SHt1x_Calc(@realh,@realt)
	
	realf = ((realt*1.8)+30.60)  ' Calculate for F plus adustment.
	
end sub
dasyar
Posts: 372
Joined: Dec 04, 2008 15:31

Re: Raspberry Pi WiFi prj

Post by dasyar »

Below is the first attempt at this writing to a file, stuff. Yes, if there is no file named "test1.txt", it creates and opens it up for appending data. I did write two pieces of data and of course it got appended on a new line. So, in the "test1.txt" file do I assume that there is a CR at the end of the data line(s)? How do I have the 'Date' and 'Time' appear in the "test1.txt" file without quotation (" ") marks around the data?

This simple form of 'Open' opens a file with ascii attributes, but I did notice that some of the examples use binary, whats the advantage of that?

Since this is basically going to be a database of sorts, how would I prepare the data so it could be manipulated by a graphics program for example? Would the external program search for commas(,) to designate end of data segment and then for an EOL a CR?

This particular code segment would be done as task in the FBmobile program since I will want to determine time duration.

Code: Select all

'FBfilea.bas
#include "vbcompat.bi"

'dim realf as single = 34.23
'dim realh as single = 75.25
dim realf as single = 30.27
dim realh as single = 76.45

open "test1.txt" for append as #1 ' Creates and/or opens a file for
                                  ' appending data.
write #1,Date,Time,realf,realh

close #1 
dasyar
Posts: 372
Joined: Dec 04, 2008 15:31

Re: Raspberry Pi WiFi prj

Post by dasyar »

I added a command called 'logit' that does a very basic data entry function. It creates and/or opens a file to be appended, then it adds data every time the 'logit' is used. Just some really straight forward stuff, the next part is the hard part.

When you start a thread does it appear to be working in the background, or does it stop the foreground activity until the thread has run its course? What I would like to do is have a logging thread running while still being able to type in the commands in the terminal screen and have them work without to much of a delay, is that possible? Or would there be a substantial delay while the logging thread runs?

Code: Select all

'FBmobile.bas
'
' UI program for Raspberry Pi terminal window.
' Oct 31, 2014
' Version 1.0
'
' Oct 31, 2014
' Version 1.0 Rev .01
' - Added access to the sht11 module
' - Added temp and humid commands
'
' Nov 02, 2014
' version 1.0 Rev .02
' - Added file access
' - Added logit command


#inclib "bcm2835"        ' Access RPi GPIO
#inclib "RPi_sht1x"      ' Access RPI sht11 module
#include "bcm2835.bi"
#include "RPi_SHT1x.bi"
#include "vbcompat.bi"   ' Time and date functions

' Check to see if bcm2835 is setup.
if bcm2835_init()=-1 then
	print "error: init() fault!"
	sleep : end
end if

' Declare global variables
dim as string Key,buffer,buffer1
' sht11
dim shared realh as single
dim shared realt as single
dim shared realf as single


' Declare Subs
declare sub menu()
declare sub sht11work()
declare sub LogData()

' Declare Functions

'**********
' Main


print "FBmobile Program"

do
' Start collecting keypress
	buffer = input$(1)
	buffer1 = buffer1 + buffer
	print "" & buffer;  ' Print the keypress.
' If Esc then end program.
	if buffer = chr$(27) then
		goto Cend  ' A way to get out of do ... loop
	end if
' Check for EOL CR then strip away.
	if buffer = chr$(13) then
		buffer1 = rtrim(buffer1, chr$(13))
		print buffer1
' Command IO selection
		if buffer1 = "quit" then
			exit do  ' Exit and run code after to do ... loop
		elseif buffer1 = "help" or buffer1 = "?" then
			menu()
		elseif buffer1 = "date" then
			print Date
		elseif buffer1 = "time" then
			print Time
		elseif buffer1 = "temp" then
			sht11work()
			print "Temp F: " & realf & "F"
		elseif buffer1 = "humid" then
			sht11work()
			print "Humidity: " & realh
		elseif buffer1 = "logit" then
			LogData()
		else 
			print "Unknown Command"
		end if
		buffer1 = ""
	end if
		
loop
'**********
Cend:
print "Program stop"
Close  ' Just to make sure file(s) are closed.

end
'**********

' Functions and Subroutines
sub menu()
	print "Menu - help, quit, time, date,  "
	print "       temp, humid, logit, "
	print "quit ends program. "
end sub

sub sht11work()
	dim humi_val as ushort
	dim temp_val as ushort
' These are now declared at the beggining of the program.
'	dim realh as single
'	dim realt as single
'	dim realf as single

' sht11 procedural steps for access.	
	SHT1x_InitPins()
	SHT1x_Reset()
	SHT1x_Measure_Start(SHT1xMeat)
	SHT1x_Get_Measure_Value(@temp_val)
	SHT1x_Measure_Start(SHT1xMeaRh)
	SHT1x_Get_Measure_Value(@humi_val)
	
	realt = CSng(temp_val)  ' Convert integer to float
	realh = CSng(humi_val)  ' Convert integer to float.
	
	SHt1x_Calc(@realh,@realt)
	
	'realf = ((realt*1.8)+30.60)  ' Calculate for F plus adustment.
	realf = ((realt*1.8)+29.20)   ' Calibrated to outdoor thermostat
	                              ' (11/02/2014)
	
end sub

sub LogData()

sht11work()  ' Grab sht11 data
open "TandHlog.txt" for append as #1  ' Open and/or create file
write #1,Date,time,realf,realh   ' Write date, time, and sht11 data
close #1  ' Close the file.

end sub
dasyar
Posts: 372
Joined: Dec 04, 2008 15:31

Re: Raspberry Pi WiFi prj

Post by dasyar »

I am working on developing a thread for the FBmobile program, but I am really having a problem understanding what a freeBasic thread really does. Is it just a sub that sort of runs in the background, doing some 'for loops' and 'do whiles', or is the thread supposed to be used like its own little programmable core? Within this thread(sub) can you create its own little 'sub ... end sub' routines?

Most of the examples that are shown, just show the 'thread' as a fancy little sub. So, I am really not sure how to figure this out. Anybody have a real clear explanation for a freeBasic thread.

Thanks
Post Reply