Bypic Bv508 Loading Settings from Sd Card

As mentioned in my previous post i have been trying out the bv508 mcu with esp8266. Check out it Here
Im currently working on a web based monitoring system for a friend and came to a problem with the ability of regularly changing the WiFi settings.

As the system would be permanently installed and just have a few buttons and no lcd and connection a serial terminal seem too difficult for my non technical friend. I decided to simply house a configuration txt file on a SD card and the system would read and load the settings on startup.

Again i was able to do this in only a few lines of code and thought it was worth sharing.

 

How it works ?

User simply modifies config file on Sd card and inserts and resets power.
On power up my script will first flash the led three times (card & Indicating config file) and will then attempt loading the wifi settings .
If connection is successful the led will light. At this point the sd card can be removed. If power was reset with no card (no led three flashes) it simply would not try to load the new wifi settings would just connected network stored in Esp8266 flash. You will still get green led on to indicate wifi connected.

Hardware

Connect the sd holder to ic2 bus.wpid-20150920_171010.jpg

SD Card Connection instructions here

Connect Green led to C3

Install bv508 Firmware

1)Install bv508 firmware with SD card support.
2)Now re-Install esp8266 /dt11 rookie.

http://www.byvac.com/mBlib/flb/Library/wifi/ESP8266_DHT.script

Config file:

On root of SD card called “CONF.TXT”
with the content as below

SSID:yourssid
PASS:Yourpass

 

Code:

// SIMPLE Bv508 LOAD WIFI Setting from SD card
// Example By Luke (www.ls-homeprojects.co.uk)
// Free to use this code how you like but please link my site


//Requirements
//bv508 with sd firmware and ESP8266 Script

// How to use
// (1) Copy and save on bv508 (see bypic homepage 
//http://www.bypic.co.uk)
//(2) Save to flash ie flsave("")
//(3) Will launch at power up ! </span>


constant GRN_LED {PORTC(PORT),3}  // Set Led pin

constant File$ "CONF.TXT"  //change this to your file name

//File structure , has to be as per this to work !

//<firstline> SSID:yourssid
//<secondline> PASS:yourpass



function flash_led()
io_pinSet(*GRN_LED(),1)         //  Flash led to indicate config file
wait(200)
io_pinSet(*GRN_LED(),0)
endf






function get_setting$(line)
dim f, a$[128], j ,s_v$[128]
dim p=0

f=fopen(File$,"r")   //Open file
if f <> 0 then
for j = 0 to line
fgets(a$,f)          // Read chosen line
next

fclose(f)  // Close file

token$(a$,*p,':') // dummy as this will be first split item ie setting ID
s_v$= token$(a$,*p,':') //



return mid$(s_v$,0,strlen(s_v$)-2)// Return the Value of the setting + strip cr
else
return 0 // return 0 as Error file doesnt exist / Card not inserted

endif
endf




function connect()



wfPower(1)
wf_start()
mode(1)


if get_setting$(0) = 0 then

print "Error reading setting does file and SD card exist ?"


else
flash_led()
wait(200)                   // flash led to indicate card with config file
flash_led()
wait(200)
flash_led()

dim ssid_r$ = get_setting$(0)  //Get ssid from line 1 settings txt file
dim pass_r$ = get_setting$(1)  //Get pass from line 2 settings txt file


join(ssid_r$,pass_r$)          // connect to wifi


endif




if isIP() = 1 then // check wifi connection !
io_pinSet(*GRN_LED(),1) // connected light green status led
else
io_pinSet(*GRN_LED(),0) // No connection , turn led off
print "\nUnable to get IP address"
return

endif

info()                      // display wifi info


endf



function main()
io_pinRole(*GRN_LED(),OUT,WOFF)     // Set led as output
io_pinSet(*GRN_LED(),0)             // Turn led off
connect()                           // Connect on power on
endf

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.