Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Minimal example to interact with Sonoff S20 using MQTT and JavaScript
<!-- Minimal example to interact with Sonoff S20 device with MQTT
for a blog post (
<!doctype html>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Sonoff example</title>
<link rel="stylesheet" href="">
<script src="" type="text/javascript"></script>
<script src=""></script>
<!-- Here is the interesting part -->
<script >
// Connect to MQTT broker
// Note. this example uses web-sockets (port 1884)
mqtt = new Paho.MQTT.Client("", Number(1884), "sonoff-example-client");
mqtt.connect({onSuccess: function() {
function toggleSonoff() {
// Send 'toggle' message to command topic
message = new Paho.MQTT.Message("toggle");
message.destinationName = "cmnd/sonoff1/power";
<div class="container">
<div class="row text-center span2">
<div class="page-header">
<h2>Sonoff example</h2>
<button type="submit" class="btn btn-primary btn-lg" onclick="toggleSonoff()">Toggle</button>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment