Moderators: roger, 99jonathan, imaqine
public class TestWifi {
/* ---------------------------------- */
private final static int BUFFER_LENGTH = 2048;
private static Logger logger = null;
/* ---------------------------------- */
/**
* @param args
*/
public static void main(String[] args) {
LCD.clear();
TestWifi.logger.debug("Starting application");
new TestWifi().go();
}
/* ---------------------------------- */
public void go() {
RS485.hsEnable(9600, TestWifi.BUFFER_LENGTH);
int writeBytes = this.RS485WriteByteByByte("ATE0");
TestWifi.logger.debug("writeBytes=" + writeBytes);
writeBytes = this.RS485WriteBytes("AT&K1", 1000);
TestWifi.logger.debug("writeBytes=" + writeBytes);
writeBytes = this.RS485WriteBytes("ATV1", 1000);
TestWifi.logger.debug("writeBytes=" + writeBytes);
writeBytes = this.RS485WriteBytes("AT+WM=0", 1000);
TestWifi.logger.debug("writeBytes=" + writeBytes);
writeBytes = this.RS485WriteBytes("AT+WAUTH=1", 1000);
TestWifi.logger.debug("writeBytes=" + writeBytes);
writeBytes = this.RS485WriteBytes("AT+WPAPSK=yourssid,yourwifikey", 1000);
TestWifi.logger.debug("writeBytes=" + writeBytes);
writeBytes = this.RS485WriteBytes("AT+WD", 2000);
TestWifi.logger.debug("writeBytes=" + writeBytes);
writeBytes = this.RS485WriteBytes("AT+WA=yourssid", 2000);
TestWifi.logger.debug("writeBytes=" + writeBytes);
writeBytes = this.RS485WriteBytes("AT+NDHCP=1", 2000);
TestWifi.logger.debug("writeBytes=" + writeBytes);
RS485.hsDisable();
}
/* ---------------------------------- */
public int RS485WriteByteByByte(String buffer) {
TestWifi.logger.debug("RS485WriteByteByByte(buffer=" + buffer + ")");
buffer += "\r";
int bytesWrited = 0;
char[] bufferArray = buffer.toCharArray();
Stopwatch stopWatch = new Stopwatch();
for (int i = 0; i < bufferArray.length; i++) {
byte[] sendByte = new byte[1];
byte[] readByte = new byte[1];
int bytesSent = 0;
int bytesRead = 0;
sendByte[0] = (byte)bufferArray[i];
bytesSent = RS485.hsWrite(sendByte, 0, 1);
bytesWrited += bytesSent;
// TestWifi.logger.debug("bytesSent=" + bytesSent);
stopWatch.reset();
while (stopWatch.elapsed() < 100);
while (bytesRead < 1) {
bytesRead = RS485.hsRead(readByte, 0, 1);
}
// TestWifi.logger.debug("bytesRead=" + bytesRead + ", readByte=" + new String(readByte));
}
return bytesWrited;
}
/* ---------------------------------- */
public int RS485WriteBytes(String buffer, int waitMillis) {
TestWifi.logger.debug("RS485WriteBuffer(buffer=" + buffer + ", waitMillis=" + waitMillis + ")");
buffer += "\r";
int bytesWrited = 0;
int bytesRead = 0;
byte[] sendBytes = buffer.getBytes();
byte[] readBytes = new byte[TestWifi.BUFFER_LENGTH];
Stopwatch stopWatch = new Stopwatch();
bytesWrited = RS485.hsWrite(sendBytes, 0, sendBytes.length);
TestWifi.logger.debug("bytesWrited=" + bytesWrited);
stopWatch.reset();
while (stopWatch.elapsed() < waitMillis);
while (bytesRead < 1) {
bytesRead = RS485.hsRead(readBytes, 0, readBytes.length);
}
TestWifi.logger.debug("bytesRead=" + bytesRead + ", readBytes=" + new String(readBytes, 0, bytesRead - 1));
return bytesWrited;
}
}
christophe.burki wrote:Hi guys,
after reading the MindBoards post (thanks Aswin), I finally have successfully sent commands to the WiFi sensor. When the WiFi module is powered on, we need to configure it through AT command. The first command to send is the ATE0 which turn off echo. By default the module has echo on. This first command must be sent byte after byte and the response from the module must be read after each byte. All the following commands can be sent with multiple bytes. Note that each command must be terminated by a carriage return (\r or 0x0d). I have a working code that turn off echo, enable software flow control, connect to a wifi network and get an address by dhcp. The WiFi module can then be pinged.
christophe.burki wrote:Hi skoehler,
thanks for your comment.
Software flow control has been enabled because as you have said it was part of the Dexter Industries example. I did yet not have made any test to communicate with the nxt through wifi. I will try not to enable software flow control for my future experience of the wifi module.
/*
* www.dexterindustries.com
* www.dexterindustries.com/wifi.html
* You may use this code as you wish, provided you give credit where it's due.
*
* This is a program to demonstrate the DIWIFI lib, by connecting to a WPA protected WIFI network.
* see more about the Wifi Sensor at www.dexterindustries.com/wifi.html
*
* TO RUN THIS SOFTWARE YOU MUST USE THE FOLLOWING CONFIGURATION:
* FIRMWARE: lms_arm_nbcnxc_132_20120205_1737.rfw OR LATER
* BRICX COMMAND CENTER: version 3.3.8.10 OR LATER
* THE LATEST VERSIONS CAN BE FOUND HERE: http://bricxcc.sourceforge.net/test_releases/
*/
#include "DIWIFI lib.nxc"
task main(){
ClearScreen();
TextOut(0, LCD_LINE1, "Test Flow");
// To test flow control:
// First call network info.
// Immediately send XOFF
// Wait for 10 seconds.
// Clear buffer.
// Send XON.
char buffer[256];
DIWIFI_Echo_Off();
RS485Read(buffer);
string sbuffer = ByteArrayToStr(buffer);
TextOut(0, LCD_LINE2, sbuffer);
byte array_out[] = {'a','t','&','k','1',13};
RS485Write(array_out);
Wait(1000);
RS485Read(buffer);
sbuffer = ByteArrayToStr(buffer);
TextOut(0, LCD_LINE4, sbuffer);
Wait(3000);
ClearScreen();
DIWIFI_Clear_Read_Buffer_HS();
// TURN OFF FLOW CONTROL
byte xoff[] = {0x1B, 0x13}; // XOFF
byte ex_proc[] = {0x1B, 0x45, 0x0D};
RS485Write(xoff); // XOFF
Wait(500);
RS485Write(ex_proc);
Wait(500);
// CHECK BUFFER FOR ANY CHARACTERS
TextOut(0, LCD_LINE1, "Buf: ");
NumOut(50, LCD_LINE1, RS485DataAvailable());
PlaySound(SOUND_DOUBLE_BEEP);
RS485Read(buffer);
sbuffer = ByteArrayToStr(buffer);
TextOut(0, LCD_LINE2, sbuffer);
// TRY NOW FOR A RESPONSE. SAY SOMETHING.
//byte plusplus[] = {'+','+','+'};
//RS485Write(0x0D);
//Wait(500);
RS485Write(array_out); // Seem to need this as a dummy fire. The first 22 characters are an error message related. Afterwards, you get ok's.
Wait(250);
RS485Write(array_out);
Wait(250);
// CHECK FOR A RESPONSE. ANY RESPONSE.
TextOut(0, LCD_LINE3, "Buf: ");
NumOut(50, LCD_LINE3, RS485DataAvailable());
PlaySound(SOUND_DOUBLE_BEEP);
RS485Read(buffer);
sbuffer = ByteArrayToStr(buffer);
TextOut(0, LCD_LINE4, sbuffer);
// TURN FLOW CONTROL BACK ON
// byte xon[] = {0x11, 0x0D};
byte xon[] = {0x1B, 0x11}; // <ESC> Q
RS485Write(xon);
Wait(500);
RS485Write(ex_proc);
Wait(500);
// READ THE BUFFER RESPONSE
TextOut(0, LCD_LINE5, "Buf: ");
NumOut(50, LCD_LINE5, RS485DataAvailable());
PlaySound(SOUND_DOUBLE_BEEP);
int buffsize = RS485DataAvailable();
RS485Read(buffer);
sbuffer = ByteArrayToStr(buffer);
TextOut(0, LCD_LINE6, sbuffer);
TextOut(0, LCD_LINE7, SubStr(sbuffer, 17, 34));
Wait(10000);
}jdc2106 wrote:...there's always the possibility that a response comes to the module at exactly the same time as a signal is going out to the module, and to handle that, I think you would need hardware control (someone correct me if I'm wrong).
Users browsing this forum: No registered users and 0 guests