Arduino disconnects from LAN Network?



  • Hello all,

    Im having some issues with my code. It works great, but only for a few hours. Then device disconnects/unresponsive. Hope someone can give me some advice on my code. I created this device to control my TV. Thank you in advanced and appreciated any advice.

    #include <IRremote.h>
    #include <IRremoteInt.h>
    #include <SPI.h>
    #include <UIPEthernet.h>
    #include <PubSubClient.h>
    
    IRsend irsend;
    
    int irledPin = 3;
    int ledPin = 5;
    
    // Current input
    int currentimput = 1;
    
    // mgmt number
    int currentmgmtping = 0;
    
    // Settings for "Status of Device"
    byte bytebuffer[30];
    
    
    // Update these with values suitable for your network.
    byte mac[]    = {  0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
    IPAddress ip(192, 168, 1, 201);
    IPAddress server(192, 168, 1, 200);
    
    //
    // Callback function header
    //
    void callback(char* topic, byte* payload, unsigned int length);
    
    //
    // toString function header
    //
    String toString(byte* payload, unsigned int length);
    
    EthernetClient ethClient;
    PubSubClient client(ethClient);
    
    void reconnect() {
      // Loop until we're reconnected
      while (!client.connected()) {
        Serial.print("Attempting MQTT connection...");
             // Attempt to connect
        if (client.connect("arduinoClient")) {
               Serial.println("connected");
               // Once connected, publish an announcement...
                       client.publish("/home/master/tv","Connected");
                // ... and resubscribe
                        client.subscribe("/home/master/tv");         
        } else {
                 Serial.print("failed, rc=");
                 Serial.print(client.state());
                 Serial.println(" try again in 5 seconds");
          // Wait 5 seconds before retrying
               delay(5000);
        }
      }
    }
    
    void lightled(){
                 digitalWrite(5, HIGH);
                 delay(1000);
                 digitalWrite(5, LOW);
      
    }
    
    // Setup Stuff ~~~~~~~~~~
    void setup(){
      Serial.begin(9600);
      delay(500);//Delay to let system boot
      Serial.println("Master rooms TV remote\n\n");
      delay(1000);//Wait before accessing Sensor
    
    //MQTT Connect
      client.setServer(server, 1883);
      client.setCallback(callback);
       Ethernet.begin(mac, ip);
       // Allow the hardware to sort itself out
      delay(1500);
    
      // initialize the pushbutton pin as an input:
      pinMode(irledPin, OUTPUT);
      pinMode(ledPin, OUTPUT);
    
    
    } //END Setup
    
    
    //Loop Stuff ~~~~~~~
    void loop() {
      
      if (!client.connected()) {
                 reconnect();
        } 
      
       client.loop();
    
    } //END Loop
    
    //
    // MQTT Callback function
    //
    
    void callback(char* topic, byte* payload, unsigned int length) {
              
              if (strcmp(topic, "/home/master/tv") == 0) {
                  String msg = toString(payload, length);
                  
                  //
                  // Send TV IR code
                  //
                  Serial.println("Received Message MSG sending Code");
                     if (msg=="POWER"){
                          irsend.sendNEC(0x20DF10EF, 32); //  TV power code
                  
                          lightled();   
                     } else{
                        // Do nothing
                        
                     }
          }
    }
    
    //
    // toString function
    //
    String toString(byte* payload, unsigned int length) {
      int i = 0;
      char buff[length + 1];
      for (i = 0; i < length; i++) {
        buff[i] = payload[i];
      }
      buff[i] = '\0';
      String msg = String(buff);
      return msg;
    }
    

  • Mod

    Hi Carl. Welcome to the MySensors forum. This forum is primarily about the MySensors library, so you might have better luck with generic Arduino questions at the Arduino forum. But we try to help anyway 🙂

    What lan module are you using?
    How are you powering the Arduino?
    What are the last few lines of serial output?



  • Hi mfalkvidd,

    Thank you for the quick reply, appreciated the help.

    Arduino Uno3 with a generic ethernet shield on it, which i picked up at local computer store. I have the Arduino powered via a 5V 2A (2000mA) switching power supply which connects directly to wall outlet.

    I dont have the serial connected. As you know, i wanted the device to power via wall outlet. So I dont need to connect the USB Serial. But you are right, this would be a good step to troubleshoot issues. However i didnt want my computer plugged in for long time, as i have other projects im working on 🙂

    But i can connect Serial and watch output. The only issues it i think it takes few hours before Arduino becomes unresponsive.


  • Mod

    You're welcome 🙂

    That power supply should be sufficient. You're not doing anything that requires much power anyway.

    Yes, I think getting some debug output is the best way forward.
    You might want to add more Serial.prinln to better determine where it stops working. You could also add some Serial.println(millis()) to get information on when things happen, so you don't need to stare at the computer screen for hours.



  • I have a one here Same shield etc that shuts down completely when i plug a DC jack into it.
    But it works perfect when plugged into a 5v USB charger kind of supply.
    Seems to me some of the copy UNO boards are not that good.


  • Mod

    @rmtucker I also blew the 5V converter on a cheap Chinese UNO clone, by just connecting a power supply. Still wondering why....
    The AMS1117 often found on these boards only allow 15V input max. I blew another one by connecting a cheap AC adapter which outputs a higher voltage unloaded.


Log in to reply
 

Suggested Topics

  • 1
  • 5
  • 2
  • 10
  • 1
  • 1

0
Online

11.2k
Users

11.1k
Topics

112.5k
Posts