DASDUINO CONNECT AS A CLIENT(A.K.A. HOW TO OPEN WEB PAGE WITH DASDUINO CONNECT?)

Dasduino CONNECT with male headers (ESP8266)-Dasduino & Arduino

SHORT INTRODUCTION

Probably the first thing you’ll do with your brand new Dasduino Connect is to open a web site. You will probably check if that small board can actually connect itself on the internet via WiFi and does it really work. Then you will open a website of some sort. Or you will want to send data in URL format on a database (for example data.sparkfun.com or dweet.io). In this mini project we’ll do exactly that, we will open a website identically as we would do it in a web browser of any sort. Only difference is that in this situation Croduino Connect will imitate the browser and it will give us it’s result in a Serial Monitor. All this means that the board is a client. The second thing it can acomplish is to be a server, in other words it can offer its services to other devices.

ARDUINO CODE

We won’t go through the list of necessary things for this project, it’s because we’re working with Dasduino Connect and a computer with installed Arduino IDE and  implemented ESP8266 boards.  In other words, you are in power of programing Dasduino Connect by yourself.

Yes, let’s go! This is how our Arduino code looks like. Copy it to Arduino IDE, chose the right options for Dasduino Connect(Tools->Board->Generic ESP8266 Board, Tools->Flash size->4M(1M SPIFFS)-, Tools->Reset Method->nodemcu, Tools->Board->*VaÅ¡ Serial port za Dasduino Connect*). Press upload and watch the blue and red LED blink 🙂

#include "ESP8266WiFi.h"
 
const char* ssid     = "your-ssid";  // name of your WiFi network
const char* password = "your-password"; // password of your WiFi network
 
const char* host = "google.com"; // page that we want to open. Here you should type only the domain, without the path
 
void setup() {
  Serial.begin(115200); // starting the serial comunication
  delay(10);
   
  WiFi.begin(ssid, password); // for starters, we're connecting to the WiFi network
   
  while (WiFi.status() != WL_CONNECTED) { // as long as NOVA doesn't connect to the network
    delay(500);                           // we're writing dots on the Serial Monitor
    Serial.print(".");                    // just for our sake, so we know what's going on
  }
 
  Serial.println("Successfully connected to WiFi! Local IP address is: "); 
  Serial.println(WiFi.localIP()); // writes NOVA's local WiFi adress
  Serial.print("Now connecting to: ");
  Serial.println(host); // Page URL that we're connecting to
   
  WiFiClient client;
  if (!client.connect(host, 80)) // 80 is the port we're connecting on
                                 // HTTP port, 80 is the most used one
  { 
    Serial.println("Can not connect to that webpage"); // if connection fails
    return; // end the program
  }
   
  String url = "/";    // since we're connected to the server (page),
                       // we still need to open something from it(path)
                       // we want the google web page
   
  Serial.print("Connected to a web page. Now opening URL: ");
  Serial.println(url);
   
  // This sends a request(GET request) to the page. Browser is doing the same thing in the background
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "Connection: close\r\n\r\n");
  delay(10);
   
  // We're reading the response of the server, and on our request we print that answer on the Serial Monitor
  while(client.available()){
    String linija = client.readStringUntil('\r'); // prints line by line
    Serial.print(linija);
  }
   
  Serial.println("Done. Closing connection. ");
}
 
void loop() { // loop remains empty since we load the page only once
}
Only thing that remains is to see the result on the Serial monitor. At the start we have a response from the server, similar in form to our GET request. After that we’re getting the HTML code of the page we’ve wanted to open. Depending of the page we’re opening of course we will get the HTML code for each of them. If we are sending informations on a server (for example data.sparkfun.com), we will get get a response to our request, which usually contains information as: is the request valid, time of receiving the request, unique record code in the database. It looks something like this:

It appears that google.com/ has a  redirect to http://www.google.hr/?gfe_rd=cr&ei=H521V4q4NKns8wfxs4rgBw, in my case. You will get a bit different gfe_rd. Bare in mind that you won’t be able to open websites that work on https, they are using SSL certificates (like for example. e-radionica.com).

THAT IS IT

Seriously. We’ve connected to a website and got the code. Or we have sent something. Our Dasduino Connect truly was a client on the internet.

WHAT TO DO WHEN IT DOESN'T WORK?

It is possible that you have typed wrong  SSID or password for your WiFi network. It will look like this (so fix it!):

If you try to connect on a web site that works over HTTPS protocol, in other words, it has SSL certificate, you won’t get a response. You need to use different code – soon more about it!

WHAT IS NEXT?

– Dasduino Connect as a server
– Send informations to a database on the internet and read it from a remote location