# Balena Sense & ESP8266

**URL:** <https://forums.balena.io/t/balena-sense-esp8266/269418>\
**Category:** Project help\
**Created:** [March 10, 2021, 11:00pm UTC](https://forums.balena.io/t/balena-sense-esp8266/269418 "2021-03-10T23:00:09Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![n1md4](https://avatars.discourse-cdn.com/v4/letter/n/d9b06d/32.png) [@n1md4](https://forums.balena.io/u/n1md4)\
**Post date:** [March 10, 2021, 11:00pm UTC](https://forums.balena.io/t/balena-sense-esp8266/269418/1 "2021-03-10T23:00:10Z")

</div>

Hi,

I would like to create a stack to capture and display my home environment metrics. I am also playing with ideas to incorporate Home Assistant, not that I have any ideas yet, but in case it drives the direction of any response.

I have read the BalenaSense project, and that looks suitable. More specifically the guide to “aggregate data from a fleet of sensors”. The twist here is I would like to use lower-power microcontrollers; something like the ESP8266. I have had this working with a vanilla TICK stack on a Pi, that had the ESP8266 write directly to InfluxDB. With the way Balena Sense is setup, should messages instead be handled by Telegraf and MQTT?

This is all a learning exercise for me too, and thank you in advance for guidance.

---

<div class="post-metadata">

**Author:** ![MTolba](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/mtolba/32/3894_2.png) [@MTolba](https://forums.balena.io/u/MTolba)\
**Post date:** [March 11, 2021, 4:50pm UTC](https://forums.balena.io/t/balena-sense-esp8266/269418/6 "2021-03-11T16:50:42Z")

</div>

Welcome to balena forums,  
We are happy to know about your project. As you mentioned you the messages should be handled by MQTT. There are a lot of resources on the internet describing how to implement that.  
[http://nilhcem.com/iot/home-monitoring-with-mqtt-influxdb-grafana](http://nilhcem.com/iot/home-monitoring-with-mqtt-influxdb-grafana)

> **[Tutorial ESP8266 Node-RED Granafa InfluxDB MQTT - PDAControl](http://pdacontrolen.com/esp8266-grafana-influxdb-node-red-arduino-mqtt/)**
>
> Interaction Test between platforms #IoT ESP8266 Node-RED #InfluxDB #Grafana Arduino and MQTT, temperature reading and storage of historical data.

Please don’t forget to share your project with us.  
Wish you happy tinkering!

---

<div class="post-metadata">

**Author:** ![n1md4](https://avatars.discourse-cdn.com/v4/letter/n/d9b06d/32.png) [@n1md4](https://forums.balena.io/u/n1md4)\
**Post date:** [March 13, 2021, 10:53pm UTC](https://forums.balena.io/t/balena-sense-esp8266/269418/8 "2021-03-13T22:53:48Z")

</div>

I have a balena-sense stack built, running mqtt, telegraf, influxdb, grafana.

I want to send data (from various network attached sources) to the mqtt broker, but that action is failing.

As a basic test (I don’t know if it’s a valid one) I try to telnet to port 1883, both locally and over a network, neither connect. I have ensured 1883 is listening, and have exposed the port in docker-compose.yml, but still I can not connect.

I want to say it’s a firewall issue, it reacts like one, but I also don’t know enough about how mqtt works to know the difference.

Any assistance would be greatly appreciated.

---

<div class="post-metadata">

**Author:** ![n1md4](https://avatars.discourse-cdn.com/v4/letter/n/d9b06d/32.png) [@n1md4](https://forums.balena.io/u/n1md4)\
**Post date:** [March 13, 2021, 11:33pm UTC](https://forums.balena.io/t/balena-sense-esp8266/269418/9 "2021-03-13T23:33:03Z")

</div>

`docker-compose.yml` extract:  
`~/c/g/balena-sense> grep -B1 TELEGRAF_MQTT_URL_PORT docker-compose.yml`  
` environment:`  
` - 'TELEGRAF_MQTT_URL_PORT=INTERNAL'`

Checking MQTT is running:  
`root@dd83991:~# ps | grep mosq`  
` 8826 1883 2964 S /usr/sbin/mosquitto -c /mosquitto/config/mosquitto.conf`

Checking 1883 is listening:  
`root@dd83991:~# netstat -tnl | grep 1883`  
`tcp 0 0 :::1883 :::* LISTEN `

Checking firewall is open to 1883:  
‘root@dd83991:~# iptables -nL | grep 1883’  
‘ACCEPT tcp – 0.0.0.0/0 172.17.0.3 tcp dpt:1883’

Testing local connection to 1883:  
`root@dd83991:~# telnet localhost 1883`  
`Connection closed by foreign host`

Error from BalenaCloud log:  
` telegraf 2021-03-13T23:18:21Z E! [agent] Failed to connect to [outputs.mqtt], retrying in 15s, error was 'Network Error : dial tcp 172.17.0.3:1883: connect: connection refused'`

---

<div class="post-metadata">

**Author:** ![rmorillo24](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/rmorillo24/32/10359_2.png) [@rmorillo24](https://forums.balena.io/u/rmorillo24)\
**Post date:** [March 16, 2021, 1:52pm UTC](https://forums.balena.io/t/balena-sense-esp8266/269418/10 "2021-03-16T13:52:55Z")

</div>

Did you try executing the following in the local host:  
mosquitto\_sub -v -t ‘test/topic’  
mosquitto\_pub -t ‘test/topic’ -m ‘helloWorld’

---

<div class="post-metadata">

**Author:** ![n1md4](https://avatars.discourse-cdn.com/v4/letter/n/d9b06d/32.png) [@n1md4](https://forums.balena.io/u/n1md4)\
**Post date:** [March 16, 2021, 2:31pm UTC](https://forums.balena.io/t/balena-sense-esp8266/269418/12 "2021-03-16T14:31:16Z")

</div>

That works on the local host, thanks for the suggestion. This prompted me to further check the mosquitto configuration and I have made good progress!

I noticed mqtt is running in local host mode only:

```
indent preformatted text by 4 spaces/ # mosquitto -v
1615902985: mosquitto version 2.0.9 starting
1615902985: Using default config.
1615902985: Starting in local only mode. Connections will only be possible from clients running on 
this machine.
1615902985: Create a configuration file which defines a listener to allow remote access.

```

Which was confirmed in this **breaking change** with the release of mosquitto version 2.0:

- When the Mosquitto broker is run without configuring any listeners it will now bind to the loopback interfaces 127.0.0.1 and/or ::1. This means that only connections from the local host will be possible.

I got around that by adding the following settings to mosquitto.conf:

```
bind_address 172.17.0.5
allow_anonymous true

```

Then running the service again:

```
/ # mosquitto -c /mosquitto/config/mosquitto.conf -d
1615904774: The 'bind_address' option is now deprecated and will be removed in a future version. 
The behaviour will default to true.

```

As can be seen, there is a deprecation warning using bind\_address but at least MQTT is now working across a local container network.

I will test and confirm the correct setup and operation of the ESP8266 part of this too before marking as resolved.

---

<div class="post-metadata">

**Author:** ![n1md4](https://avatars.discourse-cdn.com/v4/letter/n/d9b06d/32.png) [@n1md4](https://forums.balena.io/u/n1md4)\
**Post date:** [March 20, 2021, 12:45am UTC](https://forums.balena.io/t/balena-sense-esp8266/269418/14 "2021-03-20T00:45:31Z")

</div>

I’m still struggling to get data from the esp to the pi stack. As a reminder here is the stack:

esp8266 w/ temperature sensor

balena-sense: influx, telegraf, mqtt, grafana – with sensor service removed

I configured telegraf to run in INTERNAL mode which means it’s allowed remote connections. This part works, as I see hits land on the telegraf logs, which leads me nicely on to the problem.

From the telegraf logs I get this error:

```auto
telegraf 2021-03-19T21:45:47Z E! [inputs.mqtt_consumer] Error in plugin: metric parse error: expected tag at 1:16: "esp8266_sensor, sensor=temperature temperature=23.1"

```

I’m not sure where the problem lays, whether it’s message syntax for mqtt or what influxdb is expecting. I’ll post some snippets that someone can hopefully then assist me with.

**esp8266 code**

```auto
String message = String("esp8266_sensor, sensor=temperature temperature="+String(temperature));
client.publish("balena-sense/dd83991/balena-sense", msg);

```

**telegraf**

```auto
name_override = "balena-sense"

```

**influx**

```auto
...

```

Aside from the balena-sense database there are no series entries.

Really need some help on this one, been starring at it for too long, can’t now see wood for trees!!! Thanks in advance. Happy to post more config if required.

---

<div class="post-metadata">

**Author:** ![n1md4](https://avatars.discourse-cdn.com/v4/letter/n/d9b06d/32.png) [@n1md4](https://forums.balena.io/u/n1md4)\
**Post date:** [March 24, 2021, 10:18am UTC](https://forums.balena.io/t/balena-sense-esp8266/269418/15 "2021-03-24T10:18:50Z")

</div>

Rereading my previous posts, I can tell now this was clearly a learning exercise! 🙂

I have this working in principle as I would like now. It was a combination of mqtt topic, telegraf input modification and grafana dashboard creation. I doubt further details add any value to this post but it was nice to finally get it working.

As it is only a minor derivative of balena-sense, is it worth posting as a finished balena project when it is?

---

<div class="post-metadata">

**Author:** ![phil-d-wilson](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/phil-d-wilson/32/3730_2.png) [@phil-d-wilson](https://forums.balena.io/u/phil-d-wilson)\
**Post date:** [March 25, 2021, 10:32am UTC](https://forums.balena.io/t/balena-sense-esp8266/269418/16 "2021-03-25T10:32:46Z")

</div>

Hi Jon,

Thank you for getting back to us! I’m glad to see you have this working now. It would be great if you could write up your project, so others can follow along with this (and future 🤞 ) projects. 🙂

Also, check out our post on balenaBlocks: [Introducing balenaBlocks: jumpstart your IoT app development](https://www.balena.io/blog/introducing-balenablocks-jumpstart-your-iot-app-development/)  
Specifically the `connector` block. It does what you are doing here - just in case it would make things easier for future ventures. One of my colleagues is currently re-implementing balenaSense using blocks, so keep an eye on the blog.

Thanks again,  
Phil

---

<div class="post-metadata">

**Author:** ![green\_agate](https://avatars.discourse-cdn.com/v4/letter/g/41988e/32.png) [@green\_agate](https://forums.balena.io/u/green_agate)\
**Post date:** [April 11, 2021, 6:51pm UTC](https://forums.balena.io/t/balena-sense-esp8266/269418/18 "2021-04-11T18:51:15Z")

</div>

I’m having the same issue. It looks the sensor data is not getting sent or is being blocked somewhere. Like n1md4 says, it seems to be an mqtt broker issue

---

<div class="post-metadata">

**Author:** ![n1md4](https://avatars.discourse-cdn.com/v4/letter/n/d9b06d/32.png) [@n1md4](https://forums.balena.io/u/n1md4)\
**Post date:** [April 11, 2021, 11:25pm UTC](https://forums.balena.io/t/balena-sense-esp8266/269418/19 "2021-04-11T23:25:01Z")

</div>

There are 2 configuration lines that need adding to `/mosquitto/config/mosquitto.conf`.

**Listener:** By default Mosquitto does not allow external connections, should they be required you need to add a listener `listener 1883 <host-ip>`.

**Authentication** : You have two options here, either you have set a user name and password, in which case you need to add those details to mosquitto.conf, else you allow anonymous connections. I have the latter for now `allow_anonymous true`.

Add those 2 lines then run `mosquitto -c /mosquitto/config/mosquitto.conf -d` then you can logout. At which point you should see log entries within Balena Cloud UI.

**Note:** There will be a more elegant solution to add listeners and authentication details as variables and requires adding application build details, as yet I’ve not looked into that though. This means that each time you push any updates you will need to apply this hack to make it work again.

---

<div class="post-metadata">

**Author:** ![n1md4](https://avatars.discourse-cdn.com/v4/letter/n/d9b06d/32.png) [@n1md4](https://forums.balena.io/u/n1md4)\
**Post date:** [April 11, 2021, 11:29pm UTC](https://forums.balena.io/t/balena-sense-esp8266/269418/20 "2021-04-11T23:29:11Z")

</div>

Once you have that working, you will need to add a new dashboard to grafana and all the panels you wish. Once you have a config you like you can export that in json and add it to your balena sense branch. Again, there’s a bit of reworking required to change some of the default grafana bits so it instead displays your custom data, and again something else I’m yet to tidy up! 😃

---

<div class="post-metadata">

**Author:** ![n1md4](https://avatars.discourse-cdn.com/v4/letter/n/d9b06d/32.png) [@n1md4](https://forums.balena.io/u/n1md4)\
**Post date:** [April 11, 2021, 11:32pm UTC](https://forums.balena.io/t/balena-sense-esp8266/269418/21 "2021-04-11T23:32:44Z")

</div>

There may be one or two more configurations required. Let me know if you get stuck, and if I had a fix I’ll happily share it with you. One thing I found very useful during testing was to install mqttbox. Good luck and happy hacking 🙂
