2、基本原理
wifi连接到AP上,组成局域网。
arduion,使用了串口转wifi模块,它在控制器上建立了一个TCP服务器。控制端通过调用TCP服务来达到控制的目的。
下面分别介绍各个控制器。
3、电灯控制器
1)硬件构成
模块+wifi模块+继电器模块。

AC端接到零线开关上。
2)Arduino代码


void setup()
{
Serial.begin(115200);
pinMode(2,OUTPUT);
digitalWrite(2, HIGH); // switch off
}
void loop()
{
// delay(500);
if (Serial.available() > 0)
{
char val;
val = Serial.read(); // read serial data
if(val == '1')
{
digitalWrite(2, LOW); // switch on
}
else if(val == '0')
{
digitalWrite(2, HIGH); // switch off
}
Serial.print(val); //将收到是数据再通过串口发送出去
//delay(1000);
}
}

3)PC端代码


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using .Sockets;
using System.Diagnostics;
using System.Timers;
using ;
namespace WiseHouse.SI
{
public class LightController
{
// *********************************************** 全局字段 ******************************************************
TcpClient tcp = new TcpClient();
NetworkStream stream;
System.Timers.Timer tmrConnecting = new System.Timers.Timer(); // 连线的周期。 若断线,在10秒后尝试建立连接
// *********************************************** 事件 ******************************************************
// *********************************************** 属性 ******************************************************
/// <summary>
/// IP地址
/// </summary>
public string IP
{
get
{
return _IP;
}
set
{
}
}
string _IP;
/// <summary>
/// 是否在线
/// </summary>
public bool IsOnline
{
get
{
return _IsOnline;
}
set
{
}
}
bool _IsOnline = false;
/// <summary>
/// 信号强度
/// </summary>
public int SignalStrength
{
get
{
return GetSignalStrength();
}
set
{
}
}
// int _SignalStrength;
// *********************************************** 构造函数 ******************************************************
/// <summary>
/// 以IP地址为参数实例化一个对象
/// </summary>
/// <param name="ip"></param>
public LightController( string ip )
{
_IP = ip;
// 计时器1
tmrConnecting.Elapsed += new ElapsedEventHandler( tmrConnecting_Elapsed );
tmrConnecting.Interval = 10000; // 每10秒种尝试连接一次
tmrConnecting.Enabled = true;
// 首次连接
Open();
}
// *********************************************** 公共方法 ******************************************************
// 打开灯1
public void TurnOnLight1()
{
try
{
if( tcp.Connected == true )
{
// 发送
byte[] command = Encoding.ASCII.GetBytes( "1" );
stream.Write( command, 0, command.Length ); // 写入
System.Threading.Thread.Sleep( 100 ); //
// 接收
int bufferSize = 30; // 13个,00.00;00.00 // 接收字节数组
byte[] buffer = new byte[ bufferSize ];
int bytesRead = stream.Read( buffer, 0, bufferSize ); //将值放入buffer
string data = Encoding.ASCII.GetString( buffer ); // 将字节转换成字符串
// Console.WriteLine( data );
}
else
{
}
}
catch
{
Trace.WriteLine( _IP.PadRight( 20 ) + "发送指令失败" + DateTime.Now.ToString( "yyyy-MM-dd hh:mm:ss" ) );
}
}
/// <summary>
/// 关闭灯1
/// </summary>
public void TurnOffLight1()
{
try
{
if( tcp.Connected == true )
{
// 发送
byte[] command = Encoding.ASCII.GetBytes( "0" );
stream.Write( command, 0, command.Length ); // 写入
System.Threading.Thread.Sleep( 100 ); //
// 接收
int bufferSize = 30; // 13个,00.00;00.00 // 接收字节数组
byte[] buffer = new byte[ bufferSize ];
int bytesRead = stream.Read( buffer, 0, bufferSize ); //将值放入buffer
string data = Encoding.ASCII.GetString( buffer ); // 将字节转换成字符串
// Console.WriteLine( data );
}
else
{
}
}
catch
{
Trace.WriteLine( _IP.PadRight( 20 ) + "发送指令失败" + DateTime.Now.ToString( "yyyy-MM-dd hh:mm:ss" ) );
}
}
/// <summary>
/// 释放资源
/// </summary>
public void Close()
{
stream.Close();
tcp.Close();
}
// *********************************************** 私有方法 ******************************************************
/// <summary>
/// 定时进行连接。
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void tmrConnecting_Elapsed( object sender, ElapsedEventArgs e )
{
Open();
}
/// <summary>
/// 打开连接。
/// </summary>
/// <param name="ip"></param>
/// <param name="port"></param>
void Open()
{
try
{
if( tcp.Connected == false ) //若连线失败,则尝试进行连接
{
// 若是连接断了,则先关闭当前的TCP client实例。然后再开启另一个TCP client实例
tcp.Close();
tcp = new TcpClientWithTimeout( _IP, 8000, 2000 ).Connect();
tcp.ReceiveTimeout = 1000;
tcp.SendTimeout = 1000; // timeout
stream = tcp.GetStream();
_IsOnline = true;
Console.WriteLine( _IP.PadRight( 20 ) + "灯连接成功!" + DateTime.Now.ToString( "yyyy-MM-dd hh:mm:ss" ) );
}
}
catch
{
_IsOnline = false;
Trace.WriteLine( _IP.PadRight( 20 ) + "灯连接失败!" + DateTime.Now.ToString( "yyyy-MM-dd hh:mm:ss" ) );
}
}
/// <summary>
/// 获得信号强度
/// </summary>
/// <returns></returns>
int GetSignalStrength()
{
try
{
// 从页面中下载html文本
WebClient client = new WebClient();
client.Credentials = new NetworkCredential( "admin", "admin" ); // 认证
string str1 = client.DownloadString( "http://" + _IP + "/station/link_status.asp" ); //?
// 初次截取文本。
int j1 = str1.IndexOf( "Link Quality" ) + 22;
string str2 = str1.Substring( j1, 70 ); // 类似于:<td>Good 95%</td>\n <td rowspan=\"4\"><
// 再次截取文本。在str2的基础上截取
int j2 = str2.LastIndexOf( ";" ); //信号值前的文本
int j3 = str2.IndexOf( "%</td>" ); //信号值前的文本
string str3 = str2.Substring( j2 + 1, j3 - j2 - 1 ); // 截取信号值。
return int.Parse( str3 );
}
catch
{
return 0; // 若读取失败,则返回 0
}
}
}
}

4、空调控制器
1)硬件构成
模块+wifi模块+红外LED灯管。

我的空调是格力的,要控制空调首先需要捕获红外信号的原始码。关于如何捕获原始码,请参考本人的另一篇文章:
2)Arduino代码

View Code
3)PC端代码

View Code
5、房门控制器
1)硬件构成
模块+wifi模块+场效管模块+自制的稳压模块。

我的锁是电控锁,它里面有一个线圈,当线圈通电后,它中间的钢芯被磁化,吸位锁的卡扣,门就开了。由于线圈在通电时的瞬间电流较大,为避免它对电路的其它部分造成干扰(可能会造成控制器死机),加上一个自制的稳压模块。稳压模块由一些电感和电容组成。
2)Arduino代码

View Code
3)PC端代码

View Code
6、控制端
System.Speech.Recognition和 System.Speech.Synthesis。
核心代码:

View Code
7、其它资源
可通过网络远程操控的电动BB枪,类似CF
















