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
No related branches found
No related tags found
2 merge requests!3Dev to master,!2Dev to Master
......@@ -76,11 +76,11 @@ void loop() {
if(valueChanged(currentValue, THRESHOLD)){
itoa(currentValue, payload, 10); //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
newData = false; //Daten wurden gesendet
newData = false; //Flag: keine neuen Daten vorhanden
}
}
......@@ -73,14 +73,14 @@ void loop() {
boolean buttonState = digitalRead(BUTTON_PIN); //lese Poti ein und speichere Wert
if(edgeDetected(buttonState) == RISING){
ledState = !ledState; //Invertiere zu sendenden Wert "LED-Zustand"
itoa(ledState, payload, 10); //wandle Wert in ASCII-Zeichen
ledState = !ledState; //Invertiere zu sendenden Wert "LED-Zustand"
itoa(ledState, payload, 10); //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
newData = false; //Daten wurden gesendet
newData = false; //Flag: keine neuen Daten vorhanden
}
}
......@@ -11,41 +11,60 @@
//**************************************************************************
//LITTLE HELPERS ;-)
//**************************************************************************
int edgeDetected(bool currentState){
static bool lastState = false;
int edge = 0;
int edgeDetected(bool edCurrentState){
static bool edLastState = false;
int edEdge = 0;
if(currentState && !lastState){
edge = RISING;
if(edCurrentState && !edLastState){
edEdge = RISING;
}
if(!currentState && lastState){
edge = FALLING;
if(!edCurrentState && edLastState){
edEdge = FALLING;
}
lastState = currentState;
return edge;
edLastState = edCurrentState;
return edEdge;
}
bool valueChanged(int value, int threshold){
static int lastValue = 0;
bool valueChanged(int teValue, int teThreshold){
static int vcLastValue = 0;
if(abs(value-lastValue) > threshold){
lastValue = value;
if(abs(teValue-vcLastValue) > teThreshold){
vcLastValue = teValue;
return true;
}
return false;
}
bool timeElapsed(long delayTime){
static long lastTime = 0L;
bool timeElapsed(long teDelayTime){
static long teLastTime = 0L;
long currentTime = millis();
if(lastTime + (delayTime-1) < currentTime){
lastTime = currentTime;
if(teLastTime + (teDelayTime-1) < currentTime){
teLastTime = currentTime;
return true;
}
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
......
......@@ -43,6 +43,7 @@
int edgeDetected(bool);
bool valueChanged(int, int);
bool timeElapsed(long);
int readSerialData(Stream&, char*, char);
class didacticPSNet
{
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment