I've been busy in the latest months writing a book to cover some key aspects of the ESP8266 ecosystem, the chip, the cloud and a mobile application.
The book will teach you and will give you a ready-to-sell solution for an IoT product.
You will discover how to work with the GPIOs on the ESP8266, how to build your basic thermostat for your house, how to control it from your mobile with your own cloud system based on MQTT.
Securing the data using authentication at the broker level and SSL is explained in a special chapter.
Real-time communication has a dedicated chapter where you will learn how to send real-time data from an ESP8266 to an nodejs server.
You can buy the book from Amazon.
I hope that you will enjoy the book.
Esp8266 blog. Learn how to compile, how to work with the wireless chip esp8266. ESP-01 ESP-03, ESP-07, ESP-12, ESP201 all are here
Showing posts with label MQTT. Show all posts
Showing posts with label MQTT. Show all posts
Saturday, December 2, 2017
Monday, October 2, 2017
MQTT Broker on ESP8266
Running an MQTT broker to serve all devices for a house ( 20pcs) it requires at least a Raspberry Pi or an equivalent SOC.
SD card fails from time to time and requires some Linux skills to make it work and the final price is over USD 70. (Good power supply, case, HDMI cable, keyboard )
But how about running an MQTT broker on this USD 3.99 ESP8266 board ? It is possible ? Yes, it is ! Go to http://iotcentral.eu and with few clicks you will be able to flash your ESP8266 ( 4Mb) with the binary.
No need to compile code, to solve errors , just click and run. More than that your MQTT broker will communicate with your http://iotcentral.eu instance so you still get your messages on your phone if your mobile app is connecting to the websockets to your http://iotcentral.eu cloud instance.
But what I am at home and my internet connection is down ? Can I communicate with my devices or I am isolated? Don't worry, your mobile app will still be able to connect to the ESP8266 MQTT Broker over the websocket so you can control your devices.
SD card fails from time to time and requires some Linux skills to make it work and the final price is over USD 70. (Good power supply, case, HDMI cable, keyboard )
But how about running an MQTT broker on this USD 3.99 ESP8266 board ? It is possible ? Yes, it is ! Go to http://iotcentral.eu and with few clicks you will be able to flash your ESP8266 ( 4Mb) with the binary.
No need to compile code, to solve errors , just click and run. More than that your MQTT broker will communicate with your http://iotcentral.eu instance so you still get your messages on your phone if your mobile app is connecting to the websockets to your http://iotcentral.eu cloud instance.
But what I am at home and my internet connection is down ? Can I communicate with my devices or I am isolated? Don't worry, your mobile app will still be able to connect to the ESP8266 MQTT Broker over the websocket so you can control your devices.
Friday, August 5, 2016
Mqtt broker on ESP8266 #4
[Later edit]. On iotcentral.eu you will find instructions on how to install the ESP8266 MQTT broker on your EPS8266 and how to use the MQTT service provided by iotcentral.eu
I have more progress in ESP8266 as MQTT broker.
What I have for now on this ESP8266 module:
- MQTT broker functionality
- Bridging data to/from and another MQTT broker ( usually a cloud MQTT instance)
- websockets connectivity ( I can now connect with my Homy application directly to the ESP8266 acting as an MQTT broker)
- OTA (Over The Air) updates. Every time I have a new software update the ESP8266 is updating itself so, I can say that I have continuous integration (CI) and continuous deployment(CD). Also all the modules are activated over the air.
With all those function in place I still have 34kb of heap available for other features and yes is running on an ESP with a minimum 1Mb of memory. ESP8285 is a good candidate for running the broker.
Is working also on and old ESP-01 with 512kb but without OTA functionality.
Previous posts on the same subject:
IoT with ESP8266: Mqtt broker on ESP8266 #1
Previous posts on the same subject:
IoT with ESP8266: Mqtt broker on ESP8266 #1
|
|
Friday, April 22, 2016
Architecture of my Homy IoT platform
Since I received a lot of questions about the my IoT home control system, here is a description of it.
All the ESPs are sending MQTT messages to an MQTT broker (I have an ESP8266 acting as a broker) inside the house. The MQTT broker is also bridging all the data to another MQTT broker instance that I have in a cloud. From now there are two cases:
1. If I am in my WiFi coverage, my mobile app is connecting over the websokets to the MQTT broker and sees all the devices, control them etc.
2. If I am not in my Wifi coverage my mobile app is connecting to the cloud MQTT broker over websockets and sees all the devices, control them etc.
I've chosen to have connection between the mobile application and both MQTT brokers, instead to have one connection to the cloud broker, because if you are at home and your internet connection is down you can not control any device. Having a connection directly to your home MQTT broker is covering this scenario.
The mobile application first is connecting to the local MQTT broker and if is not succeed ( means you are not at home) it connecting to the cloud MQTT broker. This is done automatically and is transparent to the user.
As you can see no port forwarding, nothing to do in your router.
All the ESPs are sending MQTT messages to an MQTT broker (I have an ESP8266 acting as a broker) inside the house. The MQTT broker is also bridging all the data to another MQTT broker instance that I have in a cloud. From now there are two cases:
1. If I am in my WiFi coverage, my mobile app is connecting over the websokets to the MQTT broker and sees all the devices, control them etc.
2. If I am not in my Wifi coverage my mobile app is connecting to the cloud MQTT broker over websockets and sees all the devices, control them etc.
I've chosen to have connection between the mobile application and both MQTT brokers, instead to have one connection to the cloud broker, because if you are at home and your internet connection is down you can not control any device. Having a connection directly to your home MQTT broker is covering this scenario.
The mobile application first is connecting to the local MQTT broker and if is not succeed ( means you are not at home) it connecting to the cloud MQTT broker. This is done automatically and is transparent to the user.
As you can see no port forwarding, nothing to do in your router.
![]() |
| My IoT home control system |
Thursday, March 10, 2016
Mqtt broker on ESP8266 #2
[Later edit]. On iotcentral.eu you will find instructions on how to install the ESP8266 MQTT broker on your EPS8266 and how to use the MQTT service provided by iotcentral.eu
Working on MQTT broker on ESP8266 has reached the first milestone. Until now functionality includes MQTT broker and bridging to another instance of MQTT broker cloudmqtt.com or mosquitto. It should work also on AWS IoT but I didn't tested yet.
After more then one million of seconds uptime and hundreds of thousands of transported messages I can declare it in 1.0 version.
Next to implement:
Working on MQTT broker on ESP8266 has reached the first milestone. Until now functionality includes MQTT broker and bridging to another instance of MQTT broker cloudmqtt.com or mosquitto. It should work also on AWS IoT but I didn't tested yet.
After more then one million of seconds uptime and hundreds of thousands of transported messages I can declare it in 1.0 version.
Next to implement:
- websocket server using the
noPollmy own library (work in progressDone ) - web interface configuration
Thursday, December 17, 2015
ESP8266 as IR remote control
Today I've managed to make the ESP8266 to work as IR remote control over MQTT for my TV.
Range is now only 1m, but I am planning to increase it. Next step is to update the Android app to support TV, AC etc.
To test the code the JSON can be used:
mosquitto_pub -h broker_ip -p port -t "/62/ir/command" -m "{\"device_name\":\"ESP_47106\", \"type\":\"ir\", \"value\":\"ON\"}"
#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <PubSubClient.h>
#include <IRremoteESP8266.h>
#include <Timer.h>
#define PanasonicAddress 0x4004 // Panasonic address (Pre data)
#define PanasonicPower 0x100BCBD // Panasonic Power button
#define wifi_ssid "WLAN_62"
#define wifi_password "........"
#define mqtt_server "......."
#define mqtt_user "CATA"
#define mqtt_password "CATA"
#define mqtt_port 1880
#define IR_PIN 14
#define ir_topic "/62/ir/command"
#define device_status_topic "/62/device/status"
// Callback function header
void rx_mqtt_callback(char* topic, byte* payload, unsigned int length);
#define DEBUG false
#define Serial if(DEBUG)Serial
#define DEBUG_OUTPUT Serial
IRsend irsend(IR_PIN); //an IR led is connected to GPIO pin
WiFiClient espClient;
PubSubClient client(mqtt_server, mqtt_port, rx_mqtt_callback,espClient);
Timer t;
StaticJsonBuffer<512> jsonDeviceStatus;
JsonObject& jsondeviceStatus = jsonDeviceStatus.createObject();
char dev_name[50];
char json_buffer_status[512];
char my_ip_s[16];
void setup_wifi()
{
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(wifi_ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(wifi_ssid, wifi_password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
if (client.connect(dev_name, mqtt_user, mqtt_password)) {
Serial.println("connected");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void sendMQTTUpdate()
{
IPAddress my_ip_addr = WiFi.localIP();
sprintf(my_ip_s, "%d.%d.%d.%d", my_ip_addr[0],my_ip_addr[1],my_ip_addr[2],my_ip_addr[3]);
jsondeviceStatus ["device_name"] = dev_name;
jsondeviceStatus["type"] = "ir";
jsondeviceStatus["ipaddress"] = String(my_ip_s).c_str();
jsondeviceStatus["bgn"] = 3;
jsondeviceStatus["sdk"] = ESP.getSdkVersion();//"1.4.0";
jsondeviceStatus["version"] = "1";
jsondeviceStatus["uptime"] = "1";//ESP.getVcc();
jsondeviceStatus.printTo(json_buffer_status, sizeof(json_buffer_status));
client.publish(device_status_topic, json_buffer_status , false);
Serial.println(json_buffer_status);
}
void rx_mqtt_callback(char* topic, byte* payload, unsigned int length)
{
//reserve space for incomming message
StaticJsonBuffer<256> jsonRxMqttBuffer;
int i = 0;
char rxj[256];
Serial.println(dev_name); Serial.print("Topic:");Serial.println(topic);
for(i=0;i<length;i++)
{
rxj[i] = payload[i];
}
Serial.println(rxj);
JsonObject& root = jsonRxMqttBuffer.parseObject(rxj);
if (!root.success())
{
Serial.println("parseObject() failed");
return;
}
const char* device_name = root["device_name"];
const char* type = root["type"];
const char* value = root["value"];
Serial.println(device_name);
Serial.println(type); Serial.println(value);
//if( (type == "ir") && ((value == "ON") || (value == "OFF")))
//{
/* DO A SANITIZE ON THE MESSAGE AND IF IS CLEAN SEND IR*/
sendIR();
//}
Serial.println("<=rx_mqtt_callback");
return;
}
void sendIR()
{
int i = 0;
Serial.print("sendIR for 2 sec");
for(i=0;i<20; i++)
{
irsend.sendPanasonic(PanasonicAddress,PanasonicPower); // This should turn your TV on and off
delay(100);
}
}
void setup()
{
delay(1000);
irsend.begin();
Serial.begin(115200);
sprintf(dev_name, "ESP_%d", ESP.getChipId());
setup_wifi();
client.setServer(mqtt_server, 1880);
client.connect(dev_name, mqtt_user, mqtt_password);
client.subscribe(ir_topic);
if (!client.connected())
{
reconnect();
}
t.every(60 * 1000 , sendMQTTUpdate);
}
void loop() {
client.loop();
t.update();
}
https://github.com/bcatalin/ESP8266-Infrared-Panasonic/
Range is now only 1m, but I am planning to increase it. Next step is to update the Android app to support TV, AC etc.
To test the code the JSON can be used:
mosquitto_pub -h broker_ip -p port -t "/62/ir/command" -m "{\"device_name\":\"ESP_47106\", \"type\":\"ir\", \"value\":\"ON\"}"
Every minute the module will report its status with a JSON over MQTT
{"device_name":"ESP_470106","type":"ir","ipaddress":"192.168.8.150","bgn":3,"sdk":"1.3.0","version":"1","uptime":"1"}
#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <PubSubClient.h>
#include <IRremoteESP8266.h>
#include <Timer.h>
#define PanasonicAddress 0x4004 // Panasonic address (Pre data)
#define PanasonicPower 0x100BCBD // Panasonic Power button
#define wifi_ssid "WLAN_62"
#define wifi_password "........"
#define mqtt_server "......."
#define mqtt_user "CATA"
#define mqtt_password "CATA"
#define mqtt_port 1880
#define IR_PIN 14
#define ir_topic "/62/ir/command"
#define device_status_topic "/62/device/status"
// Callback function header
void rx_mqtt_callback(char* topic, byte* payload, unsigned int length);
#define DEBUG false
#define Serial if(DEBUG)Serial
#define DEBUG_OUTPUT Serial
IRsend irsend(IR_PIN); //an IR led is connected to GPIO pin
WiFiClient espClient;
PubSubClient client(mqtt_server, mqtt_port, rx_mqtt_callback,espClient);
Timer t;
StaticJsonBuffer<512> jsonDeviceStatus;
JsonObject& jsondeviceStatus = jsonDeviceStatus.createObject();
char dev_name[50];
char json_buffer_status[512];
char my_ip_s[16];
void setup_wifi()
{
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(wifi_ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(wifi_ssid, wifi_password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
if (client.connect(dev_name, mqtt_user, mqtt_password)) {
Serial.println("connected");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void sendMQTTUpdate()
{
IPAddress my_ip_addr = WiFi.localIP();
sprintf(my_ip_s, "%d.%d.%d.%d", my_ip_addr[0],my_ip_addr[1],my_ip_addr[2],my_ip_addr[3]);
jsondeviceStatus ["device_name"] = dev_name;
jsondeviceStatus["type"] = "ir";
jsondeviceStatus["ipaddress"] = String(my_ip_s).c_str();
jsondeviceStatus["bgn"] = 3;
jsondeviceStatus["sdk"] = ESP.getSdkVersion();//"1.4.0";
jsondeviceStatus["version"] = "1";
jsondeviceStatus["uptime"] = "1";//ESP.getVcc();
jsondeviceStatus.printTo(json_buffer_status, sizeof(json_buffer_status));
client.publish(device_status_topic, json_buffer_status , false);
Serial.println(json_buffer_status);
}
void rx_mqtt_callback(char* topic, byte* payload, unsigned int length)
{
//reserve space for incomming message
StaticJsonBuffer<256> jsonRxMqttBuffer;
int i = 0;
char rxj[256];
Serial.println(dev_name); Serial.print("Topic:");Serial.println(topic);
for(i=0;i<length;i++)
{
rxj[i] = payload[i];
}
Serial.println(rxj);
JsonObject& root = jsonRxMqttBuffer.parseObject(rxj);
if (!root.success())
{
Serial.println("parseObject() failed");
return;
}
const char* device_name = root["device_name"];
const char* type = root["type"];
const char* value = root["value"];
Serial.println(device_name);
Serial.println(type); Serial.println(value);
//if( (type == "ir") && ((value == "ON") || (value == "OFF")))
//{
/* DO A SANITIZE ON THE MESSAGE AND IF IS CLEAN SEND IR*/
sendIR();
//}
Serial.println("<=rx_mqtt_callback");
return;
}
void sendIR()
{
int i = 0;
Serial.print("sendIR for 2 sec");
for(i=0;i<20; i++)
{
irsend.sendPanasonic(PanasonicAddress,PanasonicPower); // This should turn your TV on and off
delay(100);
}
}
void setup()
{
delay(1000);
irsend.begin();
Serial.begin(115200);
sprintf(dev_name, "ESP_%d", ESP.getChipId());
setup_wifi();
client.setServer(mqtt_server, 1880);
client.connect(dev_name, mqtt_user, mqtt_password);
client.subscribe(ir_topic);
if (!client.connected())
{
reconnect();
}
t.every(60 * 1000 , sendMQTTUpdate);
}
void loop() {
client.loop();
t.update();
}
https://github.com/bcatalin/ESP8266-Infrared-Panasonic/
Subscribe to:
Posts (Atom)
