DASDUINO CONNECT AS A SERVER (A.K.A HOW TO CONNECT DEVICES TO DASDUINO CONNECT?)

CRODUINO NOVA AS A SERVER (A.K.A HOW TO CONNECT DEVICES TO CRODUINO NOVA?)-Uncategorized

INTRODUCTION

Your new Dasduino Connect arrived and it is time for you to start working? You do not know how to start and wonder what this board can do? This is the right tutorial for you!
Dasduino Connect board can work in 3 modes: as a client, as a server, or both (client and server). In the last tutorial we have described how Dasduino Connect board works as a client. Now we will explain how Dasduino Connect works as a server. By configuring Dasduino Connect board to work as a server, it allows us connecting smartphones, computers and other devices to the Dasduino Connect board. Along with that, it is possible to control the board itself using connected devices. Imagine all the posibilities which can be done, from turning the lights on/off, controlling heating or curtains using your smartphone to an automated house where it is possible to control everything using WiFi. Imagination has no limits.

 

EXAMPLE: ILLUMINATING LED DIODE USING WIFI

In the tutorial below, we will show how to configure Dasduino Connect boards to work as servers and on a simple example show how to control illuminating the LED diode using smartphone.

CONNECTING

To make the project itself, we need one LED diode and Dasduino Connect board. The connection method is shown in the picture below.

ARDUINO CODE

Before using this tutorial, it is necessary to configure Arduino program environment (Arduino IDE) to work with Dasduino Connect board in order for it to work properly (more on the link). We must also note that, in this tutorial, only the devices connected to the same (local) WiFi network can be connected to the Dasduino Connect board. This means that it is not possible to connect devices which are connected to a different WiFi network or data network to Dasduino Connect board. In continuation, we will explain new parts of the program code necessary for proper configuration and operation with WiFi network. First, we must create a “WiFiServer” object named “server” using which, we create a server on the Dasduino Connect board. The parameter within brackets is a number of the port on which it is possible to communicate to server.

WiFiServer server(80);

Then we must establish a connection to the WiFi network. This can be done with WiFi.begin class within which we enter the ssid and the WiFi network password.

WiFi.begin(ssid, password);

The program can continue working only when we have successfully connected to the defined WiFi network. We create a while loop within the Setup() function in which we check the connection status using WiFi.status() up until it returns the parameter „WL_CONNECTED“. For other parameters the WiFi.status() function can return, check this LINK.

while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

After Dasduino Connect is successfully connected to a WiFi network, the program goes into loop() loop. Within it, we create a “WiFiClient” object and check whether some client (device) is connected to the Dasduino Connect board. If not, the program returns to the beginning of the loop() function.

WiFiClient client = server.available();
if (!client) {
return;
}

When a device connects to Dasduino Connect board, the program awaits for the client to send some data. After data is sent, the program first reads and prints them on a serial port and then interprets the sent request „/LED=ON“ or „/LED=OFF“ and sets the LED diode to HIGH or LOW.

Serial.println("Novi klijent");
  while(!client.available()){
    delay(1);
  }
 
  String request = client.readStringUntil('\r');
  Serial.println(request);
  client.flush();
 
  int value = LOW;
  if (request.indexOf("/LED=ON") != -1)  {
    digitalWrite(ledPin, HIGH);
    value = HIGH;
  }
  if (request.indexOf("/LED=OFF") != -1)  {
    digitalWrite(ledPin, LOW);
    value = LOW;
  }

After that, we return a response to the client(device) according to the corresponding HTML code. For this, we use the client.print() function into which we enter an exact HTML code array in order for it to be correct. In this part it is possible to change parts of the HTML code and thus, change the web site’s appearance.

client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println("");
  client.println("");
  client.println("");
 
  client.print("LED dioda je sada ");
 
  if(value == HIGH) {
    client.print("upaljena");
  } else {
    client.print("ugasena");
  }
  client.println("
");
  client.println("<a><button>Upali LED diodu</button></a>");
  client.println("<a><button>Ugasi LED diodu</button></a>
"); 
  client.println("");
More information about the ESP8266WiFi.h library and other classes can be found on the link.
The complete code is below.
#include "ESP8266WiFi.h"
  
const char* ssid = "e-radionica"; //Enter the name of your WiFi network
const char* password = "password"; //Enter your password
  
int ledPin = 13; //The pin on which the LED diode is located
 
WiFiServer server(80);
  
void setup() {
  Serial.begin(115200); //CAUTION! change the baud rate to 115200 baud in the Serial Monitor
                        //Otherwise, the resulting characters will be interpreted incorrectly and will not be understood.
  delay(10);
  
  pinMode(ledPin, OUTPUT); //Defining LED diode's pin as an output
  digitalWrite(ledPin, LOW); //Setting LED diode's pin to LOW
  
  //Connecting to the WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Spajanje na ");
  Serial.println(ssid);
  
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {  //Printing "." sign up until it is connected to the defined WiFi network
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi spojen");
  
  //Run the server
  server.begin();
  Serial.println("Server pokrenut");
  
  //Print the IP address
  Serial.print("Kopiraj ovu adresu u preglednik: ");
  Serial.print("http://");
  Serial.print(WiFi.localIP());
  Serial.println("/");
  
}
  
void loop() {
  // Check if the client is connected
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
  
  // Wait until the client sends some data
  Serial.println("Novi klijent");
  while(!client.available()){
    delay(1);
  }
  
  // Read the first line of the request
  String request = client.readStringUntil('\r');
  Serial.println(request);
  client.flush();
  
  // Interpret the request
  
  int value = LOW;
  if (request.indexOf("/LED=ON") != -1)  {
    digitalWrite(ledPin, HIGH);
    value = HIGH;
  }
  if (request.indexOf("/LED=OFF") != -1)  {
    digitalWrite(ledPin, LOW);
    value = LOW;
  }
  
// Set the LED's status according to the request
  
  // Return a response to the client
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println("");
  client.println("");
  client.println("");
  
  client.print("LED dioda je sada ");
  
  if(value == HIGH) {
    client.print("upaljena");
  } else {
    client.print("ugasena");
  }
  client.println("
");
  client.println("<a href="/LED=ON"><button>Upali LED diodu</button></a>");
  client.println("<a href="/LED=OFF"><button>Ugasi LED diodu</button></a>
"); 
  client.println("");
  
  delay(1);
  Serial.println("Klijent je iskljucen");
  Serial.println("");
}

THE RESULT

After you have successfully connected the LED diode to Dasduino Connect board and successfully switched the code to the board, click on the Serial Monitor in the Arduino IDE program.

CAUTION, you must change the baud rate setting to 115200 baud in the Serial Monitor. Otherwise, the resulting characters will be interpreted incorrectly and will not be understood.
When starting Dasduino Connect board, it will attempt to connect to a WiFi network you have specified in the code. In the Serial Monitor, you will see if Dasduino Connect is successfully connected or not. After the board is connected to a WiFi network, it will print the IP address which you need to enter into the web browser (on you smartphone, computer, laptop, etc.). After that, a web site, the same as the one in the image below, will appear. By clicking on one of the two buttons, the text within a web browser will change, and at the same time, change the LED diode’s status. That is it, you did it!

WHAT IS NEXT?

We have reached the end of this tutorial but that is not where our productivity stops. It is possible to connect any input/output (module or sensor) to the Dasduino Connect board. It is also possible to program the Connect board in different ways to control the components connected to it. You probably already have some ideas for projects you want to make. Soldered team supports you for any “crazy” project you have imagined.