Moin,
das Projekt ist zwar schon ziemlich weit, allerdings hab ich beim letzten wichtigen Schritt ein paar Probleme.
Kurze Erklärung:
Es geht um einen Rolladen ( Modell ) der mit Stepper auf und zu gemacht wird. Er bewegt sich über eingaben mit Taster (auf/zu oder auf eine Wunsch Position)
Inhalte die bereits laufen sind:
-Steuerung des Steppers ( Nano)
-Daten Senden Empfangen (Wemos D1 mini)
-Nano sendet Daten an Wemos über I2C-Bus
-Es wird eine Logdatei im SD-Modul geschrieben
-Die RLC Uhr wird auch ausgelesen
Was mir noch fehlt, ist das die per WiFi empfangen Daten des Wemos über den I2C Bus an den Nano gesendet werden um sie in Bewegung am Stepper umzusetzen.
Hat jemand ne Idee oder findet ihr den Fehler in meinem Code?
Hoffe der Post passt soweit
Ich schreibe den Code im VS Code, deshalb die Arduino.h bib.
Master:
//______Master-CONTROLLER__________
#include <Arduino.h>
#include <Wire.h>
//_____________Variablen________________
#define Pin_SDA D2
#define Pin_SCL D1
const int16_t I2C_MASTER = 0x42; // optional
const int16_t I2C_SLAVE = 0x08;
unsigned long print_last_Time;
unsigned long print_wait_Time = 5000;
unsigned long I2C_last_Time;
unsigned long I2C_wait_Time = 1000;
String Modus;
String Rolladen;
int Rolladen_Werte[5];
////_____________Funktionen________________
void setup() {
Wire.begin(Pin_SDA, Pin_SCL, I2C_MASTER);
Serial.begin(9600);
}
int I2C_Info;
uint8_t I2C_werte_senden[2];
void loop() {
I2C_werte_senden[0] = 1;
I2C_werte_senden[1] = 74;
//_____________Rolladen Werte vom Slave empfangen______________
if ( millis() > ( I2C_last_Time + I2C_wait_Time) ) {
Wire.requestFrom(I2C_SLAVE, 8,true);
int index = 0;
while ( Wire.available() ) {
Rolladen_Werte[index] = Wire.read();
index ++;
}
I2C_last_Time = millis();
Wire.begin(I2C_SLAVE);
Wire.write(I2C_werte_senden,8);
I2C_Info = Wire.endTransmission();
}
if (Rolladen_Werte[0] == 1 && Rolladen_Werte[1] == 0 && Rolladen_Werte[2] == 0 ) {
Modus = "Automatisch";
}
if (Rolladen_Werte[0] == 0 && Rolladen_Werte[1] == 1 && Rolladen_Werte[2] == 0 ) {
Modus = "Manuell";
}
if (Rolladen_Werte[0] == 0 && Rolladen_Werte[1] == 0 && Rolladen_Werte[2] == 1 ) {
Modus = "Free";
}
//_____________Serieller Monitor________________
if (millis() > (print_last_Time + print_wait_Time) ) {
Serial.println("--------------");
Serial.print("Modus: ");
Serial.println(Modus);
Serial.print("Oeffnung(%): ");
Serial.println(Rolladen_Werte[3]);
Serial.print("Lux: ");
Serial.println(Rolladen_Werte[4]);
Serial.print("I2C Info: ");
Serial.println(I2C_Info);
Serial.println("--------------");
print_last_Time = millis();
}
}
Slave:
//______Slave-CONTROLLER__________
#include <Arduino.h>
#include <Wire.h>
#include <PaunaStepper.h>
#include <Adafruit_TSL2591.h>
#include <SPI.h>
#include <SD.h>
PaunaStepper stepper;
Adafruit_TSL2591 tsl = Adafruit_TSL2591(2591);
//_____________Pin-Belegunge________________
//I2C
#define Pin_SDA A4
#define Pin_SCL A5
//Stepper
#define Stepper_IN1 5
#define Stepper_IN2 6
#define Stepper_IN3 7
#define Stepper_IN4 8
//Bump-Sensor
#define Bump 9
//Potentiometer
#define poti A3 // 0-1021
//LEDs
#define LED_BLAU 17
#define LED_GELB 16
#define LED_GRUEN 15
//Taster
#define Taster_1 2
#define Taster_2 3
#define Taster_3 4
//SD-Card
#define PinCS 10
//_____________I2C-Adressen_________________
const int16_t I2C_MASTER = 0x42;
const int16_t I2C_SLAVE = 0x08;
#define RTC_I2C_ADDRESS 0x68 // I2C Adresse des RTC DS3231
//_____________Variablen____________________
//Zeit
unsigned long last_Time;
unsigned long wait_Time = 500;
unsigned long I2C_Write_Time;
//Serieller Monitor
int I2C_INFO;
// Steuerung
int rolladen_modus = 2; // 1= automatisch 2=manuell auf/zu 3= manuell Wunschposition
int taster_alt = 1;
int rolladen_max_step = 12500;
//Lux-Sensor
int lux;
int lux_alt;
//Logfile
unsigned long Log_time = millis();
unsigned long log_wait_time = 500;
int Log_count = 1;
String modus;
//RTC-Clock
bool century = false;
bool h12Flag;
bool pmFlag;
//_____________I2C-Arrray___________________
uint8_t I2C_werte_senden[5];
/* [0] Mosdu automatisch = 0/1 aus/ein
[1] Modus manuell = 0/1 aus/ein
[2] Rolladen auf/zu = 0/1 auf/zu
[3] Rolladen Öffnung in %
[4] Lux Wert
*/
uint8_t I2C_werte_lesen[2];
//_____________Funktionen-Anfang___________________
// Lichtsensor
void configureSensor(void) {
// You can change the gain on the fly, to adapt to brighter/dimmer light situations
//tsl.setGain(TSL2591_GAIN_LOW); // 1x gain (bright light)
tsl.setGain(TSL2591_GAIN_MED); // 25x gain
//tsl.setGain(TSL2591_GAIN_HIGH); // 428x gain
// Changing the integration time gives you a longer time over which to sense light
// longer timelines are slower, but are good in very low light situtations!
//tsl.setTiming(TSL2591_INTEGRATIONTIME_100MS); // shortest integration time (bright light)
// tsl.setTiming(TSL2591_INTEGRATIONTIME_200MS);
tsl.setTiming(TSL2591_INTEGRATIONTIME_300MS);
// tsl.setTiming(TSL2591_INTEGRATIONTIME_400MS);
// tsl.setTiming(TSL2591_INTEGRATIONTIME_500MS);
// tsl.setTiming(TSL2591_INTEGRATIONTIME_600MS); // longest integration time (dim light)
/* Display the gain and integration time for reference sake */
Serial.println(F("------------------------------------"));
Serial.print (F("Gain: "));
tsl2591Gain_t gain = tsl.getGain();
switch(gain)
{
case TSL2591_GAIN_LOW:
Serial.println(F("1x (Low)"));
break;
case TSL2591_GAIN_MED:
Serial.println(F("25x (Medium)"));
break;
case TSL2591_GAIN_HIGH:
Serial.println(F("428x (High)"));
break;
case TSL2591_GAIN_MAX:
Serial.println(F("9876x (Max)"));
break;
}
Serial.print (F("Timing: "));
Serial.print((tsl.getTiming() + 1) * 100, DEC);
Serial.println(F(" ms"));
Serial.println(F("------------------------------------"));
Serial.println(F(""));
}
int advancedRead(void)
{
// More advanced data read example. Read 32 bits with top 16 bits IR, bottom 16 bits full spectrum
// That way you can do whatever math and comparisons you want!
uint32_t lum = tsl.getFullLuminosity();
uint16_t ir, full;
int lux;
ir = lum >> 16;
full = lum & 0xFFFF;
lux = tsl.calculateLux(full, ir);
/*
Serial.print(F("[ ")); Serial.print(millis()); Serial.print(F(" ms ] "));
Serial.print(F("IR: ")); Serial.print(ir); Serial.print(F(" "));
Serial.print(F("Full: ")); Serial.print(full); Serial.print(F(" "));
Serial.print(F("Visible: ")); Serial.print(full - ir); Serial.print(F(" "));
Serial.print(F("Lux: ")); Serial.println(tsl.calculateLux(full, ir), 6);
*/
return lux;
}
void requestEvent() {
Wire.write(I2C_werte_senden,8);
}
void onReceive() {
int index = 0;
while (Wire.available()) {
I2C_werte_lesen[index] = Wire.read();
index ++;
}
}
//Convertiert binäre Zeichen in Dezimal Zeichen.
byte bcdToDec(byte val){
return ( (val/16*10) + (val%16) );
}
String rtcReadTime(){
Wire.beginTransmission(RTC_I2C_ADDRESS); //Aufbau der Verbindung zur Adresse 0x68
Wire.write(0);
Wire.endTransmission();
Wire.requestFrom(RTC_I2C_ADDRESS, 7);
int sekunde = bcdToDec(Wire.read() & 0x7f);
int minute = bcdToDec(Wire.read());
int stunde = bcdToDec(Wire.read() & 0x3f);
//Der Wochentag wird hier nicht ausgelesen da dieses mit
//dem Modul RTC DS3231 nicht über die Wire.h zuverlässig funktioniert.
/* wochentag =*/ bcdToDec(Wire.read());
int tag = bcdToDec(Wire.read());
int monat = bcdToDec(Wire.read());
int jahr = bcdToDec(Wire.read())+2000;
char timestamp[30];
sprintf(timestamp,"%02d.%02d.%4d %02d:%02d:%02d",tag,monat,jahr,stunde,minute,sekunde);
return timestamp;
}
//_____________Funktionen-ENDE___________________
void setup() {
configureSensor();
pinMode(Taster_1,INPUT_PULLUP);
pinMode(Taster_2,INPUT_PULLUP);
pinMode(Taster_3,INPUT_PULLUP);
pinMode(LED_BLAU, OUTPUT);
pinMode(LED_GELB,OUTPUT);
pinMode(LED_GRUEN,OUTPUT);
pinMode(Bump, INPUT);
digitalWrite(LED_BLAU, HIGH);
digitalWrite(LED_GELB, HIGH);
digitalWrite(LED_GRUEN, HIGH);
Serial.begin(9600);
if (!SD.begin(PinCS)) {
Serial.println("Card failed, or not present");
// don't do anything more:
while (1);
}
Serial.println("card initialized.");
//Stepper-SetUP
stepper.attach(Stepper_IN1, Stepper_IN2, Stepper_IN3, Stepper_IN4);
stepper.setRPM(15);
stepper.waitRotateStep(-1000);
delay(1000);
//Start Referenzfahrt nach Stromausfall
while(digitalRead(Bump) == 1) {
stepper.setRPM(7);
stepper.move(P_FORWARD);
}
while (digitalRead(Bump) == 0) {
stepper.move(P_STOP);
delay(500);
stepper.waitRotateStep(-500);
stepper.move(P_STOP);
digitalWrite(LED_GELB,LOW);
digitalWrite(LED_BLAU,LOW);
stepper.resetOdo(); //Reset Stepzähler auf 0
}
// Referenzfahrt Ende;
//I2C-Setup
Wire.begin(I2C_SLAVE); // SDA_PIN
Wire.onRequest(requestEvent);
Wire.onReceive(onReceive);
}
int Tag;
void loop() {
I2C_werte_senden[4] = advancedRead(); //[3] Lux Wert = Value
//_____________Rolladen-Steuerung-Anfang___________________________
if ( digitalRead(Taster_2) == 0 && taster_alt == 1) {
rolladen_modus ++;
if (rolladen_modus > 3) {
rolladen_modus = 1;
}
}
taster_alt = digitalRead(Taster_2);
// auto , manuell , manuel wunschposition
if (rolladen_modus == 1) { // automatisch Gruen
I2C_werte_senden[0] = 1; //[0] Modus automatisch = 0/1 aus/ein
I2C_werte_senden[1] = 0; //[1] Modus manuell = 0/1 aus/ein
I2C_werte_senden[2] = 0; //[1] Modus free_move = 0/1 aus/ein
digitalWrite(LED_BLAU, LOW);
digitalWrite(LED_GELB, LOW);
digitalWrite(LED_GRUEN, HIGH);
if (I2C_werte_senden[4] > 30) {
stepper.waitRotateStepTo(-rolladen_max_step);
I2C_werte_senden[3] = 100; //[2] Rolladen zu = 100%
}
if (I2C_werte_senden[4] < 20) {
stepper.waitRotateStepTo(0);
I2C_werte_senden[3] = 0; //[2] Rolladen auf = 0%
}
}
if (rolladen_modus == 2) { // manuell auf/zu Blau
I2C_werte_senden[0] = 0; //[0] Modus automatisch = 0/1 aus/ein
I2C_werte_senden[1] = 1; //[1] Modus manuell = 0/1 aus/ein
I2C_werte_senden[2] = 0; //[1] Modus free_move = 0/1 aus/ein
digitalWrite(LED_BLAU, HIGH);
digitalWrite(LED_GELB, LOW);
digitalWrite(LED_GRUEN, LOW);
if (digitalRead(Taster_3) == 0) {
stepper.waitRotateStepTo(-rolladen_max_step);
I2C_werte_senden[3] = 100; //[2] Rolladen zu = 100%
}
if (digitalRead(Taster_1) == 0) {
stepper.waitRotateStepTo(0);
I2C_werte_senden[3] = 0; //[2] Rolladen auf = 0%
}
}
if (rolladen_modus == 3) { // manuell Wunschposition GELB
I2C_werte_senden[0] = 0; //[0] Modus automatisch = 0/1 aus/ein
I2C_werte_senden[1] = 0; //[1] Modus manuell = 0/1 aus/ein
I2C_werte_senden[2] = 1; //[1] Modus free_move = 0/1 aus/ein
digitalWrite(LED_BLAU, LOW);
digitalWrite(LED_GELB, HIGH);
digitalWrite(LED_GRUEN, LOW);
while (digitalRead(Taster_3) == 0) {
stepper.rotateStepTo(-rolladen_max_step);
}
while (digitalRead(Taster_1) == 0) {
stepper.rotateStepTo(0);
}
stepper.move(P_STOP);
I2C_werte_senden[3] = (100*(-1)*stepper.getOdo())/rolladen_max_step;
}
//_____________Rolladen-Steuerung-ENDE___________________________
//_____________Logfile_Anfang____________________________________
if (millis() > Log_time + log_wait_time) {
if (I2C_werte_senden[0] == 1 && I2C_werte_senden[1] == 0 && I2C_werte_senden[2] == 0 ) {
modus = "Automatisch";
}
if (I2C_werte_senden[0] == 0 && I2C_werte_senden[1] == 1 && I2C_werte_senden[2] == 0 ) {
modus = "Manuell";
}
if (I2C_werte_senden[0] == 0 && I2C_werte_senden[1] == 0 && I2C_werte_senden[2] == 1 ) {
modus = "Free";
}
File Logfile = SD.open("log.txt", FILE_WRITE);
Logfile.print("Log_entry: ");
Logfile.print(Log_count);
Logfile.print(" ,");
Logfile.print("Date_Time: ");
Logfile.print(rtcReadTime());
Logfile.print(" ,");
Logfile.print("Modus: ");
Logfile.print(modus);
Logfile.print(" ,");
Logfile.print("Geschlossen(%): ");
Logfile.print(I2C_werte_senden[3]);
Logfile.print(" ,");
Logfile.print("Lux: ");
Logfile.print(I2C_werte_senden[4]);
Logfile.println(" ,");
Logfile.close();
Log_count ++;
Log_time = millis();
Serial.println("log");
}
//_____________Logfile_Ende______________________________________
//_____________Serieller Monitor________________
if (millis() > last_Time + wait_Time) {
//Serial.println(digitalRead(Taster_automatisch_manuell));
//Serial.print(steuerrung);
//Serial.print("taster: ");
//Serial.print(taster);
//Serial.print(" || ");
Serial.print("I2C info: ");
Serial.println(I2C_INFO);
//Serial.print(" || ");
//Serial.print("counter: ");
last_Time = millis();
}
}