Initial commit
commit
e22cc17e3f
|
@ -0,0 +1,3 @@
|
||||||
|
*/out/*
|
||||||
|
*.swp
|
||||||
|
*.py[co]
|
|
@ -0,0 +1,26 @@
|
||||||
|
mqtt-playground
|
||||||
|
===============
|
||||||
|
|
||||||
|
This repository is a storage for my mqtt and IoT (( ͡° ͜ʖ ͡°)) playground.
|
||||||
|
|
||||||
|
|
||||||
|
Hardware
|
||||||
|
--------
|
||||||
|
|
||||||
|
Current tests involve NodeMCU ESP12-E/ESP8266 boards. Switch is activated with
|
||||||
|
GPIO0 (FLASH button on NodeMCU) and light is connected to GPIO2, where blue LED
|
||||||
|
is directly connected on ESP-12E module.
|
||||||
|
|
||||||
|
|
||||||
|
Software
|
||||||
|
--------
|
||||||
|
|
||||||
|
Mosquitto contained in docker is used as a broker. ESP8266 code uses [Sming
|
||||||
|
framework](https://github.com/SmingHub/Sming). Set your Wifi configuration in
|
||||||
|
`common/common_config.h` (used by both `switch` and `light`)
|
||||||
|
|
||||||
|
|
||||||
|
Thoughts
|
||||||
|
--------
|
||||||
|
|
||||||
|
Oh my, that's slow.
|
|
@ -0,0 +1,17 @@
|
||||||
|
#ifndef __COMMON_CONFIG_H__
|
||||||
|
#define __COMMON_CONFIG_H__
|
||||||
|
|
||||||
|
#ifndef WIFI_SSID
|
||||||
|
#define WIFI_SSID "WIFI-NETWORK"
|
||||||
|
#define WIFI_PWD "PASSWORD"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef MQTT_BROKER
|
||||||
|
#define MQTT_BROKER "192.168.0.100"
|
||||||
|
#define MQTT_PORT 1883
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define BTN_PIN 0
|
||||||
|
#define LED_PIN 2
|
||||||
|
|
||||||
|
#endif
|
|
@ -0,0 +1,24 @@
|
||||||
|
#####################################################################
|
||||||
|
#### Please don't change this file. Use Makefile-user.mk instead ####
|
||||||
|
#####################################################################
|
||||||
|
# Including user Makefile.
|
||||||
|
# Should be used to set project-specific parameters
|
||||||
|
include ./Makefile-user.mk
|
||||||
|
|
||||||
|
# Important parameters check.
|
||||||
|
# We need to make sure SMING_HOME and ESP_HOME variables are set.
|
||||||
|
# You can use Makefile-user.mk in each project or use enviromental variables to set it globally.
|
||||||
|
|
||||||
|
ifndef SMING_HOME
|
||||||
|
$(error SMING_HOME is not set. Please configure it in Makefile-user.mk)
|
||||||
|
endif
|
||||||
|
ifndef ESP_HOME
|
||||||
|
$(error ESP_HOME is not set. Please configure it in Makefile-user.mk)
|
||||||
|
endif
|
||||||
|
|
||||||
|
# Include main Sming Makefile
|
||||||
|
ifeq ($(RBOOT_ENABLED), 1)
|
||||||
|
include $(SMING_HOME)/Makefile-rboot.mk
|
||||||
|
else
|
||||||
|
include $(SMING_HOME)/Makefile-project.mk
|
||||||
|
endif
|
|
@ -0,0 +1,31 @@
|
||||||
|
## Local build configuration
|
||||||
|
## Parameters configured here will override default and ENV values.
|
||||||
|
## Uncomment and change examples:
|
||||||
|
|
||||||
|
#Add your source directories here separated by space
|
||||||
|
MODULES = app
|
||||||
|
DISABLE_SPIFFS = 1
|
||||||
|
USER_CFLAGS = -I../common
|
||||||
|
## ESP_HOME sets the path where ESP tools and SDK are located.
|
||||||
|
## Windows:
|
||||||
|
# ESP_HOME = c:/Espressif
|
||||||
|
|
||||||
|
## MacOS / Linux:
|
||||||
|
#ESP_HOME = /opt/esp-open-sdk
|
||||||
|
|
||||||
|
## SMING_HOME sets the path where Sming framework is located.
|
||||||
|
## Windows:
|
||||||
|
# SMING_HOME = c:/tools/sming/Sming
|
||||||
|
|
||||||
|
# MacOS / Linux
|
||||||
|
# SMING_HOME = /opt/sming/Sming
|
||||||
|
|
||||||
|
## COM port parameter is reqruied to flash firmware correctly.
|
||||||
|
## Windows:
|
||||||
|
# COM_PORT = COM3
|
||||||
|
|
||||||
|
# MacOS / Linux:
|
||||||
|
# COM_PORT = /dev/tty.usbserial
|
||||||
|
|
||||||
|
# Com port speed
|
||||||
|
# COM_SPEED = 115200
|
|
@ -0,0 +1,37 @@
|
||||||
|
#include <user_config.h>
|
||||||
|
#include <common_config.h>
|
||||||
|
#include <SmingCore/SmingCore.h>
|
||||||
|
|
||||||
|
MqttClient mqtt(MQTT_BROKER, MQTT_PORT, *[](String topic, String message) {
|
||||||
|
Serial.printf("*** message received @ %s:\n\t%s\n***\n", topic.c_str(), message.c_str());
|
||||||
|
digitalWrite(LED_PIN, message == "1");
|
||||||
|
});
|
||||||
|
|
||||||
|
void startMqttClient()
|
||||||
|
{
|
||||||
|
String deviceName = "light-" + WifiStation.getMAC().substring(6, 12);
|
||||||
|
Serial.println("*** Connecting to MQTT as " + deviceName);
|
||||||
|
mqtt.connect(deviceName);
|
||||||
|
mqtt.subscribe("btn/status");
|
||||||
|
}
|
||||||
|
|
||||||
|
void init()
|
||||||
|
{
|
||||||
|
Serial.begin(SERIAL_BAUD_RATE); // 115200 by default
|
||||||
|
Serial.systemDebugOutput(true); // Debug output to serial
|
||||||
|
|
||||||
|
Serial.println("*** Starting ...");
|
||||||
|
WifiStation.config(WIFI_SSID, WIFI_PWD);
|
||||||
|
WifiStation.enable(true);
|
||||||
|
|
||||||
|
WifiAccessPoint.enable(false);
|
||||||
|
|
||||||
|
WifiStation.waitConnection(*[] {
|
||||||
|
Serial.println("*** Connection succeeded");
|
||||||
|
startMqttClient();
|
||||||
|
}, 20, *[] {
|
||||||
|
Serial.println("*** Connection failed");
|
||||||
|
});
|
||||||
|
|
||||||
|
pinMode(LED_PIN, OUTPUT);
|
||||||
|
}
|
|
@ -0,0 +1,45 @@
|
||||||
|
#ifndef __USER_CONFIG_H__
|
||||||
|
#define __USER_CONFIG_H__
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// UART config
|
||||||
|
#define SERIAL_BAUD_RATE 115200
|
||||||
|
|
||||||
|
// ESP SDK config
|
||||||
|
#define LWIP_OPEN_SRC
|
||||||
|
#define USE_US_TIMER
|
||||||
|
|
||||||
|
// Default types
|
||||||
|
#define __CORRECT_ISO_CPP_STDLIB_H_PROTO
|
||||||
|
#include <limits.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
// Override c_types.h include and remove buggy espconn
|
||||||
|
#define _C_TYPES_H_
|
||||||
|
#define _NO_ESPCON_
|
||||||
|
|
||||||
|
// Updated, compatible version of c_types.h
|
||||||
|
// Just removed types declared in <stdint.h>
|
||||||
|
#include <espinc/c_types_compatible.h>
|
||||||
|
|
||||||
|
// System API declarations
|
||||||
|
#include <esp_systemapi.h>
|
||||||
|
|
||||||
|
// C++ Support
|
||||||
|
#include <esp_cplusplus.h>
|
||||||
|
// Extended string conversion for compatibility
|
||||||
|
#include <stringconversion.h>
|
||||||
|
// Network base API
|
||||||
|
#include <espinc/lwip_includes.h>
|
||||||
|
|
||||||
|
// Beta boards
|
||||||
|
#define BOARD_ESP01
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif
|
|
@ -0,0 +1,4 @@
|
||||||
|
mosquitto:
|
||||||
|
image: toke/mosquitto
|
||||||
|
ports:
|
||||||
|
- "1883:1883"
|
|
@ -0,0 +1,26 @@
|
||||||
|
import paho.mqtt.client as mqtt
|
||||||
|
import time
|
||||||
|
|
||||||
|
# The callback for when the client receives a CONNACK response from the server.
|
||||||
|
def on_connect(client, userdata, rc):
|
||||||
|
print("Connected with result code "+str(rc))
|
||||||
|
# Subscribing in on_connect() means that if we lose the connection and
|
||||||
|
# reconnect then subscriptions will be renewed.
|
||||||
|
client.subscribe("btn/status")
|
||||||
|
client.publish("main/status/penisy", "dupa")
|
||||||
|
|
||||||
|
# The callback for when a PUBLISH message is received from the server.
|
||||||
|
def on_message(client, userdata, msg):
|
||||||
|
print(str(time.time())+" "+msg.topic+" "+str(msg.payload))
|
||||||
|
|
||||||
|
client = mqtt.Client("test-client")
|
||||||
|
client.on_connect = on_connect
|
||||||
|
client.on_message = on_message
|
||||||
|
|
||||||
|
client.connect("localhost", 1883, 60)
|
||||||
|
|
||||||
|
# Blocking call that processes network traffic, dispatches callbacks and
|
||||||
|
# handles reconnecting.
|
||||||
|
# Other loop*() functions are available that give a threaded interface and a
|
||||||
|
# manual interface.
|
||||||
|
client.loop_forever()
|
|
@ -0,0 +1,24 @@
|
||||||
|
#####################################################################
|
||||||
|
#### Please don't change this file. Use Makefile-user.mk instead ####
|
||||||
|
#####################################################################
|
||||||
|
# Including user Makefile.
|
||||||
|
# Should be used to set project-specific parameters
|
||||||
|
include ./Makefile-user.mk
|
||||||
|
|
||||||
|
# Important parameters check.
|
||||||
|
# We need to make sure SMING_HOME and ESP_HOME variables are set.
|
||||||
|
# You can use Makefile-user.mk in each project or use enviromental variables to set it globally.
|
||||||
|
|
||||||
|
ifndef SMING_HOME
|
||||||
|
$(error SMING_HOME is not set. Please configure it in Makefile-user.mk)
|
||||||
|
endif
|
||||||
|
ifndef ESP_HOME
|
||||||
|
$(error ESP_HOME is not set. Please configure it in Makefile-user.mk)
|
||||||
|
endif
|
||||||
|
|
||||||
|
# Include main Sming Makefile
|
||||||
|
ifeq ($(RBOOT_ENABLED), 1)
|
||||||
|
include $(SMING_HOME)/Makefile-rboot.mk
|
||||||
|
else
|
||||||
|
include $(SMING_HOME)/Makefile-project.mk
|
||||||
|
endif
|
|
@ -0,0 +1,31 @@
|
||||||
|
## Local build configuration
|
||||||
|
## Parameters configured here will override default and ENV values.
|
||||||
|
## Uncomment and change examples:
|
||||||
|
|
||||||
|
#Add your source directories here separated by space
|
||||||
|
MODULES = app
|
||||||
|
DISABLE_SPIFFS = 1
|
||||||
|
USER_CFLAGS = -I../common
|
||||||
|
## ESP_HOME sets the path where ESP tools and SDK are located.
|
||||||
|
## Windows:
|
||||||
|
# ESP_HOME = c:/Espressif
|
||||||
|
|
||||||
|
## MacOS / Linux:
|
||||||
|
#ESP_HOME = /opt/esp-open-sdk
|
||||||
|
|
||||||
|
## SMING_HOME sets the path where Sming framework is located.
|
||||||
|
## Windows:
|
||||||
|
# SMING_HOME = c:/tools/sming/Sming
|
||||||
|
|
||||||
|
# MacOS / Linux
|
||||||
|
# SMING_HOME = /opt/sming/Sming
|
||||||
|
|
||||||
|
## COM port parameter is reqruied to flash firmware correctly.
|
||||||
|
## Windows:
|
||||||
|
# COM_PORT = COM3
|
||||||
|
|
||||||
|
# MacOS / Linux:
|
||||||
|
# COM_PORT = /dev/tty.usbserial
|
||||||
|
|
||||||
|
# Com port speed
|
||||||
|
# COM_SPEED = 115200
|
|
@ -0,0 +1,40 @@
|
||||||
|
#include <user_config.h>
|
||||||
|
#include <common_config.h>
|
||||||
|
#include <SmingCore/SmingCore.h>
|
||||||
|
|
||||||
|
MqttClient mqtt(MQTT_BROKER, MQTT_PORT, *[](String topic, String message) {
|
||||||
|
Serial.printf("*** message received @ %s:\n\t%s\n***\n", topic.c_str(), message.c_str());
|
||||||
|
});
|
||||||
|
|
||||||
|
void startMqttClient()
|
||||||
|
{
|
||||||
|
String deviceName = "switch-" + WifiStation.getMAC().substring(6, 12);
|
||||||
|
Serial.println("*** Connecting to MQTT as " + deviceName);
|
||||||
|
mqtt.connect(deviceName);
|
||||||
|
mqtt.subscribe("main/status/#");
|
||||||
|
}
|
||||||
|
|
||||||
|
void init()
|
||||||
|
{
|
||||||
|
Serial.begin(SERIAL_BAUD_RATE); // 115200 by default
|
||||||
|
Serial.systemDebugOutput(false); // Debug output to serial
|
||||||
|
|
||||||
|
Serial.println("*** Starting ...");
|
||||||
|
WifiStation.config(WIFI_SSID, WIFI_PWD);
|
||||||
|
WifiStation.enable(true);
|
||||||
|
|
||||||
|
WifiAccessPoint.enable(false);
|
||||||
|
|
||||||
|
WifiStation.waitConnection(*[] {
|
||||||
|
Serial.println("*** Connection succeeded");
|
||||||
|
startMqttClient();
|
||||||
|
}, 20, *[] {
|
||||||
|
Serial.println("*** Connection failed");
|
||||||
|
});
|
||||||
|
|
||||||
|
attachInterrupt(BTN_PIN, *[] {
|
||||||
|
bool btnState = digitalRead(BTN_PIN);
|
||||||
|
Serial.printf("*** Button state: %d\n", btnState);
|
||||||
|
mqtt.publish("btn/status", String(btnState));
|
||||||
|
}, CHANGE);
|
||||||
|
}
|
|
@ -0,0 +1,45 @@
|
||||||
|
#ifndef __USER_CONFIG_H__
|
||||||
|
#define __USER_CONFIG_H__
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// UART config
|
||||||
|
#define SERIAL_BAUD_RATE 115200
|
||||||
|
|
||||||
|
// ESP SDK config
|
||||||
|
#define LWIP_OPEN_SRC
|
||||||
|
#define USE_US_TIMER
|
||||||
|
|
||||||
|
// Default types
|
||||||
|
#define __CORRECT_ISO_CPP_STDLIB_H_PROTO
|
||||||
|
#include <limits.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
// Override c_types.h include and remove buggy espconn
|
||||||
|
#define _C_TYPES_H_
|
||||||
|
#define _NO_ESPCON_
|
||||||
|
|
||||||
|
// Updated, compatible version of c_types.h
|
||||||
|
// Just removed types declared in <stdint.h>
|
||||||
|
#include <espinc/c_types_compatible.h>
|
||||||
|
|
||||||
|
// System API declarations
|
||||||
|
#include <esp_systemapi.h>
|
||||||
|
|
||||||
|
// C++ Support
|
||||||
|
#include <esp_cplusplus.h>
|
||||||
|
// Extended string conversion for compatibility
|
||||||
|
#include <stringconversion.h>
|
||||||
|
// Network base API
|
||||||
|
#include <espinc/lwip_includes.h>
|
||||||
|
|
||||||
|
// Beta boards
|
||||||
|
#define BOARD_ESP01
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif
|
Loading…
Reference in New Issue