Unable to send data to ThingsBoard gateway through MQTT - mqtt

I have installed ThingsBoard server on one PC (UBUNTU16.04) and ThingsBoard Gateway on another PC(UBUNTU18.04) ,In order to send data to ThingsBoard Gateway I installed Mosquitto MQTT broker on another PC.I followed configuration guides to connect broker to Gateway as well as server (using access token and host ip).
I connected temperature sensor to ESP32. While I am trying to send the data to gateway through MQTT the data is not getting to the gateway.The topic I used here is "v1/gateway/telemetry" in order to publish the data.
Can we use Gateway Device ID to send data?
How can I send data either by using topic or by using device id or by using device access token?(from device)
All the PC 's are connected to the same network(Private network).
I am facing this issue can Someone please sort it out...

You need to create a Proxy Layer Between MQTT Broker and your server.
var mqtt = require('mqtt'), url = require('url');
var client = mqtt.connect('mqtt://localhost:1883',
{
username: '<username>',
password: '<password>'
});
console.log("Connected to MQTT Broker:- localhostā€ + client.toString());
var awsIot = require('aws-iot-device-sdk');
var device = awsIot.device({
keyPath: Certificate key file path,
certPath: Certificate file path,
caPath: Certificate root file path,
clientId: AWS Thing Name,
region: AWS IoT Broker region,
});
device.on('connect', function ()
{
console.log("Connected to AWS IoT Broker:- " + device.toString());
});
client.on('connect', function()
{
//subscribe to a topic (#)
client.subscribe('#', function ()
{
client.on('message', function (topic, message, packet) {
console.log("Received :-" + message + " on " + topic);
device.publish(topic, message);
console.log("Sent :-" + message + " on " + topic);
});
});
});
Something like this might help you.

Related

Unable to connect to MQTT broker via ngx-mqtt in angular8

No matter what i do i can't connect to a mqtt broker via websocket in my angular application (trying in chrome and firefox).
For simplicity i'm using HiveMQ broker, i've published on the topic /gat/38/openReservationRequests some data
I've followed this medium article on how to connect to mqtt in angular using ngx-mqtt but for me it is not working.
In my app:
I've installed the module
npm install ngx-mqtt --save
i've added the configuration and set the module forRoot in my app.module.ts
...
export const MQTT_SERVICE_OPTIONS: IMqttServiceOptions = {
connectOnCreate: true,
hostname: 'broker.hivemq.com',
port: 8000,
path: '/gat/38/openReservationRequests',
protocol: 'ws',
};
...
imports: [
...
MqttModule.forRoot(MQTT_SERVICE_OPTIONS),
...
],
...
i'm executing this function inside the ngOnInit of app.component.ts
...
import { IMqttMessage, MqttConnectionState, MqttService } from 'ngx-mqtt';
...
constructor(private mqttService: MqttService) {
this.mqttService.state.subscribe((s: MqttConnectionState) => {
const status = s === MqttConnectionState.CONNECTED ? 'CONNECTED' : 'DISCONNECTED';
this.status.push(`Mqtt client connection status: ${status}`);
});
}
ngOnInit() {
this.subscription = this.mqttService
.observe('/gat/38/openReservationRequests')
.subscribe((message: IMqttMessage) => {
this.msg = message;
console.log('msg: ', message);
console.log('Message: ' + message.payload.toString() + 'for topic: ' + message.topic);
console.log('subscribed to topic: ' + /gat/38/openReservationRequests);
});
}
but i am always getting this error:
core.js:6014 ERROR TypeError: Cannot read property 'resubscribe' of undefined
at MqttClient.subscribe (mqtt.min.js:1)
at mqtt.service.js:211
at Observable._subscribe (using.js:8)
at Observable._trySubscribe (Observable.js:42)
at Observable.subscribe (Observable.js:28)
at FilterOperator.call (filter.js:13)
at Observable.subscribe (Observable.js:23)
at Observable.connect (ConnectableObservable.js:30)
at RefCountOperator.call (refCount.js:17)
at Observable.subscribe (Observable.js:23)
mqtt.min.js:1 WebSocket connection to 'ws://broker.hivemq.com:8000/gat/38/openReservationRequests' failed: Connection closed before receiving a handshake response
if i specify the clientId inside the MQTT_SERVICE_OPTIONS i still get the same error.
if i change the protocol to wss i get a different error:
core.js:6014 ERROR TypeError: Cannot read property 'resubscribe' of undefined
at MqttClient.subscribe (mqtt.min.js:1)
at mqtt.service.js:211
at Observable._subscribe (using.js:8)
at Observable._trySubscribe (Observable.js:42)
at Observable.subscribe (Observable.js:28)
at FilterOperator.call (filter.js:13)
at Observable.subscribe (Observable.js:23)
at Observable.connect (ConnectableObservable.js:30)
at RefCountOperator.call (refCount.js:17)
at Observable.subscribe (Observable.js:23)
mqtt.min.js:1 WebSocket connection to 'wss://broker.hivemq.com:8000/gat/38/openReservationRequests' failed: Error in connection establishment: net::ERR_CONNECTION_CLOSED
If i try to connect manually inside my app.component.ts ngOnInit before observing the topic:
this.mqttService.connect({
hostname: 'broker.hivemq.com',
port: 8000,
path: '/gat/38/openReservationRequests',
clientId: '34er23qwrfq42w3' //those are just random digits
});
i still get the error above.
For me it would be ideal to connect in some inner component (accessible after the user is authenticated) because i will have my private mqtt broker and the topic will depend on the logged user information.
I've tried any combination of protocol with/without cliendId etc but at this point i don't know what is wrong. I've already fully recompiled my app lots of times, i've tried publishing it on my test-server which has a ssl certificate but nothing changed.
Resolved thanks to #Anant Lalchandani i set the correct path.
The other problem was that '/mytopic' and 'mytopic' are indeed two different topic and i was using it wrong too.
This is my code, updated:
app.module.ts
export const MQTT_SERVICE_OPTIONS: IMqttServiceOptions = {
connectOnCreate: false,
hostname: 'broker.hivemq.com',
port: 8000,
path: '/mqtt'
};
appcomponent.ts (inside ngOnInit for now)
this.mqttService.connect({
hostname: 'broker.hivemq.com',
port: 8000,
path: '/mqtt',
clientId: '1234e3qer23rf'
});
this.mqttService.onConnect
.subscribe(
connack=> {
console.log('CONNECTED');
console.log(connack);
}
);
this.mqttService.observe('gat/38/openReservationRequests')
.subscribe((message: IMqttMessage) => {
this.msg = message;
console.log(new TextDecoder('utf-8').decode(message.payload));
});
I have checked the code snippets you shared in question.
In your app.module.ts, the path value should be '/mqtt'. You have set the topic as the value of path here. The topic can only be subscribed/published. As you are using a topic as a path value at the time of connecting to a websocket, your application will not be able to connect to websocket at the first place.
The reason why we need to use /mqtt as a path is it specifies you are sending MQTT messages over the WebSocket protocol.
The documentation of HiveMQ itself stated to use the path as '/mqtt' in its example. You can check the documentation here.

Send data from ESP32 to a server via WiFi

I am not new here but this is my first question.
I have searched a lot and quite frankly can't understand how this is supposed to work.
I get data periodically (temperature) to my ESP32 and while having it set as a WiFi client, connect to my router and somehow store this data on my Laptop(or somewhere else, like a local/web site, don't know if that's possible/better).
How is the connection supposed to work? I have installed XAMPP and run the Apache and MySQL servers and I tried to connect to my Laptop with some sketches from Arduino using the ESP32 libraries
// Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 80;
const char* host = "192.168.1.109"; //The local IP of my Laptop
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
but it doesn't connect.
Can someone please explain to me how this connection is supposed to take form or is this question too vague? I really just wanna know the "how-things-should-work-together" in this situation.
Thank you in advance.
OK, so after a lot of research and trying, I managed to work it out. I can now send an HTTP request (like GET or POST) from my ESP32 to a local server that is running on my laptop using XAMP and get a response. I can also connect to my local IP from my mobile phone (which is also in the same WiFi network).
Just for anyone else who wants to connect to a location in a server hosted on a PC in a local network, the steps are:
Create a local server on your PC, laptop whatever using an application like XAMPP (I have Windows 10 so WAMP would also work), download, install, open and start Apache.
Make sure that the Firewall lets your requests pass through (for me it was open by default, but I had seen elsewhere Firewall being an issue)
Go to your network settings, select the network that your devices(ESP32, phone, etc.)are connected and change its profile to Private, meaning that you trust this network, making your PC discoverable and able to accept requests. (That is really simple but took me hours to find)
Now, in order to connect from your phone to your PC, open a browser and enter the local IP (that is the IP that is given to your PC from the router as a local network name) of your PC to a browser and that's it, you are connected.
If you installed and ran XAMP, when connecting to your local IP(from same PC or other local device), it will forward you to 192.168.x.x/dashboard. If you want to create new workspaces and files, browse the XAMP folder in the installed location and inside the '/htdocs' subfolder do your testing.
For the ESP32 communication in Arduino(basic steps, not full code):
#include <WiFi.h>
#include <HTTPClient.h>
String host = "http://192.168.x.x/testfolder/";
String file_to_access = "test_post.php";
String URL = host + file_to_access;
void setup(){
WiFi.begin(ssid, password); //Connect to WiFi
HTTPClient http;
bool http_begin = http.begin(URL);
String message_name = "message_sent";
String message_value = "This is the value of a message sent by the ESP32 to local server
via HTTP POST request";
String payload_request = message_name + "=" + message_value; //Combine the name and value
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
int httpResponseCode = http.sendRequest("POST", payload_request);
String payload_response = http.getString();
}
In the test_post.php (located in "C:\xampp\htdocs\testfolder\") file I used a simple script to echo a message received using a POST request, so it's only 'readable' from POST requests. Connecting to it from your browser will give you the "Sorry, accepting..." message.
<?php
$message_received = "";
if ($_SERVER["REQUEST_METHOD"] == "POST"){
$message_received = $_POST["message_sent"];
echo "Welcome ESP32, the message you sent me is: " . $message_received;
}
else {
echo "Sorry, accepting only POST requests...";
}
?>
Finally, using Serial prints, the output is:
Response Code: 200
Payload: Welcome ESP32, the message you sent me is: This is the value of a message sent by the ESP32 to local server via HTTP POST request
There it is, hope that this helps someone.

Securing Pusher's messages

I am using Pusher Channels and delivering messages like in their tutorial:
https://pusher.com/docs/channels/getting_started/javascript
Client:
let pusher = new Pusher('APP_KEY', {
cluster: 'APP_CLUSTER'
});
let channel = pusher.subscribe('my-channel');
channel.bind('my-event', function(data) {
alert('An event was triggered with message: ' + data.message);
});
Server:
// First, run 'npm install pusher'
var Pusher = require('pusher');
var pusher = new Pusher({
appId: 'APP_ID',
key: 'APP_KEY',
secret: 'APP_SECRET',
cluster: 'APP_CLUSTER'
});
pusher.trigger('my-channel', 'my-event', {"message": "hello world"});
Is there a way to secure the connection via port 443 on the IIS?
To make sure Pusher Channels messages are sent from your server to Channels using HTTPS and then broadcast to Clients using WSS you need to the do the following:
For the nodejs library running on the server you need to set the option useTLS: true
var pusher = new Pusher({
appId: 'APP_ID',
key: 'APP_KEY',
secret: 'APP_SECRET',
cluster: 'APP_CLUSTER',
useTLS: true
});
https://github.com/pusher/pusher-http-node#configuration
For the pusher-js library running on the client you need to set the option forceTLS: true
let pusher = new Pusher('APP_KEY', {
cluster: 'APP_CLUSTER',
forceTLS: true
});
https://github.com/pusher/pusher-js#configuration
Finally to make sure that client connections are only accepted over a secure connection, you need to log into your Channels Dashboard account, find the app you need to secure, and click the App settings tab. Finally you need to tick the box "Force TLS" and click "Update" to apply settings. With this box ticked, client connections over port 80 will be rejected and the client will be forced to reconnect using a secure connection:

Not connecting user using MQTTjs on cloudmqtt

I am having this code in my file (belong to react)
const client = mqtt.connect({
host: 'mqtt://m16.cloudmqtt.com',
port: 1883,
username: 'b*******k',
password: 'gU******S',
});
client.on('connect', () => {
console.log('hello');
client.subscribe('v');
client.publish('v', 'chal pa');
});
client.on('message', (topic, message) => {
if (topic === 'v') {
console.log('here my topic is v');
// var connected = (message.toString() === 'true');
}
console.log('recived message from mqtt');
console.log(message);
});
client.on('error', er => {
console.log(er);
});
I am expecting to connect to mqtt broker and receive some message.
But nothing happened. When i check log file in cloudmqtt.com
I am stuck here can anybody help. Link to any blog/video that will help will be highly appreciated.
I am using mqttjs
You have explicitly told the MQTTjs library to use native MQTT rather than MQTT over Websockets by using mqtt:// on the start of the URI.
If you want to use MQTT over websockets the URI should start with ws://
Secondly you are using port 1883, this is normally used for native MQTT not MQTT over websockets. The cloudmqtt docs suggest you should be using a port number that starts with a 3 to access the websockets listener.

MQTT+Mosquitto+Javascript in windows

I am new in MQTT so can someone help me for connecting MQTT with Mosquitto using javascript i am using this code but it give error...
Connection failed: AMQJS0007E Socket error:undefined.
My Code is :
<script type='text/javascript' src='jquery-1.10.1.js'></script>
<script type='text/javascript' src="mqttws31.js"></script>
var client = new Messaging.Client("ns.testingindia.tld", 1883, "myclientid_" + parseInt(Math.random() * 100, 10));
//Gets called if the websocket/mqtt connection gets disconnected for any reason
client.onConnectionLost = function (responseObject) {
//Depending on your scenario you could implement a reconnect logic here
alert("connection lost: " + responseObject.errorMessage);
};
//Gets called whenever you receive a message for your subscriptions
client.onMessageArrived = function (message) {
//Do something with the push message you received
$('#messages').append('Topic: ' + message.destinationName + ' | ' + message.payloadString + '');
};
//Connect Options
var options = {
timeout: 3,
//Gets Called if the connection has sucessfully been established
onSuccess: function () {
alert("Connected");
},
//Gets Called if the connection could not be established
onFailure: function (message) {
document.write("Connection failed: " + message.errorMessage);
alert("Connection failed: " + message.errorMessage);
}
};
//Creates a new Messaging.Message Object and sends it to the HiveMQ MQTT Broker
var publish = function (payload, topic, qos) {
//Send your message (also possible to serialize it as JSON or protobuf or just use a string, no limitations)
var message = new Messaging.Message(payload);
message.destinationName = topic;
message.qos = qos;
client.send(message);
}
//]]>
You are connecting to port 1883 which is the default MQTT port. I assume you mean to use Websockets, and that would typically be configured on a different port number. If the broker you're using has Websocket support, ensure you connect to the correct port with Messaging.Client().
If you're using the Mosquitto broker, you'll need version 1.4 from its bitbucket repository for Websocket support, but note that Mosquitto 1.4 hasn't yet been released.
A quick way to test that your broker isn't causing the problem is to connect to broker.mqttdashboard.com port:8000 if that doesn't work my next guess is that you have just mosquitto installed and no websockets server, which you need if you want to use JS to connect directly to the broker over the web.
Another, but quicker way to get up and running now is downloading hivemq (trial version supports 25 connections) it has a mqtt broker with websockets built in and will run on windows and will be up and running in 5 mins.
Which version of Mosquitto are you using?
The current release version (1.3.4) does not natively support Websockets (next version will)
You can use something like lighttpd with mod_websockets to supply websocket support (instructions for linux are linked to from here: http://test.mosquitto.org/ws.html) or you can build a new version of Mosquitto from the head of the source tree

Resources