Skip to content

Instantly share code, notes, and snippets.

@Antoinebr
Created April 16, 2018 22:45
Show Gist options
  • Save Antoinebr/6c8332ab9dae2cac9a30d0b7d53eda73 to your computer and use it in GitHub Desktop.
Save Antoinebr/6c8332ab9dae2cac9a30d0b7d53eda73 to your computer and use it in GitHub Desktop.
Espruino moisture sensor
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