# MQTT with ESP8266 and Micropython

**URL:** <https://forums.losant.com/t/mqtt-with-esp8266-and-micropython/3548>\
**Category:** Help\
**Tags:** mqtt\
**Created:** [January 25, 2021, 11:43am UTC](https://forums.losant.com/t/mqtt-with-esp8266-and-micropython/3548 "2021-01-25T11:43:41Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marcelo\_Bohrer](https://avatars.discourse-cdn.com/v4/letter/m/47e85d/32.png) [@Marcelo\_Bohrer](https://forums.losant.com/u/Marcelo_Bohrer)\
**Post date:** [January 25, 2021, 11:43am UTC](https://forums.losant.com/t/mqtt-with-esp8266-and-micropython/3548/1 "2021-01-25T11:43:41Z")

</div>

Hi, good morning…

I need help to send data from ESP8266 to Losant using Micropython. Could you send me a code example? I can connect in Losant but con not send the data…  
Below my code:  
…  
topic = “losant/” + CHANNEL\_ID + “/state”  
payload = { “data” : { “Temperatura” : 34 } }  
…

# Connect Wi-Fi, this function work well

do\_connect()

client = MQTTClient(CHANNEL\_ID,SERVER,1883,WRITE\_API\_KEY,API\_KEY\_PASS)

print(‘connect server…’)  
client.connect() # In the Losant show me connect, okey

# I tried two method tho send data and not work

#client.publish(’/sala01/Temperatura’ , ‘55’)  
client.publish( topic, payload)

time.sleep(30)  
client.disconnect()

![2021-01-25_08h42_22](https://us1.discourse-cdn.com/flex015/uploads/getstructure/original/2X/5/5fed1269cf760111bf165ca21ecd93c2e66d8ebb.png)

---

<div class="post-metadata">

**Author:** ![Heath](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/heath/32/3036_2.png) [@Heath](https://forums.losant.com/u/Heath)\
**Post date:** [January 25, 2021, 2:35pm UTC](https://forums.losant.com/t/mqtt-with-esp8266-and-micropython/3548/3 "2021-01-25T14:35:28Z")

</div>

Hi @Marcelo_Bohrer,

Can you tell me which MicroPython library you’re using for MQTT in this project?

Thank you,  
Heath

---

<div class="post-metadata">

**Author:** ![Marcelo\_Bohrer](https://avatars.discourse-cdn.com/v4/letter/m/47e85d/32.png) [@Marcelo\_Bohrer](https://forums.losant.com/u/Marcelo_Bohrer)\
**Post date:** [January 25, 2021, 2:42pm UTC](https://forums.losant.com/t/mqtt-with-esp8266-and-micropython/3548/5 "2021-01-25T14:42:48Z")

</div>

Hi,

Well, I am novice in Micropython… You refer this lib below

import network  
import time  
import random  
import dht  
from umqtt.simple import MQTTClient  
from machine import Pin

---

<div class="post-metadata">

**Author:** ![Marcelo\_Bohrer](https://avatars.discourse-cdn.com/v4/letter/m/47e85d/32.png) [@Marcelo\_Bohrer](https://forums.losant.com/u/Marcelo_Bohrer)\
**Post date:** [February 5, 2021, 6:54pm UTC](https://forums.losant.com/t/mqtt-with-esp8266-and-micropython/3548/7 "2021-02-05T18:54:35Z")

</div>

Hi,

Does not work. I can connect at Losant but I can not send or publish the payload.

---

<div class="post-metadata">

**Author:** ![anaptfox](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/anaptfox/32/873_2.png) [@anaptfox](https://forums.losant.com/u/anaptfox)\
**Post date:** [February 5, 2021, 7:06pm UTC](https://forums.losant.com/t/mqtt-with-esp8266-and-micropython/3548/9 "2021-02-05T19:06:42Z")

</div>

Hey @Marcelo_Bohrer ,

Could you post the full contents of your script? ( removing keys or other confidential information )

That way, we can take a look at how you’re attempting to publish/subscribe and offer some advice.

---

<div class="post-metadata">

**Author:** ![Marcelo\_Bohrer](https://avatars.discourse-cdn.com/v4/letter/m/47e85d/32.png) [@Marcelo\_Bohrer](https://forums.losant.com/u/Marcelo_Bohrer)\
**Post date:** [February 10, 2021, 11:46am UTC](https://forums.losant.com/t/mqtt-with-esp8266-and-micropython/3548/10 "2021-02-10T11:46:01Z")

</div>

```auto
import network
import time
import random
from umqtt.simple import MQTTClient

 
WiFi_SSID = "...."
WiFi_PASS = "..."

SERVER = "broker.losant.com"

CHANNEL_ID = "..."
WRITE_API_KEY = "..."
API_KEY_PASS = '...'

topic = "losant/" + CHANNEL_ID + "/state"

#
# Define payload, just a example
#
payload = { "Temperatura": 34 } 
 

def do_connect():
    wlan = network.WLAN(network.STA_IF)
    wlan.active(True)
    print('is connected? ', wlan.isconnected())
    if not wlan.isconnected():
       print('connecting to network...')
       wlan.connect(WiFi_SSID, WiFi_PASS)
       print('is connected? ', wlan.isconnected())
       
       while not wlan.isconnected():
          pass
          print('network config:', wlan.ifconfig())   

#
# Connect Wi-Fi, OK
#

do_connect()

client = MQTTClient(CHANNEL_ID,SERVER,1883,WRITE_API_KEY,API_KEY_PASS)

print('connect server ok ...')

#
# Until here work well. I can connect at Losant
#

client.connect()

# I did this command and no way
#client.publish('/sala01/Temperatura' , '55') 

#
# Here is my problem... I can not send data, I don't know what I am doing wrong.
#
client.publish( topic, payload)

time.sleep(30)

# After time disconnect ok
client.disconnect()

    
print('Fim...The End...')

```

---

<div class="post-metadata">

**Author:** ![Marcelo\_Bohrer](https://avatars.discourse-cdn.com/v4/letter/m/47e85d/32.png) [@Marcelo\_Bohrer](https://forums.losant.com/u/Marcelo_Bohrer)\
**Post date:** [February 10, 2021, 11:48am UTC](https://forums.losant.com/t/mqtt-with-esp8266-and-micropython/3548/12 "2021-02-10T11:48:16Z")

</div>

Hi,

I am using this…

from umqtt.simple import MQTTClient

---

<div class="post-metadata">

**Author:** ![Heath](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/heath/32/3036_2.png) [@Heath](https://forums.losant.com/u/Heath)\
**Post date:** [February 10, 2021, 8:04pm UTC](https://forums.losant.com/t/mqtt-with-esp8266-and-micropython/3548/13 "2021-02-10T20:04:08Z")

</div>

@Marcelo_Bohrer,

In your Losant Application, in the overview section, you have your Application Log on the right side. When a device connects and reports will be information there, and if there is an issue, errors will be displayed there. It will look something like this:

![image](https://us1.discourse-cdn.com/flex015/uploads/getstructure/original/2X/7/747008a3675de8870ef8290f7ab705f0c17d2271.png)

What do you see in your application log when this script runs?

Thank you,  
Heath
