I am prototyping a device that will check values from force sensitive resistor (FSR) sensors and post their values to Firebase.
At the moment I am testing it with 1 sensor and it works, the value is posted to Firebase.
The problem I have is that the WiFiSSLClient blocks the loop for more than a second… If the FSR is pressed again during that time, that measure is ignored (which is not ideal).
One of the functions has a delay(200) – not 1000 – and without it, the data doesn’t get posted.
Can you suggest any improvement on the code?
void loop() {
//read sensor on pin A0:
int sensorValue = analogRead(fsrAnalogPin);
// check if it's higher than the current peak:
if (sensorValue > peakValue) {
peakValue = sensorValue;
}
if (sensorValue <= threshold - noise ) {
if (peakValue > threshold + noise) {
// 1. Detect that you have a peak value:
Serial.print("peak: ");
Serial.println(peakValue);
// 2. Capture this value to send it out
fsrSqueezed = true;
howMany++;
Serial.print("Squeeze count: ");
Serial.println(howMany);
fsrOutReading = peakValue;
fsrLastSqueeze = millis();
// reset the peak value:
peakValue = 0;
}
}
if (fsrSqueezed == true) {
digitalWrite(LEDpin, HIGH);
Serial.print("squeeze value: ");
Serial.println(fsrOutReading);
// 3. Send record to Firebase
sendToFirebase(fsrOutReading); // This call stops the loop for more than 1 second…
Serial.print("Time diff:");
Serial.println(millis() - fsrLastSqueeze); // This value gets ~1200 milliseconds!!!
digitalWrite(LEDpin, LOW);
fsrSqueezed = false;
}
}
void sendToFirebase(int fsrReading) {
if (client.connect(HOST, 443)) {
sendData(fsrReading);
client.stop();
}
}
void sendData(int pressure) {
// Creating the pathBuffer
sprintf(pathBuffer, "POST %s.json?auth=%s HTTP/1.1", PATH, SECRET);
int size = constructJson(WiFi.getTime(), pressure);
client.println(pathBuffer);
client.print("Host: ");
client.println(HOST);
client.println("Content-Type: application/json");
client.print("Content-Length: ");
client.println(size);
client.println("Accept: application/json");
client.println("Connection: close");
client.println();
client.println(bodyBuffer);
delay(200);
}