I'm a beginner of IoT.
I Try to On / Off Light using nodemcu and mqtt and node js server.
My Code:
mqtt-server.js
var mosca = require('mosca');
var settings = {
port: 1883
};
var mqtt = require('mqtt');
console.log(mqtt);
client = mqtt.createClient(1883, 'localhost');
//here we start mosca
var server = new mosca.Server(settings);
server.on('ready', setup);
// fired when the mqtt server is ready
function setup() {
console.log('Mosca server is up and running')
}
// fired whena client is connected
server.on('clientConnected', function(client) {
console.log('client connected', client.id);
});
// fired when a message is received
server.on('published', function(packet, client) {
// console.log('Published : ', packet.payload);
});
// fired when a client subscribes to a topic
server.on('subscribed', function(topic, client) {
console.log('subscribed : ', topic);
});
// fired when a client subscribes to a topic
server.on('unsubscribed', function(topic, client) {
console.log('unsubscribed : ', topic);
});
// fired when a client is disconnecting
server.on('clientDisconnecting', function(client) {
console.log('clientDisconnecting : ', client.id);
});
// fired when a client is disconnected
server.on('clientDisconnected', function(client) {
console.log('clientDisconnected : ', client.id);
});
light.ino
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]);
}
// Switch on the LED if an 1 was received as first character
if ((char)payload[0] == '1') {
digitalWrite(ledPin, LOW);
digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level
snprintf (msg, 75, "LIGHT STATUS OFF");
// but actually the LED is on; this is because
// it is acive low on the ESP-01)
} else {
digitalWrite(ledPin, HIGH);
digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off by making the voltage HIGH
snprintf (msg, 75, "LIGHT STATUS ON");
}
}
Any one please help me? What is wrong with this code?
Hi,
I'm a beginner of IoT. I Try to On / Off Light using nodemcu and mqtt and node js server. My Code:
mqtt-server.js var mosca = require('mosca'); var settings = { port: 1883 }; var mqtt = require('mqtt'); console.log(mqtt); client = mqtt.createClient(1883, 'localhost'); //here we start mosca var server = new mosca.Server(settings); server.on('ready', setup); // fired when the mqtt server is ready function setup() { console.log('Mosca server is up and running') } // fired whena client is connected server.on('clientConnected', function(client) { console.log('client connected', client.id); }); // fired when a message is received server.on('published', function(packet, client) { // console.log('Published : ', packet.payload); }); // fired when a client subscribes to a topic server.on('subscribed', function(topic, client) { console.log('subscribed : ', topic); }); // fired when a client subscribes to a topic server.on('unsubscribed', function(topic, client) { console.log('unsubscribed : ', topic); }); // fired when a client is disconnecting server.on('clientDisconnecting', function(client) { console.log('clientDisconnecting : ', client.id); }); // fired when a client is disconnected server.on('clientDisconnected', function(client) { console.log('clientDisconnected : ', client.id); });
var ledCommand = '1';
/setInterval(function() { ledCommand = (ledCommand === '1') ? '2' : '1'; console.log({topic: 'LEDToggle', payload: ledCommand}); server.publish({topic: 'LEDToggle', payload: ledCommand}); }, 5000);/
iot-server.js client = mqtt.connect('mqtt://localhost:1883')
AssetControllerApi.prototype.upsertAssetController = function(req,callback){
var self = this; var inputObj = req.body;
};
light.ino 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]); }
// Switch on the LED if an 1 was received as first character if ((char)payload[0] == '1') { digitalWrite(ledPin, LOW); digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level snprintf (msg, 75, "LIGHT STATUS OFF"); // but actually the LED is on; this is because // it is acive low on the ESP-01) } else { digitalWrite(ledPin, HIGH); digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off by making the voltage HIGH snprintf (msg, 75, "LIGHT STATUS ON"); }
}
Any one please help me? What is wrong with this code?