LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Hardware > Linux - Embedded & Single-board computer
User Name
Password
Linux - Embedded & Single-board computer This forum is for the discussion of Linux on both embedded devices and single-board computers (such as the Raspberry Pi, BeagleBoard and PandaBoard). Discussions involving Arduino, plug computers and other micro-controller like devices are also welcome.

Notices


Closed Thread
  Search this Thread
Old 11-28-2016, 04:20 AM   #1
istabraq
LQ Newbie
 
Registered: Nov 2016
Posts: 11

Rep: Reputation: Disabled
MQTT mosquitto connecting to Node.js and MongoDB but i can not see output


Hi all,
i using NodeMCU v1.0, and i`m connected to mosquitto broker using mqtt and works fine and i can see my data in the broker..
then i need to store the data so i used node.js and mongoDB but when when i run the node the data was not stored in database
i don`t know where is the problem
please help
thanks in advance.
Code:
 
#include <ESP8266WiFi.h>
#include <PubSubClient.h>

const char* ssid = "*****";
const char* password = "*****";
const char* mqtt_server = "10.10.10.117";

int pulse = 0;
char buffer[10];

WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
unsigned long lastTime =0;

void setup_wifi(void);
void callback(char* topic, byte* payload, unsigned int length);

void setup() {

  Serial.begin(115200);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
}

void setup_wifi() {

  delay(10);
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  Serial.print("Connecting to");
  Serial.println(mqtt_server);
}

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();

}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect("ESP8266Client")) {
      Serial.println("connected");
      client.publish("outTopic", "hello world");
      client.subscribe("inTopic");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      delay(5000);
    }
  }
}
void loop() {

  if (!client.connected()) {
    reconnect();
  }
  client.loop();
  
   pulse=analogRead(A0);
   delay(1000);
   Serial.println(pulse);
     if(pulse!=lastMsg) {
      if(millis()>(lastTime+1000)) {
      sprintf(buffer,"%d",pulse); 
     client.publish("pulse",buffer); 
      lastMsg=pulse;
      lastTime=millis();      
      }
     }
  client.loop();
}
Code:
var mqtt = require('mqtt'); //includes mqtt server 
var mongodb = require('mongodb'); // includes mongoDB 
var mongodbClient = mongodb.MongoClient; //initialises the mongoDB client
var mongodbURI = 'mongodb://localhost:27017/pulses'; //activating the MongoDB port 27017, here TempMontor is the name of the database
var deviceRoot = "#"; //deviceroot is topic name given in arduino code 
var collection,client; //initialise collection and client

mongodbClient.connect(mongodbURI, setupCollection); //connect the database with collecion
     
function setupCollection(err, db) {
   if(err) throw err;
   collection=db.collection("test_mqtt"); //name of the collection in the database
   client=mqtt.connect({ host: 'localhost', port: 1883 }); //connecting the mqtt server with the MongoDB database
   client.subscribe(deviceRoot+"+"); //subscribing to the topic name 
   client.on('message', insertEvent); //inserting the event
}

//function that displays the data in the MongoDataBase
function insertEvent(topic,message) {
   var key=topic.replace(deviceRoot,'');

   collection.update(
   { _id:key }, 
   { $push: { events: { event: {  value:message, when:new Date() } } } }, 
   { upsert:true },

   function(err,docs) {  
   if(err) {
      console.log("Insert fail")// Improve error handling		
	 }
 }

 );

}

Last edited by istabraq; 11-28-2016 at 04:22 AM.
 
Old 11-28-2016, 07:39 AM   #2
Timothy Miller
Moderator
 
Registered: Feb 2003
Location: Arizona, USA
Distribution: Debian, EndeavourOS, OpenSUSE, KDE Neon
Posts: 4,003
Blog Entries: 26

Rep: Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521
Please post your thread in only one forum. Posting a single thread in the most relevant forum will make it easier for members to help you and will keep the discussion in one place. This thread is being closed because it is a duplicate.
 
  


Closed Thread



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
MQTT mosquitto connecting to Node.js and MongoDB but i can not see output istabraq Linux - Desktop 1 12-17-2016 10:15 PM
mysqld node of mysql cluster system not connecting to management node coal-fire-ice Linux - Server 1 07-27-2015 08:33 AM
rhel 2 node cluster: resources do not move to second node when node halted tapuhi Linux - Software 1 03-01-2015 10:41 AM
Connecting 3 systems to a network through 1 node arjun_sh Linux - Networking 9 03-25-2006 03:57 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Hardware > Linux - Embedded & Single-board computer

All times are GMT -5. The time now is 05:14 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration