Skip to content
Snippets Groups Projects
Commit eed80fb8 authored by Anian Bühler's avatar Anian Bühler
Browse files

added readDataSerial and reorganized Client1+2 examples

parent b5fc93db
Branches
No related tags found
2 merge requests!3Dev to master,!2Dev to Master
...@@ -76,11 +76,11 @@ void loop() { ...@@ -76,11 +76,11 @@ void loop() {
if(valueChanged(currentValue, THRESHOLD)){ if(valueChanged(currentValue, THRESHOLD)){
itoa(currentValue, payload, 10); //wandle Wert in ASCII-Zeichen itoa(currentValue, payload, 10); //wandle Wert in ASCII-Zeichen
//Alternativ: sprintf(payload, "%d", currentValue); //wandle Wert in ASCII-Zeichen //Alternativ: sprintf(payload, "%d", currentValue); //wandle Wert in ASCII-Zeichen
newData = true; // neue Daten zum Senden newData = true; //Flag: neue Daten zum Senden
} }
if(timeElapsed(SEND_DELAY) && newData){ if(timeElapsed(SEND_DELAY) && newData){ //Sende neue Daten wenn Mindestwaretzeit abgelaufen
psnClient.publish(topicPublish, payload); //bereite Topic und Nutzdaten zum senden vor psnClient.publish(topicPublish, payload); //bereite Topic und Nutzdaten zum senden vor
newData = false; //Daten wurden gesendet newData = false; //Flag: keine neuen Daten vorhanden
} }
} }
...@@ -76,11 +76,11 @@ void loop() { ...@@ -76,11 +76,11 @@ void loop() {
ledState = !ledState; //Invertiere zu sendenden Wert "LED-Zustand" ledState = !ledState; //Invertiere zu sendenden Wert "LED-Zustand"
itoa(ledState, payload, 10); //wandle Wert in ASCII-Zeichen itoa(ledState, payload, 10); //wandle Wert in ASCII-Zeichen
//Alternativ: sprintf(payload, "%d", currentValue); //wandle Wert in ASCII-Zeichen //Alternativ: sprintf(payload, "%d", currentValue); //wandle Wert in ASCII-Zeichen
newData = true; // neue Daten zum Senden newData = true; //Flag: neue Daten zum Senden
} }
if(timeElapsed(SEND_DELAY) && newData){ if(timeElapsed(SEND_DELAY) && newData){ //Sende neue Daten wenn Mindestwaretzeit abgelaufen
psnClient.publish(topicPublish, payload); //bereite Topic und Nutzdaten zum senden vor psnClient.publish(topicPublish, payload); //bereite Topic und Nutzdaten zum senden vor
newData = false; //Daten wurden gesendet newData = false; //Flag: keine neuen Daten vorhanden
} }
} }
...@@ -11,41 +11,60 @@ ...@@ -11,41 +11,60 @@
//************************************************************************** //**************************************************************************
//LITTLE HELPERS ;-) //LITTLE HELPERS ;-)
//************************************************************************** //**************************************************************************
int edgeDetected(bool currentState){ int edgeDetected(bool edCurrentState){
static bool lastState = false; static bool edLastState = false;
int edge = 0; int edEdge = 0;
if(currentState && !lastState){ if(edCurrentState && !edLastState){
edge = RISING; edEdge = RISING;
} }
if(!currentState && lastState){ if(!edCurrentState && edLastState){
edge = FALLING; edEdge = FALLING;
} }
lastState = currentState; edLastState = edCurrentState;
return edge; return edEdge;
} }
bool valueChanged(int value, int threshold){ bool valueChanged(int teValue, int teThreshold){
static int lastValue = 0; static int vcLastValue = 0;
if(abs(value-lastValue) > threshold){ if(abs(teValue-vcLastValue) > teThreshold){
lastValue = value; vcLastValue = teValue;
return true; return true;
} }
return false; return false;
} }
bool timeElapsed(long delayTime){ bool timeElapsed(long teDelayTime){
static long lastTime = 0L; static long teLastTime = 0L;
long currentTime = millis(); long currentTime = millis();
if(lastTime + (delayTime-1) < currentTime){ if(teLastTime + (teDelayTime-1) < currentTime){
lastTime = currentTime; teLastTime = currentTime;
return true; return true;
} }
return false; return false;
} }
int readSerialData(Stream& rsStream, char* rsDataArray, char rsEndSign) {
static int rsCounter = 0;
if (rsStream.available()) {
char charBuffer = rsStream.read();
rsDataArray[rsCounter] = charBuffer;
if (charBuffer == rsEndSign) {
rsDataArray[rsCounter] = '\0';
int nrOfChars = rsCounter-1;
rsCounter = 0;
return nrOfChars;
} else {
rsCounter++;
}
}
return 0;
}
//************************************************************************** //**************************************************************************
//ROOT //ROOT
......
...@@ -43,6 +43,7 @@ ...@@ -43,6 +43,7 @@
int edgeDetected(bool); int edgeDetected(bool);
bool valueChanged(int, int); bool valueChanged(int, int);
bool timeElapsed(long); bool timeElapsed(long);
int readSerialData(Stream&, char*, char);
class didacticPSNet class didacticPSNet
{ {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment