Sunday, January 29, 2012

sensorNET - code

As promised on my last post, here's the code as it stands today new items tagged as [NEW]:

1. Ability to choose DHCP or static.2. If static, ability to set IP, subnet and gateway addresses.3. Saves settings to EEPROM so they are available even after power down.4. DHCP selected out of the box5. EEPROM space for MAC address and device serial number6. Configuration done by Serial connection at 115200baud7. Rock solid with 12V power supply (will reset randomly with USB power only).

8. [NEW]: console automatically scans up to 32 client addresses and gets board id number (to be used to collect list of commands for the board from the server) for the ones it finds.

9. [NEW]: device scanning is shown through a serial connection for debugging purposes

12. [NEW]: Main console HW includes 20nF capacitor between RESET and GND to allow for proper reset of the wiznet Ethernet chip (I was having trouble with this), so I think the capacitor is a requirement.

I am currently at the point where I need to start testing the physical devices with the server software. I already have some read and write APIs and a some degree of interaction with the server (create users, create feeds, etc) that I will start using until the final server code is developed by my colleagues. I will have to develop a simple client database (see my previous post on this). Probably at the end of next week I will be able to start posting some of teh server side software along with the device software.

code is posted after the break (note two segments, one for the console, another for the client) BEFORE USING run this through a text editor like notepad and replace "lessthen" by "<" - I had to do it to allow it to be posted on the blog and not be used as a HTML tag:

//MASTER CONSOLE CODE:

#include lessthenSPI.h>

#include lessthenEthernet.h>

#include lessthenEEPROM.h>

#include lessthenWire.h>

byte ip_static[] = { 0, 0, 0, 0 };

byte mac[] = { 0, 0, 0, 0, 0, 0 };

byte subnet[] = { 0, 0, 0, 0 };

byte gateway[] = { 0, 0, 0, 0 };

byte server_address[] = { 0, 0, 0, 0 };

int state = 0;

int incomingByte;

int char_count;

char netstring[37];//size of set network parameters string

char serverstring[12];//size of server address

char device_id[32];

EthernetServer server(80);

void setup() {

Serial.begin(115200);

Wire.begin();

//intialize device_id array:

for (int i = 0; i lessthensizeof(device_id); i++){

device_id[i] = 0x00;//0x00 means no device on that address

}

//is this the first time running the program?

if (EEPROM.read(2) == 255){

//if so, we need to program the ROM with defaults (basically set DHCP):