Yaler Library for Arduino WiFi

Yaler Library for Arduino WiFi
Author: Thomas Amberg
Contact: tamberg@yaler.net

Description | Download | Methods | Examples

Description

This library enables an Arduino board connected to a local network to become accessible from the Web, via the Yaler.net relay service.

Download

Download YalerWiFiServer.zip or browse the source code at http://www.bitbucket.org/yaler/yalercontrib/src/tip/Arduino/YalerWiFiServer.

This version works with the WiFi shield. There is also a Yaler Arduino library for WiFi101, a Yaler Arduino library for Ethernet, a Yaler Arduino library for CC3000 and a Yaler tutorial for the Arduino Yún.

Methods

YalerWiFiServer()

Description

Create a server that listens for incoming connections on the specified relay domain.

Syntax

YalerWiFiServer(const char *host, uint16_t port, const char *id);

Parameters

host
the relay host name or ip
port
the relay port
id
the relay domain

available()

Description

Gets a client that is connected to the server via the relay and has data available for reading.

Syntax

server.available()

Parameters

None

Returns

a Client object

Examples

YalerWebService

A minimal Web service accessible via the Yaler relay.

// Copyright (c) 2014, Yaler GmbH, Switzerland
// All rights reserved

#include <SPI.h>
#include <WiFi.h>
#include <YalerWiFiServer.h>

char ssid[] = "LOCAL_NETWORK_SSID"; 
char pass[] = "LOCAL_NETWORK_PASSWORD";

int status = WL_IDLE_STATUS;

// Local WiFiServer at http://LOCAL_IP/ (e.g. http://192.168.0.7/)
//WiFiServer server(80);

// Get a relay domain at https://yaler.net/ to replace RELAY_DOMAIN below
// The YalerWiFiServer is accessible at http://RELAY_DOMAIN.try.yaler.io/
YalerWiFiServer server("try.yaler.io", 80, "RELAY_DOMAIN");

void setup() {
  Serial.begin(9600);
  Serial.println("Aquiring IP address...");
  if (WiFi.begin(ssid, pass) != WL_CONNECTED) {
    Serial.println("Connecting to WiFi failed.");
  } else {
    Serial.println(WiFi.localIP());
    server.begin();
  }
}

void sendResponse(WiFiClient client) {
  client.print("HTTP/1.1 200 OK\r\n");
  client.print("Connection: close\r\n");
  client.print("Content-Length: 5\r\n");
  client.print("\r\n");
  client.print("Hello");
}

void loop() {
  WiFiClient client = server.available();
  if (client && client.connected()) {
    client.find("\r\n\r\n"); // Consume incoming request
    sendResponse(client);
    delay(3000); // Give the Web browser time to receive the data
    client.stop();
  }
}

For more examples, see: http://www.bitbucket.org/yaler/yalercontrib/src/tip/Arduino

Share