mirror of
https://gitlab.com/fabinfra/fabhardware/fablock.git
synced 2025-03-12 22:51:45 +01:00
Init
This commit is contained in:
parent
9dee1bc689
commit
b70c90529e
7
FabLock/.gitignore
vendored
Normal file
7
FabLock/.gitignore
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
.pio
|
||||
.vscode/.browse.c_cpp.db*
|
||||
.vscode/c_cpp_properties.json
|
||||
.vscode/launch.json
|
||||
.vscode/ipch
|
||||
|
||||
src/config.h
|
10
FabLock/.vscode/extensions.json
vendored
Normal file
10
FabLock/.vscode/extensions.json
vendored
Normal file
@ -0,0 +1,10 @@
|
||||
{
|
||||
// See http://go.microsoft.com/fwlink/?LinkId=827846
|
||||
// for the documentation about the extensions.json format
|
||||
"recommendations": [
|
||||
"platformio.platformio-ide"
|
||||
],
|
||||
"unwantedRecommendations": [
|
||||
"ms-vscode.cpptools-extension-pack"
|
||||
]
|
||||
}
|
12
FabLock/.vscode/settings.json
vendored
Normal file
12
FabLock/.vscode/settings.json
vendored
Normal file
@ -0,0 +1,12 @@
|
||||
{
|
||||
"files.associations": {
|
||||
"optional": "cpp",
|
||||
"ostream": "cpp",
|
||||
"system_error": "cpp",
|
||||
"array": "cpp",
|
||||
"functional": "cpp",
|
||||
"tuple": "cpp",
|
||||
"type_traits": "cpp",
|
||||
"utility": "cpp"
|
||||
}
|
||||
}
|
39
FabLock/include/README
Normal file
39
FabLock/include/README
Normal file
@ -0,0 +1,39 @@
|
||||
|
||||
This directory is intended for project header files.
|
||||
|
||||
A header file is a file containing C declarations and macro definitions
|
||||
to be shared between several project source files. You request the use of a
|
||||
header file in your project source file (C, C++, etc) located in `src` folder
|
||||
by including it, with the C preprocessing directive `#include'.
|
||||
|
||||
```src/main.c
|
||||
|
||||
#include "header.h"
|
||||
|
||||
int main (void)
|
||||
{
|
||||
...
|
||||
}
|
||||
```
|
||||
|
||||
Including a header file produces the same results as copying the header file
|
||||
into each source file that needs it. Such copying would be time-consuming
|
||||
and error-prone. With a header file, the related declarations appear
|
||||
in only one place. If they need to be changed, they can be changed in one
|
||||
place, and programs that include the header file will automatically use the
|
||||
new version when next recompiled. The header file eliminates the labor of
|
||||
finding and changing all the copies as well as the risk that a failure to
|
||||
find one copy will result in inconsistencies within a program.
|
||||
|
||||
In C, the usual convention is to give header files names that end with `.h'.
|
||||
It is most portable to use only letters, digits, dashes, and underscores in
|
||||
header file names, and at most one dot.
|
||||
|
||||
Read more about using header files in official GCC documentation:
|
||||
|
||||
* Include Syntax
|
||||
* Include Operation
|
||||
* Once-Only Headers
|
||||
* Computed Includes
|
||||
|
||||
https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html
|
46
FabLock/lib/README
Normal file
46
FabLock/lib/README
Normal file
@ -0,0 +1,46 @@
|
||||
|
||||
This directory is intended for project specific (private) libraries.
|
||||
PlatformIO will compile them to static libraries and link into executable file.
|
||||
|
||||
The source code of each library should be placed in a an own separate directory
|
||||
("lib/your_library_name/[here are source files]").
|
||||
|
||||
For example, see a structure of the following two libraries `Foo` and `Bar`:
|
||||
|
||||
|--lib
|
||||
| |
|
||||
| |--Bar
|
||||
| | |--docs
|
||||
| | |--examples
|
||||
| | |--src
|
||||
| | |- Bar.c
|
||||
| | |- Bar.h
|
||||
| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html
|
||||
| |
|
||||
| |--Foo
|
||||
| | |- Foo.c
|
||||
| | |- Foo.h
|
||||
| |
|
||||
| |- README --> THIS FILE
|
||||
|
|
||||
|- platformio.ini
|
||||
|--src
|
||||
|- main.c
|
||||
|
||||
and a contents of `src/main.c`:
|
||||
```
|
||||
#include <Foo.h>
|
||||
#include <Bar.h>
|
||||
|
||||
int main (void)
|
||||
{
|
||||
...
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
PlatformIO Library Dependency Finder will find automatically dependent
|
||||
libraries scanning project source files.
|
||||
|
||||
More information about PlatformIO Library Dependency Finder
|
||||
- https://docs.platformio.org/page/librarymanager/ldf.html
|
18
FabLock/platformio.ini
Normal file
18
FabLock/platformio.ini
Normal file
@ -0,0 +1,18 @@
|
||||
; PlatformIO Project Configuration File
|
||||
;
|
||||
; Build options: build flags, source filter
|
||||
; Upload options: custom upload port, speed and extra flags
|
||||
; Library options: dependencies, extra library storages
|
||||
; Advanced options: extra scripting
|
||||
;
|
||||
; Please visit documentation for the other options and examples
|
||||
; https://docs.platformio.org/page/projectconf.html
|
||||
|
||||
[env:esp12e]
|
||||
platform = espressif8266
|
||||
board = esp12e
|
||||
framework = arduino
|
||||
lib_deps =
|
||||
knolleary/PubSubClient@^2.8
|
||||
adafruit/Adafruit MCP23017 Arduino Library@^2.1.0
|
||||
monitor_speed = 115200
|
7
FabLock/src/config_example.h
Normal file
7
FabLock/src/config_example.h
Normal file
@ -0,0 +1,7 @@
|
||||
#define WLAN_SSID ""
|
||||
#define WLAN_PASS ""
|
||||
#define MQTT_BROKER ""
|
||||
#define FABLOCKID 1
|
||||
#define MPC_RELAIS_ADDR 0x02
|
||||
#define HAS_FEEDBACK 1
|
||||
#define MPC_FEEDBACK_ADDR 0x03
|
184
FabLock/src/main.cpp
Normal file
184
FabLock/src/main.cpp
Normal file
@ -0,0 +1,184 @@
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <PubSubClient.h>
|
||||
#include <Adafruit_MCP23X17.h>
|
||||
|
||||
#include "config.h"
|
||||
#define MCP_INPUTS 16 // MCP23017
|
||||
#define TRIGGER_TIME 1500
|
||||
|
||||
WiFiClient espClient;
|
||||
PubSubClient client(espClient);
|
||||
Adafruit_MCP23X17 mcp_relais;
|
||||
Adafruit_MCP23X17 mcp_feedback;
|
||||
|
||||
unsigned long lastMsg = 0;
|
||||
#define MSG_BUFFER_SIZE (50)
|
||||
char msg[MSG_BUFFER_SIZE];
|
||||
|
||||
#define TOPIC_BUFFER_SIZE (50)
|
||||
char topic_recv[TOPIC_BUFFER_SIZE];
|
||||
char topic_send[TOPIC_BUFFER_SIZE];
|
||||
|
||||
void setup_wifi()
|
||||
{
|
||||
delay(10);
|
||||
|
||||
Serial.println();
|
||||
Serial.print("Connecting to ");
|
||||
Serial.println(WLAN_SSID);
|
||||
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.begin(WLAN_SSID, WLAN_PASS);
|
||||
|
||||
while (WiFi.status() != WL_CONNECTED)
|
||||
{
|
||||
delay(500);
|
||||
Serial.print(".");
|
||||
}
|
||||
|
||||
randomSeed(micros());
|
||||
|
||||
Serial.println("");
|
||||
Serial.println("WiFi connected");
|
||||
Serial.println("IP address: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
}
|
||||
|
||||
void callback(char* topic, byte* payload, unsigned int length)
|
||||
{
|
||||
Serial.println("Receive message");
|
||||
|
||||
int topic_len = (int)strlen(topic);
|
||||
int trigger_len = (int)strlen("/trigger");
|
||||
int set_state_len = (int)strlen("/set_state");
|
||||
if(strcmp(topic + topic_len - trigger_len, "/trigger"))
|
||||
{
|
||||
char str_id[4] = {0};
|
||||
strncpy(str_id, topic + topic_len - trigger_len - 3, 3);
|
||||
int id = atoi(str_id);
|
||||
|
||||
if ((char)payload[0] == '1')
|
||||
{
|
||||
mcp_relais.digitalWrite(id, HIGH);
|
||||
delay(TRIGGER_TIME);
|
||||
mcp_relais.digitalWrite(id, LOW);
|
||||
}
|
||||
else
|
||||
{
|
||||
mcp_relais.digitalWrite(id, LOW);
|
||||
delay(TRIGGER_TIME);
|
||||
mcp_relais.digitalWrite(id, HIGH);
|
||||
}
|
||||
}
|
||||
|
||||
if(strcmp(topic + topic_len - set_state_len, "/set_state"))
|
||||
{
|
||||
char str_id[4] = {0};
|
||||
strncpy(str_id, topic + topic_len - trigger_len - 3, 3);
|
||||
int id = atoi(str_id);
|
||||
|
||||
if ((char)payload[0] == '1')
|
||||
{
|
||||
mcp_relais.digitalWrite(id, HIGH);
|
||||
}
|
||||
else
|
||||
{
|
||||
mcp_relais.digitalWrite(id, LOW);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void reconnect()
|
||||
{
|
||||
while (!client.connected())
|
||||
{
|
||||
Serial.print("Attempting MQTT connection...");
|
||||
String clientId = "FABLOCK-";
|
||||
clientId += String(random(0xffff), HEX);
|
||||
if (client.connect(clientId.c_str()))
|
||||
{
|
||||
snprintf (topic_recv, TOPIC_BUFFER_SIZE, "fablock/%03d/+/trigger", FABLOCKID);
|
||||
client.subscribe(topic_recv);
|
||||
snprintf (topic_recv, TOPIC_BUFFER_SIZE, "fablock/%03d/+/set_state", FABLOCKID);
|
||||
client.subscribe(topic_recv);
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.print("failed, rc=");
|
||||
Serial.print(client.state());
|
||||
Serial.println(" try again in 5 seconds");
|
||||
delay(5000);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void setup()
|
||||
{
|
||||
int i = 0;
|
||||
|
||||
pinMode(BUILTIN_LED, OUTPUT);
|
||||
Serial.begin(115200);
|
||||
setup_wifi();
|
||||
|
||||
client.setServer(MQTT_BROKER, 1883);
|
||||
client.setCallback(callback);
|
||||
|
||||
if (!mcp_relais.begin_I2C(MPC_RELAIS_ADDR))
|
||||
{
|
||||
Serial.println("Error.");
|
||||
while (1);
|
||||
}
|
||||
for(i = 0; i++; i<MCP_INPUTS)
|
||||
{
|
||||
mcp_feedback.pinMode(i, OUTPUT);
|
||||
}
|
||||
|
||||
if(HAS_FEEDBACK)
|
||||
{
|
||||
if (!mcp_feedback.begin_I2C(MPC_FEEDBACK_ADDR))
|
||||
{
|
||||
Serial.println("Error.");
|
||||
while (1);
|
||||
}
|
||||
for(i = 0; i++; i<MCP_INPUTS)
|
||||
{
|
||||
mcp_feedback.pinMode(i, INPUT_PULLUP);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
if (!client.connected())
|
||||
{
|
||||
reconnect();
|
||||
}
|
||||
client.loop();
|
||||
|
||||
unsigned long now = millis();
|
||||
if (now - lastMsg > 30000)
|
||||
{
|
||||
lastMsg = now;
|
||||
int value;
|
||||
|
||||
Serial.print("Publish State and Feedback");
|
||||
for(int i = 0; i++; i<MCP_INPUTS)
|
||||
{
|
||||
value = mcp_relais.digitalRead(i);
|
||||
snprintf (msg, MSG_BUFFER_SIZE, "%ld", value);
|
||||
snprintf (topic_send, TOPIC_BUFFER_SIZE, "fablock/%03d/%03d/state", FABLOCKID, i);
|
||||
client.publish(topic_send, msg);
|
||||
}
|
||||
|
||||
if(HAS_FEEDBACK)
|
||||
{
|
||||
for(int i = 0; i++; i<MCP_INPUTS)
|
||||
{
|
||||
value = mcp_feedback.digitalRead(i);
|
||||
snprintf (msg, MSG_BUFFER_SIZE, "%ld", value);
|
||||
snprintf (topic_send, TOPIC_BUFFER_SIZE, "fablock/%03d/%03d/feedback", FABLOCKID, i);
|
||||
client.publish(topic_send, msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
11
FabLock/test/README
Normal file
11
FabLock/test/README
Normal file
@ -0,0 +1,11 @@
|
||||
|
||||
This directory is intended for PlatformIO Test Runner and project tests.
|
||||
|
||||
Unit Testing is a software testing method by which individual units of
|
||||
source code, sets of one or more MCU program modules together with associated
|
||||
control data, usage procedures, and operating procedures, are tested to
|
||||
determine whether they are fit for use. Unit testing finds problems early
|
||||
in the development cycle.
|
||||
|
||||
More information about PlatformIO Unit Testing:
|
||||
- https://docs.platformio.org/en/latest/advanced/unit-testing/index.html
|
Loading…
x
Reference in New Issue
Block a user