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
}
}
);
}