Skip to content

Project Connectivity and Time Tracking

Real Time Clock Module

Since our project needed time tracking we needed to use to an RTC. The one we had was DS3231.

This module uses a 3V coin battery to track time even after it has been unplugged from its main power source. It also uses I2C communication which can help reduce the number of pins used for a project.

I used the RTC library available here.
The library come with some useful examples, such as code to set the time automatically based on system time, and code to set time manually.
To check the time that the RTC was tracking I used the following code:

#include <I2C_RTC.h>

static DS3231 RTC;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  RTC.setHourMode(CLOCK_H24);
}

void loop() {
  // put your main code here, to run repeatedly:
  Serial.println(" ");
  Serial.print(RTC.getDay());
  Serial.print("-");
  Serial.print(RTC.getMonth());
  Serial.print("-");
  Serial.print(RTC.getYear());

  Serial.print(" ");

  Serial.print(RTC.getHours());
  Serial.print(":");
  Serial.print(RTC.getMinutes());
  Serial.print(":");
  Serial.print(RTC.getSeconds());
  Serial.print(" ");

  delay(2000);
}

The RTC worked perfectly on an Arduino Uno, however, when we transferred it to our Arduino Mega we found that the time reading kept on malfunctioning, it may have had something to do with I2C transfer rates, but nevertheless we could not solve the issue. To use time tracking in our device we elected to use the built in RTC functionality available in Blynk.

Connecting Arduino to wifi using ESP-01S

The microcontroller we used for our final project was the Arduino Mega, this is because we needed A LOT of pins to connect all of our components. The mega does not have built in WiFi Capabilities, so to fulfil our requirements we had to to use a WiFi module to add IoT connectivity to out project.
The module we used was the ESP-01S:

This is actually a standalone microcontroller than can be programmed normally using a special dongle, however, it can also be used as a simple WiFi module, and since we are only using it as such, we can program it using an arduino.

I followed This guide to program the ESP-01s using an Arduino Uno.

Firstly, connect the ESP to the arduino as follows:

ESP-01S Arduino Uno
VCC 3.3V
EN (or Ch-Pd) 3.3V
GND GND
TX TX
RX RX

Note that only for the programming phase that the TX will be connected to TX, and RX connected to RX.

Next we will input AT commands using the serial monitor.
All this is done with arduino uno set as the board:

Additionally, the generic esp8266 board needs to be installed from the board manager:

To make sure it is installed, go here and make sure it is available (don’t click on it, we still use Arduino Uno as the selected board):

After installing these, we can run AT commands. If they don’t work make sure you have to correct wiring, if is still doesn’t work remove and plug back in the devices and maybe even use different wires.

These commands have no space. So the ones I used were:

First set serial monitor to 115200 and Both NL and CR

AT
This must return OK

AT+UART_DEF=9600,8,1,0,0
This is to permanently set baud rate of the esp to 9600

AT+CWMODE=1
configure the module in Station Mode to act as Client of your Wi-Fi network. This is important because we want to connect to a WiFi network, not act as one.

AT+CWJAP="Wifi Name","WiFi Password"
Replace the name and password with the network you want to connect to. For example:

AT+CWJAP="BSC-F2","bsc@2030"
This should connect to Bahrain Science Center’s second floor wifi.

AT+CIFSR
This is used to get the IP. For me the result is:
+CIFSR:STAIP,”192.168.100.52”
+CIFSR:STAMAC,”bc:dd:c2:b2:9b:70”

After programming the ESP, we can use it to add WiFi functionality to an Arduino by using the RX and TX pins.
The new wiring will be as follows:

ESP-01S Arduino Uno Arduino Mega (instead of Uno)
VCC 3.3V 3.3V
EN (or Ch-Pd) 3.3V 3.3V
GND GND GND
TX Pin 6 Pin 19
RX Pin 7 Pin 18

Pins 6 and 7 are used on the Uno because typically the RX and TX pins of 0 and 1 are left free (I am actually not sure why), and so to simulate the RX and TX pins a library called SoftwareSerial is used. SoftwareSerial is compatible with pins 6 and 7 on the Uno.
For the Arduino Mega there are more communication pins, so we can use HardwareSerial using Pins 18 and 19.

The next steps on the guide are used if you want to use MIT App Inventor for the data input, and Thingspeak for data visualization.
MIT App Inventor is an easy and free tool if you want simple interaction over WiFi with the Arduino, however, it is not suitable for our project because it is not a real app, you can only connect to a website you create, this means that to send commands using MIT app inventor the phone needs to be on the same network. Additionally, a code needs to be scanned every time you want to open the website. Furthermore, because it is only a website and not a real app, notifications are either really hard or not even possible.
Thingspeak is a great tool for collecting and analyzing data while displaying it in easy to understand graphs and charts. It is more suitable for projects that deal with sensor data.

To achieve maximum ease of use for the user and to be able to push notifications as needed, we will be using Blynk

Using Blynk

Blynk is considered an IoT platform used to control Arduino, Raspberry Pi, and NodeMCU using the Internet. It can be used to create apps available on the web, Android, and iOS.

It has a free version that allows 2 devices and 5 datastreams which may be sufficient for some people, however, because our project is complex we needed to subscribe to the maker subscription for $6.99/month (2.63 BHD).

To start using Blynk you first have to create a template. We are using Arduino and we want to use WiFi connectivity:

After creating a template, you can create a device. After creating a device, you will be shown some essential data that you need to copy for the code:

The device Auth token can be found and copied if you scroll horizontally on the device pane:

After copying these 3 parameters you can obtain example code to operate the WiFi module from the Blynk example code here.
You can generate example codes for different features in Blynk.
To run this code you need the Blynk library available here. Note that you may need to extract the different library folders from the zip then add them to Arduino IDE one library at a time.

After entering your network name and password, and specifying the serial communication type, the example code should just work.

For our case, we need to set up data sending and receiving between the arduino and the web.

To start using Blynk features, you need to use datastreams. Datastreams are like variables that can be assigned to values obtained from the pins on the arduino, or as virtual pins on the web that can allow for more freedom.

To interact with Blynk as a user, you will be using a dashboard on the web or an App on an android or iOS device. The web version is slightly different from the mobile version and needs to be set up separately. Both the dashboard and the app use widgets, these widgets are typically setup using datastreams and can be used to output or input data as needed, however, there are exceptions such as gifs and icons that can be used to personalize a dashboard.

Sending Data from Arduino to Internet

To send data from arduino to the web, the function Blynk.virtualWrite is used. To manage how many times data is sent, a Timer function is used. This is a separate function that is written above the setup. For our project the timer looks like this:

void myTimer() {
  // This function describes what will happen with each timer tick.
  //I am using it to send the pill count to the web app, by assigning them to virtual pins on blynk
  Blynk.virtualWrite(V1, Medicine1_Pills); //This function is used to send data to the web
  Blynk.virtualWrite(V2, Medicine2_Pills);
  Blynk.virtualWrite(V3, Medicine3_Pills);
}

Then in the setup we can define how often the timer function happens using the setInterval function. In our case, every one second is enough:

timer.setInterval(1000L, myTimer); //This means the web app updates every second

Sending Data from Internet to Arduino

To send data using the web app, the function BLYNK_WRITE is used. This is a separate function that needs to be defined for each datastream you need to send. For example, to send the data from the Numeric Input widget in Blynk to add pills to the pill count of medicine 1 in the arduino, I used this function:

BLYNK_WRITE(V4) {
  // These functions are used to receive data from the web.
  // They are called when the datastream V4 value changes.
  // They assign incoming value from pin V4 to a variable
  // according to the datastream data type
  int Add_Med_1 = param.asInt();

  Medicine1_Pills = Medicine1_Pills + Add_Med_1; //This is for adding more pills
}

Since these are separate functions you cannot call variables defined here in the void loop, so to use variables in the BLYNK_WRITE you must define them outside of any void function.
Also note that the data sent whenever the value of the thing of you want to send changes, this means that whenever the value in the Numeric Input widget changes, the new value gets sent to the arduino.

Pushing Notifications

To create notifications using Blynk, you need to create a new event from the Events & Notifications tab.
When setting up notifications make sure to enable expose to automation for it, this is very important. Also make sure to select to whom the notifications are pushed.
The settings I used to set up notifications for our device are as follows:


Last update: October 9, 2024