I think i will buy a new DCF receiver. i can get it to work but its not stable. not on the arduino nor on the espruino.
Code:
function wlan() {
digitalWrite(B9,1); // enable on Pico Shim V2
Serial2.setup(115200, { rx: A3, tx : A2 });
var wifi = require("ESP8266WiFi_0v25").connect(Serial2, function(err) {
if (err) throw err;
wifi.reset(function(err) {
if (err) throw err;
console.log("Connecting to WiFi");
wifi.getAPs(function(err, aps) {
console.log("Scanning Aps");
//console.log(aps);
});
wifi.connect("XXX","XXX", function(err) {
if (err){
return console.log("NO Wifi");
}
var myip;
wifi.getIP(function(err, ip) {
myip = ip;
console.log("Connected to:",myip);
//printscreen(myip,13);
});
var http = require("http");
http.createServer(function (req, res) {
var a = url.parse(req.url, true);
text1 = (a.query && a.query.t1 || "");
text2 = (a.query && a.query.t2 || "");
if (a.query && (a.query.t1 || a.query.t2)){
printscreen(text1,10);
setTimeout(function(){printscreen(myip,13);},2500);
}
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('<html><head><meta name="viewport" content="width=320"/></head><body>');
res.write('LCD Display:<input id="t1" value="'+text1+'" type="text"><br>');
res.write('LCD Display:<input id="t2" value="'+text2+'"type="text"><br>');
res.write('<input type="button" onclick="get()" value="senden"><br>');
res.write('<script> function get(){var e;window.XMLHttpRequest&&(e=new XMLHttpRequest),e.onreadystatechange=function(){4==e.readyState&&200==e.status&&console.log("done")};var t=document.getElementById("t1").value,n=document.getElementById("t2").value;e.open("GET","lcd/?t1="+t+"&t2="+n,!0),e.send()} </script>');
res.end('</body></html>');
}).listen(80);
});
});
});
}
function onInit(){
console.log("start");
wlan();
oled();
}
var img2 = {
width : 32, height : 32, bpp : 1,
transparent : 0,
buffer : E.toArrayBuffer(atob("AAAAAAAAeAAAf/4AH/+bAH/ABQBoAAeAaAADwHQAAf5aAfCfS/8/sUXP//1G//AdQ/wAHUAAAB1AAAAdQAAAHWAAABNgAAAeYAAAEOAAABDgAAAQ8AAAGPgAABz4AAAcfAAAnD4AP/A+B//AHz//wA///4AAP+AAABgAAAAAAAA="))
};
function image(){
//g.clear();
g.drawImage(img2, 96, 0);
//g.flip();
}
var g;
function printscreen(line1,line2,size){
if (init !== 0){
clearTimeout(init);
init=0;
}
g.clear();
// write some text
g.setFontVector(size);
g.drawString(line1,2,2);
g.drawString(line2,2,20);
// write to the screen
g.flip();
}
var init=0;
var step=1;
function clear(){
var p;
var p1=". ";
var p2=" . ";
var p3=" .";
if (step==1){
p=p1;
}
if (step==2){
p=p2;
}
if (step==3){
p=p3;
}
step++;
if (step == 4) step=1;
g.clear();
g.setFontVector(14);
g.drawString("waiting "+p,2,4);
image();
g.flip();
init=setTimeout(clear,300);
}
function oled(){
digitalWrite(B4, LOW);
digitalWrite(B4, HIGH);
I2C2.setup({scl:B10, sda:B3});
g = require("SSD1306").connect(I2C2, clear,{ height : 32 });
}
function DCF77(){
// Decode 4 bits into a number
function decode4(s) {
return (0|s[0])*1 + (0|s[1])*2 + (0|s[2])*4 + (0|s[3])*8;
}
// xor all items in s and return the result (for parity checks)
function xor(s) {
var r = 0;
for (var i=0;i<s.length;i++) r^=s[i];
return r;
}
// decode the DCF77 time transmission
function decode(d, callback) {
if (xor(d.substr(21,7))!=d[28])
return callback("Bad minutes");
var minute = decode4(d.substr(21,4)) + decode4(d.substr(25,3))*10;
if (xor(d.substr(29,6))!=d[35])
return callback("Bad hours");
var hour = decode4(d.substr(29,4)) + decode4(d.substr(33,2))*10;
if (xor(d.substr(36,22))!=d[58])
return callback("Bad date");
var day = decode4(d.substr(36,4)) + decode4(d.substr(40,2))*10;
var doy = decode4(d.substr(42,3));
var month = decode4(d.substr(45,4)) + decode4(d.substr(49,1))*10;
var year = decode4(d.substr(50,4)) + decode4(d.substr(54,4))*10;
//console.log(hour+":"+minute+", "+day+"/"+month+"/"+year);
var date = new Date(2000+year, month-1, day, hour, minute, 0, 0);
return callback(null, date, { CEST:!!d[17], CET:!!d[18] } );
}
this.connect = function(dataPin, callback) {
var dcf = {
last : getTime(),
bits : ""
};
setWatch(function (e) {
// Work out what bit we got
var d = e.time-e.lastTime;
console.log("pulsewidth:",(d*1000).toString().substring(0, 3));
var bit = (d<0.12)?0:1;
// if we had a 2 sec gap then it's the beginning of a minute
if (e.time - dcf.last > 1.5) {
decode(dcf.bits, callback);
dcf.bits = "";
}
dcf.last = e.time;
// now add this bit of data
dcf.bits += bit;
if (dcf.bits.length>59)
dcf.bits = dcf.bits.substr(-59);
}, dataPin, { edge:"falling", repeat:true, debounce:60 });
return dcf;
};
}
var Clock = require("clock").Clock;
var clk=new Clock(0);
var dcf = new DCF77();
dcf.connect(B6, function(err, date, info) {
if (err){
console.log("Invalid time received: " + err);
}else{
console.log(date.toString(), info);
clk.setClock(date.getTime());
}
});
var woche = ["So","Mo","Di","Mi","Do","Fr","Sa"];
setInterval(function(){
var date = clk.getDate();
var weekday = date.getDay(); //0-31
var wochentag = woche[weekday];
var jahr = date.getFullYear();
var monat = date.getMonth()+1; monat=(monat<10)?"0"+monat:monat;
var tag = date.getDate(); tag=(tag<10)?"0"+tag:tag;
var stunden = date.getHours(); stunden=(stunden<10)?"0"+stunden:stunden;
var minute = date.getMinutes();minute=(minute<10)?"0"+minute:minute;
var sek = date.getSeconds(); sek=(sek<10)?"0"+sek:sek;
var s1 = wochentag+". "+tag+"."+monat+"."+jahr;
var s2 = stunden+":"+minute+":"+sek;
printscreen(s1,s2,10);
},1000);
save();
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
not working:
https://youtu.be/LK4eFga2P7E
working:
I think i will buy a new DCF receiver. i can get it to work but its not stable. not on the arduino nor on the espruino.
Code:
@Gordon thank you for help.