I’m using the Wifly library (latest version) with the Wifly shield. I feel like this should be simple, but how can I communicate with the WiFly module from within a sketch to change settings, etc as outlined in the module’s data sheet? I understand you can use telnet and the included terminal sketch, but I need to be able to change settings on the fly…no pun intended.
The sketch has to perform exactly those steps that you would take using telnet or a terminal program to change the settings manually. That is, send characters over the serial interface to force the module into command mode, receive characters and check that the response is appropriate, issue a command, check that the response to the command is appropriate, then terminate the transaction and make sure that the transaction was properly terminated.
The process can be simple, if everything works as expected, or quite complex if a problem appears that requires sophisticated error checking to determine the appropriate response. It is not easy to foresee all the things that can go wrong and plan for them in your program.
Here is a simple (sortof) example. This is my UART terminal program, (ran on a PIC16F628A, but you can get the point) which was used for interfacing a security module I was tasked to make.
if(uartbufferpointer != 0x00){ //Uartbufferpointer is a char that would increment everytime a new char came in the buffer
buffer = 0x01;//Temporary char
while(buffer < uartbufferpointer){
uartbuffer[buffer-1] = uartbuffer[buffer];
buffer++;
}//We moved the buffer up one, to read the next char and to make room for the next char to come in the UART
uartbufferpointer--; //We indicated we made room for the next char
if(uartbuffer[0] == 0x0D){ //uartbuffer[0] is the next char to be processed
linkstatus = 0x0D; //The return char was my way of restoring the system if I hit a wrong key or it went corrupt(in some way, its not that complex
}
switch(linkstatus){
case 0x0D: //Again, this is the restore character. Please ignore it.
linkstatus = 0x01;
break;
case 0x01: //This is my neutral status. I return here when I want to process
linkstatus = uartbuffer[0];
if(linkstatus == 'i'){ //The following if statements before the break are for options that did not require other options
punch(timeoutu);
punch(timeoutl);
punch(0x0D);
linkstatus = 0x01;
}
if(linkstatus == 'a'){
arm = !arm;
if(arm){
punch('y');
}
else{
punch('n');
}
punch(0x0D);
linkstatus = 0x01;
}
if(linkstatus == 'o'){
enabletimeo = !enabletimeo;
if(enabletimeo){
punch('y');
}
else{
punch('n');
}
punch(0x0D);
linkstatus = 0x01;
}
if(linkstatus == 'k'){
punch('K');
punch(0x0D);
linkstatus = 0x01;
}
if(linkstatus == 'w'){
punch(upper);
punch(lower);
punch(0x0D);
linkstatus = 0x01;
}
if(linkstatus == 'r'){
punch(eeprom_read(pointer));
char temporary = 0;
while(temporary != eeprom_read(pointer)){
punchdt(retrievefromlogs(temporary));
temporary++;
}
punch(0x0D);
linkstatus = 0x01;
}
if(linkstatus == 'g'){
punchdt(rtcc);
linkstatus = 0x01;
punch(0x0D);
}
if(linkstatus == 'p'){
punch(movementu);
punch(movementl);
punch(0x0D);
linkstatus = 0x01;
}
if(linkstatus == 'e'){
enabletime = !enabletime;
if(enabletime){
punch('y');
}
else{
punch('n');
}
punch(0x0D);
linkstatus = 0x01;
}
substatus = 0x00;
break;
case 't': //The rest are for when other options are required for action.
switch(substatus){
case 0x00:
rtcc.time.hour = uartbuffer[0];
break;
case 0x01:
rtcc.time.minutes = uartbuffer[0];
break;
case 0x02:
rtcc.time.seconds = uartbuffer[0];
break;
case 0x03:
rtcc.date.day = uartbuffer[0];
break;
case 0x04:
rtcc.date.month = uartbuffer[0];
break;
case 0x05:
rtcc.date.year = uartbuffer[0];
break;
}
substatus++;
if(substatus == 0x06){
linkstatus = 0x01;
punch(0x0D);
}
break;
case 'o':
if(substatus == 0x00){
timeoutu =uartbuffer[0];
substatus++;
}
else if(substatus == 0x01){
timeoutl =uartbuffer[0];
punch(0x0D);
substatus = 0x01;
}
break;
case 'c':
if(uartbuffer[0] == 'y'){
eeprom_write(pointer,0x00);
}
linkstatus = 0x01;
punch(0x0D);
break;
case 'u':
if(substatus == 0x00){
upper = uartbuffer[0];
substatus++;
}
else{
lower = uartbuffer[0];
linkstatus = 0x01;
punch(0x0D);
}
break;
}
I will admit, I could have done much better in writing this code. I was given 2 days from the time I was assigned it to the time the unit needed to be installed and operational. But, it works. All it is is a large finite state machine.
Thank you very much for the replies. I was really hoping the answer would be more simple, or perhaps was part of the wifly library. Oh well…
Well making a terminal is actually very simple. Are you aware on how to make a finite state machine?
I think it might be a bit over my head for the time being...I'm not sure what a finite state machine is. :)Joeisi:
Well making a terminal is actually very simple. Are you aware on how to make a finite state machine?