Arduino MKR1000 Scheduled WiFi SSL Web Client

View previous topic View next topic Go down

Arduino MKR1000 Scheduled WiFi SSL Web Client

Post  Admin on Wed Apr 13, 2016 4:51 pm

MKR1000 Scheduled WiFi SSL Web Client


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 console.

 Hardware Required  



Arduino MKR1000 Board
WiFi access to the Internet
The Circuit

No circuit is required for this tutorial.

  Software Essentials  

Includes:

 
<SPI.h>

This library allows you to communicate with SPI devices, with the Arduino or Genuino as the master 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 recent WiFi enabled boards to manage the connections to the Internet through WiFi.

 
<RTCZero.h>

This library allows an Arduino Zero or MKR1000 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.
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.

 
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.

 Code  


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. You can alsp combine this tutorial with the WiFi RTC to automatically set the time of the RTC. Please remember that RTC gets resetted every time the board is powered up.






Code:

/*
  Scheduled WiFi SSL Web Client for MKR1000

  This sketch connects to the Arduino website every minute and downloads the ASCII logo to display it on the serial monitor

  created 19 Jan 2016
  by Arturo Guadalupi <a.guadalupi@arduino.cc>

  http://arduino.cc/en/Tutorial/

  This code is in the public domain.
*/

#include <SPI.h>
#include <WiFi101.h>
#include <RTCZero.h>

char ssid[] = "officine1";      //  your network SSID (name)
char pass[] = "IoTisMagic";     // your network password
int keyIndex = 0;                 // your network key Index number (needed only for WEP)

int status = WL_IDLE_STATUS;

// Initialize the Wifi client library
WiFiSSLClient client;

// server address:
char server[] = "www.arduino.cc";

bool sendRequest = true; // used to understand if the http request must be sent

/* Create an rtc object */
RTCZero rtc;

/* Change these values to set the current initial time */
const byte seconds = 50;
const byte minutes = 00;
const byte hours = 17;

/* Change these values to set the current initial date */
const byte day = 17;
const byte month = 11;
const byte year = 15;

void setup() {
  //Initialize Serial and wait for port to open:
  Serial.begin(115200);

  connectToAP();    // connect the board to the access point
  printWifiStatus();
  httpRequest();
  listenToClient();

  rtc.begin();
  rtc.setTime(hours, minutes, seconds);
  rtc.setDate(day, month, year);

  rtc.setAlarmTime(0, 0, 0);    //in this way the request is sent every minute at 0 seconds
  rtc.enableAlarm(rtc.MATCH_SS);

  rtc.attachInterrupt(alarmMatch);
}

void loop() {
  if (sendRequest) {
    sendRequest = false;
    httpRequest();
    listenToClient();
  }
}

void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

void alarmMatch() {
  sendRequest = true;
}

void connectToAP() {
  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue:
    while (true);
  }

  // attempt to connect to Wifi network:
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);

    // wait 1 second for connection:
    delay(1000);
  }
}

// this method makes a HTTP connection to the server:
void httpRequest() {
  sendRequest = false;

  // Print request time
  Serial.println();
  Serial.print("Request sent @ ");
  print2digits(rtc.getHours());
  Serial.print(":");
  print2digits(rtc.getMinutes());
  Serial.print(":");
  print2digits(rtc.getSeconds());
  Serial.println();
  Serial.println();

  if (client.connect(server, 443)) {
    // Make a HTTP request:
    client.println("GET /asciilogo.txt HTTP/1.1");
    client.println("Host: www.arduino.cc");
    client.println("Connection: close");
    client.println();
  }
  else {
    Serial.println("connection failed");
  }
}

void listenToClient()
{
  unsigned long startTime = millis();
  bool received = false;

  while ((millis() - startTime < 5000) && !received) { //try to listen for 5 seconds
    while (client.available()) {
      received = true;
      char c = client.read();
      Serial.write(c);
    }
  }
  client.stop();
  Serial.println();
}

void print2digits(int number) {
  if (number < 10) {
    Serial.print("0");
  }
  Serial.print(number);
}

avatar
Admin
Admin

Posts : 49
Join date : 2012-08-15
Location : Mumbai,INDIA

View user profile http://embeddedforus.com

Back to top Go down

View previous topic View next topic Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum