Skip to content

Commit 512dc2e

Browse files
authored
Merge pull request #104 from sellensr/master
Disconnect and ssid check
2 parents f7ecc16 + 58fd0c4 commit 512dc2e

14 files changed

+349
-149
lines changed

src/AdafruitIO.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,21 @@ void AdafruitIO::connect()
160160

161161
}
162162

163+
/**************************************************************************/
164+
/*!
165+
@brief Disconnects from WiFi.
166+
@return none
167+
*/
168+
/**************************************************************************/
169+
void AdafruitIO::wifi_disconnect()
170+
{
171+
172+
AIO_DEBUG_PRINTLN("AdafruitIO::wifi_disconnect()");
173+
174+
_disconnect();
175+
176+
}
177+
163178

164179
/**************************************************************************/
165180
/*!

src/AdafruitIO.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class AdafruitIO {
5353
virtual ~AdafruitIO();
5454

5555
void connect();
56+
void wifi_disconnect();
5657
aio_status_t run(uint16_t busywait_ms = 0, bool fail_fast = false);
5758

5859
AdafruitIO_Feed* feed(const char *name);
@@ -75,6 +76,7 @@ class AdafruitIO {
7576

7677
protected:
7778
virtual void _connect() = 0;
79+
virtual void _disconnect() = 0;
7880
aio_status_t _status = AIO_IDLE;
7981
uint32_t _last_ping = 0;
8082
uint32_t _last_mqtt_connect = 0;

src/AdafruitIO_Definitions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ class AdafruitIOGroupCallback {
7676
#define AIO_MQTT_CONNECTION_TIMEOUT 60000
7777
// Time to wait for a successful reconnection after network disconnect
7878
#define AIO_NET_CONNECTION_TIMEOUT 60000
79+
// Time to wait for a net disconnect to take effect
80+
#define AIO_NET_DISCONNECT_WAIT 300
7981

8082
#define AIO_ERROR_TOPIC "/errors"
8183
#define AIO_THROTTLE_TOPIC "/throttle"
@@ -105,6 +107,7 @@ typedef enum {
105107
AIO_CONNECT_FAILED = 11, // Failed to connect to Adafruit IO
106108
AIO_FINGERPRINT_INVALID = 12, // Unknown AIO_SSL_FINGERPRINT
107109
AIO_AUTH_FAILED = 13, // Invalid Adafruit IO login credentials provided.
110+
AIO_SSID_INVALID = 14, // SSID is "" or otherwise invalid, connection not attempted
108111

109112
// SUCCESS
110113
AIO_NET_CONNECTED = 20, // Connected to Adafruit IO

src/wifi/AdafruitIO_AIRLIFT.h

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -158,24 +158,42 @@ class AdafruitIO_AIRLIFT : public AdafruitIO {
158158
/**************************************************************************/
159159
void _connect()
160160
{
161-
// setup ESP32 pins
162-
if (_ssPin != -1) {
163-
WiFi.setPins(_ssPin, _ackPin, _rstPin, _gpio0Pin, _wifi);
161+
if(strlen(_ssid) == 0) {
162+
_status = AIO_SSID_INVALID;
163+
} else {
164+
_disconnect();
165+
// setup ESP32 pins
166+
if (_ssPin != -1) {
167+
WiFi.setPins(_ssPin, _ackPin, _rstPin, _gpio0Pin, _wifi);
168+
}
169+
170+
// check esp32 module version against NINAFWVER
171+
firmwareCheck();
172+
173+
// check for esp32 module
174+
if (WiFi.status() == WL_NO_MODULE)
175+
{
176+
AIO_DEBUG_PRINTLN("No ESP32 module detected!");
177+
return;
178+
}
179+
180+
WiFi.begin(_ssid, _pass);
181+
_status = AIO_NET_DISCONNECTED;
164182
}
165-
166-
// check esp32 module version against NINAFWVER
167-
firmwareCheck();
168-
169-
// check for esp32 module
170-
if (WiFi.status() == WL_NO_MODULE)
171-
{
172-
AIO_DEBUG_PRINTLN("No ESP32 module detected!");
173-
return;
174-
}
175-
176-
WiFi.begin(_ssid, _pass);
177-
_status = AIO_NET_DISCONNECTED;
178183
}
184+
185+
/**************************************************************************/
186+
/*!
187+
@brief Disconnect the wifi network.
188+
@return none
189+
*/
190+
/**************************************************************************/
191+
void _disconnect()
192+
{
193+
WiFi.disconnect();
194+
delay(AIO_NET_DISCONNECT_WAIT);
195+
}
196+
179197
};
180198

181199
#endif // ADAFRUITIO_AIRLIFT_H

src/wifi/AdafruitIO_ESP32.cpp

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
//
2-
// Adafruit invests time and resources providing this open source code.
3-
// Please support Adafruit and open source hardware by purchasing
4-
// products from Adafruit!
5-
//
6-
// Copyright (c) 2015-2016 Adafruit Industries
7-
// Authors: Tony DiCola, Todd Treece
8-
// Licensed under the MIT license.
9-
//
10-
// All text above must be included in any redistribution.
11-
//
1+
/*!
2+
* @file AdafruitIO_ESP32.cpp
3+
*
4+
* Adafruit invests time and resources providing this open source code.
5+
* Please support Adafruit and open source hardware by purchasing
6+
* products from Adafruit!
7+
*
8+
* Copyright (c) 2015-2016 Adafruit Industries
9+
* Authors: Tony DiCola, Todd Treece
10+
* Licensed under the MIT license.
11+
*
12+
* All text above must be included in any redistribution.
13+
*/
1214
#ifdef ARDUINO_ARCH_ESP32
1315

1416
#include "AdafruitIO_ESP32.h"
@@ -32,12 +34,28 @@ AdafruitIO_ESP32::~AdafruitIO_ESP32()
3234

3335
void AdafruitIO_ESP32::_connect()
3436
{
37+
if(strlen(_ssid) == 0) {
38+
_status = AIO_SSID_INVALID;
39+
} else {
40+
_disconnect();
41+
delay(100);
42+
WiFi.begin(_ssid, _pass);
43+
delay(100);
44+
_status = AIO_NET_DISCONNECTED;
45+
}
3546

36-
delay(100);
37-
WiFi.begin(_ssid, _pass);
38-
delay(100);
39-
_status = AIO_NET_DISCONNECTED;
47+
}
4048

49+
/**************************************************************************/
50+
/*!
51+
@brief Disconnect the wifi network.
52+
@return none
53+
*/
54+
/**************************************************************************/
55+
void AdafruitIO_ESP32::_disconnect()
56+
{
57+
WiFi.disconnect();
58+
delay(AIO_NET_DISCONNECT_WAIT);
4159
}
4260

4361
aio_status_t AdafruitIO_ESP32::networkStatus()

src/wifi/AdafruitIO_ESP32.h

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
//
2-
// Adafruit invests time and resources providing this open source code.
3-
// Please support Adafruit and open source hardware by purchasing
4-
// products from Adafruit!
5-
//
6-
// Copyright (c) 2015-2016 Adafruit Industries
7-
// Authors: Tony DiCola, Todd Treece
8-
// Licensed under the MIT license.
9-
//
10-
// All text above must be included in any redistribution.
11-
//
1+
/*!
2+
* @file AdafruitIO_ESP32.h
3+
*
4+
* Adafruit invests time and resources providing this open source code.
5+
* Please support Adafruit and open source hardware by purchasing
6+
* products from Adafruit!
7+
*
8+
* Copyright (c) 2015-2016 Adafruit Industries
9+
* Authors: Tony DiCola, Todd Treece
10+
* Licensed under the MIT license.
11+
*
12+
* All text above must be included in any redistribution.
13+
*/
14+
1215
#ifndef ADAFRUITIO_ESP32_H
1316
#define ADAFRUITIO_ESP32_H
1417

@@ -32,6 +35,7 @@ class AdafruitIO_ESP32 : public AdafruitIO {
3235

3336
protected:
3437
void _connect();
38+
void _disconnect();
3539

3640
const char *_ssid;
3741
const char *_pass;

src/wifi/AdafruitIO_ESP8266.cpp

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
//
2-
// Adafruit invests time and resources providing this open source code.
3-
// Please support Adafruit and open source hardware by purchasing
4-
// products from Adafruit!
5-
//
6-
// Copyright (c) 2015-2016 Adafruit Industries
7-
// Authors: Tony DiCola, Todd Treece
8-
// Licensed under the MIT license.
9-
//
10-
// All text above must be included in any redistribution.
11-
//
1+
/*!
2+
* @file AdafruitIO_8266.cpp
3+
*
4+
* Adafruit invests time and resources providing this open source code.
5+
* Please support Adafruit and open source hardware by purchasing
6+
* products from Adafruit!
7+
*
8+
* Copyright (c) 2015-2016 Adafruit Industries
9+
* Authors: Tony DiCola, Todd Treece
10+
* Licensed under the MIT license.
11+
*
12+
* All text above must be included in any redistribution.
13+
*/
14+
1215
#ifdef ESP8266
1316

1417
#include "AdafruitIO_ESP8266.h"
@@ -33,12 +36,28 @@ AdafruitIO_ESP8266::~AdafruitIO_ESP8266()
3336

3437
void AdafruitIO_ESP8266::_connect()
3538
{
39+
if(strlen(_ssid) == 0) {
40+
_status = AIO_SSID_INVALID;
41+
} else {
42+
_disconnect();
43+
delay(100);
44+
WiFi.begin(_ssid, _pass);
45+
delay(100);
46+
_status = AIO_NET_DISCONNECTED;
47+
}
3648

37-
delay(100);
38-
WiFi.begin(_ssid, _pass);
39-
delay(100);
40-
_status = AIO_NET_DISCONNECTED;
49+
}
4150

51+
/**************************************************************************/
52+
/*!
53+
@brief Disconnect the wifi network.
54+
@return none
55+
*/
56+
/**************************************************************************/
57+
void AdafruitIO_ESP8266::_disconnect()
58+
{
59+
WiFi.disconnect();
60+
delay(AIO_NET_DISCONNECT_WAIT);
4261
}
4362

4463
aio_status_t AdafruitIO_ESP8266::networkStatus()

src/wifi/AdafruitIO_ESP8266.h

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
//
2-
// Adafruit invests time and resources providing this open source code.
3-
// Please support Adafruit and open source hardware by purchasing
4-
// products from Adafruit!
5-
//
6-
// Copyright (c) 2015-2016 Adafruit Industries
7-
// Authors: Tony DiCola, Todd Treece
8-
// Licensed under the MIT license.
9-
//
10-
// All text above must be included in any redistribution.
11-
//
1+
/*!
2+
* @file AdafruitIO_ESP8266.h
3+
*
4+
* Adafruit invests time and resources providing this open source code.
5+
* Please support Adafruit and open source hardware by purchasing
6+
* products from Adafruit!
7+
*
8+
* Copyright (c) 2015-2016 Adafruit Industries
9+
* Authors: Tony DiCola, Todd Treece
10+
* Licensed under the MIT license.
11+
*
12+
* All text above must be included in any redistribution.
13+
*/
14+
1215
#ifndef ADAFRUITIO_ESP8266_H
1316
#define ADAFRUITIO_ESP8266_H
1417

@@ -32,6 +35,7 @@ class AdafruitIO_ESP8266 : public AdafruitIO {
3235

3336
protected:
3437
void _connect();
38+
void _disconnect();
3539

3640
const char *_ssid;
3741
const char *_pass;

src/wifi/AdafruitIO_MKR1000.cpp

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
//
2-
// Adafruit invests time and resources providing this open source code.
3-
// Please support Adafruit and open source hardware by purchasing
4-
// products from Adafruit!
5-
//
6-
// Copyright (c) 2015-2016 Adafruit Industries
7-
// Authors: Tony DiCola, Todd Treece
8-
// Licensed under the MIT license.
9-
//
10-
// All text above must be included in any redistribution.
11-
//
1+
/*!
2+
* @file AdafruitIO_MKR1000.cpp
3+
*
4+
* Adafruit invests time and resources providing this open source code.
5+
* Please support Adafruit and open source hardware by purchasing
6+
* products from Adafruit!
7+
*
8+
* Copyright (c) 2015-2016 Adafruit Industries
9+
* Authors: Tony DiCola, Todd Treece
10+
* Licensed under the MIT license.
11+
*
12+
* All text above must be included in any redistribution.
13+
*/
14+
1215
#if defined(ARDUINO_SAMD_MKR1000)
1316

1417
#include "AdafruitIO_MKR1000.h"
@@ -32,14 +35,31 @@ AdafruitIO_MKR1000::~AdafruitIO_MKR1000()
3235

3336
void AdafruitIO_MKR1000::_connect()
3437
{
38+
if(strlen(_ssid) == 0) {
39+
_status = AIO_SSID_INVALID;
40+
} else {
41+
// no shield? bail
42+
if(WiFi.status() == WL_NO_SHIELD)
43+
return;
3544

36-
// no shield? bail
37-
if(WiFi.status() == WL_NO_SHIELD)
38-
return;
45+
_disconnect();
3946

40-
WiFi.begin(_ssid, _pass);
41-
_status = AIO_NET_DISCONNECTED;
47+
WiFi.begin(_ssid, _pass);
48+
_status = AIO_NET_DISCONNECTED;
49+
}
50+
51+
}
4252

53+
/**************************************************************************/
54+
/*!
55+
@brief Disconnect the wifi network.
56+
@return none
57+
*/
58+
/**************************************************************************/
59+
void AdafruitIO_MKR1000::_disconnect()
60+
{
61+
WiFi.disconnect();
62+
delay(AIO_NET_DISCONNECT_WAIT);
4363
}
4464

4565
aio_status_t AdafruitIO_MKR1000::networkStatus()

0 commit comments

Comments
 (0)