WiFiEsp

bportaluriさんのWiFiEspライブラリをGR-ROSE用にポーティングしたライブラリです。API仕様についてはGitHubのREADME を参照してください。

使用する場合は、#include <WiFiEsp.h>を記述してください。クライアントとサーバーのサンプルプログラムのみ記載します。


サンプルプログラム

WebClientとしてarduino.ccからデータを取得します。


#include <Arduino.h>
#include <WiFiEsp.h>
char ssid[] = "Twim";            // your network SSID (name)
char pass[] = "12345678";        // your network password
int status = WL_IDLE_STATUS;     // the Wifi radio's status
 
char server[] = "arduino.cc";
 
// Initialize the Ethernet client object
WiFiEspClient client;
void printWifiStatus();
 
void setup()
{
  // initialize serial for debugging
  Serial.begin(115200);
  // initialize serial for ESP module
  Serial6.begin(115200);
  // initialize ESP module
  WiFi.init(&Serial6);
 
  // check for the presence of the shield
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue
    while (true);
  }
 
  // attempt to connect to WiFi network
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network
    status = WiFi.begin(ssid, pass);
  }
 
  // you're connected now, so print out the data
  Serial.println("You're connected to the network");
 
  printWifiStatus();
 
  Serial.println();
  Serial.println("Starting connection to server...");
  // if you get a connection, report back via serial
  if (client.connect(server, 80)) {
    Serial.println("Connected to server");
    // Make a HTTP request
    client.println("GET /asciilogo.txt HTTP/1.1");
    client.println("Host: arduino.cc");
    client.println("Connection: close");
    client.println();
  }
  while(!client.available()); // wait for 1st response from server
 
}
 
void loop()
{
  // if there are incoming bytes available
  // from the server, read them and print them
  while (client.available()) {
    char c = client.read();
    Serial.write(c);
  }
 
  // if the server's disconnected, stop the client
  if (!client.connected()) {
    Serial.println();
    Serial.println("Disconnecting from server...");
    client.stop();
 
    // do nothing forevermore
    while (true);
  }
}
 
 
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");
}

GR-ROSE becomes an access point, and when connected, it returns the value of pin A0 as WebServer.


#include <Arduino.h>
#include <WiFiEsp.h>
char ssid[] = "TwimEsp";         // your network SSID (name)
char pass[] = "12345678";        // your network password
int status = WL_IDLE_STATUS;     // the Wifi radio's status
int reqCount = 0;                // number of requests received
 
WiFiEspServer server(80);
 
// use a ring buffer to increase speed and reduce memory allocation
RingBuffer buf(8);
void sendHttpResponse(WiFiEspClient client);
void printWifiStatus();
 
void setup()
{
  Serial.begin(115200);   // initialize serial for debugging
  Serial6.begin(115200);    // initialize serial for ESP module
  WiFi.init(&Serial6);    // initialize ESP module
 
  // check for the presence of the shield
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    while (true); // don't continue
  }
 
  Serial.print("Attempting to start AP ");
  Serial.println(ssid);
 
  // uncomment these two lines if you want to set the IP address of the AP
  //IPAddress localIp(192, 168, 111, 111);
  //WiFi.configAP(localIp);
 
  // start access point
  status = WiFi.beginAP(ssid, 10, pass, ENC_TYPE_WPA2_PSK);
 
  Serial.println("Access point started");
  printWifiStatus();
 
  // start the web server on port 80
  server.begin();
  Serial.println("Server started");
}
 
 
void loop()
{
  WiFiEspClient client = server.available();  // listen for incoming clients
 
  if (client) {                               // if you get a client,
    Serial.println("New client");             // print a message out the serial port
    buf.init();                               // initialize the circular buffer
    while (client.connected()) {              // loop while the client's connected
      if (client.available()) {               // if there's bytes to read from the client,
        char c = client.read();               // read a byte, then
        buf.push(c);                          // push it to the ring buffer
 
        // you got two newline characters in a row
        // that's the end of the HTTP request, so send a response
        if (buf.endsWith("\r\n\r\n")) {
          sendHttpResponse(client);
          break;
        }
      }
    }
 
    // give the web browser time to receive the data
    delay(10);
 
    // close the connection
    client.stop();
    Serial.println("Client disconnected");
  }
}
 
void sendHttpResponse(WiFiEspClient client)
{
  client.print(
    "HTTP/1.1 200 OK\r\n"
    "Content-Type: text/html\r\n"
    "Connection: close\r\n"  // the connection will be closed after completion of the response
    "Refresh: 20\r\n"        // refresh the page automatically every 20 sec
    "\r\n");
  client.print("\r\n");
  client.print("\r\n");
  client.print("<h1>Hello World!</h1>\r\n");
  client.print("Requests received: ");
  client.print(++reqCount);
  client.print("
\r\n");
  client.print("Analog input A0: ");
  client.print(analogRead(0));
  client.print("
\r\n");
  client.print("\r\n");
}
 
void printWifiStatus()
{
  // print your WiFi shield's IP address
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
 
  // print where to go in the browser
  Serial.println();
  Serial.print("To see this page in action, connect to ");
  Serial.print(ssid);
  Serial.print(" and open a browser to http://");
  Serial.println(ip);
  Serial.println();
}