Wifi shield configuration from SD card

CKEasyWifiConfig

A library for the Digilent chipKIT development boards. It lets you configure the wifi settings of the digilent wifi shield from a configuration file on the SD card. Library main page : http://kurdymalloy.github.io/CKEasyWifiConfig

Why

Problem

In most example sketches that you will find, the wifi settings are always hard-coded in the sketch; this is kind of problematic if every time that you want to connect to a different network you have to change code and rebuild the sketch and reload it on the board.

Solution

This library will let you configure the wifi shield with settings read from a configuration file saved on a SD card loaded on the shield. So to change the settings, you just have to change the configuration file on the SD card. This is a lot better especially when your device is deployed in a project.

Installation

The project files are available at : https://github.com/KurdyMalloy/CKEasyWifiConfig This is arduino’s standard way of installing a custom library:
  • Copy the folder named “CKEasyWifiConfig” from the repository to the sub-folder  /libraries/ under your sketchbook directory.
  • Use the “NetConf.cfg” file from the root of the repository as a template for your configuration file that will go on the SD card.

Dependencies

The library is dependent on four other libraries: You need to include these in your main sketch before you include this one. This library was tested with mpide 0023.

Configuration File

The configuration file included in the project is already self-documented. Any line beginning with the character ‘#’ or ‘/’ will be ignored and considered a comment. The parameter names in the file are case sensitive, they need to be uppercase. Here are the possible values for each parameters: SECURITY:
  • OPEN
  • WPA2_WITH_KEY
  • WPA2_WITH_PASS_PHRASE
  • WPA_WITH_KEY
  • WPA_WITH_PASS_PHRASE
  • WPA_AUTO_WITH_KEY
  • WPA_AUTO_WITH_PASS_PHRASE
  • WEP_40
  • WEP_104
IP:
  • A valid ipv4 address in the form xxx.xxx.xxx.xxx
  • DHCP or left blank to use dhcp
SSID:
  • The ssid of your network; it is usually case sensitive.
KEY:
  • Your key in hexadecimal if you can provide it.
KEYCHAR:
  • Your key if you want to provide it in text.
KEYINDEX:
  • The index of your key if it is indexed
Important: the KEY and KEYCHAR parameters are mutually exclusive; if you provide both, the last one read in the file will be the one that is used. Example of a configuration file to connect with dhcp and an hexadecimal wpa2 key :
SECURITY:WPA_AUTO_WITH_KEY
SSID:MYNETWORK
IP:DHCP
KEY:5312826e170e6d0ef8691670cabc5c9c3072cf8c003e4ee541a499d060746cfa
Example of a configuration file to connect with a static address and a wpa2 passphrase key :
SECURITY:WPA_AUTO_WITH_PASS_PHRASE
SSID:MYNETWORK
IP:192.168.1.252
KEYCHAR:hermionegranger

Usage

There are two example projects provided with the library; one to use a configuration file on the card and one where you can pass the security infos in the code. To use the library, make sure that you include all the dependency libraries before you include CKEasyWifiConfig. If you want the library to output some debugging information in the serial port, make sure to define “CKEASYWIFICONFIG_SERIAL_DEBUG” before you include the library. You will also have to make sure that you setup the serial port before you call the “Setup()” method of the library. It is also very important to initialize the SD card before you call the “Setup()” method of the CKEasyWifiConfig class since it will use it. Example:
#include <SD.h>
#include <Streaming.h>
#include <WiFiShieldOrPmodWiFi.h>  // This is for the MRF24WBxx on a pmodWiFi or WiFiShield
//#include <WiFiShieldOrPmodWiFi_G.h> // This is for the MRF24WGxx on a pmodWiFi or WiFiShield
#include <DNETcK.h>
#include <DWIFIcK.h>

// Remove the next line to stop the serial debugging of the library
#define CKEASYWIFICONFIG_SERIAL_DEBUG  
#include <CKEasyWifiConfig.h>

//To enable serial debugging in the main sketch
#define USE_SERIAL_DEBUG  

// Name of the config file on the SD card
char networkConfigFile[] = "NetConf.cfg";

CKEasyWifiConfig networkConfig(networkConfigFile);

void setup() {
  // put your setup code here, to run once:

  #ifdef USE_SERIAL_DEBUG
    Serial.begin(9600);
    Serial << "Initializing SD card..." << endl;
  #endif

  // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  if (!SD.begin(4)) {
    #ifdef USE_SERIAL_DEBUG
      Serial << "SD Card initialization failed!" << endl;
    #endif
    return;
  }
  else
  {
    #ifdef USE_SERIAL_DEBUG
      Serial << "SD Card initialization success." << endl;
      if (SD.exists(networkConfigFile))
        Serial << "Network Config File --> " << networkConfigFile << " is present on the SD card." << endl;
      else
        Serial << "Network Config File --> " << networkConfigFile << " is NOT present on the SD card." << endl;
    #endif // USE_SERIAL_DEBUG
  }

  // Call the initialization
  networkConfig.Setup();
}

void loop() {
  // put your main code here, to run repeatedly: 

  // every pass through loop(), keep the IP stack alive
  DNETcK::periodicTasks(); 
}

Limitations

The SD card library only supports file names in the 8.3 format. So your configuration file and any sub-folder that you want to put it in, need to have a name in this format. Due to some architecture differences; this library is not really suitable for the arduino platform. The arduino has a very different memory model especially to store static stuff into the flash memory (PROGMEM). This code was not designed with that in mind and take fully advantage of the fact that the PIC architecture is much easier to code for. Also since there is a lot more space available on the chipKIT platform, more time was spent developing for the usability of the library than on trying to reduce its size. The dependency to the Digilent networking libraries is also a factor to the non-portability of the library.  
VN:F [1.9.22_1171]
Rating: 6.5/10 (2 votes cast)
VN:F [1.9.22_1171]
Rating: +2 (from 2 votes)

Install MPLAB X IDE and XC32 compiler

MPLAB X is a open source cross platform that is used to develop applications based on PIC micro controllers. MPLAB X IDE is similar to MPIDE except that compiling the code before running it is possible in MPLAB X. All the library functions and more supported by MPIDE are supported by MPLAB X IDE. In this section, you will be guided through step-by-step instructions on installing MPLAB X IDE and it’s necessary XC compilers. Followed by, you can do a tutorial on Quick start blinky LED project to test and get acquainted with MPLAB X IDE which you have installed in your computer.

Installing MPLAB X IDE

  1. Access the page www.microchip.com/mplab in a new tab. In the MPLAB X IDE page, click on the red button under MPLAB X Links that’s says “MPLAB® X Free Download”. In the “Downloads” list that appears, download MPLAB® X IDE vx.xx for the OS you have.                                                                                                                                                                      
  2. Once the download is complete, extract MPLABX-vx.xx-windows-installer.exe and run the application as shown below,                                                                                                         
  3. In the setup window that pops up, select next.
  4. Accept the license agreement and click next.
  5. Save the software in a folder, preferably in the default directory and click next.
  6. At this point, setup is complete and you are ready to install the IDE. Click next in the Setup window for to begin the installation process.Once the installation is completed, you will be prompted to install a compiler. To program in C, you will need a compiler. To program for the chipKIT platform, you will need the MPLAB XC32 compiler for 32-bit devices. Leave the checkbox checked and click finish.                                                                     mplab9

Installing MPLAB X C32 compiler

  1. In the browser page that automatically opens up, scroll to the Downloads section on the left pane and download the XC32 compiler for your OS.                                                                                  mplab10
  2. Once the download is complete, extract xc32-vx.xx-windows-installer.exe and run the application as shown below,                                                                                                                                      mplab11
  3. In the setup window that pops up, select next. Accept the license agreement, choose the installer, choose installation type and keep proceeding on by clicking next.
  4. In the License activation window, leave the Activation Key blank and click next, to download the compiler in free mode. Click yes in the next window to confirm that you wish to install the FREE version of the compiler.
  5. Save the software in a folder, preferably in the default directory and click next to begin installation. Click next in the following window without any changes. Start the setup by clicking next in the ready to install window and the installation will start.Once the installation is complete, click finish. Double click on MPLAB X IDE vx.xx icon in the start menu or desktop to launch the program. Your desktop would look like this.                                        mplab23
VN:F [1.9.22_1171]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.22_1171]
Rating: 0 (from 0 votes)

Alternative (to MPIDE) Build Tools

Here are some build tools which you can consider using as an alternative to MPIDE:

Using chipKIT compatible Arduino-based Makefile

Here is the link on the topic – Blog Posting by Christopher Peplin To download the chipKIT.mk Makefile from github, click on – git repository

Using avrdude-gui tool to install hex file onto chipKIT board without MPIDE

1. Downloaded avrdude-gui and unzipped. 2. Replaced avrdude.exe that came with it with the avrdude.exe that is in MPIDE: mpide-0022-windows-20110822\hardware\tools\avr\bin\avrdude.exe Also needed to grap the libusb0.dll from the same location and place it into the avrdude-gui dir. 3. Replaced the avrdude.conf file with the avrdude.conf that is in MPIDE: mpide-0022-windows-20110822\hardware\tools\avr\etc\avrdude.conf 4. The GUI program only recognizes the first 3 COM ports so I had to remap my Uno32 to COM2. 5. Dropped my hex file into the avrdude-gui dir. 6. Run the program and selected the following parameters (for Uno32): Target Device: 32MX320F128H Programmer: stk500v2 Flash: my_hex_file.hex 7. Clicked the Flash Program button.
VN:F [1.9.22_1171]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.22_1171]
Rating: 0 (from 0 votes)

avrdude and the chipKIT Platform

The bootloader on the chipKIT is based on the same bootloader that I developed for the mega2560. It uses stk500v2 protocol.

Avrdude can be used from the command line with the chipKIT boards or any other board that has the bootloader installed The command would be something like this >avrude -c stk500v2 -p pic32 -P /dev/tty.usbmodem1234 -b 115200 -U flash:w:filename.hex Or, under Windows(If you’ve copied avrdude.exe and avrdude.conf into the MPLABX bin folder): “C:\Program Files\Microchip\MPLABX\mplab_ide\bin\avrdude.exe” -C”C:\Program Files\Microchip\MPLABX\mplab_ide\bin\avrdude.conf” -v -p32MX795F512L -cstk500v2 -P COM40 -b 115200 -U flash:w:pic32_template_1.production.hex

Getting an hex file for chipKIT

When using mpide, default is to remove all the intermediate files between the mpide source of your project (.mpe file) and code uploaded on the chipKIT. But all the files generated by mpide (including the hex file) can be kept by changing mpide preference file. From Linux:
  1. close any running mpide
  2. edit ~/.mpide/preferences.txt
  3. look for preproc.save_build_files attribute
  4. Set it to true: preproc.save_build_files=true
  5. save the preference file
Now, all temporary files from build command will be kept. To locate the right directory with your build, from mpide, press the “shift” key while starting the build/verify command. You will have details about the build process (commands, outputs, path …) in mpide output console. Example:
/opt/mpide-0023-linux32-20111221/hardware/pic32/compiler/pic32-tools/bin/pic32-g++  -O2  -c  -mno-smart-io  -w  -fno-exceptions  -ffunction-sections  -fdata-sections  -G1024  -g  -mdebugger  -Wcast-align  -mprocessor=32MX320F128H  -DF_CPU=80000000L  -DARDUINO=23  -D_BOARD_UNO_  -Danything_you_want  -Danything=1   -I/home/fred/Arduino/Serial2   -I/opt/mpide-0023-linux32-20111221/hardware/pic32/cores/pic32   -I/opt/mpide-0023-linux32-20111221/hardware/pic32/variants/Uno32    /tmp/build6773645236763377018.tmp/Serial2.cpp  -o  /tmp/build6773645236763377018.tmp/Serial2.cpp.o

...

/opt/mpide-0023-linux32-20111221/hardware/pic32/compiler/pic32-tools/bin/pic32-ar  rcs  /tmp/build6773645236763377018.tmp/core.a  /tmp/build6773645236763377018.tmp/main.cpp.o
/opt/mpide-0023-linux32-20111221/hardware/pic32/compiler/pic32-tools/bin/pic32-g++  -Os  -Wl,--gc-sections  -mdebugger  -mprocessor=32MX320F128H  -o  /tmp/build6773645236763377018.tmp/Serial2.cpp.elf  /tmp/build6773645236763377018.tmp/Serial2.cpp.o    /tmp/build6773645236763377018.tmp/core.a  -L/tmp/build6773645236763377018.tmp  -lm  -T  /opt/mpide-0023-linux32-20111221/hardware/pic32/cores/pic32/chipKIT-UNO32-application-32MX320F128L.ld
/opt/mpide-0023-linux32-20111221/hardware/pic32/compiler/pic32-tools/bin/pic32-objcopy  -O  ihex  -j  .eeprom  --set-section-flags=.eeprom=alloc,load  --no-change-warnings  --change-section-lma  .eeprom=0  /tmp/build6773645236763377018.tmp/Serial2.cpp.elf  /tmp/build6773645236763377018.tmp/Serial2.cpp.eep
/opt/mpide-0023-linux32-20111221/hardware/pic32/compiler/pic32-tools/bin/pic32-bin2hex  -a  /tmp/build6773645236763377018.tmp/Serial2.cpp.elf
Binary sketch size: 10748 bytes (of a 126976 byte maximum)
  • temporary directory is /tmp/build6773645236763377018.tmp/
  • Hex file will be named /tmp/build6773645236763377018.tmp/Serial2.cpp.hex
WARNING: the temporary directory will be removed when mpide will be closed. You have to backup the hex file before closing mpide. To go back to initial configuration (files removed after each build), just set the preproc.save_build_files attribute to false
preproc.save_build_files=false

Using AVRDUDE from Linux console

copy avrdude.conf file from mpide to ~/.avrduderc, example:
cp /opt/mpide-0023-linux32-20111221/hardware/tools/avrdude.conf ~/.avrdude.rc
Use ever avrdude from mpide or from your own Linux distro… Here are details about avrdude from Ubuntu Precise Penguin:
dpkg-query -l avrdude
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name                             Version                          Description
+++-================================-================================-================================================================================
ii  avrdude                          5.11.1-1                         software for programming Atmel AVR microcontrollers
Check if the avrdude configuration file from mpide is used, and which PIC32 devices are available:
fred@kat:~/MPLABXProjects/Blink$ avrdude -p ?

Valid parts are:
  pic32-440 = 32MX440F512H    [/home/fred/.avrduderc:16438]
  pic32-460 = 32MX460F512L    [/home/fred/.avrduderc:16251]
  pic32-360 = 32MX320F128H    [/home/fred/.avrduderc:16054]
  pic32-360 = 32MX320F064H    [/home/fred/.avrduderc:15863]
  pic32-360 = 32MX360F512L    [/home/fred/.avrduderc:15672]
  pic32 = 32MX795F512L    [/home/fred/.avrduderc:15483]

...
We can see that pic32-360 has to be used for Uno32 (32MX320F128H) Example, with:
  • avrdude from Ubuntu distro (Precise Penguin – avrdude 5.11.1)
  • Uno32 located on /dev/ttyUSB0
  • hex file build by using mpide
fred@kat:~/MPLABXProjects$ avrdude -P /dev/ttyUSB0 -p pic32-360  -b 115200 -c stk500v2 -v -v -U flash:w:Haribo.cpp.hex

avrdude: Version 5.11.1, compiled on Oct 30 2011 at 10:37:28
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2009 Joerg Wunsch

         System wide configuration file is "/etc/avrdude.conf"
         User configuration file is "/home/fred/.avrduderc"

         Using Port                    : /dev/ttyUSB0
         Using Programmer              : stk500v2
         Overriding Baud Rate          : 115200
avrdude: ser_recv(): programmer is not responding
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: ser_recv(): programmer is not responding
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: ser_recv(): programmer is not responding
avrdude: stk500v2_ReceiveMessage(): timeout
Oops… Upload error ? I have to press the Uno32 reset button, then code is uploaded:
         AVR Part                      : 32MX320F128H
         Chip Erase delay              : 9000 us
         PAGEL                         : PD7
         BS2                           : PA0
         RESET disposition             : dedicated
         RETRY pulse                   : SCK
         serial program mode           : yes
         parallel program mode         : yes
         Timeout                       : 200
         StabDelay                     : 100
         CmdexeDelay                   : 25
         SyncLoops                     : 32
         ByteDelay                     : 0
         PollIndex                     : 3
         PollValue                     : 0x53
         Memory Detail                 :

                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           eeprom        65    10     8    0 no       4096    8      0  9000  9000 0x00 0x00
           flash         65    10   256    0 yes    131072  256    512  4500  4500 0x00 0x00
           lfuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           hfuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           efuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           lock           0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           calibration    0     0     0    0 no          1    0      0     0     0 0x00 0x00
           signature      0     0     0    0 no          3    0      0     0     0 0x00 0x00

         Programmer Type : STK500V2
         Description     : Atmel STK500 Version 2.x firmware
         Programmer Model: AVRISP
         Hardware Version: 15
         Firmware Version Master : 2.10
         Vtarget         : 0.0 V
         SCK period      : 0.1 us

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.01s

avrdude: Device signature = 0x504943
avrdude: safemode: lfuse reads as 0
avrdude: safemode: hfuse reads as 0
avrdude: safemode: efuse reads as 0
avrdude: NOTE: FLASH memory has been specified, an erase cycle will be performed
         To disable this feature, specify the -D option.
avrdude: current erase-rewrite cycle count is -1145324613 (if being tracked)
avrdude: erasing chip
avrdude: reading input file "Haribo.cpp.hex"
avrdude: input file Haribo.cpp.hex auto detected as Intel Hex
avrdude: writing flash (32664 bytes):

Writing | ################################################## | 100% 9.15s

avrdude: 32664 bytes of flash written
avrdude: verifying flash memory against Haribo.cpp.hex:
avrdude: load data flash data from input file Haribo.cpp.hex:
avrdude: input file Haribo.cpp.hex auto detected as Intel Hex
avrdude: input file Haribo.cpp.hex contains 32664 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 3.29s

avrdude: verifying ...
avrdude: 32664 bytes of flash verified

avrdude: safemode: lfuse reads as 0
avrdude: safemode: hfuse reads as 0
avrdude: safemode: efuse reads as 0
avrdude: safemode: Fuses OK

avrdude done.  Thank you.

Using AVRDUDE from within MPLAB X

To use avrdude to upload a hex file you just built in MPLAB X, there are a couple steps you need to follow:
  1. (optional) From the MPIDE install folder, copy (as an example) C:\Program Files\mpide-0023-windows-20120122-test\hardware\tools\avr\bin\avrdude.exe to someplace you want to put avrdude and its configuration file, like C:\Program Files\Microchip\MPLABX\mplab_ide\bin\avrdude.exe
  2. (optional) From the MPIDE install folder, copy (as an example) C:\Program Files\mpide-0023-windows-20120122-test\hardware\tools\avr\etc\avrdude.conf to someplace you want to put avrdude and its configuration file, like C:\Program Files\Microchip\MPLABX\mplab_ide\bin\avrdude.conf
  3. From within MPLABX, open the properties for your current project. Select the Building/Loading screen, then enter the following for “Execute this line after build” (and check the box as well):
    • “C:\Program Files\Microchip\MPLABX\mplab_ide\bin\avrdude.exe” -C”C:\Program Files\Microchip\MPLABX\mplab_ide\bin\avrdude.conf” -v -p32MX795F512L -cstk500v2 -P COM40 -b 115200 -U flash:w:${ImagePath}
    • (Note that you will need to substitute the proper COM port number of your board, as well as the actual location of avrdude.exe and avrdude.conf into this command line. Also, if you’re not using an MX795 you’ll have to update that as well.)
  4. Make sure you have copied the proper linker script into your project. For example, if you’re using a chipKIT MAX32 board, just copy the chipKIT-MAX32-application-32MX795F512L.ld file from C:\Program Files\mpide-0023-windows-20120122-test\hardware\pic32\cores\pic32 into your MPLABX project folder, and then add that file to the Linker Files folder in the Project Properties.
If all of the paths are set up properly, you should get an automatic download every time you rebuild your project in MPLABX. Many times, I need to reset my chipKIT board in order to get the download to work, even though I don’t need to do this when using MPIDE. I’m not sure why this is – if anyone can suggest a workaround, that would really help.
VN:F [1.9.22_1171]
Rating: 10.0/10 (1 vote cast)
VN:F [1.9.22_1171]
Rating: +1 (from 1 vote)