Announcement

Collapse
No announcement yet.

Arduino - SSL IPv6 Web Client

Collapse
X
Collapse

  • Arduino - SSL IPv6 Web Client

    This is an example of using Arduino Uno/Mega and PHPoC [WiFi] Shield to make an HTTPS request to a IPv6 web server and get web content in response.
    Web content is a HTML file and printed to serial monitor.


    Hardware Required


    Circuit
    • Stack PHPoC Shield or PHPoC WiFi Shield on Arduino Uno or Mega

    Note that: Arduino communicates with PHPoC [WiFi] Shield via pins 10, 11, 12 and 13 on the Uno, and pins 10, 50, 51 and 52 on the Mega. Therefore, these pins CANNOT be used for general I/O.



    Install Arduino IDE

    If you have not install Arduino IDE yet, please download and install Arduino IDE .




    Install Library
    • Run Arduino IDE.
    • Navigate to Sketch > Include Library > Manage Libraries

    • Search "Phpoc" on search bar of the Library Manager.

    • Select the PHPoC library and press the [Install] button.

    • Restart Arduino IDE for the next step.


    Setup Network Information
    This part is needed only for the first use.

    1. If Ethernet is used
    In case of using PHPoC Shield (P4S-348), you have two options to connect to network: Ethernet or WiFi.
    If using Ethernet, please follow this instruction to connect the shield to Ethernet.


    2. If WiFi is used
    WiFi is available in both P4S-347 and P4S-348. Please follow:

    Enable IPv6 on PHPoC Shield
    Go to setup page of PHPoC Shield, enable IPv6 and save.


    Note that: In order to connect to IPv6, your LAN/WLAN network MUST support IPv6.

    Source Code
    • Open "WebSSLClientIPv6" example on Arduino IDE

      Code:
      	// Arduino SSL IPv6 web client - GET request for index.html or index.php
      	//
      	// PHPoC Shield and PHPoC WiFi Shield are Internet Shields for Arduino Uno and
      	// Mega.
      	//
      	// This is an example of using Arduino Uno/Mega and PHPoC [WiFi] Shield to make
      	// an HTTPS request to a IPv6 web server and get web content in response. Web
      	// content is a HTML file and printed to serial monitor.
      	//
      	// Arduino communicates with PHPoC [WiFi] Shield via pins 10, 11, 12 and 13 on
      	// the Uno, and pins 10, 50, 51 and 52 on the Mega. Therefore, these pins CANNOT
      	// be used for general I/O.
      	//
      	// This example code was written by Sollae Systems. It is released into the
      	// public domain.
      	//
      	// Tutorial for the example is available here:
      	// https://forum.phpoc.com/articles/tutorials/1242-arduino-ssl-ipv6-web-client
      
      	#include <Phpoc.h>
      
      	// hostname of ipv6 web server:
      	char server_name[] = "example.phpoc.com";
      
      	PhpocClient client;
      
      	void setup() {
      	  Serial.begin(9600);
      	  while(!Serial)
      	  ;
      
      	  Serial.println("Sending GET request to SSL IPv6 web server");
      
      	  // initialize PHPoC [WiFi] Shield:
      	  Phpoc.begin(PF_LOG_SPI | PF_LOG_NET);
      	  //Phpoc.begin();
      
      	  // initialize IPv6:
      	  Phpoc.beginIP6();
      
      	  // connect to web server on port 443:
      	  if(client.connectSSL(server_name, 443)) {
      	    // if connected:
      	    Serial.println("Connected to server");
      	    // make a HTTP request:
      	    //client.println("GET / HTTP/1.0");
      	    client.println("GET /asciilogo.txt HTTP/1.0");
      	    //client.println("GET /remote_addr/ HTTP/1.0");
      	    client.println("Host: example.phpoc.com");
      	    client.println();
      	  }
      	}
      
      	void loop() {
      	  // if there are incoming bytes available from the server, read them and print
      	  // them:
      	  while (client.available()) {
      	    char c = client.read();
      	    Serial.write(c);
      	  }
      
      	  // if the server's disconnected, stop the client:
      	  if (!client.connected()) {
      	    // if the server's disconnected, stop the client:
      	    Serial.println();
      	    Serial.println("disconnecting from server.");
      	    client.stop();
      
      	    // do nothing forevermore:
      	    while (true)
      	      ;
      	  }
      	}
    • The line-by-line explaination of code is presented inside the code.
      For more detail of functions's reference, please refer to PHPoC Shield for Arduino Library Reference



    Test and Result

    An URL includes two parts: hostname and pathname. For example, in URL: "example.phpoc.com/index.php" , hostname is "example.phpoc.com" and pathname is the remaining part "index.php". When accessing via a web browser, we should add:
    • "http://" before URL if we want to use HTTP,
    • "https://" before URL if we want to use HTTPS.


    Now, let's see the default code and modify it.


    Default Code: Making HTTP request to https://example.phpoc.com/asciilogo.txt
    • In this example code, hostname "example.phpoc.com", and pathname is "asciilogo.txt". Therefore, URL is "example.phpoc.com/asciilogo.txt".
    • Compile the example code and upload to Arduino by clicking "Upload" button on Arduino IDE
    • Open Serial Monitor tool on Arduino IDE to see the output log:

      It is content of a text file that contains PHPoC icon.
    • Access https://example.phpoc.com/asciilogo.txt via a web browser. Make sure that you enable IPv6 in your PC or devices that you use web browser.
    • We can see that HTML in Web Browser and HTML in Serial Monitor are the same.


    Modify Code 1: Making HTTP request to https://example.phpoc.com
    • Comment line 47 and 48
    • Uncomment line 46

      In this case, hostname "example.phpoc.com", and pathname is "". Therefore, URL is "example.phpoc.com/".
    • Compile the example code and upload to Arduino.
    • See log in Serial Monitor.

      Output log is in HTML format. It is content of web page.
    • Access https://example.phpoc.com/ via a web browser and view page source (mouse right-click -> view page source)

    • We can see that HTML in Web Browser and HTML in Serial Monitor are similar. There are some differences on dynamic data (such as time...)


    Modify Code 2: Making HTTP request to REST API via https://example.phpoc.com/remote_addr
    • Comment line 46 and 47
    • Uncomment line 48

      In this case, hostname "example.phpoc.com", and pathname is "". Therefore, URL is "example.phpoc.com/".
    • Compile the example code and upload to Arduino.
    • See log in Serial Monitor.

      Output log is the return result of REST API. This REST API returns the IPv6 address of PHPoC Shield.
    • Access https://example.phpoc.com/remote_addr via a web browser
    • We can see that return result in Web Browser and in Serial Monitor are the same.

    Change URL of Web Server as you want to.


    See Also


    References
      Posting comments is disabled.

    Categories

    Collapse

    Latest Articles

    Collapse

    • Arduino - RS-485 Expansion Board
      by support
      PES-2607 is an easy-to-use RS422/RS485 Expansion Board for Arduino Uno and Mega, which allows Arduino to exchange data with serial device via RS422 or RS485.
      Especially, Arduino does NOT use UART pins to communicate with RS422/RS485 expansion board. Therefore, users can use Arduino UART pins for other purposes.
      Moreover, A single Arduino Uno/Mega can communicate with multiple RS422/RS485 expansion boards (up to 14) without using Arduino UART pins.

      Library and examples for...
      11-13-2018, 02:45 PM
    • Arduino - RS-422 Expansion Board
      by support
      PES-2607 is an easy-to-use RS422/RS485 Expansion Board for Arduino Uno and Mega, which allows Arduino to exchange data with serial device via RS422 or RS485.
      Especially, Arduino does NOT use UART pins to communicate with RS422/RS485 expansion board. Therefore, users can use Arduino UART pins for other purposes.
      Moreover, A single Arduino Uno/Mega can communicate with multiple RS422/RS485 expansion boards (up to 14) without using Arduino UART pins.

      Library and examples for...
      11-13-2018, 02:44 PM
    • Arduino - RS-232 Expansion Board
      by support
      PES-2606 is an easy-to-use RS-232 Expansion Board for Arduino Uno and Mega, which allows Arduino to exchange data with serial device via RS-232.
      Especially, Arduino does NOT use UART pins to communicate with RS-232 expansion board. Therefore, users can use Arduino UART pins for other purposes.
      Moreover, A single Arduino Uno/Mega can communicate with multiple RS-232 expansion boards (up to 14) without using Arduino UART pins.

      Library and example for the RS-232 expansion board...
      11-13-2018, 02:43 PM
    • Arduino - Stepper Motor Controller
      by support
      PES-2605 is an easy-to-use stepper motor controller for Arduino Uno and Mega, which uses micro-stepping method to precisely control stepper motor.
      Library and example for the stepper motor controller are part of of PhpocExpansion library for Arduino. The library reference is available here.

      This tutorial shows how to use the step motor controller with an example of PhpocExpansion library for Arduino.


      Hardware Required...
      11-13-2018, 02:41 PM
    • Arduino - DC Motor Controller
      by support
      PES-2604 is an easy-to-use DC motor controller for Arduino Uno and Mega.
      Library and example for the DC motor controller are part of of PhpocExpansion library for Arduino. The library reference is available here.

      This tutorial shows how to use the DC motor controller with an example of PhpocExpansion library for Arduino.


      Hardware Required...
      11-13-2018, 02:40 PM
    • Arduino - Digital Input Board
      by support
      PES-2602 is an easy-to-use 4-port Input Expansion Board for Arduino Uno and Mega, which allows Arduino to monitor state of DC electric device. In addition, it can monitor NPN, PNP and dry contact(relay).
      Library and example for the 4-port input expansion board are part of of PhpocExpansion library for Arduino. The library reference is available here.

      This tutorial shows how to use 4-port input expansion board with an example of PhpocExpansion library for Arduino.

      ...
      11-13-2018, 02:39 PM
    Working...
    X