Hey, everyone! I’m trying to control Blynk with Espruino IDE with JavaScript language. I’ve run and download the modules but the program won’t run…
Here’s my code
var WIFI_NAME = “TAMU-BRIGHT”;
var WIFI_OPTIONS = “PLNBatam”;
var MQTT_HOST = “postman.cloudmqtt.com”; //free public broker, hosted by HiveMQ
var Wifi;
var mqtt;
var PATH = ‘KenanganUntukelen/’;
var staWi;
var BlynkLib = require(‘blynk-espruino’);
var blynk = new BlynkLib.Blynk(‘3aSlJWfZ5oQmakdmaLWfwobsdZQgwPeQ’);
var v1 = new blynk.VirtualPin(1);
/* Initialization function. Connects to predetermined Wifi access point and
then calls mqttConnect function if connection is successful. If not, it will
return an error message along with the error code. */
function onInit() {
wifi = require(‘Wifi’);
wifi.connect(‘TAMU-BRIGHT’, {password : ‘PLNBatam’}, function(error) {
if(error) {
console.log(“wifi tidak konek”);
staWi = false;
}
else {
console.log(“wifi konek”); //jika konek
staWi = true;
}
mqttConnect();
}) ;
}
/* Connects to broker (host). Will start other function if connection
successful. Otherwise, it will attempt to reconnect. */
function mqttConnect() {
console.log(‘Connecting MQTT…’);
mqtt = require(‘MQTT’).connect({
host : MQTT_HOST,
keep_alive: 60, // keep alive time in seconds
port: 13085, // port number
clean_session: true,
username: “regina”, // default is undefined
password: “regiinaa”, // default is undefined
protocol_name: “MQTT”, // or MQIsdp, etc…
protocol_level: 4, // protocol level
});
mqtt.on(‘connected’, function() { //CONNECTION SUCCESS
console.log(‘MQTT connected!’);
mqtt.subscribe(PATH + ‘#’); //subscribes to everthing under Kenanganuntukelen/
startYL69(); //calls function startDHT11 to start reading sensor
});
mqtt.on(‘publish’, mqttMessage);
mqtt.on(‘disconnected’, function() { //CONNECTION FAILED
console.log(‘MQTT disconnected… reconnecting…’);
setTimeout(function() {
mqtt.connect();
}, 5000); //attempt reconnect to host after 5 sec
});
}
//This function retrieves message from the broker
function mqttMessage(pub) {
console.log('Message received from broker: ');
console.log(pub.topic, pub.message); //prints out topic and message
}
//This function reads the DHT11 and publish it to the broker
function startYL69(){
setInterval(function() { //function interval yg dijalankan selama dua detik
var hum = analogRead(A0)*100; //baca analogRead
hum = 100-hum;
hum = hum.toFixed(3); //tofixed bakal jadi string
hum = parseFloat(hum); //ubah jadi variabel lagi
if (mqtt.connected){ //cek mqtt,kalau mqtt connect
console.log(`Humidity: ${hum}%`);
mqtt.publish(PATH + 'yl69data',hum);
v1.on('write', function(param) {
console.log('Humidity:', param);
});
}
else{
var wi = wifi.getStatus().station; //minta status,konek atau ngga
if(wi != "connected"){ //cek lagi status wifi
staWi = false;
}
else { //cek internet kalau wifinya connected
var p = require("Ping");
p.ping({ address: 'google.com' }, function(err,data) {
console.log(data); //kalau di ping ada data, ada balasan dari google
if (data!==null){ //kalau ga ada data berarti null atau kosong
mqtt.connect();
}
});
}
}
count = 0;
},2000); //sends data every 5 secs
}
onInit();
Does anyone know what’s the problem? Thank you so much !