Go Down

Topic: Weird issue when using 433 RF transmitter and ethernet shield (Read 1 time) previous topic - next topic

jorgibravo

Hello Everyone,
Can you please give me your opinion on something I am working on? I am stuck on the same error for days and I can't figure this one out.

I modified W.A. Smith's SD Card Web Server I/O tutorial to fit my needs, the tutorial is great and I completed it with no issues.
I added a DHT11 sensor for temperature and humidity for this setup and I would like to use an RF transmitter to remotely operate my radio controlled power outlets.

I read the values from the remote and I created a sketch that can turn on and off each outlet - so that part is working fine.
I am able to read and display the DHT sensor values and I can control LED-s remotely from the SD card hosted website.

But when I try to modify the script to send a " mySwitch.send();" command instead of turning on the LED, it translates fine, I can upload to the Arduino but I can't access the webpage this way... the request times out and nothing happens.

My RF transmitter is on pin7 and the DHT is on pin 2.
I tried to use other available pins but didn't help. I am using an Uno with 328-PU.
The website is hosted on the sD card.

I attached my code, please advice, any suggestion is highly appreciated since I am out of ideas on my own:

Code: [Select]

/*--------------------------------------------------------------

Pin0: Free
Pin1: Free
Pin2: DHT
Pin3: Free
Pin4: SD?
Pin5: Free
Pin6: Free
Pin7: RF
Pin8: Free
Pin9: Free
pin10: Ethernet?
Pin11: Ethernet?
Pin12: Ethernet?
Pin13: Ethernet?

A0: Ethernet
A1: Ethernet
Based on the script by Author:  W.A. Smith, http://startingelectronics.com
  --------------------------------------------------------------
*/

#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
#include <RCSwitch.h>
#include "DHT.h"
// size of buffer used to capture HTTP requests
#define REQ_BUF_SZ   60
#define DHTPIN 2
#define DHTTYPE DHT11

//RadioControl:
RCSwitch mySwitch = RCSwitch();
int RCTransmissionPin = 7;

//Ethernet
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,177);
EthernetServer server(80);
File webFile;
char HTTP_req[REQ_BUF_SZ] = {0};
char req_index = 0;
boolean LED_state[4] = {0};

//DHT
DHT dht(DHTPIN, DHTTYPE);

void setup()
{
  // disable Ethernet chip
  pinMode(10, OUTPUT);
  digitalWrite(10, HIGH);

  Serial.begin(9600);       // for debugging

  // initialize SD card
  Serial.println("Initializing SD card...");
  if (!SD.begin(4)) {
    Serial.println("ERROR - SD card initialization failed!");
    return;    // init failed
  }
  Serial.println("SUCCESS - SD card initialized.");
  // check for index.htm file
  if (!SD.exists("index.htm")) {
    Serial.println("ERROR - Can't find index.htm file!");
    return;  // can't find index file
  }
  Serial.println("SUCCESS - Found index.htm file.");

  Ethernet.begin(mac, ip);  // initialize Ethernet device
  server.begin();           // start to listen for clients

  //initialize RF Transmiter
  mySwitch.enableTransmit( RCTransmissionPin );
  mySwitch.setPulseLength(185);

  //Initialize DHT sensor
  dht.begin();
  pinMode(9, OUTPUT);
}

void loop()
{
  EthernetClient client = server.available();  // try to get client
  Serial.println("server.available");
  if (client) {  // got client?
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {   // client data available to read
        char c = client.read(); // read 1 byte (character) from client
        // limit the size of the stored received HTTP request
        // buffer first part of HTTP request in HTTP_req array (string)
        // leave last element in array as 0 to null terminate string (REQ_BUF_SZ - 1)
        if (req_index < (REQ_BUF_SZ - 1)) {
          HTTP_req[req_index] = c;          // save HTTP request character
          req_index++;
        }
        // last line of client request is blank and ends with \n
        // respond to client only after last line received
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          // remainder of header follows below, depending on if
          // web page or XML page is requested
          // Ajax request - send XML file
          if (StrContains(HTTP_req, "ajax_inputs")) {
            // send rest of HTTP header
            client.println("Content-Type: text/xml");
            client.println("Connection: keep-alive");
            client.println();
            SetLEDs();
            // send XML file containing input states
            XML_response(client);
          }
          else {  // web page request
            // send rest of HTTP header
            client.println("Content-Type: text/html");
            client.println("Connection: keep-alive");
            client.println();
            // send web page
            webFile = SD.open("index.htm");        // open web page file
            if (webFile) {
              while(webFile.available()) {
                client.write(webFile.read()); // send web page to client
              }
              webFile.close();
            }
          }
          // display received HTTP request on serial port
          Serial.print(HTTP_req);
          // reset buffer index and all buffer elements to 0
          req_index = 0;
          StrClear(HTTP_req, REQ_BUF_SZ);
          break;
        }
        // every line of text received from the client ends with \r\n
        if (c == '\n') {
          // last character on line of received text
          // starting new line with next character read
          currentLineIsBlank = true;
        }
        else if (c != '\r') {
          // a text character was received from client
          currentLineIsBlank = false;
        }
      } // end if (client.available())
    } // end while (client.connected())
    delay(1);      // give the web browser time to receive the data
    client.stop(); // close the connection
  } // end if (client)
}

void SetLEDs(void)
{
//switch1 ON
  if (StrContains(HTTP_req, "Kapcsolo1ON")) {
  mySwitch.send(4527411, 24);
  }

//switch1 OFF
  if (StrContains(HTTP_req, "Kapcsolo1OFF")) {
  mySwitch.send(4527420, 24);
  }
 
  if (StrContains(HTTP_req, "Kapcsolo2ON")) {
  mySwitch.send(4527555, 24);
  }

  if (StrContains(HTTP_req, "Kapcsolo2OFF")) {
  mySwitch.send(4527564, 24);
  }

  if (StrContains(HTTP_req, "Kapcsolo3ON")) {
  mySwitch.send(4527875, 24);
  }

  if (StrContains(HTTP_req, "Kapcsolo3OFF")) {
  mySwitch.send(4527884, 24);
  }

  if (StrContains(HTTP_req, "MindenON")) {
  mySwitch.send(4527411, 24); 
  mySwitch.send(4527555, 24); 
  mySwitch.send(4527875, 24);
  }

  if (StrContains(HTTP_req, "MindenOFF")) {
  mySwitch.send(4527420, 24); 
  mySwitch.send(4527464, 24); 
  mySwitch.send(4527884, 24);
  }

}

// send the XML file with analog values, switch status
//  and LED status
void XML_response(EthernetClient cl)
{
  //int analog_val;            // stores value read from analog inputs
  int count;                 // used by 'for' loops
  float h = dht.readHumidity();
  float t = dht.readTemperature();

  cl.print("<?xml version = \"1.0\" ?>");
  cl.print("<inputs>");

  cl.print("<TEMP>");
  cl.print(t);
  cl.println("</TEMP>");

  cl.print("<HUM>");
  cl.print(h);
  cl.println("</HUM>");

  cl.print("</inputs>");
}

// sets every element of str to 0 (clears array)
void StrClear(char *str, char length)
{
  for (int i = 0; i < length; i++) {
    str[i] = 0;
  }
}

// searches for the string sfind in the string str
// returns 1 if string found
// returns 0 if string not found
char StrContains(char *str, char *sfind)
{
  char found = 0;
  char index = 0;
  char len;

  len = strlen(str);

  if (strlen(sfind) > len) {
    return 0;
  }
  while (index < len) {
    if (str[index] == sfind[found]) {
      found++;
      if (strlen(sfind) == found) {
        return 1;
      }
    }
    else {
      found = 0;
    }
    index++;
  }

  return 0;
}


Thanks and regards,


B83s

#1
Apr 11, 2014, 10:21 pm Last Edit: Apr 12, 2014, 09:29 am by B83s Reason: 1
I see that req_index is reset to 0 at the end of the loop ... Try to set it at 0 just after the if (client) ... It will be better for understanding the program

Maybe add a '\0' car at the end of the string HTTP_req once all the request has been read ...

You should serial.print the HTTP_req before using it to see what it contains Just after the the line if (c == '\n' && ....)

jorgibravo

Thanks thats a good idea!

I didn't find a fix for this issue... instead I searched for another RF library and after implementing the RemoteSwitch from https://github.com/hjgode/homewatch/tree/master/arduino/libraries/RemoteSwitch it worked perfectly fine!

Thanks for the help!

henrikl2000

Hi jorgibravo,

Thanks a lot for the link to Randy Simons library. It works great.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy