Hallo Community,
Es ist mein erstes Mal das ich mit IoT arbeite, meistens wenn ich die Variablenen von Außen Verändere dann bricht die Verbindung ab.
Hat Jemand Ideen, Tipps und so damit keine Verbindungsabbrüche passieren?
Hier noch der Aktuelle Code:
#include "thingProperties.h"
#include <Wire.h>
#define Gelb 0
// Hue: 60.00 Sat: 100.00
#define Weiss 1
// Hue: 0.00 Sat: 0.00
#define Gold 2
// Hue: 50.00 Sat: 100.00
#define Gruen 3
// Hue: 120.00 Sat: 100.00
#define Karmesinrot 4
// Hue: 348.00 Sat: 90.87
#define Rosa 5
// Hue: 348.00 Sat: 25.10
#define Tuerkis 6
// Hue: 174.00 Sat: 71.88
#define Lavendel 7
// Hue: 255.00 Sat: 50.00
#define Lachsrot 8
// Hue: 17.00 Sat: 52.00
#define Rot 9
// Hue: 0.00 Sat: 100.00
#define Magenta 10
// Hue: 300.00 Sat: 100.00
#define Lila 11
// Hue: 277.00 Sat: 86.19
#define Himmelblau 12
// Hue: 197.00 Sat: 42.31
#define Orange 13
// Hue: 39.00 Sat: 100.00
#define Cyan 14
// Hue: 180.00 Sat: 100.00
#define Blau 15
// Hue: 240.00 Sat: 100.00
#define TimerMax 10
byte Timer = 0;
boolean TimerRun = false;
struct Data{
byte a_b;
byte c_d;
byte e;
byte slider_b;
byte slider_c;
byte slider_d;
byte slider_e;
};
Data data;
void setup() {
Serial.begin(115200);
// Zeige mit der OnBoard-LED das du noch nicht verbunden bist
pinMode(LED_BUILTIN,OUTPUT);
digitalWrite(LED_BUILTIN,LOW);
Wire.begin();// Starte als Wire Master
// Defined in thingProperties.h
initProperties();
// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
ArduinoCloud.addCallback(ArduinoIoTCloudEvent::CONNECT, onConnectionEstablished);
ArduinoCloud.addCallback(ArduinoIoTCloudEvent::DISCONNECT, onConnectionLost);
/*
The following function allows you to obtain more information
related to the state of network and IoT Cloud connection and errors
the higher number the more granular information you’ll get.
The default is 0 (only errors).
Maximum is 4
*/
setDebugMessageLevel(4);
ArduinoCloud.printDebugInfo();
}
void loop() {
ArduinoCloud.update();
// Your code here
if(TimerRun){
if(Timer){
Timer--;
}
else{
// Übertrage Daten
TimerRun = false;
if(adruino_Send_IO_Data_A.getSwitch()&&adruino_Send_IO_Data_B.getSwitch()&&adruino_Send_IO_Data_C.getSwitch()&&adruino_Send_IO_Data_D.getSwitch()&&adruino_Send_IO_Data_E.getSwitch()){
sendData();
}
adruino_Send_IO_Data_A.setSwitch(false);
adruino_Send_IO_Data_B.setSwitch(false);
adruino_Send_IO_Data_C.setSwitch(false);
adruino_Send_IO_Data_D.setSwitch(false);
adruino_Send_IO_Data_E.setSwitch(false);
}
}
}
void onConnectionEstablished() {
// Wenn die Verbindung hergestellt ist, leuchte die On-Board-LED
digitalWrite(LED_BUILTIN, HIGH);
}
void onConnectionLost() {
// Wenn die Verbindung verloren geht, schalte die On-Board-LED aus
digitalWrite(LED_BUILTIN, LOW);
}
/*
Since AdruinoSendIODataA is READ_WRITE variable, onAdruinoSendIODataAChange() is
executed every time a new value is received from IoT Cloud.
*/
void onAdruinoSendIODataAChange() {
// Add your code here to act upon AdruinoSendIODataA change
TimerReset();
}
/*
Since AdruinoSendIODataB is READ_WRITE variable, onAdruinoSendIODataBChange() is
executed every time a new value is received from IoT Cloud.
*/
void onAdruinoSendIODataBChange() {
// Add your code here to act upon AdruinoSendIODataB change
TimerReset();
}
/*
Since AdruinoSendIODataC is READ_WRITE variable, onAdruinoSendIODataCChange() is
executed every time a new value is received from IoT Cloud.
*/
void onAdruinoSendIODataCChange() {
// Add your code here to act upon AdruinoSendIODataC change
TimerReset();
}
/*
Since AdruinoSendIODataD is READ_WRITE variable, onAdruinoSendIODataDChange() is
executed every time a new value is received from IoT Cloud.
*/
void onAdruinoSendIODataDChange() {
// Add your code here to act upon AdruinoSendIODataD change
TimerReset();
}
/*
Since AdruinoSendIODataE is READ_WRITE variable, onAdruinoSendIODataEChange() is
executed every time a new value is received from IoT Cloud.
*/
void onAdruinoSendIODataEChange() {
// Add your code here to act upon AdruinoSendIODataE change
TimerReset();
}
inline void TimerReset(){
if(!TimerRun) TimerRun = true;
Timer = TimerMax;
}
inline void sendData() {
byte half_a = defineColor(adruino_Send_IO_Data_A.getHue(),adruino_Send_IO_Data_A.getSaturation());
byte half_b = defineColor(adruino_Send_IO_Data_B.getHue(),adruino_Send_IO_Data_B.getSaturation());
byte half_c = defineColor(adruino_Send_IO_Data_C.getHue(),adruino_Send_IO_Data_C.getSaturation());
byte half_d = defineColor(adruino_Send_IO_Data_D.getHue(),adruino_Send_IO_Data_D.getSaturation());
byte half_e = defineColor(adruino_Send_IO_Data_E.getHue(),adruino_Send_IO_Data_E.getSaturation());
if(!(half_a == 255 || half_b == 255 || half_c == 255 || half_d == 255 || half_e == 255)) {
// Restlicher Code für die Datenübertragung
data.a_b = (half_b << 4) + half_a;
data.c_d = (half_d << 4) + half_c;
data.e = half_e;
data.slider_b = adruino_Send_IO_Data_B.getBrightness();
data.slider_c = adruino_Send_IO_Data_C.getBrightness();
data.slider_d = adruino_Send_IO_Data_D.getBrightness();
data.slider_e = adruino_Send_IO_Data_E.getBrightness();
Wire.beginTransmission(adruino_Send_IO_Data_A.getBrightness());
Wire.write((byte*)&data, sizeof(data));
Wire.endTransmission();
}
}
byte defineColor(float in_hue,float in_sat){
int hue = round(in_hue);
int sat = round(in_sat);
if(hue == 60 && sat == 100) return Gelb;
else if(hue == 0 && sat == 0) return Weiss;
else if(hue == 50 && sat == 100) return Gold;
else if(hue == 120 && sat == 100) return Gruen;
else if(hue == 348 && sat == 91) return Karmesinrot;
else if(hue == 348 && sat == 25) return Rosa;
else if(hue == 174 && sat == 72) return Tuerkis;
else if(hue == 255 && sat == 50) return Lavendel;
else if(hue == 17 && sat == 52) return Lachsrot;
else if(hue == 0 && sat == 100) return Rot;
else if(hue == 300 && sat == 100) return Magenta;
else if(hue == 277 && sat == 86) return Lila;
else if(hue == 197 && sat == 42) return Himmelblau;
else if(hue == 39 && sat == 100) return Orange;
else if(hue == 180 && sat == 100) return Cyan;
else if(hue == 240 && sat == 100) return Blau;
else return 255;
}
MFG Speed Fariydragon