Cytron ESPWiFi Shield with Arduino

Talk about Arduino board, sheilds. Sharing Arduino projects, program, problems, solutions, suggestions..... many more, all are welcome.

Cytron ESPWiFi Shield with Arduino

Postby vincent94 » Tue Oct 11, 2016 10:37 pm

Hello, I'm currently doing a mini project involving the communication from Arduino to MySQL through WiFi connection (Cytron ESPWiFi Shield). The database is hosted on localhost (computer).

I am using the "connect_wifi" example from MySQL Connector Arduino library, it is using the WiFi.h header file instead of the Cytron ESPWiFi Shiled library. I cannot successfully connect the Wifi shield to the internet. Is it possible that the "WiFi.h" header file be used on Cytron ESPWiFi Shield? If not, can anyone help to provide the solution? Thank you very much.

*Attached is the link to the MySQL Connector Arduino library:
https://github.com/ChuckBell/MySQL_Connector_Arduino

Below shows the coding from the example: "connect_wifi" from MySQL Connector Arduino Library

CODE: SELECT_ALL_CODE
#include <WiFi.h>                  // Use this for WiFi instead of Ethernet.h
#include <MySQL_Connection.h>
#include <MySQL_Cursor.h>

byte mac_addr[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

IPAddress server_addr(10,0,1,35);  // IP of the MySQL *server* here
char user[] = "root";              // MySQL user login username
char password[] = "1234";        // MySQL user login password

// WiFi card example
char ssid[] = "BatmanBB16";    // your SSID
char pass[] = "password";       // your SSID Password

WiFiClient client;            // Use this for WiFi instead of EthernetClient
MySQL_Connection conn((Client *)&client);

void setup() {
  Serial.begin(115200);
  while (!Serial); // wait for serial port to connect. Needed for Leonardo only

  // Begin WiFi section
  int status = WiFi.begin(ssid, pass);
  if ( status != WL_CONNECTED) {
    Serial.println("Couldn't get a wifi connection");
    while(true);
  }
  // print out info about the connection:
  else {
    Serial.println("Connected to network");
    IPAddress ip = WiFi.localIP();
    Serial.print("My IP address is: ");
    Serial.println(ip);
  }
  // End WiFi section

  Serial.println("Connecting...");
  if (conn.connect(server_addr, 3306, user, password)) {
    delay(1000);
  }
  else
    Serial.println("Connection failed.");
  conn.close();
}

void loop() {
}
vincent94
Freshie
 
Posts: 6
Joined: Tue Oct 11, 2016 10:20 pm

Re: Cytron ESPWiFi Shield with Arduino

Postby bengchet » Thu Oct 13, 2016 9:48 am

Hi,

The library from Arduino WiFi is not compatible with Cytron ESPWiFi Shield.

However you can tweak the program a bit to make it work.

1. Replace
CODE: SELECT_ALL_CODE
#include <WiFi.h>

with
CODE: SELECT_ALL_CODE
#include <CytronWiFiShield.h>
#include <CytronWiFiClient.h>
#include <SoftwareSerial.h>
#define WiFi wifi


2. Replace
CODE: SELECT_ALL_CODE
int status = WiFi.begin(ssid, pass);
if ( status != WL_CONNECTED) {
    Serial.println("Couldn't get a wifi connection");
    while(true);
}

with
CODE: SELECT_ALL_CODE
if(!wifi.begin(2, 3)) // RX-D2, TX-D3
{
    Serial.println(F("Error talking to shield"));
    while(1);
}
int status = wifi.connectAP(ssid, pass);
if(!status)
{
    Serial.println(F("Couldn't get a wifi connection"));
    while(true);
}

Make sure you put mini jumpers to D2 and D3 on Cytron ESPWiFi Shield as stated in coding at no 2. Of course you want to change to other pins, you are free to do so, just need to make sure the pins specified in the program match the pins selected on shield.

3. Replace
CODE: SELECT_ALL_CODE
WiFiClient client;            // Use this for WiFi instead of EthernetClient

with
CODE: SELECT_ALL_CODE
ESP8266Client client;


4. Delete this line
CODE: SELECT_ALL_CODE
byte mac_addr[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

because it is unnecessary because it is meant for Ethernet Shield.

If you have any other problems using Cytron WiFi Shield, feel free to post here.

Thanks. ;)
bengchet
Moderator
 
Posts: 237
Joined: Tue Aug 25, 2015 8:29 am

Re: Cytron ESPWiFi Shield with Arduino

Postby vincent94 » Thu Oct 13, 2016 9:57 am

Thanks bengchet for the detailed reply. Will try your suggestion out and post the outcome here. :D
vincent94
Freshie
 
Posts: 6
Joined: Tue Oct 11, 2016 10:20 pm

Re: Cytron ESPWiFi Shield with Arduino

Postby bengchet » Thu Oct 13, 2016 11:16 am

Hi,

Forgot to mention, you will have to download the library. Open Arduino IDE, go to Sketch -> Include library -> Manage libraries. Type Cytron ESPWiFi Shield on searchbox. Install the library.
bengchet
Moderator
 
Posts: 237
Joined: Tue Aug 25, 2015 8:29 am

Re: Cytron ESPWiFi Shield with Arduino

Postby vincent94 » Fri Oct 21, 2016 12:47 am

Hi bengchet,

Sorry for the late reply. Thank you so much for the advice, manged to solve the wifi connection after using your suggestion.

However, I got another question,

Are there any tutorials on how to use the wifi.tcpconnect code?

CODE: SELECT_ALL_CODE
wifi.tcpConnect(uint8_t linkID, const char * destination, uint16_t port, uint16_t keepAlive)


I'm not clear with the "linkID" and "keepAlive" part.

Thank you in advance.
vincent94
Freshie
 
Posts: 6
Joined: Tue Oct 11, 2016 10:20 pm

Re: Cytron ESPWiFi Shield with Arduino

Postby bengchet » Fri Oct 21, 2016 5:25 pm

Hi,

1. linkID refers to which socket you use for making connections, ranges from 0 - 4.
2 .keepalive means how long you want your connection to be maintained with server before server itself stops the connection.

Normally, it is preferable to use client.connect to do the job as it will find the socket available for connection, automatically solve the issue that which number should be used in linkID.

If you wish to set keepalive value, you can use client.connect(your_server,your_port, value_for_keepalive);

Note that value_for_keepalive is in unit of 0.5s, which means that if you put the value 120, the total time for keepalive is 120 * 0.5s = 60s = 1minute

Hope it helps.
bengchet
Moderator
 
Posts: 237
Joined: Tue Aug 25, 2015 8:29 am

Re: Cytron ESPWiFi Shield with Arduino

Postby vincent94 » Fri Oct 21, 2016 6:58 pm

Hi beng chet,

Thanks for the reply again.

Based on your reply, I can conclude that its not the tcp connection that causes the problem.

I'm currently facing problem with HTTP Client. Given the example attached with the library, HTTPSClient, I modified it based on my beginner's knowledge. I manage to connect to the wifi, database server, but I couldn't perform the get method in order to write data (in this case it's the Student_ID and Student_Name) into database.
CODE: SELECT_ALL_CODE
String url = "/submitcust.php?Student_ID=1234125&Student_Name=ASDFGG";


The program stuck at "Server Timeout" and I don't know what is wrong with my code. Any advice for a newbie like me? :lol:

CODE: SELECT_ALL_CODE
/*
 *  HTTP over TLS (HTTPS) example sketch
 *
 *  This example demonstrates how to use
 *  secure_connect function to access HTTPS API.
 *  We fetch and display the status of
 *  esp8266/Arduino project continuous integration
 *  build.
 *
 *  Created by Ivan Grokhotkov, 2015.
 *  This example is in public domain.
 * 
 *  Modified by Ng Beng Chet, 2016
 */

#include <CytronWiFiShield.h>
#include <CytronWiFiClient.h>
#include <SoftwareSerial.h>

const char* ssid = "...";
const char* pass = "...";

const char* host = "...";
const int httpsPort = 80;

ESP8266Client client;

void setup() {
  Serial.begin(115200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
 
  // Begin WiFi section

  //Checking WiFi Shield
  if(!wifi.begin(2, 3)){
        Serial.println(F("Error talking to shield"));
        while(1);
    }
    else
    Serial.println(F("Start wifi connection"));


  //Connecting WiFi 
  if(!wifi.connectAP(ssid, pass))
    Serial.println(F("Error connecting to WiFi"));
  Serial.print(F("Connected to: "));Serial.println(wifi.SSID());
  Serial.print(F("IP address: "));Serial.println(wifi.localIP());

  delay(2000); //allow time for connection

  //Connecting to server
  Serial.print("Connecting to ");
  Serial.println(host);
  if (!client.connect(host, httpsPort))
  {
    Serial.println(F("Failed to connect to server."));
    client.stop();
    return;
  }
  if (client.connect(host, httpsPort))
  {
    Serial.println(F("Successfully connected to server"));
    client.stop();
    return;
  }

}

void loop() {
  httpget();
  delay(20000);
}

void httpget()
  {
    //Start TCP connection
//  wifi.tcpConnect(uint8_t linkID, host, httpsPort);
 
  String url = "/submitcust.php?Student_ID=1234125&Student_Name=ASDFGG";
  Serial.print("requesting URL: ");
  Serial.println(url);

  String getRequest = "GET " + url + " HTTP/1.1\r\nHost: " + host + "\r\n\r\n";

  client.print(getRequest);

  Serial.println("request sent");

  //20 seconds timeout for response from server
  int i=20000;
  while (client.available()<=0&&i--)
  {
    delay(1);
    if(i==1) {
      Serial.println(F("Server Timeout"));
      client.stop();
      return;
      }
  }

  //Check if the Get url is transferred
  if(client.available())
  {
    if (client.find("\r\n\r\n"))
      Serial.println(F("headers received"));

    String line = client.readStringUntil('\n');
    if (line.startsWith("{\"state\":\"success\""))
      Serial.println(F("esp8266/Arduino CI successfull!")); 
    else
      Serial.println(F("esp8266/Arduino CI has failed"));
   
    Serial.println(F("reply was:"));
    Serial.println(F("=========="));
    Serial.println(line);
    Serial.println(F("=========="));
    Serial.println(F("closing connection"));
   
    //flush the rest of received data
    while(client.available()>0)
      client.flush(); 
  }
  client.stop();
  }
vincent94
Freshie
 
Posts: 6
Joined: Tue Oct 11, 2016 10:20 pm

Re: Cytron ESPWiFi Shield with Arduino

Postby bengchet » Fri Oct 21, 2016 7:20 pm

if (client.connect(host, httpsPort))
{
Serial.println(F("Successfully connected to server"));
client.stop();
return;
}


The function client.stop() has actually closed your connection with the server after you have successfully connected to your server.
For your case, this whole chunk of coding is not needed.

For a proper method, you could use a small code snippet from example CytronWiFiDemo

CODE: SELECT_ALL_CODE
void clientTest()
{
  const char destServer[] = "www.adafruit.com";
  ESP8266Client client;
  if (!client.connect(destServer, 80))
  {
    Serial.println(F("Failed to connect to server."));
    client.stop();
    return;
  }
 
  const char *httpRequest = "GET /testwifi/index.html HTTP/1.1\r\n"
                           "Host: www.adafruit.com\r\n"
                           "Connection: close\r\n\r\n";
  if(!client.print(httpRequest))
  {
    Serial.println(F("Sending failed"));
    client.stop();
    return;;
  }

  // set timeout approximately 5s for server reply
  int i=5000;
  while (client.available()<=0&&i--)
  {
    delay(1);
    if(i==1) {
      Serial.println(F("Timeout"));
      return;
      }
  }

  while (client.available()>0)
  {
    //char c = (char)client.read();
    //Serial.print(c);
    Serial.write(client.read());
  }
 
  client.stop();
}


Modify this function (host, httprequest, etc), and use it with your delays in void loop(). In your void setup(), you can remove the section starting from connecting to your server. The function clientTest() will do the job for connecting to your server and send some request. Global variable ESP8266Client client is not needed also in this case.
bengchet
Moderator
 
Posts: 237
Joined: Tue Aug 25, 2015 8:29 am

Re: Cytron ESPWiFi Shield with Arduino

Postby vincent94 » Tue Oct 25, 2016 12:39 pm

Hi beng chet,

I'm wondering, will the Cytron ESP Wifi Shield still work without connecting to the ICSP ports on my Arduino Uno?
vincent94
Freshie
 
Posts: 6
Joined: Tue Oct 11, 2016 10:20 pm

Re: Cytron ESPWiFi Shield with Arduino

Postby bengchet » Wed Oct 26, 2016 8:09 am

Hi,

ICSP are only for microSD card. If you are not using it, ICSP port is not used. However it is advisable to remain those pins intact because some of the ICSP pins are tied to 5V via pull-up resistors by default on the shield.
bengchet
Moderator
 
Posts: 237
Joined: Tue Aug 25, 2015 8:29 am

Next

Return to Arduino Based

Who is online

Users browsing this forum: No registered users and 17 guests

cron