Android Tutorial [B4X][IoT] - Monitor the home temperature with Adafruit IO

Erel

Administrator
Staff member
Licensed User
Adafruit IO is a cloud service built for IoT solutions. It provides two interfaces: MQTT and REST API. It is very simple to use it with B4X as demonstrated in this example.

It is more than a MQTT broker. It also saves the historic values and allows integration with other services.
As this is a cloud service you can use it over the internet, which is very nice.

Each topic is mapped to a "feed". For this example we will be using two feeds:



The ping feed is required for the reconnect method.

In this example an ESP8266 is connected to a BMP180 temperature sensor and it sends the temperature to the temperature topic.
Other connected devices monitor the temperature:


And:


Tips

- The Arduino connects to the non-ssl port. Other platforms should connect to the ssl port.
- You need to update the username and password in the examples. The password is the AIO key.
- The payload should be a string. Raw bytes will not be sent properly. If you need to send more complex messages then you can use B4RSerializator and convert the bytes to a hex string (ByteConverter.HexFromBytes) and then back from a hex string to bytes.
- The B4A, B4i and B4J code is very similar. The main difference is that in B4A the communication is managed in the Starter service.
 

Attachments

Last edited:

Peter Simpson

Expert
Licensed User
Very nice Erel,
I might look into this but parts of my home is already running on MQTT from another supplier, so changing to Adafruit IO will just be time consuming to me for basically the same results as I'm already getting with your MQTT library.

It does look interesting though, so I'll definitely be learning it and I might even use it for other things in the future, but just not for my personal Smart Home solution.

Thank you again for yet another awesome solution.
 

Erel

Administrator
Staff member
Licensed User
the same results as I'm already getting with your MQTT library.
Note that this example is also based on the same MQTT library.

The main differences between Adafruit IO and a local MQTT broker are:
- Adafruit IO is accessible over the internet.
- It stores historic values which are available through the REST API.

It can be useful if you want to create a smart home solution that is also accessible outside of the house. You can also use both a local broker and Adafruit IO.
 

Peter Simpson

Expert
Licensed User
Good Morning @Erel,
I already use a free internet based MQTT broker called www.cloudmqtt.com, I use your jMQTT library with Android and also your rMQTT library with WeMos. I personally don't use an internal MQTT broker, I don't really want to set one up. I use www.cloudmqtt.com to interact with devices in my house when I'm not actually there, like some MQTT connected lights and hopefully my blinds sooner rather than later.

I might look into Adafruit IO when I get some spare time, but I can already control devices in my house from anywhere on the planet through your aforementioned libraries with an app that I've created from my Android devices.

"- It stores historic values which are available through the REST API", now this does sound rather interesting to me though ;)
 
Last edited:

cliv

Member
Licensed User
Example not work in Android 4.1...but work well in Android 5+.

B4X:
Logger connected to:  iTab1012 iTab1012 3G
--------- beginning of /dev/log/main
*** Service (starter) Create ***
Trying to connect
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Error connecting.
Trying to connect
Error connecting.
Trying to connect
 

Erel

Administrator
Staff member
Licensed User
The Android version shouldn't matter. Make sure that the device is connected to the internet.
 

cliv

Member
Licensed User
In services mqtt.Initialize take a long time to wait.
What is the best approach to start with a splash screen and/or display a ProgressDialog until connection is made?
I try this but...nothing to display.
B4X:
'STARTER
Sub Service_Create
   '...
    CallSub3(Main,"progress_sub","",False)
    Sleep(100)
   '...
End Sub

'MAIN
Sub progress_sub
   '...
    ProgressDialogShow2(cs, False)
End Sub
 
Top