About

Given the low cost of sensors, one interesting application would be to create a sensor cocktail, that includes various sensors in order to gather telemetry from the atmosphere, similar to a weather station but with some telemetry perhaps exceeding the needs of a weather monitor.

The goals of the project will be the following:

  • the sensor cocktail has to be waterproof such that it can be used outside on a permanent basis,
  • be built as an extensible system where more sensors can be added over time if need be,
  • consume as little power as possible,
  • be created around analog inputs and calibrated sensors such that the data retrieved is meaningful

One of the uses of collecting telemetry off the atmosphere is not as much an exercise or a purpose in itself but the data provided is typically used for other projects that would require the telemetry. For instance, sunrise and sunset cycles, or weather predictions are all great, but it is much more local and elegant to rely on your own collected data. The telemetry can then be coupled with other projects, say, turning on the heating depending on the temperature, or closing the windows via actuators in case it is raining.

That being said, the sensor cocktail is not as much an experiment, as it is a tool that will be used in other experiments where such data is useful.

Components

  • Waterproof sealed storage box; the main problem always seems to be finding an appropriately sized and featured container that is able to hold all the components of the project. To that end, it seems that for this project, sealed storage boxes, of the kind that can be used in fridges or as lunch boxes seem to be an excellent choice as well as being remarkably cheap compared to the more context-specific "project boxes" or "junction boxes".
  • Sensors! So far, the project includes the following sensors:
    • decibel-meter; the Analog Sound Level Meter from DFU Robot seems like a precise instrument that will measure between $30$ and $130$ decibels with an error of $1.5db$. The sensor also does not need any calibration and the sensor is very easy to program because the sensor has its analog output scaling linearly with the amount of decibels.

  • temperature and humidity sensor; based on a DHT11, simple but effective, the DHT11 can measure both the temperature in Celsius as well as the humidity as a ratio of water to air expressed as a percentage.

  • gas sensor; the MQ-2 is a cheap sensor that can detect the concentration of harmful gasses in the air. Note that the MQ-2 cannot distinguish between the gasses themselves but the sensor can output the concentration of gasses like propane, hydrogen, butane, LPG and other flammable gasses in PPM.

Enclosure Design

Ideally, the box containing all the circuitry would be suspended in mid-air and off the ground in order to not bias the sensors with the proximity to other bodies. The box would also have to be cut, and an opening made in order to allow the air, sounds and other atmospheric variables to reach the sensors. However, the box should also be closed off and water-tight in order to prevent water from spilling onto the circuitry.

The box that was chosen for the project has an airtight cover with a snap-on lid with hinges. The box was chosen to be transparent, but the plan is to paint the box and preferably black or white in order to hide the circuitry as well as prevent the LEDs to be seen (also a way to lower the curse threshold of the build by not attracting attention).

The enclosure based on the storage box is designed to have an opening at the bottom where a grate will be installed. Due to the internal distancers, and the fact that the opening is at the bottom of the box, it is anticipated that water will not seep into the circuitry. Similarly, power is supplied also via the bottom of the storage box whilst the box is actually suspended from the top using a regular suspension hook and some rope.

Inside the box, the electronics consisting in sensors, an ESP for data acquisition and Dupont cables will be placed on layers, built like shelves on top of each other. The shelves can be stacked on top of each other and then expanded if new sensors will have to be added. The shelves can be made out of some malleable material, perhaps even wood or sheets of plastic, or other materials that are easy and convenient to work with.

Realization

This section covers the realization of both the shelves and the enclosure box.

Shelves

The shelves themselves have been realized out of wood, with PCB standoffs inserted into holes corresponding to the holes on the sensor board PCBs. Wood is easy to work with, forgiving to mistakes such that dents can be brushed over, the resistance to heat is adequate and wood is a great electrical isolator.

In previous variants, the standoffs were inserted into the wood and JB weld used to glue to them to the wood but in follow-up variants, the standoffs were fixed using a washer on the other side. Either way, both solutions are fair and should provide sufficient sustenance for the PCBs.

The "distancers" as per the figure in the "Enclosure Design" section, are also made out of wood and are glued to the bottom of the first shelf that will be placed just above the hole at the bottom.

Ideally, the distancers provide some extra level of clearance above the hole at the bottom in order to prevent water from entering the box. Additionally, the shelves are tailored to match the girth of the storage box perfectly, such that the cumulative effect of having the hole at the bottom, the hole actually being covered by a grate, the distancers and the bottom shelf acting as a shield, should make it impossible for water to enter the enclosure to the upper shelves even under harsh rain.

Of course, if the box is soaked or inserted entirely in water, the protections will not hold, but ideally the enclosure will be suspended in mid air such that rainfall should not enter in any way directly into the box.

The shelves themselves are separated and held together using struts also fashioned out of wood.

However, in the final variant, the struts have been left out for convenience and the shelves just rest on top of each other.

At this point, the shelves look fairly ugly, with the JB weld contrasting with the wood, such that the shelves will have to be painted.

The holes are measured relative to the PCBs used, by simply overlaying the PCBs onto the wood layer, using a marker to highlight the holes and then drilling through the wood at the indicated places.

Enclosure

The enclosure box does not have to be modified too much, perhaps the most important modification is to create a hole at the bottom of the enclosure.

The hole is created using a Dremel and by cutting a rectangular or square hole into the bottom of the box. A sheet of metal mesh wire is overlayed on top of the hole and glued down using both super-glue and JB weld.

After an ample application, the JB weld is left to dry properly and then the Dremel is used to sand off the excess material. The box will have to be painted, such that the mesh sheet is covered with some painter's tape to prevent the mesh being sprayed over by paint and potentially covering the small holes.

The process is repeated and the hole is covered with painter's tape from the interior of the box as well.

Now, all that is left to do is to paint the box. A carton box is dismantled and turned into a collimator for the spray paint - this is also a great way to spray-paint indoors because the carton box will prevent the paint from jumping onto other surfaces. First, the primer paint is applied several times, by spraying one side, then turning the box around, then spraying again, and so on till the primer is applied uniformly around the enclosure.

Then black spray paint is applied, very much the same way, within the carton box, uniformly and left to dry out between stages till the final result is obtained. Finally, some varnish is sprayed on top of the black paint, in order to provide some last-stage resistance against moisture.

The process is not difficult, yet tedious, with lots of time wasted waiting for the paint to dry properly. When spray painting, the trick is that the more time you have the patience to wait, the better the grip on the surface and the better the final result.

Electronics

It is now time to insert the electronics and fit the project together. The electronics are first mounted onto the shelves by using the PCB spacers and wired up together using Dupont cables. It is true that hard-soldering the cables might provide a better resistence, but if the project succeeds, then there is no need for the extra torque resistence that a solder joint would provide. Otherwise, the Dupont cables provide an easy and convenient way to to rewire the project if needed.

Here is the component diagram of the electronics being used:

A power bus is created with a $5V$ line and a $3.3V$ line, due to most of the IoT electronics being rated at that voltage; for example, the ESP32 (a LOLIN32 Lite) can be powered via its 3.3V pin by $3.3V$, while some of the sensors need $3.3V$ and others like the gas sensor needing $5V$.

The ESP32 is as usual a waste of equipment, being way too powerful for the project, a dual core CPU, Bluetooth and a bunch of other features that will not be needed. A very basic ESP8266 would have done the job but unfortunately the ESP8266 has only one single analog input (which is terrible) and most of these sensors are deliberately chosen to provide analog signals because analog signals provide a precise measurement with digital output only being suitable for sensing.

With the electronics laid out and the enclosure connected via a long cable providing power, it is now time to assemble the sensor cocktail together.

As planned, the shelves are easy to overlay inside the box and the enclosure closes flawlessly.

As a final touch, the top hook is installed, by clearing the foam and then applying some JB weld, followed by some water-resistant epoxy putty. This will be the hook by which the entire enclosure will be suspended in mid air, with the power cable feeding from the bottom of the box.

For the record, the project was powered on for the duration of the fitting of the electronics and the sensor cocktail continued emitting telemetry to the server where the data was collected and displayed.

Programming the ESP32

Programming the ESP32 for the purpose of a sensor cocktail is so trivial that the whole code has been left out in favor of providing the relevant snippets of code for the other sensor types for which projects have been already accomplished. In essence, the Arduino project takes some generic boilerplate code that has been used before in numerous Wizardry and Steamworks project, perhaps even the template that is used for the sound level meter and then just bolts onto it the various instructions needed to read the sensors.

The template itself is boilerplate code responsible with the usual tasks to be found in any project:

  • connecting and reconnecting to Wifi,
  • subscribing to an MQTT topic,
  • building payloads to send to the MQTT broker,

and so on and so forth.

The sensor data is pulled in the loop function, at the very end, just like the sound level meter template, and here is the only relevant code:

  // create blank JSON payload
  StaticJsonDocument<256> msg;
  msg["id"] = String(MQTT_CLIENT_ID().c_str());
 
  // measure noise level (dBA)
  float ai, vc, noise;
  ai = analogRead(DECIBELMETER_PIN_ANALOG);`
  vc = mapValueToRange(ai, 0, 4095, 0, MAX_ANALOG_VOLTAGE);
  noise = vc * 50.0;
  msg["noise"] = noise;
 
  // grab temperature
  DHT_Unified dht(DHT_PIN_ANALOG, DHTTYPE);
  dht.begin();
  sensors_event_t event;
  // grab temperature
  dht.temperature().getEvent(&event);
  while (isnan(event.temperature)) {
      dht.temperature().getEvent(&event);
      delay(1);
  }
  msg["temperature"] = event.temperature;
  // grab humidity
  dht.humidity().getEvent(&event);
  while (isnan(event.relative_humidity)) {
      dht.humidity().getEvent(&event);
      delay(1);
  }
  msg["humidity"] = event.relative_humidity;
 
  // grab gas
  float gv, gas;
  gv = analogRead(GAS_PIN);
  gas = mapValueToRange(gv, 0, 255, 0, MAX_ANALOG_VOLTAGE);
  msg["gas"] = gas;
 
  // serialize to character array and publish payload onto MQTT broker
  char output[256];
  serializeJson(msg, output, 256);
  mqttClient.publish(MQTT_TOPIC().c_str(), output);
 
  delay(250);

The code can be expanded as more sensors are added yet it pulls data from all the sensors and sends the values to an MQTT broker.

Data Acquisition, Storage and Display

One part of the trinity is satisfied, with the acquisition being the sensor cocktail and its realization, the rest that remains is to implement some storage and display of the data. Most of it is a matter of preference and also a choice where the current architecture has to be factored in.

A NoSQL and non RDBMS database could be chosen, given that the payload sent out by the ESP is in JSON format, but it is important to realize that the payload being sent is really just a container for numeric values, such that the whole benefit of a JSON-oriented database is lost given the simplicity of the structure. The usual MySQL, MariaDB and others non-flat-file database are a good choice, but they require an infrastructure with a running server somewhere on the network. In essence, the storage of a few values, even given a fairly large timespan still represents a minor consumption and resource expenditure. That being said, SQLite has been chosen again as a flat-file relational database that also has the benefit that nothing special has to be installed and the database can be created on the fly.

The display and the wiring up of the trinity will be carried out in Node-Red, mostly due to wanting to work with high-level blocks without wanting to spend too much time re-implementing the low-level programming required by the project (ie: string tranformations, object walking, serialization, etc.). The node-red flow is split up into blocks, each denoting the acquisition, the storage and display of data distinctively.

Link-out and link-in nodes are used to connect the blocks, because as mentioned at the start of the document, the sensor cocktail is supposed to provide telemetry to other flows.

In the acquisition group, the sensor data is read off the MQTT broker and then stored both in a global variable and to files, just for the convenience of connectivity with other flows. A snapshot of the most recent measurement is stored for the noise level and will be stored for the temperature, humidity and the readings of the other sensors.

The storage group stores all the telemetry to a database indexed by the current ISO UTC date timestamp. The timestamp with the "1" marker is a node that will trigger whenever the flow is saved and will send a message to the followup database node to create the table structure if it is not yet created. The SQL parameters are then generated on the subsequent level and inserted into the database using prepared statements.

The display group currently displays the noise level on the node-red dashboard using a simple line plot - unfortunately, the line plot is incapable of displaying error bars in order to display the $1.5dB$ error level of the decibelmeter but switching to a different chart display might be an option in the future. Displaying the error margin is very important in case the telemetry needs to bear precise (legal) weight such that they should not be left out of the plot.

Observations

Measuring sound levels throughout the day leads to an interesting overview of the noise distribution over the entire day.

More than half of the measured noise is, according to the local legislation, just sound pollution clearly jump over the $55dB$ legal mark during the day and $45dB$ legal mark during the night even when accounting for the noise level meter error range (marked as error bars). There are frequent peaks over the $80dB$ mark, most of these consisting in police or ambulance cars that use their sirens senselessly and cars revving up for no other purpose than to be a nuisance (a cultural thing). The $80dB$ mark seems pivotal, in that clinical studies have shown that prolonged exposure can lead to deafness. Whilst less than half the points fall below the daytime threshold of $55dB$, the night-time threshold of $45dB$ is practically useless given that all recorded points fall well above the night-time noise pollution limit. Looking at the chart, it is as if the laws governing noise pollution simply have no application and the various agencies responsible for keeping the noise levels down are clearly not doing their job.

Another generated graph consists in taking the average of all measurements between the hours of the day.

The measurements indicate that the majority of the points fall well above the $55dB$ mark with only a small reprise between 10pm and 3am, mostly making "living by the night" and ideal policy in case living in peace is your goal. As mentioned previously, regardless whether it is day or night, all measurements are way over the night time $45dB$ limit.

Further Work

The project is now complete and could maybe be extended with other sensors for more telemetry. However, the infrastructure is there and the project is easily expandable.


iot/creating_a_sensor_cocktail.txt ยท Last modified: 2024/07/21 08:50 by office

Access website using Tor Access website using i2p Wizardry and Steamworks PGP Key


For the contact, copyright, license, warranty and privacy terms for the usage of this website please see the contact, license, privacy, copyright.