我已经创建了一个使用arduino控制led的网页。如果满足某些条件,我希望网页在同一个标签中自动打开一个网址。我想要上述问题的代码。谢谢
答案 0 :(得分:0)
下面描述了3种可能的方式:
您可以使用HTTP响应将用户重定向到不同的页面,例如:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Threading;
using System.Speech.Synthesis;
namespace CpuandMemoryMonitor
{
class Program
{
/// <summary>
/// Entry Point into the Program | Program: CPU & Memory Monitor |By:
Andrew.
/// </summary>
///
static void Main(string[] args)
{
#region My performance Counters
// This will greet the user in the default voice
SpeechSynthesizer synth = new SpeechSynthesizer();
synth.Speak("Welcome to the CPU and Memory Monitor");
// This will pull the current CPU load in percentage.
PerformanceCounter perfCpuCount = new PerformanceCounter("Processor Information", "% Processor Time", "_Total");
// This will pull the current available Memory in Megabytes
PerformanceCounter perfMemCount = new PerformanceCounter("Memory", "Available MBytes");
// This will give us system uptime (in Seconds)
PerformanceCounter perfUptimecount = new PerformanceCounter("System", "System Up Time");
#endregion
#region Perfromance Counters Program Loop
//Infinite While Loop.
while (true)
{
// Get the current perforance counter values
int currentcpupercentage = (int)perfCpuCount.NextValue();
int currentavailablememory = (int)perfMemCount.NextValue();
//Print the performance counter values to the console screen
Console.WriteLine("CPU Load: {0}%", currentcpupercentage);
Console.WriteLine("Available Memory: {0}MB", currentavailablememory);
// Speech synthasiser warns user when CPU Load is above 80 percent
if (currentcpupercentage > 80)
{
///If CPU Load is at 100 % Warn the user in a a female voice!
if (currentcpupercentage == 100)
{
synth.SelectVoiceByHints(VoiceGender.Female);
string cpuLoadVocalMessage = String.Format("Oh dear! You're CPU is about to catch on Fire!");
synth.Speak(cpuLoadVocalMessage);
}
else
//If CPU Load is at 80 % Warn the user in a a male voice!
{
synth.SelectVoiceByHints(VoiceGender.Male);
string cpuLoadVocalMessage = String.Format("The Current Cpu Load is {0}", currentcpupercentage);
synth.Speak(cpuLoadVocalMessage);
}
// Speech synthasiser warns user when memory is less then 1 gigabyte
if (currentavailablememory < 1024)
{
// If CPU Load is at 100 % Warn the user ina a female voice!
string memavailableVocalMessage = string.Format("You currently have {0} gigabytes of memory available", currentavailablememory / 1024);
synth.Speak(memavailableVocalMessage);
}
//Sleep for 1 second
Thread.Sleep(1000);
}
#endregion
}
}
}
我会选择这个,因为它需要从Arduino发送最少量的字节
当满足arduino的条件时,您可以在HTML中输出以下代码:
HTTP/1.1 302 Found
Location: http://google.com
它会将您重定向到Google。
如果您想重新定向,请使用Javascript执行此操作:
<meta http-equiv="Refresh" content="URL=http://google.com" />