Creating a Web Thing on the ESP8266

Today I’ m going to stroll you through creating a simple Web Thing using an inexpensive off-the-shelf ESP8266 table .

The power associated with web things comes from their capability to connect the digital world associated with web pages with the physical world associated with things. We recently released the particular Things Construction , a collection of software intended to allow it to be easy to create new web matters. The relevant library for this example could be the webthing-esp8266 collection , which makes easy it for connecting Arduino-programmed ESP8266 boards with the Internet of Things. We hope that this reduces the barrier to creating compelling encounters with our gateway and the Web Factor API.

Lamp example running on the ESP8266

The first step in our journey is to set up the Arduino IDE and its ESP8266 board support. Visit Adafruit’ s ESP8266 documents for a very comprehensive walkthrough of setting up. At the end of set up, you should have an Arduino IDE set up that has the ESP8266WiFi, ESP8266mDNS, plus ESP8266WebServer libraries available.

System diagram

Now that we have all the requirements out of the way, let’ s get to the particular fun part. The webthing-esp8266 collection works by assembling a collection of components that can come together to expose the Web Thing API . The main coordinator is the WebThingAdapter which keeps a record of a ThingDevice that in turn has an range of ThingProperties .

The WebThingAdapter knows how to talk the Web of Things API with the Gateway and handles all the interpretation necessary for the Gateway to discover plus interact with the ThingDevice . The ThingDevice represents the actual object we want to put on the web. Within a simple case, this may be a few LEDs. Once we get more complex, this could be the quadcopter, an OLED display, or perhaps a Tesla coil. Before we succeed of ourselves, let’ s action through a basic example which unearths the ESP8266’ s built-in BROUGHT.

To start, create a brand new sketch in the Arduino IDE. Since we have a place to write code, we have to include all the libraries this draw uses. These are ESP8266WiFi and WiFiClient for connecting to the WiFi network, Issue for creating Web-of-Things-compatible items, and WebThingAdapter for translating these items into a web server.

 #include < ESP8266WiFi. h>
#include < WiFiClient. h>
#include < Thing. h>
#include < WebThingAdapter. h>
 
 

The next step is to configure the particular constants we’ re using. They are ssid for our WiFi network’ s title, password for its password, and lampPin for the pin number of the LED we want to control.

 const char* ssid = "...... ";
const char* password = ".......... inch;

const int lampPin = LED_BUILTIN;
 
 

Now all of us get to specify what kind of web factor we’ re creating. First, we all create the adapter, which pieces the name of the board. If you want to have multiple ESP8266 boards on the same network, you’ lmost all need to make sure their names are distinctive.

 WebThingAdapter adapter("esp8266");
 
 

After that we need to specify the ThingDevice we want to possess on our gateway. In this case, we want to show the LED as a dimmableLight called “ My Lamp” which will allow us to turn this on and control its lighting from the gateway.

 ThingDevice lamp("lamp", "My Lamp", "dimmableLight");
 
 

Following we define the properties we would like the ThingDevice to have. A dimmableLight needs 2 properties: “ on” and “ level”.

 ThingProperty lampOn("on", "Whether the light is turned on", BOOLEAN);
ThingProperty lampLevel("level", "The level of light through 0-100", NUMBER);
 
 

In the start of our setup functionality we initialize the LED, connect with our WiFi network, and turn over the Serial port for debugging.

  void setup() 
  pinMode(lampPin, OUTPUT);
  digitalWrite(lampPin, HIGH);
  analogWriteRange(255);

  Serial.begin(115200);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  Serial.println("");

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) 
    delay(500);
    Serial.print(".");
  

  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
  
 

With that boilerplate out of the way, we can tie together our ThingProperties , ThingDevice , and WebThingAdapter . The lamp needs to know that it owns the lampOn and lampLevel properties, while the adapter needs to know that the lamp exists.

 
    lamp.addProperty(&lampOn);
  lamp.addProperty(&lampLevel);
  adapter.addDevice(&lamp);
  adapter.begin();
  Serial.println("HTTP server started");

 
 

Within our continuously running cycle function we initial update the adapter so it are designed for talking to the gateway or any various other Web of Things clients. Following, we update the light based on the residence values. Note that we take the zero to 100 brightness level and map this from 255-0 because the brightness from the ESP8266’ s built-in LED is definitely inverted.

  void loop() 
  adapter.update();
  if (lampOn.getValue().boolean) 
    int level = map(lampLevel.getValue().number, 0, 100, 255, 0);
    analogWrite(lampPin, level);
   else 
    analogWrite(lampPin, 255);
  

 
 

Our sketch is done! In case you upload this to your ESP8266 you have to be able to see the “ My Lamp” thing in your gateway’ s Include Things list. You can then click to show the LED on and off, or click the “ splat” icon to control the brightness level.

Plus, if you need more inspiration, check out the demonstration of my LED lamp:

You may also control your web thing straight by issuing HTTP requests in order to http://esp8266.local/ . For instance , you can use this curl command to show the LED on from the fatal.

 snuggle -X PUT http://esp8266.local/things/lamp/properties/on -H 'Content-Type: application/json' -d ' "on":true '
 
 

This is only the beginning of what you can do with the Internet Thing API. If you’ m prefer working in a higher-level vocabulary, check out our other web thing examples . If you think on/off and level aren’ t cool enough on their own, all of us also support string properties !

Degree 24 Computer Wizard on a pursuit to keep the Internet of Things totally free and open.

More articles simply by James Hobin…

If you liked Creating a Web Thing on the ESP8266 by James Hobin Then you'll love Web Design Agency Miami

Add a Comment

Your email address will not be published. Required fields are marked *

Shares