204 lines
5.0 KiB
C++
204 lines
5.0 KiB
C++
#define MY_DEBUG
|
|
// Enable and select radio type attached
|
|
#define MY_RADIO_RF24
|
|
// #define MY_RF24_PA_LEVEL RF24_PA_MIN
|
|
// #define MY_RF24_PA_LEVEL RF24_PA_LOW
|
|
// #define MY_RF24_PA_LEVEL RF24_PA_HIGH
|
|
#define MY_RF24_PA_LEVEL RF24_PA_MAX
|
|
|
|
#define MY_RF24_CHANNEL 14
|
|
|
|
#define MY_REPEATER_FEATURE
|
|
|
|
#define MY_DEFAULT_RX_LED_PIN A0
|
|
#define MY_DEFAULT_TX_LED_PIN A1
|
|
#define MY_DEFAULT_ERR_LED_PIN A2
|
|
|
|
#include <SPI.h>
|
|
#include <MySensors.h>
|
|
|
|
// https://lastminuteengineers.com/multiple-ds18b20-arduino-tutorial/
|
|
#include <OneWire.h>
|
|
#include <DallasTemperature.h>
|
|
|
|
// Data wire is plugged into port 11 on the Arduino
|
|
#define ONE_WIRE_BUS 7
|
|
// Setup a oneWire instance to communicate with any OneWire devices
|
|
OneWire oneWire(ONE_WIRE_BUS);
|
|
|
|
// Pass our oneWire reference to Dallas Temperature.
|
|
DallasTemperature sensors(&oneWire);
|
|
|
|
#define ROTATION_CHILD_ID 0
|
|
#define CHILD_ID_TEMP 1
|
|
|
|
MyMessage dimmerMsg(ROTATION_CHILD_ID, V_DIMMER);
|
|
MyMessage lightMsg(ROTATION_CHILD_ID, V_LIGHT); // we need init light message to make the dimmer appear in HA
|
|
MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
|
|
|
|
// https://lastminuteengineers.com/28byj48-stepper-motor-arduino-tutorial/
|
|
#include "AccelStepper.h"
|
|
|
|
|
|
// ULN2003 Wiring:
|
|
const int In1 = 3;
|
|
const int In2 = 4;
|
|
const int In3 = 5;
|
|
const int In4 = 6;
|
|
|
|
// Create stepper object called 'myStepper', note the pin order:
|
|
AccelStepper myStepper(4, In1, In3, In2, In4);
|
|
|
|
int prevDimValue=0;
|
|
int newDimValue=0;
|
|
char convBuffer[10];
|
|
const int debattement = 2038 * 5;
|
|
|
|
bool homed=false;
|
|
// atmega328p pin 26 -> pc3 -> arduino A3;
|
|
const int homing_pin = A3;
|
|
int stopCourseBtnState = 0;
|
|
|
|
unsigned long previousMillis = 0;
|
|
const long tempInterval = 60000;
|
|
float DTemp;
|
|
float old_DTemp;
|
|
|
|
|
|
void presentation()
|
|
{
|
|
#ifdef MY_DEBUG
|
|
Serial.println("- - - - - presentation - - - - -");
|
|
#endif
|
|
|
|
sendSketchInfo("Registre Air Chaud", "0.3");
|
|
|
|
present(ROTATION_CHILD_ID, S_DIMMER, "rotation");
|
|
present(CHILD_ID_TEMP, S_TEMP, "Temperature");
|
|
}
|
|
|
|
void setup() {
|
|
#ifdef MY_DEBUG
|
|
Serial.println("- - - - - setup - - - - -");
|
|
#endif
|
|
|
|
myStepper.setMaxSpeed(1000.0);
|
|
myStepper.setAcceleration(50.0);
|
|
myStepper.setSpeed(200);
|
|
// myStepper.moveTo(2038);
|
|
// todo make a homing of stepper motor OR find a way to sensorize where it is
|
|
// myStepper.setCurrentPosition(0);
|
|
|
|
pinMode(homing_pin, INPUT);
|
|
digitalWrite(homing_pin, HIGH);
|
|
|
|
// send(dimmerMsg.set(prevDimValue));
|
|
// send(lightMsg.set(1));
|
|
|
|
sensors.begin();
|
|
}
|
|
|
|
void loop() {
|
|
#ifdef MY_DEBUG
|
|
// Serial.println("- - - - - loop - - - - -");
|
|
#endif
|
|
|
|
unsigned long currentMillis = millis();
|
|
// do measurement every one minute
|
|
if (currentMillis - previousMillis >= tempInterval)
|
|
{
|
|
previousMillis = currentMillis;
|
|
sensors.requestTemperatures();
|
|
old_DTemp = DTemp;
|
|
DTemp = sensors.getTempCByIndex(0);
|
|
if (DTemp != old_DTemp && DTemp != -127)
|
|
{
|
|
send(msgTemp.set(DTemp, 1));
|
|
old_DTemp = DTemp;
|
|
}
|
|
}
|
|
|
|
// myStepper.moveTo(myStepper.currentPosition() + 2048);
|
|
|
|
// delay(5000);
|
|
|
|
// Change direction once the motor reaches target position
|
|
// if (myStepper.distanceToGo() == 0)
|
|
// myStepper.moveTo(-myStepper.currentPosition());
|
|
|
|
|
|
// stopCourseBtnState = digitalRead(homing_pin);
|
|
// if (stopCourseBtnState == HIGH){
|
|
// Serial.print('stopCourseBtnState HIGH');
|
|
// }
|
|
|
|
if (!homed)
|
|
{
|
|
myStepper.setSpeed(200);
|
|
if (myStepper.distanceToGo() == 0) {
|
|
myStepper.moveTo(myStepper.currentPosition() + 2038); // turn clockwise to go up
|
|
}
|
|
stopCourseBtnState = digitalRead(homing_pin);
|
|
if (stopCourseBtnState == HIGH){
|
|
Serial.print("stopCourseBtnState is HIGH");
|
|
// stepper
|
|
myStepper.stop();
|
|
myStepper.setCurrentPosition(0);
|
|
// HA
|
|
send(dimmerMsg.set(prevDimValue));
|
|
send(lightMsg.set(1));
|
|
|
|
homed=true;
|
|
}
|
|
}
|
|
|
|
// Move the motor one step
|
|
if(!myStepper.run()){
|
|
// release the stepper motor to free power consumption
|
|
digitalWrite(In1, 0);
|
|
digitalWrite(In2, 0);
|
|
digitalWrite(In3, 0);
|
|
digitalWrite(In4, 0);
|
|
}
|
|
}
|
|
|
|
// Handle incoming messages
|
|
void receive(const MyMessage &message) {
|
|
|
|
#ifdef MY_DEBUG
|
|
Serial.println("- - - - - receive - - - - -");
|
|
Serial.print("Last reading for sensor: ");
|
|
#endif
|
|
|
|
if (message.type == V_LIGHT) {
|
|
|
|
#ifdef MY_DEBUG
|
|
Serial.println("- - - - - receive V_LIGHT - - - - -");
|
|
#endif
|
|
|
|
} else if (message.type == V_DIMMER) {
|
|
|
|
#ifdef MY_DEBUG
|
|
Serial.println("- - - - - receive V_DIMMER - - - - -");
|
|
#endif
|
|
|
|
newDimValue = atoi(message.getString(convBuffer));
|
|
|
|
#ifdef MY_DEBUG
|
|
Serial.print("prevDimValue: ");
|
|
Serial.println(prevDimValue);
|
|
Serial.print("newDimValue: ");
|
|
Serial.println(newDimValue);
|
|
#endif
|
|
|
|
if (homed)
|
|
{
|
|
// todo instead of 1 revolution (2038) define how much revolution we want to do for full opening (100% of the dimmer in HA)
|
|
myStepper.setSpeed(400);
|
|
int pos = (debattement / 100) * newDimValue * -1; // turn counter clock wise to go done
|
|
myStepper.moveTo(pos); // move the stepper
|
|
|
|
}
|
|
|
|
}
|
|
} |