• Home
  • Archive
  • Tools
  • Contact Us

The Customize Windows

Technology Journal

  • Cloud Computing
  • Computer
  • Digital Photography
  • Windows 7
  • Archive
  • Cloud Computing
  • Virtualization
  • Computer and Internet
  • Digital Photography
  • Android
  • Sysadmin
  • Electronics
  • Big Data
  • Virtualization
  • Downloads
  • Web Development
  • Apple
  • Android
Advertisement
You are here:Home » How to Run Your Own IoT Cloud on a VPS

By Abhishek Ghosh March 25, 2021 10:03 am Updated on March 25, 2021

How to Run Your Own IoT Cloud on a VPS

Advertisement

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.

Advertisement

---

In this article, we are providing you with the basic steps to install Mosquitto and Mosquitto client. Installation is just easy :

Vim
1
sudo apt-get install mosquitto mosquitto-clients

Test it by publishing a message :

Vim
1
mosquitto_pub -h localhost -t test -m "hello world"

Subscribe has a similar command :

Vim
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 :

Vim
1
sudo mosquitto_passwd -c /etc/mosquitto/passwd abhishek

Now, create the configuration file :

Vim
1
sudo nano /etc/mosquitto/conf.d/default.conf

Paste these three lines :

Vim
1
2
3
allow_anonymous false
password_file /etc/mosquitto/passwd
listener 1883

Save it and reload Mosquitto :

Vim
1
sudo systemctl restart mosquitto

Now, if you run the above command to publish a message, it will be rejected :

Vim
1
mosquitto_pub -h localhost -t test -m "hello world"

You have to run the command with username and password (P is capital) :

Vim
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.

How to Run Your Own IoT Cloud on a VPS

Here is a sample sketch for ESP32 Arduino to test the thing :

Vim
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
Facebook Twitter Pinterest

Abhishek Ghosh

About Abhishek Ghosh

Abhishek Ghosh is a Businessman, Surgeon, Author and Blogger. You can keep touch with him on Twitter - @AbhishekCTRL.

Here’s what we’ve got for you which might like :

Articles Related to How to Run Your Own IoT Cloud on a VPS

  • How to Control Multiple Relays With Single Arduino ESP32?

    Before How to Control Multiple Relays With Single Arduino ESP32 Testing, You Need to Learn How to Create Multiple MQTT Channels & Fetch Data.

  • WROOM ESP32 Example Codes For IBM Watson IoT Platform

    Here Are Few WROOM ESP32 Example Codes For IBM Watson IoT Platform So That Anyone Can Get Started With Both of Them Without Huge Experience.

  • Connecting ESP32 Arduino with DHT11 with IBM Watson IoT

    Earlier, we described how to create graph on IBM Watson IoT dashboard by using the default widgets. In previous guide, we described how to use ESP32 Arduino with DHT11 sensor. Here is the Code and Diagram to Connect ESP32 Arduino with DHT11 with IBM Watson IoT and Get Odometer Like Gauges on Dashboard. For this […]

  • Detect Smartwatch With ESP32 on IBM Watson IoT Widget

    In our previous guide, we have shown that we can trigger ESP32 (with Arduino IDE) to send message to IBM Watson IoT in Presence of a Particular Samsung Galaxy Smartwatch. That process involves BLE and WiFi. In our one series of articles on Samsung Smartwatch as Proximity Switch, we triggered a local event, such as […]

performing a search on this website can help you. Also, we have YouTube Videos.

Take The Conversation Further ...

We'd love to know your thoughts on this article.
Meet the Author over on Twitter to join the conversation right now!

If you want to Advertise on our Article or want a Sponsored Article, you are invited to Contact us.

Contact Us

Subscribe To Our Free Newsletter

Get new posts by email:

Please Confirm the Subscription When Approval Email Will Arrive in Your Email Inbox as Second Step.

Search this website…

 

Popular Articles

Our Homepage is best place to find popular articles!

Here Are Some Good to Read Articles :

  • Cloud Computing Service Models
  • What is Cloud Computing?
  • Cloud Computing and Social Networks in Mobile Space
  • ARM Processor Architecture
  • What Camera Mode to Choose
  • Indispensable MySQL queries for custom fields in WordPress
  • Windows 7 Speech Recognition Scripting Related Tutorials

Social Networks

  • Pinterest (24.3K Followers)
  • Twitter (5.8k Followers)
  • Facebook (5.7k Followers)
  • LinkedIn (3.7k Followers)
  • YouTube (1.3k Followers)
  • GitHub (Repository)
  • GitHub (Gists)
Looking to publish sponsored article on our website?

Contact us

Recent Posts

  • Hybrid Multi-Cloud Environments Are Becoming UbiquitousJuly 12, 2023
  • Data Protection on the InternetJuly 12, 2023
  • Basics of BJT TransistorJuly 11, 2023
  • What is Confidential Computing?July 11, 2023
  • How a MOSFET WorksJuly 10, 2023
PC users can consult Corrine Chorney for Security.

Want to know more about us?

Read Notability and Mentions & Our Setup.

Copyright © 2023 - The Customize Windows | dESIGNed by The Customize Windows

Copyright  · Privacy Policy  · Advertising Policy  · Terms of Service  · Refund Policy