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

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:

SS-2017-06-19_17.24.33.png


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:
SS-2017-06-19_17.26.20.jpg


And:
SS-2017-06-19_17.09.26.png


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

  • B4R_IO_Adafruit.zip
    1.1 KB · Views: 1,242
  • B4J_IO_Adafruit.zip
    2.4 KB · Views: 1,054
  • B4i_IO_Adafruit.zip
    3.6 KB · Views: 915
  • B4A_IO_Adafruit.zip
    8.1 KB · Views: 1,653
Last edited:

Peter Simpson

Expert
Licensed User
Longtime 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

B4X founder
Staff member
Licensed User
Longtime 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
Longtime 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:

chaitanya gautam

New Member
hey i am trying to implement the project even though i am not getting any error it does not display any readings on the phone even though state is "connected"
 

Attachments

  • Capture.JPG
    Capture.JPG
    15.5 KB · Views: 769
  • WhatsApp Image 2019-02-22 at 22.51.13 (1).jpeg
    WhatsApp Image 2019-02-22 at 22.51.13 (1).jpeg
    14.2 KB · Views: 661

cliv

Member
Licensed User
Longtime 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
 

cliv

Member
Licensed User
Longtime 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