This commit is contained in:
Paul Zinselmeyer 2022-05-21 12:05:40 +02:00
commit b1be6b712b
Signed by: pfzetto
GPG Key ID: 4EEF46A5B276E648
9 changed files with 392 additions and 0 deletions

6
.gitignore vendored Executable file
View File

@ -0,0 +1,6 @@
.pio
.vscode/.browse.c_cpp.db*
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch
configuration.h

10
.vscode/extensions.json vendored Executable file
View 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"
]
}

5
.vscode/settings.json vendored Executable file
View File

@ -0,0 +1,5 @@
{
"files.associations": {
"*.d": "cpp"
}
}

39
include/README Executable file
View 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
lib/README Executable file
View 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

17
platformio.ini Executable file
View File

@ -0,0 +1,17 @@
; 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:nodemcuv2]
platform = espressif8266
board = nodemcuv2
framework = arduino
lib_deps =
waspinator/AccelStepper@^1.61
knolleary/PubSubClient@^2.8

94
src/calibration.cpp Executable file
View File

@ -0,0 +1,94 @@
#include <Arduino.h>
#include <AccelStepper.h>
const int stepperPinDefinitions[][3] = {{D0,D1,D2}, {D3,D4,D5}}; //Enable, Step, Dir
AccelStepper stepper[sizeof(stepperPinDefinitions)];
int microsteps = 8;
void setup() {
for(int i = 0; i<sizeof(stepperPinDefinitions);i++){
stepper[i] = AccelStepper(AccelStepper::FULL2WIRE, stepperPinDefinitions[i][1], stepperPinDefinitions[i][2]);
pinMode(stepperPinDefinitions[i][0], OUTPUT);
digitalWrite(stepperPinDefinitions[i][0], LOW);
stepper[i].setMaxSpeed(5000);
stepper[i].setAcceleration(5000);
}
Serial.begin(9600);
}
void loop() {
if(Serial.available()>0){
char c = (char)Serial.read();
switch(c){
case 'q':
stepper[0].moveTo(stepper[0].targetPosition()+50*microsteps);
break;
case 'a':
stepper[0].moveTo(stepper[0].targetPosition()-50*microsteps);
break;
case 'w':
stepper[0].moveTo(stepper[0].targetPosition()+200*microsteps);
break;
case 's':
stepper[0].moveTo(stepper[0].targetPosition()-200*microsteps);
break;
case 'e':
stepper[0].moveTo(stepper[0].targetPosition()+1200*microsteps);
break;
case 'd':
stepper[0].moveTo(stepper[0].targetPosition()-1200*microsteps);
break;
case 'r':
stepper[1].moveTo(stepper[0].targetPosition()+50*microsteps);
break;
case 'f':
stepper[1].moveTo(stepper[0].targetPosition()-50*microsteps);
break;
case 't':
stepper[1].moveTo(stepper[0].targetPosition()+200*microsteps);
break;
case 'g':
stepper[1].moveTo(stepper[0].targetPosition()-200*microsteps);
break;
case 'z':
stepper[1].moveTo(stepper[0].targetPosition()+1200*microsteps);
break;
case 'h':
stepper[1].moveTo(stepper[0].targetPosition()-1200*microsteps);
break;
case 'y':
stepper[0].setCurrentPosition(0);
break;
case 'x':
stepper[1].setCurrentPosition(0);
break;
case '1':
digitalWrite(stepperPinDefinitions[0][0], LOW);
break;
case '2':
digitalWrite(stepperPinDefinitions[0][0], HIGH);
break;
case '3':
digitalWrite(stepperPinDefinitions[1][0], LOW);
break;
case '4':
digitalWrite(stepperPinDefinitions[1][0], HIGH);
break;
}
Serial.println(stepper[0].targetPosition());
}
for(int i = 0; i<sizeof(stepperPinDefinitions);i++){
stepper[i].run();
}
}

164
src/main.cpp Executable file
View File

@ -0,0 +1,164 @@
#include <Arduino.h>
#include <AccelStepper.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <configuration.h>
AccelStepper stepper[sizeof(stepperPinDefinitions)];
int stepperStatus[sizeof(stepperPinDefinitions)];
/*
* 0: Homing Queued
* 1: Homing Moving Down
* 3: Homing Waiting For Drop
* 2: Regular Operation
*/
int targetPosition[sizeof(stepperPinDefinitions)];
int targetTransparency[sizeof(stepperPinDefinitions)];
WiFiClient espClient;
PubSubClient client(espClient);
void logText(const char* message);
void connectWifi();
void connectMqtt();
void mqttCallback(char* topic, byte* payload, unsigned int length);
void setPositionAndTransparency(int position, int stepper, int transparency);
void setPosition(int stepper, int position);
void setTransparency(int stepper, int transparency);
int getPosition(int stepper);
int getTransparency(int stepper);
void setup() {
for(int i = 0; i<sizeof(stepperPinDefinitions);i++){
stepper[i] = AccelStepper(AccelStepper::FULL2WIRE, stepperPinDefinitions[i][1], stepperPinDefinitions[i][2]);
pinMode(stepperPinDefinitions[i][0], OUTPUT);
digitalWrite(stepperPinDefinitions[i][0], LOW);
stepper[i].setMaxSpeed(5000);
stepper[i].setAcceleration(1000);
stepperStatus[i]=0;
}
Serial.begin(9600);
Serial.println("[WIFI] Starting...");
Serial.print("[WIFI] Connecting to ");
Serial.println(WIFI_SSID);
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASS);
connectWifi();
client.setServer(MQTT_BROKER, MQTT_PORT);
client.setCallback(mqttCallback);
connectMqtt();
}
void loop() {
if(WiFi.status() != WL_CONNECTED){
connectWifi();
}
if (!client.connected()) {
connectMqtt();
}
client.loop();
for(int i = 0; i < sizeof(stepper); i++){
stepper[i].run();
}
}
void logText(const char* message){
Serial.println(message);
client.publish(("tele/"+String(MQTT_NAME)).c_str(), message);
}
void connectWifi(){
Serial.print("[WIFI] Connecting");
while(WiFi.status() != WL_CONNECTED){
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("[WIFI] Connected to WiFi. IP: ");
Serial.println(WiFi.localIP());
}
void connectMqtt(){
Serial.print("[MQTT] Connecting");
while (!client.connected()) {
if (!client.connect(MQTT_NAME, MQTT_USER, MQTT_PASS)) {
Serial.print("(failed, rc=");
Serial.print(client.state());
Serial.print(")");
delay(5000);
}
}
Serial.println("");
for(int i = 0; i < sizeof(stepper); i++){
client.subscribe(("cmnd/"+String(MQTT_NAME)+"/"+String(i)+"/position").c_str());
client.subscribe(("cmnd/"+String(MQTT_NAME)+"/"+String(i)+"/transparency").c_str());
client.subscribe(("cmnd/"+String(MQTT_NAME)+"/"+String(i)+"/command").c_str());
client.subscribe(("cmnd/"+String(MQTT_NAME)+"/"+String(i)+"/steps").c_str());
}
}
void mqttCallback(char* topic, byte* payload, unsigned int length){
String msg = "";
for (int i = 0; i < length; i++) {
msg += (char)payload[i];
}
Serial.print("[MQTT] Received message on ");
Serial.print(topic);
Serial.print(": ");
Serial.println(msg);
for(int i = 0; i < sizeof(stepper); i++){
if(String(topic).equals(("cmnd/"+String(MQTT_NAME)+"/"+String(i)+"/steps").c_str())){
logText(("[MOVE] Moving "+msg+" Steps on Stepper "+String(i)+". New Position: "+String(stepper[i].targetPosition())).c_str());
stepper[i].move(msg.toInt());
}else if(String(topic).equals(("cmnd/"+String(MQTT_NAME)+"/command").c_str())){
if(msg.equals("OPEN")){
setPositionAndTransparency(i, 100, 0);
}else if(msg.equals("CLOSE")){
setPositionAndTransparency(i, 0, 0);
}else if(msg.equals("STOP")){
stepper[i].moveTo(stepper[i].currentPosition());
}
}else if(String(topic).equals(("cmnd/"+String(MQTT_NAME)+"/position").c_str())){
setPosition(i, msg.toInt());
}else if(String(topic).equals(("cmnd/"+String(MQTT_NAME)+"/transparency").c_str())){
setTransparency(i, msg.toInt());
}
}
}
void updatePositionAndTransparency(int stepperId){
int position = targetPosition[stepperId];
int transparency = targetTransparency[stepperId];
int closedPosition = map(position, 0, 100, 0, sizeof(stepperPinDefinitions)-1);
int openPosA = closedPosition;
int openPosB = closedPosition+1>sizeof(stepperPinDefinitions)-1?closedPosition-1:closedPosition+1;
int openPosSteps = map(transparency, 0, 100, 0, abs(openPosA-openPosB));
int targetStep = curtainClosedSteps[stepperId][closedPosition]+openPosSteps;
stepper[stepperId].moveTo(targetStep);
}
void setPosition(int stepperId, int position){
targetPosition[stepperId]=position;
updatePositionAndTransparency(stepperId);
}
void setTransparency(int stepperId, int transparency){
targetTransparency[stepperId]=transparency;
updatePositionAndTransparency(stepperId);
}

11
test/README Executable file
View File

@ -0,0 +1,11 @@
This directory is intended for PlatformIO Unit Testing 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/page/plus/unit-testing.html