Skip to content

Commit

Permalink
Merge pull request #1 from AlexWaclawik/testing
Browse files Browse the repository at this point in the history
Testing
  • Loading branch information
AlexWaclawik authored Feb 13, 2022
2 parents 4b0577c + 3111eba commit 3128651
Show file tree
Hide file tree
Showing 10 changed files with 143 additions and 31 deletions.
38 changes: 25 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,41 +1,52 @@
## Arduino IoT Switch
![header](media/enclosure.jpg)

This project is for a remote AC outlet switch that is made using an Arduino microcontroller. This
was achieved using the [Blynk IoT platform and libraries](https://docs.blynk.io/en/), which creates a neat mobile application
interface for interacting with the microcontroller.
was achieved using the [Blynk IoT platform and libraries](https://docs.blynk.io/en/), which creates a neat smartphone
application interface for interacting with the microcontroller.

<p align="right"></p>

### Components
![components](media/components.jpg)

1. [ELEGOO UNO R3 Board ATmega328P with USB Cable (Arduino UNO Clone)](https://www.elegoo.com/products/elegoo-uno-r3-board)
- Acts as the main board, and is fully compatible with Arduino.
- ![board](media/board-modem.jpg)
2. [Botletics™ SIM7000A LTE CAT-M1/NB-IoT + GPS Shield Kit](https://www.botletics.com/products/sim7000-shield)
- This kit acts as a modem. It also offers expanability for the future.
- ![antenna](media/antenna.jpg)
- This kit acts as a modem, and also offers future expanability.
3. [1NCE FlexSIM Card](https://1nce.com/en/sim-cards/)
- This pre-paid SIM card is offered for 10 years with a 500 MB limit, making it perfect for low-bandwith IoT devices.
- TCP was chosen over SMS, as the 1NCE SIM Card only has an SMS limit of 250 messages, which is not enough for the 10 year period.
4. [MCIGICM 2 Channel DC 5V Relay Module for Arduino UNO](https://www.amazon.com/gp/product/B072BY3KJF?psc=1)
- Opto-isolated relay for maximum safety of the outlet.
- ![relay](media/relay.jpg)
5. [Adafruit 328 Lithium Ion Battery, 3.7V, 2500mAh](https://www.adafruit.com/product/328)
- Along with the power supply, provides additonal power for activating the relay.
- ![battery](media/battery.jpg)
- Provides additional current capability during the device start-up.

<p align="right"></p>

### Operation

**Blynk Heartbeat Configuration**
- As the devices with 1NCE connectivity are placed behind a NAT gateway, the public IP traffic is routed through the NAT Gateway/Internet Breakout.
After 350 seconds of no packets being transmitted, the established connection via the Internet Breakout will be closed automatically.
To keep the connection alive within 350 seconds a device must send a keep-alive packet at least once in the 350-second timeframe.
Otherwise, the device must re-establish the connection after 350 seconds. The default heartbeat interval for GSM is 60 seconds, but you can
adjust it to suit your application. When setting the Blynk heartbeat, the following formula is applied: ***heartbeat interval = time * 2.3***.
For example, with a 300 second heartbeat you will get a notification regarding the connection status with a 12 minute delay in the worst case.

**Checking Signal Quality**
- The signal quality is measured in dBm as per IEEE RSSI specification. However the device does not return a dBm value. Instead it
gives a number from 2-30, which corresponds to a predefined level of signal strength. In the program, I have included this last
conversion before transmitting the current signal strength via Blynk. The values and their respective RSSI dBm measurements [can be found here.](https://m2msupport.net/m2msupport/atcsq-signal-quality/)

<p align="right"></p>

### Dependancies

- [TinyGSM](https://github.com/vshymanskyy/TinyGSM)
- [Blynk](https://github.com/blynkkk/blynk-library)
**This project was built using specific versions of the libraries, which are included in the release.**
**Using newer/current versions of the libraries may break functionality.**

To Install:
How To Install Libraries:
1. [Download the latest release from here](https://github.com/AlexWaclawik/Arduino-IoT-Switch/releases)
2. Unzip the contents. The necessary libraries are located in ```/include```
3. Place the dependancies into your Arduino Libraries Folder
Expand All @@ -47,7 +58,8 @@ To Install:

### Credits

1. [Project idea inspired by video from GaryExplains](https://www.youtube.com/watch?v=7swG4XVSx50)
2. [Code for manipulating relay pins taken from GaryExplains](https://github.com/garyexplains/examples/blob/master/MKR1000/mysmartswitch.ino)
- [Project idea inspired by video from GaryExplains](https://www.youtube.com/watch?v=7swG4XVSx50)
- [TinyGSM by Volodymyr Shymanskyy](https://github.com/vshymanskyy/TinyGSM)
- [Blynk](https://github.com/blynkkk/blynk-library)

<p align="right"></p>
5 changes: 5 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Version 1.1
- added functionality to record the elapsed time while relay is running
- added functionality to check the current signal quality
- added line to configure the Blynk Heartbeat Interval
- added push notifications to startup and relay operation
2 changes: 1 addition & 1 deletion include/Blynk/Blynk/BlynkConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

// Heartbeat period in seconds.
#ifndef BLYNK_HEARTBEAT
#define BLYNK_HEARTBEAT 600
#define BLYNK_HEARTBEAT 60
#endif

// Network timeout in milliseconds.
Expand Down
129 changes: 112 additions & 17 deletions main/main.ino
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
/*
* Author: Alex Waclawik
* Github: https://github.com/AlexWaclawik/Arduino-IoT-Switch
* This project is for a remote AC outlet switch that is made using an Arduino microcontroller.
*
* IMPORTANT: Adjust BLYNK_HEARTBEAT (in sec) in BlynkGsmClient.h to suit your application.
* newHeartbeatInterval * 2.3 formula will be applied. For example, for a 600 sec heartbeat
* you will get a notification regarding the connection status with 23 min delay in worst case.
* Version: 1.1
* This project is for a remote AC outlet switch that is made using
* an Arduino UNO-like microcontroller, and the Blynk IoT library.
*/

// these Blynk definitions ALWAYS have to be first
#define BLYNK_TEMPLATE_ID "TEMPLATE-ID"
#define BLYNK_DEVICE_NAME "DEVICE-NAME"
#define BLYNK_DEVICE_NAME "DEVICE NAME"
#define BLYNK_AUTH_TOKEN "YOUR-AUTH-TOKEN"
#define BLYNK_PRINT Serial

/*
* IMPORTANT: Configuration of Blynk Heartbeat Interval
* Please read the Blynk Heartbeat Configuration section of the README
* as configuring this is very important for your application
* https://github.com/AlexWaclawik/Arduino-IoT-Switch/tree/testing#operation
*/
#define BLYNK_HEARTBEAT 300

// define the SIM type
#define TINY_GSM_MODEM_SIM7000

Expand All @@ -37,10 +43,10 @@
SoftwareSerial SerialAT(TX, RX);

// configure GPRS settings (user and pass may be optional depending on your SIM card)
const char apn[] = "iot.1nce.net";
const char apn[] = "iot.1nce.net"; // currently set for 1NCE SIMs
const char user[] = "";
const char pass[] = "";
// configure Blynk authentication token (same as above)
// configure Blynk authentication token
const char auth[] = "YOUR-AUTH-TOKEN";

// initialize modem
Expand All @@ -49,15 +55,18 @@ TinyGsm modem(SerialAT);
// define global variables
bool startup = true;
float deviceUptime = 0;
bool relayStatus = false;
float relayStartTime = 0;


/*
* called whenever a widget writes to virtual pin V1.
* It will switch the relay on and off, as well as turn the status LED.
* Lastly it will call the updateDeviceTime() function which will update the uptime.
* called whenever a widget writes to virtual pin V1
* it will switch the relay on and off, turn on the status LED, and push a notification
* lastly it will call the updateDeviceTime() function which will update the uptime
*/
BLYNK_WRITE(V1) {
// retrieve value from virtual pin V1
int pinValue = param.asInt();
uint8_t pinValue = param.asInt();
if (pinValue != 0) {
// turn on relay
digitalWrite(RELAY_PIN1, LOW);
Expand All @@ -66,8 +75,13 @@ BLYNK_WRITE(V1) {
SerialMon.println("RELAY ON");
// turn on Blynk LED
Blynk.virtualWrite(V4, 255);
// push notification that the relay is ON
Blynk.logEvent("relay", "The relay is ON");
// update device uptime
updateDeviceTime();
// start relay uptime
relayStatus = true;
relayStartTime = deviceUptime;
}
else {
// turn off relay
Expand All @@ -77,11 +91,84 @@ BLYNK_WRITE(V1) {
SerialMon.println("RELAY OFF");
// turn off Blynk LED
Blynk.virtualWrite(V4, 0);
// push notification that the relay is OFF
Blynk.logEvent("relay", "The relay is OFF");
// update device uptime
updateDeviceTime();
// stop relay uptime
relayStatus = false;
}
}


/*
* called whenever a widget reads virtual pin V7
* first it will update the device uptime then use it to calulate the relay uptime
* after which it will then write it to virtual pin V6
* if relay is not on, it will instead return a value of 0
*/
BLYNK_WRITE(V7) {
uint8_t pinValue = param.asInt();
if (pinValue != 0) {
if (relayStatus) {
updateDeviceTime();
Blynk.virtualWrite(V6, deviceUptime - relayStartTime);
SerialMon.println("Relay Uptime: ");
SerialMon.print(deviceUptime - relayStartTime);
}
}
}


/*
* called whenever a widget reads virtual pin V8 and returns signal quality to virtual pin V9
* for more information: https://github.com/AlexWaclawik/Arduino-IoT-Switch/tree/testing#operation
*/
BLYNK_WRITE(V8) {
uint8_t pinValue = param.asInt();
if (pinValue != 0) {
String signalCond = "";
String signalColor = "";
uint8_t signalQuality = 0;
signalQuality = modem.getSignalQuality();
if (signalQuality == 99) {
signalCond = "UNKNOWN";
// grey color
signalColor = "#A0A0A0";
}
// RSSI less than -93 dBm
else if (signalQuality < 10) {
signalCond = "MARGINAL";
// red color
signalColor = "#FF0000";
}
// RSSI greater than -95 dBm and less than -83 dBm
else if (signalQuality > 9 && signalQuality < 15) {
signalCond = "OK";
// orange color
signalColor = "#FF7B00";
}
// RSSI greater than -85 dBm and less than -73 dBm
else if (signalQuality > 14 && signalQuality < 20) {
signalCond = "GOOD";
// light green color
signalColor = "#6EFF00";
}
// RSSI greater than -75 dBm
else if (signalQuality > 19) {
signalCond = "EXCELLENT";
// brighter darker green color
signalColor = "#04D700";
}
Blynk.virtualWrite(V9, signalCond);
Blynk.virtualWrite(V10, 255);
Blynk.setProperty(V10, "color", signalColor);
SerialMon.println("Signal Quality: ");
SerialMon.print(signalCond);
}
}


/*
* updates the device uptime in hours, and
* then pushes the uptime to virtual pin V5
Expand All @@ -91,6 +178,7 @@ void updateDeviceTime() {
Blynk.virtualWrite(V5, deviceUptime);
}


/*
* setup and initializes device on startup (or reboot) and will only run once
*/
Expand All @@ -105,7 +193,7 @@ void setup() {

// set console baud rate
SerialMon.begin(9600);
delay(10);
delay(1000);

// set GSM module baud rate
SerialAT.begin(9600);
Expand All @@ -114,7 +202,7 @@ void setup() {
// initialize modem
SerialMon.println("Initializing modem...");
modem.restart();
delay(8000);
modem.waitForNetwork(600000L);

// get modem info
String modemInfo = modem.getModemInfo();
Expand All @@ -124,21 +212,28 @@ void setup() {
// connect to Blynk
Blynk.begin(auth, modem, apn, user, pass);

// verify heartbeat is set correctly
// verify heartbeat is configured correctly
SerialMon.print("Heartbeat (sec): ");
SerialMon.println(BLYNK_HEARTBEAT);
}


/*
* main function that loops during device operation
* On startup, the device will sync the current value of the virtual pin V1 and V8
* which will call the write functions for those respective pins
* this ensures that if the relay was ON when the device loses power, it will resume on reboot
* it also makes the device check signal quality on startup
*/
void loop() {
Blynk.run();
// syncs the current value of the virtual pin V1 which will trigger a call of the BLYNK_WRITE(V1) function above
// this ensures that if the relay was ON when the device loses power, on reboot it will resume
if (startup) {
// check relay status
Blynk.syncVirtual(V1);
// checks signal quality
Blynk.syncVirtual(V8);
startup = false;
// sends push notification that device has rebooted
Blynk.logEvent("startup","The device has been rebooted");
}
}
Binary file removed media/antenna.jpg
Binary file not shown.
Binary file removed media/battery.jpg
Binary file not shown.
Binary file removed media/board-modem.jpg
Binary file not shown.
Binary file added media/components.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added media/enclosure.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed media/relay.jpg
Binary file not shown.

0 comments on commit 3128651

Please sign in to comment.