Olá Galera. 

Sou novo na área e fiz uns testes com um Wifi Shield, mas não consegui fazer que ele "logasse" em uma rede com IP fixo, muito embora eu tenha conseguido que ele conectasse em uma rede com IP dinâmico. Por isso, queria saber se alguém sabe o que devo fazer para alterar o código abaixo para eu conseguir que o Wifi Shield conectasse a uma rede com IP fixo?

Desde já, muito obrigado!

Abaixo segue o código:

#undef PROGMEM
#define PROGMEM __attribute__(( section(".progmem.data") ))
#undef PSTR
#define PSTR(s) (__extension__({static prog_char __c[] PROGMEM = (s); &__c[0];}))

#include <WiFlyHQ.h>

#include <SoftwareSerial.h>
SoftwareSerial wifiSerial(4,5);

//#include <AltSoftSerial.h>
//AltSoftSerial wifiSerial(8,9);

WiFly wifly;

/* Change these to match your WiFi network */
const char mySSID[] = "Nome da rede";
const char myPassword[] = "Rede";

//byte ip[] = { 192,168,254, 200 }; // ip que o arduino assumirá
//byte gateway[] = { 192,168,254, 254 }; // ip do roteador
//byte subnet[] = { 255, 255, 0, 0 };

void sendIndex();
void sendGreeting(char *name);
void send404();

char buf[80];

void setup()
{
Serial.begin(115200);
Serial.println(F("Starting"));
Serial.print(F("Free memory: "));
Serial.println(wifly.getFreeMemory(),DEC);

wifiSerial.begin(9600);
if (!wifly.begin(&wifiSerial, &Serial)) {
Serial.println(F("Failed to start wifly"));
wifly.terminal();
}

/* Join wifi network if not already associated */
if (!wifly.isAssociated()) {
/* Setup the WiFly to connect to a wifi network */
Serial.println(F("Joining network"));
wifly.setSSID(mySSID);
wifly.setPassphrase(myPassword);
wifly.enableDHCP();
wifly.save();

if (wifly.join()) {
Serial.println(F("Joined wifi network"));
} else {
Serial.println(F("Failed to join wifi network"));
wifly.terminal();
}
} else {
Serial.println(F("Already joined network"));
}

wifly.setBroadcastInterval(0); // Turn off UPD broadcast

//wifly.terminal();

Serial.print(F("MAC: "));
Serial.println(wifly.getMAC(buf, sizeof(buf)));
Serial.print(F("IP: "));
Serial.println(wifly.getIP(buf, sizeof(buf)));

wifly.setDeviceID("WebServer - Sem fio");

if (wifly.isConnected()) {
Serial.println(F("Old connection active. Closing"));
wifly.close();
}

wifly.setProtocol(WIFLY_PROTOCOL_TCP);
if (wifly.getPort() != 80) {
wifly.setPort(80);
/* local port does not take effect until the WiFly has rebooted (2.32) */
wifly.save();
Serial.println(F("Set port to 80, rebooting to make it work"));
wifly.reboot();
delay(3000);
}
Serial.println(F("Ready"));
}

void loop()
{
if (wifly.available() > 0) {

/* See if there is a request */
if (wifly.gets(buf, sizeof(buf))) {
if (strncmp_P(buf, PSTR("GET / "), 6) == 0) {
/* GET request */
Serial.println(F("Got GET request"));
while (wifly.gets(buf, sizeof(buf)) > 0) {
/* Skip rest of request */
}
sendIndex();
Serial.println(F("Sent index page"));
} else if (strncmp_P(buf, PSTR("POST"), 4) == 0) {
/* Form POST */
char username[16];
Serial.println(F("Got POST"));

/* Get posted field value */
if (wifly.match(F("user="))) {
wifly.gets(username, sizeof(username));
wifly.flushRx(); // discard rest of input
sendGreeting(username);
Serial.println(F("Sent greeting page"));
}
} else {
/* Unexpected request */
Serial.print(F("Unexpected: "));
Serial.println(buf);
wifly.flushRx(); // discard rest of input
Serial.println(F("Sending 404"));
send404();
}
}
}
}

/** Send an index HTML page with an input box for a username */
void sendIndex()
{
/* Send the header direclty with print */
wifly.println(F("HTTP/1.1 200 OK"));
wifly.println(F("Content-Type: text/html"));
wifly.println(F("Transfer-Encoding: chunked"));
wifly.println();

/* Send the body using the chunked protocol so the client knows when
* the message is finished.
* Note: we're not simply doing a close() because in version 2.32
* firmware the close() does not work for client TCP streams.
*/
wifly.sendChunkln(F("<html>"));
wifly.sendChunkln(F("<title>Servidor HTTP Exemplo</title>"));
wifly.sendChunkln(F("<h1>"));
wifly.sendChunkln(F("<p>Interface em desenvolvimento...</p>"));
wifly.sendChunkln(F("</h1>"));
wifly.sendChunkln(F("<form name=\"input\" action=\"/\" method=\"post\">"));
wifly.sendChunkln(F("Usuario:"));
wifly.sendChunkln(F("<input type=\"text\" name=\"user\" />"));
wifly.sendChunkln(F("<input type=\"submit\" value=\"Submit\" />"));
wifly.sendChunkln(F("</form>"));
wifly.sendChunkln(F("</html>"));
wifly.sendChunkln();
}

/** Send a greeting HTML page with the user's name and an analog reading */
void sendGreeting(char *name)
{
/* Send the header directly with print */
wifly.println(F("HTTP/1.1 200 OK"));
wifly.println(F("Content-Type: text/html"));
wifly.println(F("Transfer-Encoding: chunked"));
wifly.println();

/* Send the body using the chunked protocol so the client knows when
* the message is finished.
*/
wifly.sendChunkln(F("<html>"));
wifly.sendChunkln(F("<title>WiFly HTTP Server Example</title>"));
/* No newlines on the next parts */
wifly.sendChunk(F("<h1><p>Seja bem-vindo "));
wifly.sendChunk(name);
/* Finish the paragraph and heading */
wifly.sendChunkln(F("</p></h1>"));

/* Include a reading from Analog pin 0 */
snprintf_P(buf, sizeof(buf), PSTR("<p>Valor lido na porta analogica 0 = %d</p>"), analogRead(A0));
wifly.sendChunkln(buf);

wifly.sendChunkln(F("</html>"));
wifly.sendChunkln();
}

/** Send a 404 error */
void send404()
{
wifly.println(F("HTTP/1.1 404 Not Found"));
wifly.println(F("Content-Type: text/html"));
wifly.println(F("Transfer-Encoding: chunked"));
wifly.println();
wifly.sendChunkln(F("<html><head>"));
wifly.sendChunkln(F("<title>404 Not Found</title>"));
wifly.sendChunkln(F("</head><body>"));
wifly.sendChunkln(F("<h1>Not Found</h1>"));
wifly.sendChunkln(F("<hr>"));
wifly.sendChunkln(F("</body></html>"));
wifly.sendChunkln();
}

Exibições: 604

Responder esta

© 2024   Criado por Marcelo Rodrigues.   Ativado por

Badges  |  Relatar um incidente  |  Termos de serviço