Skip to content

SESSION 2

GOAL: Small experiment (a single sensor). Prove you can integrate sensors suitable for you monitoring purpose. Focus on effectiveness, namely doing the right things, i.e., the Maker approach.

The reference hardware: ESP32-DevKit

Let's start by simulating it

Wokwi is an online Electronics simulator. You can use it to simulate Arduino, ESP32, STM32, and many other popular boards, parts and sensors.

  • It's time to be connected by MQTT. The mosto convenient way is to use your mobile an Access Point and configure SSID and password consequently.
mosquitto_pub -h test.mosquitto.org -t "topicName/led" -m "on"
mosquitto_pub -h test.mosquitto.org -t "topicName/led" -m "off"
mosquitto_sub -h test.mosquitto.org -t "wokwi/temperature"

Another possible broker is mqtt://mqtt.eclipseprojects.io

https://thingsboard.io/docs/getting-started-guides/helloworld/?connectdevice=mqtt-linux

mosquitto_pub -d -q 1 -h "$THINGSBOARD_HOST_NAME" -p "1883" -t "v1/devices/me/telemetry" -u "$ACCESS_TOKEN" -m {"temperature":25}

NOTE: for the sake of convenience, we will use WiFi connectivity, however it should be now clear WiFi is usually not appropriate for IoT applications due to the excessive energy demand.

It is time to Work with a real device

  • The only novelty is the vibration sensor SW-420 ... but please have a look to the SR04 Ultrasonic Sensor and adapt it to make it working with the SW-420.

This sketch could help

// Watch video here: https://www.youtube.com/watch?v=235BLk7vk00

/* Vibration sensor connected to Arduino pins as follows:

 ESP32 Arduino Vibration Sensor
 https://wolles-elektronikkiste.de/esp32-mit-arduino-code-programmieren
   D18 --> GPIO18 --> G18        DOut
   GND                           GND
   +5V --> 3.3V                  VCC      
*/

int EP = 18;

void setup(){
  pinMode(EP, INPUT); //set EP input for measurment
  Serial.begin(9600); //init serial 9600
}
void loop(){
  long measurement =TP_init();
  delay(50);
  Serial.println(measurement);
}

long TP_init(){
  delay(10);
  long measurement=pulseIn (EP, HIGH);  //wait for the pin to get HIGH and returns measurement
  return measurement;
}

Through pulseIn() we can measure the duration of a vibration event exceeding the threshold, not the frequency of the vibrations themselves. Indeed, the Arduino pulseIn() function waits for a change in the binary input (Low to High in this instance) and returns the duration that the detected pulse was active (time for it to go back High to Low after going High).

QUESTION: Can we use this function to help in anomaly detection? see here for inspiration!