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

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
}
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
Products used in this tutorial
No products found