• 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 :)

About

Avatar for Svarog @Svarog started