-
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.nÂtp.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.nÂtp.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 :)
-
@MaBe sure,
Here is the new thread that I opened few minutes ago. If you don't mind, we can continue there...
http://forum.espruino.com/conversations/Â344154/#comment15121192 -
-
-
Hi there,
I am new to espruino and I setup few days ago greenhouse in my garden and I want to make it autonomous as much as I can.
So, in order to achieve that, I was thinking about using espruino as high level language, but I do not have an experience with the language partially because I am coming from Kotlin and Swift languages and I do not know how reliable espruino is.
My question is can esp32 with espruino sustain controlling few relays, reading 2-3 dht22 sensors, reading water level meter and sending data over mqtt? How reliable is it? What problems can I expect with the language? What about multitasking/multi threading, is it possible since esp32 has 2 cors if I am not mistaken?
Can I relay on the combination of the board and espruino?EDIT:
I already did this with raspberry pi, but I think it is overkill to have raspberry pi board for turning on/off things and sending some small data as humidity level and temperature. I would rather use raspberry as mqtt server and database server with small web app so I can control green house from a distance.I will appreciate any help and advice.
Thanks
-
Hi there,
I came here from micropython and I am surprised with power of espruino and JS. However I have a few question that I couldn't find...
I successfully flashed the board and pushed the blinking code example, but if I reconnect the board or press reset button, the code seems doesn't remain on the board. So, my question is, why is this happening and how can I write the code to the flash?
thanks
Hi MaBe, thank you for your answer.
That is the tricky part, if i use setSntp, I am getting error :
Uncaught Error: Cannot read property 'connect' of undefined
at line 57 col 5
wifi.connect(WIFI_NAME, WIFI_OPTIONS, function(err) {
Any idea why is this happening?
Thanks 😊