Scheduled WiFi SSL Web Client

Print an Arduino ASCII logo by using RTC.

Introduction

With this tutorial you will use the Real Time Clock (RTC) alarm function and interrupt to make an https GET request to the Arduino.cc website every minute. The request downloads the Arduino ASCII logo and the data is streamed to the Serial Monitor.

Goals

  • To use the Real Time Clock (RTC)
  • To make a GET request that downloads an Arduino ASCII logo and print it to the Serial Monitor.

Hardware & Software Needed

The Circuit

No circuit is required for this tutorial. Simply connect your Arduino board to your computer and upload the sketch found in this tutorial to you IDE.

Real Time Clock (RTC)

Isn't Real Time Clock just another word for actual time? The answer is yes, it is actually just the tracking of actual time. But what is interesting is the component that does this. Most electronic devices that need to track current time use an RTC component, often in the form of an Integrated Circuit (IC). They typically consist of a crystal oscillator, which is used to create electronic signals with a constant frequency. The frequency is typically set to 32.768 kHz, which is the same frequency used for most watches.

The frequency is equal to 2^15 cycles per second, which means it is a convenient rate to use for binary counter circuits. This operation also does not require a lot of power, and can still run while the board is in a sleep mode. This can be a quite powerful feature, to for example tell the board to wake up at a certain time, or go to sleep at a certain time, automatically.

Programming the Board

1. First, let's make sure we have correct the drivers installed. If we are using the Web Editor, we do not need to install anything. If we are using an offline editor, we need to install it manually. This can be done by navigating to Tools > Board > Board Manager.... Here we need to look for the Arduino SAMD boards (32-bits ARM Cortex-M0+) and install it.

2. Now, we need to install the libraries needed. Simply go to Tools > Manage libraries... and search for the libraries listed below and install them.

Code

Before we begin, let's take a look at the libraries used and some of the core functions of the program:

The Libraries

  • <SPI.h>
    - This library allows you to communicate with SPI devices, with the Arduino as the controller device. In this tutorial the WiFi101 library uses it to control the WiFi radio. It is included for compatibillity with IDE versions before 1.6.5.
  • <WiFi101.h>
    - This is the library shared across the old WiFi enabled boards to manage the connections to the Internet through WiFi.

or

  • <WiFiNINA.h>
    - This is the library shared across the new WiFi enabled boards to manage the connections to the Internet through WiFi. Please change
    <WiFi101.h>
    into
    <WiFiNINA.h>
    if you are using an Arduino MKR WiFi 1010 or MKR VIDOR 4000.
  • <RTCZero.h>
    - This library allows an Arduino Zero, MKR1000 or MKR WiFi 1010 board to control and use the internal Real Time Clock. When an alarm is set, the board can be put in sleep mode, with very low power consumption, waiting to be woken up by the alarm. The RTC supports just one active alarm at a time.

Functions Defined in the Sketch

  • printWifiStatus()
    - Prints to Serial console a full set of information including the SSID of the network you're connected to, the local IP address and the signal strength.
  • alarmMatch()
    - This is the function that is triggered by the interrupt set in the setup part. It is activated at every 00 seconds occurrence.
  • connectToAP()
    - It uses the WiFi library functions to connect to the Access Point (AP) with Access point ID (SSID) and password embedded in the code. It also works with a WiFi shield.
  • httpRequest()
    - This function prints the time on Serial console, then it sends out an HTTP GET request to Arduino.cc website.
  • listenToClient()
    -Using millis() this function waits for 5 seconds the response from the Arduino.cc website and it prints out on Serial console any character received, then the client is closed.
  • print2digits(int number)
    -An handy function that puts a leading zero to any single digit number to be printed on Serial console, offering a better formatting on screen.

Note: Arduino.cc uses SHA 256 certificate. Be sure to connect to sites that use SHA 256, because SHA 348 is still not yet supported. Comodo for example has certificate SHA-348 with RSA encryption.

Upload the sketch below to your board:

1/*
2
3 Scheduled WiFi SSL Web Client for MKR1000
4
5 This sketch connects to the Arduino website every minute and downloads the ASCII logo to display it on the serial monitor
6
7 created 19 Jan 2016
8
9 by Arturo Guadalupi <a.guadalupi@arduino.cc>
10
11 https://docs.arduino.cc/tutorials/generic/scheduled-wifi-ssl-web-client
12
13 This code is in the public domain.
14
15*/
16
17#include <SPI.h>
18#include <WiFi101.h>
19#include <RTCZero.h>
20
21char ssid[] = "yourPassword"; // your network SSID (name)
22char pass[] = "yourNetwork"; // your network password
23int keyIndex = 0; // your network key Index number (needed only for WEP)
24
25int status = WL_IDLE_STATUS;
26
27// Initialize the Wifi client library
28
29WiFiSSLClient client;
30
31// server address:
32char server[] = "www.arduino.tips";
33
34bool sendRequest = true; // used to understand if the http request must be sent
35
36/* Create an rtc object */
37
38RTCZero rtc;
39
40/* Change these values to set the current initial time */
41
42const byte seconds = 50;
43
44const byte minutes = 00;
45
46const byte hours = 17;
47
48/* Change these values to set the current initial date */
49
50const byte day = 17;
51
52const byte month = 11;
53
54const byte year = 15;
55
56void setup() {
57
58 //Initialize Serial and wait for port to open:
59
60 Serial.begin(115200);
61
62 connectToAP(); // connect the board to the access point
63
64 printWifiStatus();
65
66 httpRequest();
67
68 listenToClient();
69
70 rtc.begin();
71
72 rtc.setTime(hours, minutes, seconds);
73
74 rtc.setDate(day, month, year);
75
76 rtc.setAlarmTime(0, 0, 0); //in this way the request is sent every minute at 0 seconds
77
78 rtc.enableAlarm(rtc.MATCH_SS);
79
80 rtc.attachInterrupt(alarmMatch);
81}
82
83void loop() {
84
85 if (sendRequest) {
86
87 sendRequest = false;
88
89 httpRequest();
90
91 listenToClient();
92
93 }
94}
95
96void printWifiStatus() {
97
98 // print the SSID of the network you're attached to:
99
100 Serial.print("SSID: ");
101
102 Serial.println(WiFi.SSID());
103
104 // print your WiFi shield's IP address:
105
106 IPAddress ip = WiFi.localIP();
107
108 Serial.print("IP Address: ");
109
110 Serial.println(ip);
111
112 // print the received signal strength:
113
114 long rssi = WiFi.RSSI();
115
116 Serial.print("signal strength (RSSI):");
117
118 Serial.print(rssi);
119
120 Serial.println(" dBm");
121}
122
123void alarmMatch() {
124
125 sendRequest = true;
126}
127
128void connectToAP() {
129
130 // check for the presence of the shield:
131
132 if (WiFi.status() == WL_NO_SHIELD) {
133
134 Serial.println("WiFi shield not present");
135
136 // don't continue:
137
138 while (true);
139
140 }
141
142 // attempt to connect to Wifi network:
143
144 while ( status != WL_CONNECTED) {
145
146 Serial.print("Attempting to connect to SSID: ");
147
148 Serial.println(ssid);
149
150 // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
151
152 status = WiFi.begin(ssid, pass);
153
154
155 // wait 1 second for connection:
156
157 delay(1000);
158
159 }
160}
161
162// this method makes a HTTP connection to the server:
163void httpRequest() {
164
165 sendRequest = false;
166
167 // Print request time
168
169 Serial.println();
170
171 Serial.print("Request sent @ ");
172
173 print2digits(rtc.getHours());
174
175 Serial.print(":");
176
177 print2digits(rtc.getMinutes());
178
179 Serial.print(":");
180
181 print2digits(rtc.getSeconds());
182
183 Serial.println();
184
185 Serial.println();
186
187 if (client.connect(server, 443)) {
188
189 // Make a HTTP request:
190
191 client.println("GET /asciilogo.txt HTTP/1.1");
192
193 client.println("Host: www.arduino.tips");
194
195 client.println("Connection: close");
196
197 client.println();
198
199 }
200
201 else {
202
203 Serial.println("connection failed");
204
205 }
206}
207
208void listenToClient()
209{
210
211 unsigned long startTime = millis();
212
213 bool received = false;
214
215 while ((millis() - startTime < 5000) && !received) { //try to listen for 5 seconds
216
217 while (client.available()) {
218
219 received = true;
220
221 char c = client.read();
222
223 Serial.write(c);
224
225 }
226
227 }
228
229 client.stop();
230
231 Serial.println();
232}
233
234void print2digits(int number) {
235
236 if (number < 10) {
237
238 Serial.print("0");
239
240 }
241
242 Serial.print(number);
243}

Testing It Out

We want to get an alarm every minute with the RTC functions. We use

rtc.setAlarmSeconds( 0);
and then
rtc.enableAlarm(rtc.MATCH_SS);
. The first functions sets at "00" the value of seconds to compare; the second function enables the alarm and sets its trigger on the match of the actual time and the set value for seconds. The condition is met every time the seconds digits go to "00", that is every minute. When the condition is met, the RTC generates an interrupt request.

The request is serviced by the function

alarmMatch
that sets to true the flag
sendRequest
used by the main loop. There, If this flag is true, the request to the Arduino.cc site is made, otherwise nothing is done. This solution allows you to do something else between the interrupts because the code execution flows without being blocked by any big
delay()
function. Please remember that RTC resets every time the board is powered up. After having uploaded the code, you should see an ASCII Arduino logo being printed on the Serial Monitor.

Arduino ASCII logo
Arduino ASCII logo

Troubleshoot

If the code is not working, there are some common issues we can troubleshoot:

  • You have not installed the correct drivers for the board.
  • You have not installed the correct libraries.
  • Your
    ssid
    or
    pass
    is incorrect.
  • You are using an incorrect configuration for your network type.

Conclusion

In this tutorial we have used RTC and interrupt to make a GET request to the arduino.cc website. This request downloads an Arduino ASCII logo which is printed to the Serial Monitor in the IDE.

Suggest changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.

License

The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.