Most recent activity
-
-
I am trying to sync my esp8266 with NTP server, but I am running into a two problems.
The first one is that if I use setSNTP(sntpServer, timeOffset) method on require("Wifi")
var wifi = require("Wifi").setSNTP('2.europe.pool.ntp.org',1);
I am getting error msg:
Uncaught Error: Cannot read property 'connect' of undefined at line 57 col 5 wifi.connect(WIFI_NAME, WIFI_OPTIONS, function(err) {
The second problem:
If I ditch the idea of using SNTP and just use Date(), when esp is connected I print time and it sometimes shows my local time, and sometimes UTC time. But, if I press reset button on the board, it set time to 01.01.1970.What am I missing here?
Here is my code:
var on = false; digitalWrite(D2,on) var WIFI_NAME = "name"; var WIFI_OPTIONS = { password : "passwd" }; var server = "192.168.1.24"; // the ip of your MQTT broker var wifi = require("Wifi").setSNTP('2.europe.pool.ntp.org',1); var mqttOptions = { // ALL OPTIONAL - the defaults are below client_id : "random", // the client ID sent to MQTT - it's a good idea to define your own static one based on `getSerial()` keep_alive: 60, // keep alive time in seconds port: 1883, // port number clean_session: true, username: undefined, // default is undefined password: undefined, // default is undefined protocol_name: "MQTT", // or MQIsdp, etc.. protocol_level: 4, // protocol level }; var mqtt = require("MQTT").create(server, mqttOptions); //When Mqtt connection is enstablished subscribe on following topics mqtt.on('connected', function() { mqtt.subscribe("test"); mqtt.subscribe("led"); mqtt.subscribe("time"); }); mqtt.on('publish', function (pub) { console.log("topic: "+pub.topic); console.log("message: "+pub.message); if(pub.topic === "led"){ console.log('Led msg'); on = !on; digitalWrite(D2,on); mqtt.publish('ledStatus',on); } else if(pub.topic === 'time') { console.log(`I got time`); console.log(Date().toString()); mqtt.publish('time', Date()); } }); wifi.connect(WIFI_NAME, WIFI_OPTIONS, function(err) { if (err) { console.log("Connection error: "+err); return; } wifi.save(); console.log("Connected!"); mqtt.connect(); console.log(`Time : ${Date()}`); console.log(`Time Zone : ${Date().getTimezoneOffset()}`); });
Thanks :)
-
-
-
Hi Robin,
It's been a year and I didn't work on this and I completely forgot how I did it at the time. I wanted to refresh knowledge and got stuck with flashing and pushing the code. Sorry for the inconvenience.
After a few hours of reading the documentation and examination I managed to make my esp8266 to connect to wifi and mqtt...
I have a new questions since you answered my previous question.
- I do not understand the concept how espruino works. I am used to Arduino loop function and I can't get my head around how it works on espruino? How can I achieve the same behaviour in espruino?
I guess that this code snippet listens for message 'connected' from mqtt broker and then executes the subscription to certain topics?
mqtt.on('connected', function(pub) {//subscribe to topics});
Can you please point me how to define pins for digital read and write? I am unable to find the documentation for that?
Thanks :)
- I do not understand the concept how espruino works. I am used to Arduino loop function and I can't get my head around how it works on espruino? How can I achieve the same behaviour in espruino?
-
-
@Robin, thanks for response. My greenhouse is just near my hose, so I don't need LORA wan for communication. However, I will buy espruino board.
@allObjects thanks for this elaboration, I will order a few boards and try to build the system.
Thanks for your help guys :)
noob :)