Re: SerialPort et WiFi
Publié : mar. 17/avr./2012 22:43
Faut que tu regardes la doc de ton shield , ca dois pas être bien compliqué, après si tu m'en payes un , je t'aiderais bien mieux 

Code : Tout sélectionner
/*
* A simple sketch that uses WiServer to serve a web page
*/
#include <WiServer.h>
#define WIRELESS_MODE_INFRA 1
#define WIRELESS_MODE_ADHOC 2
// Wireless configuration parameters ----------------------------------------
unsigned char local_ip[] = {
192,168,0,84}; // IP address of WiShield
unsigned char gateway_ip[] = {
192,168,0,254}; // router or gateway IP address
unsigned char subnet_mask[] = {
255,255,255,0}; // subnet mask for the local network
const prog_char ssid[] PROGMEM = {
"JPPW"}; // max 32 bytes
unsigned char security_type = 2; // 0 - open; 1 - WEP; 2 - WPA; 3 - WPA2
// WPA/WPA2 passphrase
const prog_char security_passphrase[] PROGMEM = {
"0123456789"}; // max 64 characters
// WEP 128-bit keys
// sample HEX keys
prog_uchar wep_keys[] PROGMEM = {
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, // Key 0
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 1
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 2
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // Key 3
};
// setup the wireless mode
// infrastructure - connect to AP
// adhoc - connect to another WiFi device
unsigned char wireless_mode = WIRELESS_MODE_INFRA;
unsigned char ssid_len;
unsigned char security_passphrase_len;
// End of wireless configuration parameters ----------------------------------------
// This is our page serving function that generates web pages
boolean sendMyPage(char* URL) {
// Check if the requested URL matches "/"
if (strcmp(URL, "/") == 0) {
// Use WiServer's print and println functions to write out the page content
links ();
// URL was recognized
return true;
}
else if (strcmp(URL, "/on") == 0) {
digitalWrite(13, HIGH); // turn Left on
links ();
// URL was recognized
return true;
}
else if (strcmp(URL, "/off") == 0) {
digitalWrite(13, LOW); // turn Right on
links ();
// URL was recognized
return true;
}
// URL not found
return false;
}
void links () {
WiServer.print("<html>");
WiServer.print("<center>[ <a href=\"/on\">ON</a> ]<br />");
WiServer.print("[ <a href=\"/off\">OFF</a> ]</center>");
WiServer.print("</html>");
}
void setup() {
// Initialize WiServer and have it use the sendMyPage function to serve pages
WiServer.init(sendMyPage);
pinMode(13, OUTPUT);
// Enable Serial output and ask WiServer to generate log messages (optional)
Serial.begin(57600);
WiServer.enableVerboseMode(true);
}
void loop(){
// Run WiServer
WiServer.server_task();
}
Code : Tout sélectionner
// Tiny Robot Wifi Control Program
// November 9th, 2009
// Robert Svec
#include <WiShield.h>
#include <Servo.h>
#define WIRELESS_MODE_INFRA 1
#define WIRELESS_MODE_ADHOC 2
Servo LeftServo;
Servo RightServo;
Servo IRServo;
unsigned char local_ip[] = {192,168,1,220};
unsigned char gateway_ip[] = {192,168,1,1};
unsigned char subnet_mask[] = {255,255,255,0};
const prog_char ssid[] PROGMEM = {"Airwavers"};
unsigned char security_type = 3; // 0 - open; 1 - WEP; 2 - WPA; 3 - WPA2
const prog_char security_passphrase[] PROGMEM = {"testwpakey"};
prog_uchar wep_keys[] PROGMEM = {};
unsigned char wireless_mode = WIRELESS_MODE_INFRA;
unsigned char ssid_len;
unsigned char security_passphrase_len;
char mybuff[20], sendbuff;
int myintbuff, objdist1, objdist2, objdist3;
void setup()
{
WiFi.init();
LeftServo.attach(5);
RightServo.attach(6);
IRServo.attach(7);
RightServo.write(90);
delay(20);
LeftServo.write(90);
delay(20);
IRServo.write(100);
delay(20);
myintbuff = 0;
Serial.begin(9600);
}
void loop()
{
WiFi.run();
myintbuff = atoi(mybuff);
if (myintbuff == 1)
{
MoveForward();
mybuff[0] = '9';
}
if (myintbuff == 2)
{
TurnLeft();
mybuff[0] = '9';
}
if (myintbuff == 3)
{
TurnRight();
mybuff[0] = '9';
}
if (myintbuff == 4)
{
MoveBackward();
mybuff[0] = '9';
}
if (myintbuff == 5)
{
AutoRoam();
mybuff[0] = '9';
}
if (myintbuff == 6)
{
IRLeft();
mybuff[0] = '9';
}
if (myintbuff == 7)
{
IRRight();
mybuff[0] = '9';
}
if (myintbuff == 8)
{
IRFront();
mybuff[0] = '9';
}
else
{
LeftServo.write(90);
RightServo.write(90);
}
// objdist1 = analogRead(0);
// sendbuff = char(objdist1);
}
// ==========================
// ===== AUTO ROAMING =====
// ==========================
void AutoRoam()
{
for (int i = 1; i < 20; i++)
{
objdist1 = analogRead(0);
if (objdist1 <= 400)
{
MoveForward();
delay(200);
}
else
{
IRLeft();
objdist2 = analogRead(0);
delay(200);
IRFront();
IRRight();
objdist3 = analogRead(0);
delay(200);
IRFront();
if (objdist2 < objdist3)
{
TurnLeft();
}
if (objdist3 <objdist2)
{
TurnRight();
}
if (objdist2 == objdist3)
{
MoveBackward();
TurnRight();
TurnRight();
TurnRight();
TurnRight();
}
}
}
}
// =================================
// ===== MOTOR SERVO CONTROL =====
// =================================
void TurnLeft()
{
LeftServo.write(0);
RightServo.write(0);
delay(500);
// LeftServo.write(90);
// RightServo.write(90);
}
void TurnRight()
{
LeftServo.write(180);
RightServo.write(180);
delay(500);
// LeftServo.write(90);
// RightServo.write(90);
}
void MoveForward()
{
LeftServo.write(180);
RightServo.write(0);
delay(500);
// LeftServo.write(90);
// RightServo.write(90);
}
void MoveBackward()
{
LeftServo.write(0);
RightServo.write(180);
delay(300);
// LeftServo.write(90);
// RightServo.write(90);
}
// ======================================
// ===== INFRARED SERVO CONTROL =====
// ======================================
void IRRight()
{
// IR Right
IRServo.write(30);
delay(200);
}
void IRFront()
{
// IR Front
IRServo.write(100);
delay(200);
}
void IRLeft()
{
// IR Left
IRServo.write(170);
delay(200);
}