Bluetooth Communication with Palm OS -
Collecting data to display (Part 1/2)
06.06.2020
Since a few years I am collection temperature and humidity data from different rooms e.g. the basement or bedroom. Especially for the bedroom I wanted to make sure, that the air do not get too dry or humid. So I got some ESP8266 / NodeMCU and DHT22, later I did it with BME280 and SHT3x. In this article-series I want to describe how I collect the data, store them on a home-server and display them on Bluetooth-enabled Palm OS device.
The article is splitted in two parts: this first part is about building the „collector-unit“ with an ESP8266 / DHT22 and storing the data on a server. The second part is about the Palm OS Bluetooth application and how I got the data displayed via an ESP32 over Bluetooth on a PalmOS device.
Needed parts
These parts are required for collection the data and store them on a server:
- A linux server (e.g. a Raspberry PI – v1 with 700MHz is good enough)
- one or more ESP8266 / NodeMCU
- one ore more DHT22 sensor
Of course it would work also with other parts. I only listed them, because these are the parts I used.
Setting up an ESP8266
At this point, I will skip the part of explaining how to set up ESP8266 with the Arduino IDE. There are already good tutorials, which explain this very well, like this one: https://randomnerdtutorials.com/how-to-install-esp8266-board-arduino-ide/
In this section I want to explain how to wire up the ESP8266 / NodeMCU with a DHT22 and getting some data out of it.
The wiring
The wiring is straightforward first pin from left of the DHT22 needs to be connected to 3.3V, second pin to D2 and a 4.7kOhm resistor to 3.3V like shown in the picture. The third pin is left out and the last bin (the right one) must be connected to ground. That’s it. If you got an DHT22 already on a PCB with a resistor on it and three pins: Connect “VCC” to 3.3V, “DAT” to D2 and “GND” ground.
The code
A ESP8266 is not able to get data from a DHT22 by default or at least to make things easier it is recommended to install the “DHT”-library. Just click in the Arduino IDE on “Tools” => “Manage Libraries” and search for "DHT sensor library" by Adafruit. It turned out, that Version 1.2.3 works best, with newer versions, it was not possible to get useful values from the DHT22.
First we need to include the libraries for Wifi, the web server and for the DHT22. In line 13 and 14, the credentials for the own Wifi are set. After instantiate the WebServer with the default port 80 (line 17), the setup-function is defined, in which the serial connection for debugging-output gets established, the wifi gets connected and the web server registers the paths we want to call later. At the end, the web server gets started (line 32).
In the loop-function, we need to tell the web server, that it must handle incoming client requests (line 38) and reconnection to the Wifi if the ESP8266 gets disconnected.
The handle_DefaultConnect-fuction simply tries to read the temperature and humidity form the DHT22 as long as the result isn’t a float-value (line 53 – 62). In the next step, the output string gets formatted (it is always like “humidity,temperature,”). We send the string to our serial monitor (line 73,74) and finally via the web server to the client (line 76).
If the client sends an unknown request, it just gets the information “404 - page not found” (line 82).
The function “startWIFI” (line 87) is not very complicated. In line 91 we try to connect to the Wifi and in line 93, we wait, until it is connected. In line 102, we are printing the current IP address - that is all.
Setting up the server
The server needs, besides from an OS (I would recommend Raspbian or Ubuntu Server Edition), following software stack:
- Nginx
- PHP 7
Nginx is used later to provide the data, we want to display on the Palm PDA. PHP7 is used to fetch the data via a cron-job periodically and storing them in simple text-files. So we do not need a heavy MySQL-Server at this point. Also moving the project to another sever is a bit easier, because you only need to copy the files to the other server, instead of exporting and importing them in to a database. (I admit it's not the greatest time saver.)
Digital Ocean provides a great tutorial, that explains how to install Nginx, PHP and more on Ubuntu: How To Install Linux, Nginx, MySQL, PHP (LEMP stack) on Ubuntu 20.04
For Raspbian you can follow these instructions on raspberrypi.org: https://www.raspberrypi.org/documentation/remote-access/web-server/nginx.md
Normally the content you want to be delivered by the webserver (in this case: Nginx) is located under “/var/www/html”. So, this is where these need to be:
- config.php: for config-parameters like the IP-Addresses of the ESP8266
- dataGrabber.php: This script will fetch the data from the ESPs and store them on the disk
- dataProvider.php: This script will provide the data, which we want later display on a Palm
The config.php
The config.php only contains a static array with IP-addresses as key and the room names as values:
The advantage of this config.php is that we can easily integrate it with the other PHP scripts and at the same time have a central place where we can make changes.
The dataGrabber.php
The dataGrabber.php has the task to grab/fetch the data from the ESP8266 and store it in a text-file. It is not very complicated and can look like this:
function run()
This is the entry-point of the script and gets called, when we call the script via “http://
$dataGrabber = new dataGrabber();
$dataGrabber->run();
The run-function itself simply calls the following functions for each configured sensor in the config.php:
function fetchData(String $ip, String $name)
This function simply downloads the data from an ESP8266 by a http-request. The data gets verified and returned by the function. If the data is not valid, it just returns null.
function processData($data)
This function appends the time-String, so we known when the temp/hum-data was recorded and returns the unformatted data, which we want to display on a Palm PDA.
function writeToFile($data)
This function stores the fetched data into a text-file, so we do not have to wait until the ESP8266 read the data from the sensor (DHT22).
The dataProvider.php
The dataProvider only contains one function: “provideData()”. This function expects the sensor-name as GET-Parameter, checks if this name exists in the config and if so, it will read and output the stored value in the text-file, which was written by the dataGrabber.php. If the sensor-name does not exist or the text-file is not present, the function returns a default-value: "-1,-1,<currentDateTime>". If we see this later on the Palm PDA, we know, that something went wrong. Of course, we could implement an error reporting at this point, but I think it is a bit too much for such a simple task. At least we do not deal with highly sensitive data.
Testing the setup
When the files (config.php, dataGrabber.php and dataProvider.php) are saved in the document-root of the web-sever (default path: “/var/www/html”), it should be possible to call them via a web-browser:
- http://<server-ip>/dataGrabber.php
- http://<server-ip>/dataProvider.php
The call of “http://
Final thoughts
Now, we have all needed parts for the next step: a ESP8266 (or more) to read the humidity and temperature, a dataGrabber, which collects the wanted data. Also the dataProvider, which simply outputs the collected data via a HTTP-request. In order to collect the data, we need to create a cronjob, which periodically executes the dataGrabber.php. With the command “sudo crontab -e” on the linux server, this line can be added:
*/30 * * * * php /var/www/html/dataGrabber.php
This executes the php-command very 30 minutes. Of course, it would be possible to fetch the information more often, by changing the 30 to something lower. Save the Cron-file and the system is ready.
The system is easy to extend. Just flash another ESP8266, add the IP to config.php and that’s it.
In part two we will flash an ESP32, which has bluetooth and write a small Palm OS Application, which requests data from the ESP32 and displays it: Bluetooth Communication with Palm OS - The Palm OS Application (Part 2/2)