Page 1 of 1

Making Arduino as client

PostPosted: Thu Apr 20, 2017 3:35 pm
by Crystal
Hi, I meet some problem in making Arduino as a client.
This is my original code:
CODE: SELECT_ALL_CODE

#include <CytronWiFiShield.h>
#include <CytronWiFiServer.h>
#include <SoftwareSerial.h>
#define WiFi wifi

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

ESP8266Server server(80);
bool status = false;
int i;
const int button[2] = {5,6};
int thisPin;
int val[2];
int out;

void setup() {
  for (int thisPin = 0; thisPin < 2; thisPin++) {
  pinMode(button[thisPin], INPUT);
}
  Serial.begin(9600);      // initialize serial communication

  // check for the presence of the shield:
  if (!WiFi.begin(2, 3)) {
    Serial.println("WiFi shield not present");
    while (true);       // don't continue
  }

  String fv = WiFi.firmwareVersion();
  Serial.println(fv);

  // attempt to connect to Wifi network:
  while (!status) {
   
    Serial.print("Attempting to connect to Network named: ");
    Serial.println(ssid);                   // print the network name (SSID);
   
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.connectAP(ssid, pass);
  }
 
  server.begin();                           // start the web server on port 80
  printWifiStatus();                        // you're connected now, so print out the status
}


void loop() {
 
  ESP8266Client client = server.available();   // listen for incoming clients

  if(!client) return;
  Serial.println("new client");
 
  if (client.connected()) //if client is present and connected
  {             
      String s = client.readStringUntil('\r');   //get the first line of request       
      // Check to see if the client request was "GET /H" or "GET /L":
      for (int i=0;i<2;i++){
            val[i]=digitalRead(button[i]);         
        }
         
      Serial.print(s);
      while(client.available())     
        Serial.write(client.read());                    // print the client request out the serial monitor

      // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
      // and a content-type so the client knows what's coming, then a blank line:
      client.println("HTTP/1.1 200 OK");
      client.println("Content-type:text/html");
      client.println();
      //client.println("<TITLE>Read Sensor</TITLE>");
//            client.println("<center>");
//            client.println("</HEAD>");
//            client.println("<BODY>");
//            client.println("<H1>Sensor Reading</H1>");
//            client.println("<hr />");
//            client.println("<br />");
      // the content of the HTTP response follows the header:
//      for (int button = 0; button < 2; button++) {
//              client.print("Station ");
//              client.print(button + 1);
//              client.print(" : ");
//              client.print(val[button]);
//              client.println("<br />");
           // }

       client.println("0000");

      // The HTTP response ends with another blank line:
      //client.println();
      //delay(10);
      // close the connection:
      client.stop();
      Serial.println("client disonnected");
  }
}

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");
  // print where to go in a browser:
  Serial.print("To see this page in action, open a browser to http://");
  Serial.println(ip);
}


It works well i would like to make my Arduino as a client so i modified my code into the code below:
CODE: SELECT_ALL_CODE

#include <CytronWiFiShield.h>
#include <CytronWiFiServer.h>
#include <CytronWiFiClient.h>
#include <SoftwareSerial.h>
#define WiFi wifi

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

ESP8266Client client(80);
bool status = false;
int i;
const int button[2] = {5,6};
int thisPin;
int val[2];
int out;

void setup() {
  for (int thisPin = 0; thisPin < 2; thisPin++) {
  pinMode(button[thisPin], INPUT);
}
  Serial.begin(9600);      // initialize serial communication

  // check for the presence of the shield:
  if (!WiFi.begin(2, 3)) {
    Serial.println("WiFi shield not present");
    while (true);       // don't continue
  }

  String fv = WiFi.firmwareVersion();
  Serial.println(fv);

  // attempt to connect to Wifi network:
  while (!status) {
   
    Serial.print("Attempting to connect to Network named: ");
    Serial.println(ssid);                   // print the network name (SSID);
   
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.connectAP(ssid, pass);
  }
 
  client.begin();                           // start the web server on port 80
  printWifiStatus();                        // you're connected now, so print out the status
}


void loop() {
 
  ESP8266Server server = client.available();   // listen for incoming clients

  if(!server) return;
  Serial.println("new server");
 
  if (server.connected()) //if client is present and connected
  {             
      String s = server.readStringUntil('\r');   //get the first line of request       
      // Check to see if the client request was "GET /H" or "GET /L":
      for (int i=0;i<2;i++){
            val[i]=digitalRead(button[i]);         
        }
         
      Serial.print(s);
      while(server.available())     
        Serial.write(server.read());                    // print the client request out the serial monitor

      // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
      // and a content-type so the client knows what's coming, then a blank line:
      server.println("HTTP/1.1 200 OK");
      server.println("Content-type:text/html");
      server.println();
      //client.println("<TITLE>Read Sensor</TITLE>");
//            client.println("<center>");
//            client.println("</HEAD>");
//            client.println("<BODY>");
//            client.println("<H1>Sensor Reading</H1>");
//            client.println("<hr />");
//            client.println("<br />");
      // the content of the HTTP response follows the header:
//      for (int button = 0; button < 2; button++) {
//              client.print("Station ");
//              client.print(button + 1);
//              client.print(" : ");
//              client.print(val[button]);
//              client.println("<br />");
           // }

       server.println("0000");

      // The HTTP response ends with another blank line:
      //client.println();
      //delay(10);
      // close the connection:
      server.stop();
      Serial.println("client disonnected");
  }
}

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");
  // print where to go in a browser:
  Serial.print("To see this page in action, open a browser to http://");
  Serial.println(ip);
}


This code cannot be compiled and i got an error message 'class ESP8266Client' has no member named 'begin'.
Can anyone please help?

Re: Making Arduino as client

PostPosted: Thu Apr 20, 2017 9:12 pm
by bengchet
Hi,

Indeed there is no function begin() for client. You can refer to example CytronWiFiDemo clientTest() to make Arduino as wifi client. Basically the function makes Arduino to connect to Adafruit server as client, and get some response.