Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
ESP8266 Home Automation Projects

You're reading from  ESP8266 Home Automation Projects

Product type Book
Published in Nov 2017
Publisher Packt
ISBN-13 9781787282629
Pages 196 pages
Edition 1st Edition
Languages
Authors (2):
Catalin Batrinu Catalin Batrinu
Profile icon Catalin Batrinu
Constantin Tambrea Constantin Tambrea
View More author details

Chapter 7. Real-Time Communication

All the communication that has been presented until now was based on a request response  method, in which one entity was sending a request and an other entity was sending back a response. But there are situations when you need real-time communications between the ESP8266 module and a server, not just transactions. To achieve real-time communication, we will use WebSockets to stream the acceleration values from an acceleration sensor to a server to display them in a real-time graphic, and also to store them in a time-series database.

WebSockets


WebSockets is a communication protocol, providing full-duplex messages, streaming on top of a Transport Control Protocol (TCP). It is standardized by the W3 organizations and it is present on all major browsers (Internet Explorer must be at version 11+). WebSockets enables the communication between a browser and a server and between our module and a server. Through the server, the data from the ESP8266 can arrive in a browser. Another big advantage for WebSockets is the bidirectional communication without creating a new request. Every communication is done on the opened TCP connection.

Protocol details

A WebSocket connection starts as an HTTP connection with the request to upgrade to a websocket protocol.

In this case, the client is sending:

GET /chat HTTP/1.1Host: server.example.comUpgrade: websocketConnection: UpgradeSec-WebSocket-Key: t3JJHjbGL5EzHkh8GBMXGw==Sec-WebSocket-Protocol: chat, superchatSec-WebSocket-Version: 13Origin: http://example.com

And if the server is websocket...

Streaming data from ESP8266


To stream data from ESP8266 we need first to establish a WebSocket connection between the ESP8266 and a server; data that will be streamed over the WebSocket connection will be the acceleration values for the X, Y and Z axes. ESP8266 will read them from an ADXL345 chip and will send them to a nodeJS server. From the server, data can be sent to a connected browser on the same server or can be written to a database for further analysis:

The final circuit can also include a time-series database such as InfluxDb, for storing the values transmitted by the ADXL345.

Adding a database can let you collect various data from multiple acceleration sensors, store them in the database, retrieve them on request to compare them with the current values, or draw nice graphs with the current and historical data.

A server can also react to some values and send alerts (email, SMS) and send data to other ESP8266 modules to react, or to other servers.

ADXL345 accelerometer

Produced by the...

Summary


In this chapter, we completed another important functionality that can be accomplished with ESP8266, real-time communication. You have learned how to stream real-time acceleration from an ADXL345 3 axes accelerometer to a backend Node.js server that will forward the received data to connected browsers. Data is drawn nicely in real time with smoothie.js. As a continuation of this chapter, I encourage you to store the received data in a time-series database, use Grafana as a display tool for your values, and why not send them over MQTT to an MQTT broker. Adding the database and a nice tool for displaying the values can turn this solution into a commercial one. You can develop a platform for storing and showing real-time data for other companies or private users.

lock icon The rest of the chapter is locked
You have been reading a chapter from
ESP8266 Home Automation Projects
Published in: Nov 2017 Publisher: Packt ISBN-13: 9781787282629
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}