Created
April 16, 2018 22:45
-
-
Save Antoinebr/6c8332ab9dae2cac9a30d0b7d53eda73 to your computer and use it in GitHub Desktop.
Espruino moisture sensor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const wifi = require("Wifi"); | |
const http = require("http"); | |
wifi.setHostname("antoine-espruino-plant"); | |
wifi.connect("SSID", {password:"Csdsddd**%%dddd"}, function(err){ | |
if (err) throw err; | |
console.log("WiFi connected.", wifi.getIP() ); | |
}); | |
wifi.stopAP(); | |
wifi.save(); | |
function get() { | |
return analogRead(NodeMCU.A0 / 1023); | |
} | |
function percentage (){ | |
return Math.round(100 - ( get() * 100 ), 1); | |
} | |
function onPageRequest(req, res) { | |
res.writeHead(200, { | |
'Content-Type': 'application/json', | |
"Access-Control-Allow-Origin": "*" | |
}); | |
res.end(JSON.stringify( | |
{ | |
moisture : percentage() | |
} | |
)); | |
} | |
function onInit(){ | |
require("http").createServer(onPageRequest).listen(80); | |
} | |
setInterval ( () => { | |
console.log( 'Soil moisture is : ', percentage(),"%" ); | |
},3000); | |
onInit(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment