Hi everyone,
I recently got my arduino and a wifi shield based on the Wifly RN-171 wifi module. Now for testing purpose i was trying to turn a led on and off by typing ipadress/on and ipadress/off in my browser. However i wasnt able to succeed in actually controlling that led. There is a WiflySerial Library i was using for programming. Also my sketch is based on the example sketch that comes with this library aswell as a code from the sparkfun forums: RN-XV + Arduino Uno = slow closing connection? - SparkFun Electronics.
#include "DHT.h"
#include <SoftwareSerial.h>
#include <Streaming.h>
#include <WiFlySerial.h>
#define RX 2
#define TX 3
#define BUFFER_SIZE 80
#define DHTTYPE DHT11
#define DHTPIN 2
DHT dht(DHTPIN, DHTTYPE);
WiFlySerial wifi(RX, TX);
char buffer[BUFFER_SIZE];
unsigned long t1;
unsigned long t2;
char error1[40] = "Failed to read from DHT";
int e1 = 0;
int led = 13;
boolean Reconnect() {
wifi.setDHCPMode(WIFLY_DHCP_CACHE );
wifi.SendCommand("set u m 0x1", ">",buffer, BUFFER_SIZE);
wifi.SendCommand("set comm remote 0", ">",buffer, BUFFER_SIZE);
wifi.SendCommand("set comm match 0x9", ">",buffer, BUFFER_SIZE);
wifi.leave() << endl;
// join
wifi.setPassphrase("-myPassphrase-");
wifi.join("--mySSID");
wifi.exitCommandMode();
}
void setup()
{
pinMode(led,OUTPUT);
digitalWrite(led,LOW);
Serial.begin(9600);
wifi.begin();
Serial << F("Starting...")<< endl;
wifi.setAuthMode(WIFLY_AUTH_WPA2_PSK);
wifi.setJoinMode(WIFLY_JOIN_AUTO);
wifi.setDHCPMode(WIFLY_DHCP_ON);
Reconnect();
delay(1000);
wifi.getDeviceStatus();
while (! wifi.isAssociated() )
{
Reconnect();
}
Serial << F("NETWORK") << endl
<< F("---MAC: ") << wifi.getMAC(buffer, BUFFER_SIZE) << endl
<< F("---IP: ") << wifi.getIP(buffer, BUFFER_SIZE) << endl
<< F("---Netmask: ") << wifi.getNetMask(buffer, BUFFER_SIZE) << endl
<< F("---Gateway: ") << wifi.getGateway(buffer, BUFFER_SIZE) << endl
<< F("---DNS: ") << wifi.getDNS(buffer, BUFFER_SIZE) << endl
<< F("---RSSI: ") << wifi.getRSSI(buffer, BUFFER_SIZE) << endl
<< F("---battery: ") << wifi.getBattery(buffer, BUFFER_SIZE) << endl;
Serial << "Ready." << endl << endl;
}
void loop()
{
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(t) || isnan(h))
{
Serial.println(error1); endl;
e1=0;
}
else
{
Serial << "Temperature: "<<t<<" "<<"Grad C " <<"Humidity: "<<h<<"%";
e1=1;
}
wifi.getDeviceStatus();
while (! wifi.isAssociated() )
{
Reconnect();
}
if(wifi.serveConnection())
{
t1 = millis();
Serial << t1 << ":" << "Connected" << endl;
wifi.ScanForPattern(buffer, BUFFER_SIZE, " HTTP/1.1", 250);
delay(100);
if (strstr(buffer,"/on"))
{
digitalWrite(led,HIGH);
}
else if (strstr(buffer,"/off"))
{
digitalWrite(led,LOW);
}
}
Serial << "GET request, bytes: " << strlen(buffer) << " request: " << buffer << endl;
if (e1=1)
{
wifi << "HTTP/1.1 200 OK \r Content-Type: text/html;charset=UTF-8\r Connection: close \r\n\r\n \r" <<"Server running for:" <<millis()/1000<< " seconds" <<"\r"<< "Humidity: "<< h <<" "<< "%"<< "\r"<<"Temperature: "<< t<<" "<<"Grad Celsius"<< "\r\n\r\n"<<"\t";
}
else
{
wifi << "HTTP/1.1 200 OK \r Content-Type: text/html;charset=UTF-8\r Connection: close \r\n\r\n \r" << millis() <<"\r"<< error1<< "\r\n\r\n" << "\t";
}
wifi.closeConnection();
t2 = millis();
Serial << t2 << ":" << "Connection Closed, Duration: " << (t2 -t1) / 1000 << " seconds" << endl;
}
Library : http://sourceforge.net/projects/arduinowifly/files/
So basically how i thought it would work is , the wifi shield would listen for incomming Client requests then store that request in the buffer array as a string and if the buffer array contains a certain string for example “/on” it would turn on my led. However that dont seem to be the case so if anyone could tell me what are my mistakes i would be really thankful.