There are many reasons why some of the users do not like commercial IoT platforms such as IBM Watson IoT for simple works like that of controlling light, motor and other simple electrical devices. If we take IBM Watson IoT as an example commercial platform, just to use ESP32 Arduino, you have to depend on the peoples of the community like us. You have to check our sample projects such as ESP32-IBM-Watson-IoT-Example and adapt for your need. Unlike the vanilla MQTT installation, you are restricted to easily use any Android app for the MQTT dashboard. Unfortunately, there are too many commercial IoT platforms and the number of makers for the IoT segment is not huge in number. Official documentation of IBM Watson IoT is not easy to understand after reading once by a newbie. The second problem is the cost. Commercial IoT platforms are great within the free usage tier. They are super costly when you want to implement your system for your whole house. Keep in mind, IBM uses mostly open-source things. The situation goes worse with many other platforms.
So, for making a bunch of switches smart, you probably should run an MQTT broker on a VPS. If your code once, you’ll not need to modify the code for many years. I must warn you – this is not easy work. As we have mentioned before on this website, on lowendbox.com
you’ll get reviews of low-cost Virtual Private Servers with 64MB or more RAM at less than $1/month running cost. You’ll get free domain name somewhere on this internet (or just use a sub-domain of your existing TLD), free Let’s Encrypt is more than enough to secure the connection.
You can use the original Mosquitto, Mosquitto client, Paho for web GUI, Node-RED for simplified flow-based programming. These are the basic packages behind IBM Watson IoT.
---
In this article, we are providing you with the basic steps to install Mosquitto and Mosquitto client. Installation is just easy :
1 | sudo apt-get install mosquitto mosquitto-clients |
Test it by publishing a message :
1 | mosquitto_pub -h localhost -t test -m "hello world" |
Subscribe has a similar command :
1 | mosquitto_sub -h localhost -t test |
We have to create a default configuration file. As the first step, create a user with a password :
1 | sudo mosquitto_passwd -c /etc/mosquitto/passwd abhishek |
Now, create the configuration file :
1 | sudo nano /etc/mosquitto/conf.d/default.conf |
Paste these three lines :
1 2 3 | allow_anonymous false password_file /etc/mosquitto/passwd listener 1883 |
Save it and reload Mosquitto :
1 | sudo systemctl restart mosquitto |
Now, if you run the above command to publish a message, it will be rejected :
1 | mosquitto_pub -h localhost -t test -m "hello world" |
You have to run the command with username and password (P is capital) :
1 | mosquitto_pub -h ip.address -t "test" -m "hello world" -u "abhishek" -P "password" |
If you can run the above command without facing an error, you are done completing a basic setup.
Here is a sample sketch for ESP32 Arduino to test the thing :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | #include <WiFi.h> #include <PubSubClient.h> // Update these with values suitable for your network. const char* ssid = "paste-here"; const char* password = "paste-here"; const char* mqtt_server = "paste-here"; #define mqtt_port 1883 #define MQTT_USER "abhishek" #define MQTT_PASSWORD "password" #define MQTT_SERIAL_PUBLISH_CH "/example/ESP32/serialdata/tx" #define MQTT_SERIAL_RECEIVER_CH "/example/ESP32/serialdata/rx" WiFiClient wifiClient; PubSubClient client(wifiClient); void setup_wifi() { delay(10); // We start by connecting to a WiFi network Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } randomSeed(micros()); Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); } void reconnect() { // Loop until we're reconnected while (!client.connected()) { Serial.print("Attempting MQTT connection..."); // Create a random client ID String clientId = "ESP32Client-"; clientId += String(random(0xffff), HEX); // Attempt to connect if (client.connect(clientId.c_str(),MQTT_USER,MQTT_PASSWORD)) { Serial.println("connected"); //Once connected, publish an announcement... client.publish("/example/ESP32/serialdata/messages", "hello world"); // ... and resubscribe client.subscribe(MQTT_SERIAL_RECEIVER_CH); } else { Serial.print("failed, rc="); Serial.print(client.state()); Serial.println(" try again in 5 seconds"); // Wait 5 seconds before retrying delay(5000); } } } void callback(char* topic, byte *payload, unsigned int length) { Serial.println("-------new message from broker-----"); Serial.print("channel:"); Serial.println(topic); Serial.print("data:"); Serial.write(payload, length); Serial.println(); } void setup() { Serial.begin(115200); Serial.setTimeout(500);// Set time out for setup_wifi(); client.setServer(mqtt_server, mqtt_port); client.setCallback(callback); reconnect(); } void publishSerialData(char *serialData){ if (!client.connected()) { reconnect(); } client.publish(MQTT_SERIAL_PUBLISH_CH, serialData); } void loop() { client.loop(); if (Serial.available() > 0) { char mun[501]; memset(mun,0, 501); Serial.readBytesUntil( '\n',mun,500); publishSerialData(mun); } } |
If you complete the total thing without facing any error, then try some MQTT Dashboard App for Android. You have to read the MQTT documentation for commands, install a web GUI like Paho.
Tagged With vps netcup howto language:en