mysensors_registre_airchaud/registre_airchaud_code.ino

154 lines
3.8 KiB
Arduino
Raw Normal View History

#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
2023-12-10 12:47:03 +01:00
// #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
2023-12-10 12:47:03 +01:00
#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>
2023-12-10 12:47:03 +01:00
// 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
2023-12-10 12:47:03 +01:00
#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
2023-12-10 12:47:03 +01:00
MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
/*
control a 28BYJ-48 stepper motor with ULN2003 driver board
https://www.makerguides.com/28byj-48-stepper-motor-arduino-tutorial/
*/
#include "Stepper.h"
// Define number of steps per rotation:
const int stepsPerRevolution = 2048; // can be 4096
// ULN2003 Wiring:
// Pin 3 to IN1
// Pin 4 to IN2
// Pin 5 to IN3
// Pin 6 to IN4
// Create stepper object called 'myStepper', note the pin order:
Stepper myStepper = Stepper(stepsPerRevolution, 3, 5, 4, 6);
int prevDimValue=0;
int newDimValue=0;
char convBuffer[10];
2023-12-10 12:47:03 +01:00
float DTemp;
float old_DTemp;
void presentation()
{
#ifdef MY_DEBUG
Serial.println("- - - - - presentation - - - - -");
#endif
2023-12-10 12:47:03 +01:00
sendSketchInfo("Registre Air Chaud", "0.2");
present(ROTATION_CHILD_ID, S_DIMMER, "rotation");
2023-12-10 12:47:03 +01:00
present(CHILD_ID_TEMP, S_TEMP, "Temperature");
}
void setup() {
#ifdef MY_DEBUG
Serial.println("- - - - - setup - - - - -");
#endif
myStepper.setSpeed(5); // Set the speed to 5 rpm:
// todo make a homing of stepper motor OR find a way to sensorize where it is
send(dimmerMsg.set(prevDimValue));
send(lightMsg.set(1));
2023-12-10 12:47:03 +01:00
sensors.begin();
}
void loop() {
#ifdef MY_DEBUG
// Serial.println("- - - - - loop - - - - -");
#endif
2023-12-10 12:47:03 +01:00
sensors.requestTemperatures();
old_DTemp = DTemp;
DTemp = sensors.getTempCByIndex(0);
if (DTemp != old_DTemp && DTemp != -127)
{
send(msgTemp.set(DTemp, 1));
old_DTemp = DTemp;
}
delay(5000);
}
// 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
int quart = stepsPerRevolution / 4; // how many rotation do we want max (here 45° so from 0 to 512 steps)
int diff = prevDimValue - newDimValue; // get the difference between the previous and the new dimm value
prevDimValue = newDimValue; // record new dimm as previous dimm
int steps = (quart / 100) * diff; // convert diff on percent to steps
#ifdef MY_DEBUG
Serial.print("quart: ");
Serial.println(quart);
Serial.print("diff: ");
Serial.println(diff);
Serial.print("steps: ");
Serial.println(steps);
#endif
myStepper.step(steps); // move the stepper
// todo release the stepper motor to free power consumption
}
}