making a trigger from html to ifttt - javascript

Background:
I am working on a temperature monitoring project Obniz microcontroller. The goal is to make a notification whenever the temperature exceed the selected threshold.
I searched a lot about making a trigger so that ifttt can read my trigger but I can't find the perfect way for my temperature monitoring project.
This is my js code that I want to be triggered:
var t = document.getElementById("tThresh");
var tempThresh = t.options[t.selectedIndex].value;
if (obj.temperature > tempThresh){
led1.on()
obniz.display.print("The temperature is too high")
obniz.display.clear();
};
Can anyone help me?

Why not a litle setTimeout looking each 1 - 5 minutes the latest temperature and determinate in this one if latest temperature is higher than the limit if yes do what you need to do.
Example:
var limit = 60;
var current = 30;
setTimeout(function(){
if(current >= limit){
window.alert("Temperature is being a litle bit high.");
}
}, 3000);

Related

Is my code wrong or is the platform incorrect?

I am trying to run my JavaScript code on google scripts and it is having some trouble running at times. I was wondering if there was anything wrong with my code and how i could get this to run? It's a very simple calculation.
function myFunction() {
var heavy = 10;
var light = 45;
var petro = 55;
var heavyoil = 0;
var lightoil = 0;
var petrooil = 0;
var counter = 0;
var equals = true;
while (equals == true) {
while (heavyoil >= 40) {
heavyoil -= 40;
lightoil += 30;
}
while (lightoil >= 30) {
lightoil -= 30;
petrooil += 20;
}
heavyoil += heavy;
lightoil += light;
petrooil += petro;
counter++;
if (heavyoil == 0 && lightoil == 0) {
equals = false;
}
}
}
more than likely due to the issues Google hosted libraries was having today. We were getting 503s, CORS policy issues, as well as all hosted libraries resources behind captchas all the sudden. See article: https://thenextweb.com/2017/09/12/google-down-gmail-youtube-maps/#.tnw_D3G3c5M3
You're probably running into an infinite loop issue.
Google Scripts allows you to have the script ran automatically after a configurable amount of time (e.g. every 5 min). Try using that automation option.
To run a script at a time or times you designate:
From the Script Editor, choose Resources > Current project's triggers.
You see a panel with the message No triggers set up. Click here to
add one now.
Click the link that says No triggers set up. Click here to add one
now.
Under Run, select the function you want executed on schedule.
Under Events, select Time-driven.
On the first drop-down list that appears, select Week timer, Day
timer, Hour timer, or Minutes timer, or Specific date and time.
Depending on which you select, you see one or more additional lists
or a text box.
To test the trigger and your function, you might want to pick a
short duration so that you can see the execution without having to
wait hours or days.
-If you picked Week timer, select a day of the week and time of day.
-If you picked Day timer, select an hour. If you picked Hour timer, select an interval of hours. If you picked Minutes timer, select aninterval of minutes.
-If you picked Specific date and time, enter a
date in YYYY-MM-DD HH:MM format.
Click Save.
To ensure that the script runs at the correct time for a particular
time zone, click File > Properties, select a time zone, and
click Save.

How to calculate when video is finished

I'm creating a rewards site where people can watch videos to earn points. The problem is that people will skip to the end of the video to earn their points. Therefore, I need to track when the video is done by a timer.
I have the following code:
var video_percent_count = 0;
function video_percent() {
var prize_video = document.getElementById("prize_video");
total_duration = Math.floor(prize_video.duration) + 1;
video_percent_count++;
percent = =total_duration / video_percent_count;
alert(percent);
}
To summarize, the code is adding to a variable every second, this is the timer. The function then grabs the total duration, then divides it by the timer for a percentage.
The function is not outputting a proper percentage, why is this code incorrect?
You've got a syntax error:
percent = =total_duration / video_percent_count;
Should be:
percent = total_duration / video_percent_count;
Notice the second = is removed.
In the future, you can use the web console to find simple syntax errors such as this one.

Sync displays using current time

I'm making a digital signage system and I'd like to sync the displays so that they all show the same screen at the same time. I'm thinking that the simplest way of doing this is using the current time as a marker - all the machines running the displays will have the accurate time, and the amount of slides they have will be the same.
Is there a calculation I could perform on the current time to work out which slide to display which could be used on each screen, with a slide display time of 30 seconds for example?
I have no experience working with this.
But speaking with a colleague whom has, he pointed me in the direction of Synchroscope.
Seems like the right way to approach it?
Hope it helps!
Synchroscope Guide
Thanks to Bergi's suggestion of using Modulo, I've found a solution. Here is my Javascript code that changes the screen/slide every 30 seconds:
var sec = Math.floor(Date.now() / 1000);
var nearest30Sec = Math.round(sec / 30);
var currentSlide = ((nearest30Sec - 1) % 10) + 1;
currentSlide will produce a number between 1 and 10, which will change every 30 seconds.

Creating a countdown timer script that instead of a specific date counts the duration of a sound file

I've created a sound installation for an exhibition. The sound file last for 24 hours. What I would like to do is to create a site just for this file. I want it to be as stark and simple as possible. A dark background and a white countdown that start once the file start's streaming and countdowns until the file ends. That's from hour 24 to 00:00.
All the countdown scripts count to an specific date and rarely restart themselves.
Is this even possible?
So if I get it right you want to know how to make a progress bar?
I'd say if you don't want to get too much into the niddy-griddy parts, I'd recommend bootsrap and jquery.
I made an example of something I would do:
http://jsfiddle.net/1tq6scga/3/
//JS
var song_seconds = 10;
c = 0;
i = 0;
var invt = setInterval(function(){
c = i/song_seconds;
c = (Math.round(c * 100 * 100)/100) + '%';
$('.progress-bar').html(c);
$('.progress-bar').css({'width':c});
i++;
if(i === song_seconds + 1) {
clearInterval(invt);
}
}, 1000);
so for you I would make it so that the variable max is the length of the song in seconds. Then I'd wrap this up in a function and do it so once a button play is clicked this code is ran in the background, and when I pause the song then the interval gets cleared.
I really don't wanna write more than this, because it requires coding a whole webpage. But this should be enough to get you started.

ajax/JS timed countdown?

Let me explain what I'm trying to do.
I want to make a simple box which counts down numbers at intervals I specify.
For example, I'd like to set it to start at 150, and then I want to set it to drop by 15 every 30 seconds.
Is this possible with AJAX/Javascript? If so, could someone point me in the right direction?
Would really appreciate any help on this script, been Googling for hours now! :(
Cheers
Kieran
Have a look at the setTimeout or setInterval methods, they allow you to execute a function after a specified number of milliseconds (1000ms = 1second). Use that, to call a function that keeps dropping the number and writes it to a HTML element to the user can see it.
this isn't tested, but i hope it shows you the way to go.
var start = 150;
var drop = 15;
var interval = 30;
function countdown(){
document.getElementById('mybox').innerHTML = start;
start-=drop;
window.setTimeout("countdown",interval*1000);
}
countdown();
You may use jQuery to do that, see http://keith-wood.name/countdown.html -> tab Callbacks
Keep in mind that 30 seconds in my browser are not necessarily equal to 30 seconds in your browser. It depends on the workload of the browser.
The time difference is minor for a short time but can increase over a long time. The times will drift apart. If the times must not be equal (or nearly equal) between two visitors than such simple solution should be fine.
We had once a problem to introduce a live clock / countdown in one of our projects. We build a script with javascript, ajax and PHP for clock synchronisation (server time was timeserver).
You should use setInterval / clearInterval which is made for this kind of tasks:
function cooldown(element, start, stop, step, delay) {
var current = start;
element.innerHTML = current;
var timer = setInterval(function () {
current -= step;
if(current < stop) current=stop;
element.innerHTML = current;
if(current == stop) clearInterval(timer);
}, delay*1000);
}
Demonstrated here : http://jsfiddle.net/PCMHn/

Categories