So, I have an ESP8266. On this board I’m running the newest Version of the MicroPython firmware (1Mib).
I have a Java Vaadin Web Application with 2 Buttons. One Button is for publishing, that the LED should turn off and one Button for turn the LED on.
The MicroPython program on the ESP is running good, till I publish from the Vaadin App. I get the OSError -1 with no other args.
Some informations:
Broker: hivemq in dockerhub localhost
Program for flashing: Thonny
Here is my MicroPython code: (I deleted the ssid and password for data protection -> the client sucessfully connect with the wifi)
from umqtt.simple import MQTTClient
import network
import time
ssid = "ssid"
password = "password"
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
while not wlan.isconnected():
pass
print("hallo")
MQTT_TOPIC = "led/control"
LEDstatus = "off"
ClientID = "Alabama"
LED_PIN = machine.Pin(2, machine.Pin.OUT)
LED_PIN.value(1)
def mqtt_callback(topic, msg):
if msg.decode() == "on":
LEDstatus = "on"
LED_PIN.value(0)
elif msg.decode() == "off":
LEDstatus = "off"
LED_PIN.value(1)
client = MQTTClient(b"Alabama", b"192.168.2.125", 1883)
client.set_callback(mqtt_callback)
client.connect()
client.subscribe(MQTT_TOPIC)
while True:
try:
client.check_msg()
time.sleep(10)
except OSError as e:
print("Fehler beim Empfangen der Nachricht:")
print(e.args)
client.disconnect()
break```
here is my subscribe publish method in Java Vaadin:
```public void publishToDashboard(String topic, String message, String clientID)
throws ExecutionException, InterruptedException, JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
Map<String, String> payload = new HashMap<>();
payload.put("clientID", clientID);
payload.put("message", message);
String strPayload = objectMapper.writeValueAsString(payload);
Mqtt5Publish mqttMessage = Mqtt5Publish.builder()
.topic(topic)
.payload(strPayload.getBytes())
.contentType("json")
.qos(MqttQos.AT_LEAST_ONCE)
.build();
this.client.publish(mqttMessage).get();
System.out.println("published");
}```
also for the Java MQTT Client i'm using this one from hivemq.
and here is the method, that I call when a Button get clicked:
```private void publishLED(ClickEvent<Button> event, String msg, String clientID) {
CompletableFuture.runAsync(() -> {
try {
client.publishToDashboard("led/control", msg, clientID);
} catch (ExecutionException | InterruptedException | JsonProcessingException ex) {
throw new RuntimeException(ex);
}
});
}```
It's my first project with ESP8266 and MQTT, and I literally searched 5h for this Exception but i did'nt find something.
If any informations missing please let me know.
Thank you for trying to help me. :)
Have a good one
Tried: Published message from Web App with button
Expected: ESP8266 catch the message with subscribe and turn LED on or off
What happened: OSError -1
Larshle is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.