commit e22cc17e3f81347edca0ef48434d945881963e3a Author: Piotr Dobrowolski Date: Sun Dec 27 00:29:30 2015 +0100 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f957ba3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*/out/* +*.swp +*.py[co] diff --git a/README.md b/README.md new file mode 100644 index 0000000..76ff1e9 --- /dev/null +++ b/README.md @@ -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. diff --git a/common/common_config.h b/common/common_config.h new file mode 100644 index 0000000..4b5d0ac --- /dev/null +++ b/common/common_config.h @@ -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 diff --git a/light/Makefile b/light/Makefile new file mode 100644 index 0000000..16d76cd --- /dev/null +++ b/light/Makefile @@ -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 diff --git a/light/Makefile-user.mk b/light/Makefile-user.mk new file mode 100644 index 0000000..46f2ec6 --- /dev/null +++ b/light/Makefile-user.mk @@ -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 diff --git a/light/app/application.cpp b/light/app/application.cpp new file mode 100644 index 0000000..766ac4d --- /dev/null +++ b/light/app/application.cpp @@ -0,0 +1,37 @@ +#include +#include +#include + +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); +} diff --git a/light/include/user_config.h b/light/include/user_config.h new file mode 100644 index 0000000..1c6ac36 --- /dev/null +++ b/light/include/user_config.h @@ -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 + #include + + // 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 + #include + + // System API declarations + #include + + // C++ Support + #include + // Extended string conversion for compatibility + #include + // Network base API + #include + + // Beta boards + #define BOARD_ESP01 + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/master/docker-compose.yml b/master/docker-compose.yml new file mode 100644 index 0000000..175025f --- /dev/null +++ b/master/docker-compose.yml @@ -0,0 +1,4 @@ +mosquitto: + image: toke/mosquitto + ports: + - "1883:1883" diff --git a/master/test-client.py b/master/test-client.py new file mode 100644 index 0000000..6f52bb8 --- /dev/null +++ b/master/test-client.py @@ -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() diff --git a/switch/Makefile b/switch/Makefile new file mode 100644 index 0000000..16d76cd --- /dev/null +++ b/switch/Makefile @@ -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 diff --git a/switch/Makefile-user.mk b/switch/Makefile-user.mk new file mode 100644 index 0000000..46f2ec6 --- /dev/null +++ b/switch/Makefile-user.mk @@ -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 diff --git a/switch/app/application.cpp b/switch/app/application.cpp new file mode 100644 index 0000000..a8a4a9f --- /dev/null +++ b/switch/app/application.cpp @@ -0,0 +1,40 @@ +#include +#include +#include + +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); +} diff --git a/switch/include/user_config.h b/switch/include/user_config.h new file mode 100644 index 0000000..1c6ac36 --- /dev/null +++ b/switch/include/user_config.h @@ -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 + #include + + // 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 + #include + + // System API declarations + #include + + // C++ Support + #include + // Extended string conversion for compatibility + #include + // Network base API + #include + + // Beta boards + #define BOARD_ESP01 + +#ifdef __cplusplus +} +#endif + +#endif