RCSwitch doesnt work in loop()

Hi yall, i had quite a few arduino projects, thus im not a bloody beginner hahaha.

But im now stuck as HELL, cant figure out why my 433Hz Transmitter behaves like that:

My plan was to switch remote outlets on and off depending on a webserver. However that is working just fine.

simultaneously i wanted to program the arduino (mega 2560), for autonomy. in other words the transmitter should transmit the on and off signals after a specific time (on signal after 5Min off; off signal after 2Sec on).
I dont know why but its only controlling while in void setup(). As soon as void loop(), the transmitter doesnt transmits anything … what am i missing??? Please help meee

My code so far:

#include <SPI.h>
#include <Ethernet.h>

#include <RCSwitch.h>

#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#define DHTPIN 12
#define DHTTYPE DHT22 
DHT dht(DHTPIN, DHTTYPE);
float hum=0;
float temp=0;

int led = 6;

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };   //physical mac address
byte ip[] = { 192, 168, 0, 45 };                      // ip in lan (that's what you need to use in your browser. ("192.168.1.178")
byte gateway[] = { 192, 168, 0, 1 };                   // internet access via router
byte subnet[] = { 255, 255, 255, 0 };                  //subnet mask
EthernetServer server(80);                             //server port     
String readString;

RCSwitch mySwitch = RCSwitch();

unsigned long preMillisDht=0;
unsigned long postMillisDht=0;

unsigned long preMillisHpPump=0;
unsigned long postMillisHpPump=0;
unsigned long MillisHpPumpAn=0;
unsigned long MillisHpPumpAus=0;
int state = 0;

void setup() {
 // Open serial communications and wait for port to open:
  Serial.begin(9600);

  dht.begin();
  hum = dht.readHumidity();
  temp = dht.readTemperature();


  mySwitch.enableTransmit(10);  // Transmitter is connected to Arduino Pin #10
  mySwitch.setPulseLength(351);
  mySwitch.setRepeatTransmit(15);

//for debugging:
      mySwitch.send(4294901821, 32);
      delay(1000); 
      Serial.println("Funk1 on");
 
      mySwitch.send(4294901819, 32); 
      delay(1000); 
      Serial.println("Funk1 off");

  pinMode(led, OUTPUT);
  digitalWrite(led, HIGH);

  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
   
  preMillisDht = millis();
    preMillisHpPump = millis(); 

}


void loop() {
  // Create a client connection

  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {   
      if (client.available()) {
        char c = client.read();
     
        //read char by char HTTP request
        if (readString.length() < 100) {
          //store characters to string
          readString = readString + c;
          //Serial.print(c);
         }
         
         //if HTTP request has ended
         if (c == '\n') {          
          // Serial.println(readString); //print to serial monitor for debuging
     
           if (readString.indexOf("button1on") > 0){
               digitalWrite(led, LOW);
               Serial.println("Fan on");
           }
           if (readString.indexOf("button1off") > 0){
               digitalWrite(led, HIGH);
               Serial.println("Fan off");
           }
           
           if (readString.indexOf("funk1on") > 0){
               mySwitch.send(4294901821, 32);/* on signal */
               Serial.println("Funk1 on");           
                } 
           if (readString.indexOf("funk1off") > 0){
               mySwitch.send(4294901819, 32); /* off signal */
               Serial.println("Funk1 off"); 
           }

           
            //clearing string for next read
            readString="";  

           postMillisDht = millis();
           if (postMillisDht - preMillisDht > 10000){
           hum = dht.readHumidity();
           temp = dht.readTemperature();
           Serial.print("Temp:");
           Serial.println(temp);
           Serial.print("Hum:");
           Serial.println(hum);
           preMillisDht = millis();
           }
           
           client.println("HTTP/1.1 200 OK"); //send new page
           client.println("Content-Type: text/html");
           client.println();     
           client.println("<HTML>");
           client.println("<HEAD>");
           client.println("<TITLE>Aeroponik-Steuerung</TITLE>");
           client.println("<STYLE>");
           client.println(".button {");
           client.println("background-color: #4CAF50;");
           client.println("border: none;");
           client.println("color: white;");
           client.println("padding: 60px 128px;");
           client.println("text-align: center;");
           client.println("text-decoration: none;");
           client.println("display: inline-block;");
           client.println("font-size: 60px;");
           client.println("margin: 4px 2px;");
           client.println("cursor: pointer;");
           client.println("}");
           client.println("table {");
           client.println("margin: auto;");
           client.println("widh: 100%;");
           client.println("border-collapse: collapse");
           client.println("border: 5px solid black");
           client.println("</STYLE>");
           client.println("</HEAD>");
           client.println("<BODY>");
           client.println("<H1>Pumpe, Lüftung, Druckpumpe</H1>");
           client.println("<hr />");
           client.println("
");  
           client.println("<H2>Arduino with Ethernet Shield</H2>");
           client.println("
");  
           client.println("<a href=\"button1on\" class=\"button\">Turn On Fan</a>");
           client.println("<a href=\"button1off\" class=\"button\">Turn Off Fan</a>");  
           client.println("
");     
           client.println("
"); 
           client.println("<a href=\"funk1on\" class=\"button\">Turn On Funk1</a>");
           client.println("<a href=\"funk1off\" class=\"button\">Turn Off Funk1</a>");
           client.println("
"); 
           client.println("<p>Webserver control</p>");  
           client.println("
"); 
           client.println("
"); 
           client.println("
");
           client.println("<table>");
           client.println("<tr>");
           client.println("<th><big><big><big>Temp</big></big></big></th>");
           client.println("<th><big><big><big>Hum</big></big></big></th>");
           client.println("</tr>");
           client.println("<tr>");
           client.print("<td><big><big>");
           client.print(temp);
           client.print("C");
           client.println("</big></big></td>");
           client.print("<td><big><big>");
           client.print(hum);
           client.print("%");
           client.println("</big></big></td>");
           client.println("</tr>");
           client.println("</table>");
           client.println("</BODY>");
           client.println("</HTML>");
     
           delay(1);
           //stopping client
           client.stop();
           //controls the Arduino if you press the buttons
         }
           
         }
       }
    }

 cOn();
 cOff();
}

void cOn(){
   postMillisHpPump = millis();
    if (postMillisHpPump - preMillisHpPump > 10000 && state == 0) {
    mySwitch.send(4294901821, 32); /* on signal */
    delay(1000);
    Serial.println("Funk1 on");
    MillisHpPumpAn = millis();
    state = 1;}
}
void cOff(){
  MillisHpPumpAus = millis();
    if (MillisHpPumpAus - MillisHpPumpAn > 2000 && state == 1){
    mySwitch.send(4294901819, 32); /* off signal */
    delay(1000);
    Serial.println("Funk1 off");
    preMillisHpPump = millis();
    state = 0;}
}

Did you try using Serial.print() in your code to track where it is going wrong?
Paul

I don’t see anything that leaps out from your code except for the cryptic variable names and copious comments. . You may be the only person on the planet who knows what cOn() does.

While you aren’t hurting for RAM, putting frequently repeated code into a function is a good practice:

void funk1On(){
  mySwitch.send(4294901821, 32);
  delay(1000);
  Serial.println("Funk1 on");
}

If you put your on and off code into two functions, you eliminate the scope as a problem, though I don’t see it.

How do you know the transmit doesn’t send anything? I assume that the Serial.println() statements execute where you think the transmit should occur else you would be looking for a logic error. How do you know that the receiver isn’t the problem?

Paul_KD7HB:
Did you try using Serial.print() in your code to track where it is going wrong?
Paul

There are Serial.prints at every place the transmit should be sending data. But they are identical- not differentiated as to where in the code the print is taking place.

SteveMann:
I don’t see anything that leaps out from your code except for the cryptic variable names and copious comments. . You may be the only person on the planet who knows what cOn() does.

While you aren’t hurting for RAM, putting frequently repeated code into a function is a good practice:

void funk1On(){

mySwitch.send(4294901821, 32);
 delay(1000);
 Serial.println(“Funk1 on”);
}




If you put your on and off code into two functions, you eliminate the scope as a problem, though I don't see it.

How do you know the transmit doesn't send anything? I assume that the Serial.println() statements execute where you think the transmit should occur else you would be looking for a logic error. How do you know that the receiver isn't the problem?

i placed a debug on and off in void setup() - the transmitter transmits as expected, outlet turns on.
But as soon as the code enters void loop() the outlet doesnt react anymore, although the code is exactly the same as in void setup()…

im loosing my mind :fearful:

FINALLY…

i found my stupid mistake, the transmitter was connected to pin #10, which interfers with the webserver.

I dont know why exactly, but now its connected to pin #2 and works just as expected. 8)

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.