0

Ethernet Shield reconnect and led issue
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Ethernet Shield reconnect and led issue

by stoneyguitar on Wed Dec 19, 2018 5:09 pm

First let me say, I am new to MQTT. I have an ethernet shield(W5200) by seeed attached to an arduino uno. I have a solid state relay that I have setup to turn a led light strip on in my man cave. I have an account on io.adafruit.com and have setup a toggle switch on my dashboard to turn the led on/off. Although it works, I do have a couple of issues. I have tried to figure this out myself, but am really new to using MTQQ. and haven't had any success. With the Ethernet shield, I have to manually reset it, which I know how to fix by adding a 0.1uf (100nF) capacitor between the pins on the reset switch, but am wondering if this is causing the issues that I am having. The first issue is the toggle button does not always turn the led light on/off. The serial monitor shows that the packet was dropped. I have an LCD attached and that doesn't change either when I clicked the toggle button, nor will the led will come on. Sometimes it works, sometimes it doesn't. The second issue that I have, which I just fixed, but am going to put my fix in case anyone else has this issue. Once the arduino disconnects, it doesn't want to reconnect. I added some code, so I may have fixed it.
Here is the code to my led. If someone could help me with why the led sometimes doesn't work, I would greatly aprreciate it.
"I think the led doesn't change sometimes, because I think the arduino keeps getting disconnected., but not sure how to fix"
Code: Select all | TOGGLE FULL SIZE
#include <SPI.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"

#include <Ethernet.h>
#include <EthernetClient.h>
#include <Dns.h>
#include <Dhcp.h>
#include <LiquidCrystal.h>

/************************* Ethernet Client Setup *****************************/
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};

//Uncomment the following, and set to a valid ip if you don't have dhcp available.
IPAddress iotIP (192, 168, x, xx);
//Uncomment the following, and set to your preference if you don't have automatic dns.
IPAddress dnsIP (192, 168, x, x);
//If you uncommented either of the above lines, make sure to change "Ethernet.begin(mac)" to "Ethernet.begin(mac, iotIP)" or "Ethernet.begin(mac, iotIP, dnsIP)"

const int rs = 8, en = 9, d4 = 5, d5 = 7, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

/************************* Adafruit.io Setup *********************************/

#define AIO_SERVER      "io.adafruit.com"
#define AIO_SERVERPORT  1883
#define AIO_USERNAME    "username"
#define AIO_KEY         "abcdefghijklmonopqrstuvwxyz1234567890"


//Set up the ethernet client
EthernetClient client;

Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);

#define halt(s) { Serial.println(F( s )); while(1);  }


/****************************** Feeds ***************************************/
// Setup a feed called 'onoff' for subscribing to changes.
Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/onoff");

/*************************** Sketch Code ************************************/

#define LEDPIN 6

void setup() {
  lcd.begin(16, 2);
  lcd.print(F("Adafruit MQTT demo"));

  // Initialise the Client
  lcd.setCursor(0, 1);
  lcd.print(F("\nInit the Client..."));
  Ethernet.begin(mac, iotIP, dnsIP);
  delay(1000); //give the ethernet a second to initialize
 
  pinMode(LEDPIN, OUTPUT);
  mqtt.subscribe(&onoffbutton);
}

uint32_t x=0;

void loop() {
  // Ensure the connection to the MQTT server is alive (this will make the first
  // connection and automatically reconnect when disconnected).  See the MQTT_connect
  // function definition further below.
  MQTT_connect();

  // this is our 'wait for incoming subscription packets' busy subloop
  Adafruit_MQTT_Subscribe *subscription;
  while ((subscription = mqtt.readSubscription(2000))) {
    if (subscription == &onoffbutton) {
      //Serial.print(F("Got: "));
      //Serial.println((char *)onoffbutton.lastread);

      if (0 == strcmp((char *)onoffbutton.lastread, "OFF")) {
        digitalWrite(LEDPIN, LOW);
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("Led Off!");
      }
      if (0 == strcmp((char *)onoffbutton.lastread, "ON")) {
        digitalWrite(LEDPIN, HIGH);
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("Led On!");
      }
    }
  }
  // ping the server to keep the mqtt connection alive
  if(! mqtt.ping()) {
    mqtt.disconnect();
  }

}

// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care if connecting.
void MQTT_connect() {
  int8_t ret;

  //attempt to connect to ethernet network:
 
  // Stop if already connected.
  if (mqtt.connected()) {
    return;
  }
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Connecting to MQTT");

  while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
       //Serial.println(mqtt.connectErrorString(ret));
       lcd.clear();
       lcd.setCursor(0, 0);
       lcd.print("Retrying MQTT");
       lcd.setCursor(0, 1);
       lcd.print("connection in 2");
       mqtt.disconnect();
       delay(2000);  // wait 2 seconds
       reconnect();
  }
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("MQTT Connected!");
}

void reconnect(){
  Ethernet.begin(mac, iotIP);
  //delay(1000); //give the ethernet a second to initialize
 +
 
  lcd.print(Ethernet.localIP());
  mqtt.connect();
  mqtt.subscribe(&onoffbutton);
}


This is what I added to get the arduino to reconnect
Code: Select all | TOGGLE FULL SIZE
void reconnect(){
  Ethernet.begin(mac, iotIP);
  //delay(1000); //give the ethernet a second to initialize
 +
 
  lcd.print(Ethernet.localIP());
  mqtt.connect();
  mqtt.subscribe(&onoffbutton);
}
Last edited by adafruit_support_carter on Thu Dec 20, 2018 9:30 pm, edited 2 times in total.
Reason: removed bold

stoneyguitar
 
Posts: 4
Joined: Wed Dec 19, 2018 4:42 pm

Re: Ethernet Shield reconnect and led issue

by adafruit_support_carter on Thu Dec 20, 2018 9:44 pm

It sounds like it's working but occasionally you get dropped packets. When that happens your code probably doesn't go into the part that actually updates your LCD and LED. Does it also not work when you do *not* get indication of dropped packets?

adafruit_support_carter
 
Posts: 8415
Joined: Tue Nov 29, 2016 2:45 pm

Re: Ethernet Shield reconnect and led issue

by stoneyguitar on Thu Dec 20, 2018 10:30 pm

When I first turn on the device. It doesn't work until about the 6th click, which I only click about every 2 seconds. It appears to work when there are no dropped packets. It seems to be worse when I first turn it on and after I let it sit a while say 30 minutes.

stoneyguitar
 
Posts: 4
Joined: Wed Dec 19, 2018 4:42 pm

Re: Ethernet Shield reconnect and led issue

by adafruit_support_carter on Thu Dec 20, 2018 10:33 pm

It could be related to your network. The initial part being related to getting on the network in the first place. The issues after that being related to intermittent loss of network connectivity. That generally shouldn't happen with a wired connection though.

Can you post a photo of your setup so we can see if it might be something hardware related.

adafruit_support_carter
 
Posts: 8415
Joined: Tue Nov 29, 2016 2:45 pm

Re: Ethernet Shield reconnect and led issue

by stoneyguitar on Thu Dec 20, 2018 11:46 pm

What you say makes sense. It would be hard to show my setup, but now that you say that, it makes total sense. I can explain. My modem is on the second floor of my house. My man cave is on the first. The house is old and doesn't have RJ45 connectors so to take care of is that I have a repeater that connects to a switch for my internet needs. I may have to do something else

stoneyguitar
 
Posts: 4
Joined: Wed Dec 19, 2018 4:42 pm

Re: Ethernet Shield reconnect and led issue

by stoneyguitar on Fri Dec 21, 2018 12:17 am

If I do a continual ping from my laptop to the Arduino that should tell me if a something is wrong.

I am getting all replies back but the time goes from 6ms to 77ms and back down

I watched it for 10 minutes and it dropped response using ping -t.
Sorry for all the updates. So I just noticed that ping -t does a few request timed out at the same time I was trying to Click the button on m dashboard to turn the LED on. So thank you for the guided info. I didn't even think about that.

stoneyguitar
 
Posts: 4
Joined: Wed Dec 19, 2018 4:42 pm

Please be positive and constructive with your questions and comments.