家庭自动化与动态Web项目全解析
立即解锁
发布时间: 2025-08-21 00:15:10 阅读量: 1 订阅数: 4 


英特尔Galileo开发实战指南
# 家庭自动化与动态 Web 项目全解析
## 1. TMP36 温度传感器功能与连接
### 1.1 TMP36 传感器功能
TMP36 温度传感器有三个引脚,分别是 VIN(输入电压,范围在 2.7 - 5.5V DC)、GND(接地)和电压输出(VOUT)。不同精度的电路可参考[相关资料](http://www.analog.com/static/imported-files/data_sheets/TMP35_36_37)。
### 1.2 与 Intel Galileo 的连接
在连接 TMP36 传感器和 Intel Galileo 时,要注意当前资料中 TMP36 引脚显示的是底部视图而非顶部。部分开发者反映传感器过热,实际是由于连接反了。连接时,需为传感器提供 5V 电源,连接接地端,并选择一个模拟端口连接到传感器的 VOUT。本项目使用模拟端口 A0 连接 VOUT,并在接地和 VCC 之间使用一个 0.1uF 的电容。
### 1.3 连接步骤
1. 为传感器提供 5V 电源。
2. 连接传感器的接地端。
3. 将模拟端口 A0 连接到传感器的 VOUT。
4. 在接地和 VCC 之间连接一个 0.1uF 的电容。
### 1.4 代码测试 TMP36 传感器
以下是测试 TMP36 传感器的代码:
```cpp
//TMP36 VOUT pin connection
const byte sensorAnalogPin = 0;
/*
* setup() - this function runs once you turn your Arduino on
* We initialize the serial connection with the computer
*/
void setup()
{
Serial.begin(115200);
}
void loop()
{
//getting the voltage reading from the temperature sensor
int reading = analogRead(sensorAnalogPin);
float VOUT = (reading * 5.0)/1024.0;
Serial.print(" volts");
Serial.println(VOUT);
// converting to Celsius according to the datasheet
float tempCelsius = (VOUT - 0.5) * 100 ;
Serial.print(" degrees Celsius:");
Serial.println(tempCelsius);
// converting to Fahrenheit
float tempF = (tempCelsius * 9.0 / 5.0) + 32.0;
Serial.print("degrees Fahrenheit:");
Serial.println(tempF);
delay(1000);
}
```
代码解释:
- `analogRead(sensorAnalogPin)`:读取模拟端口的电压值。
- `VOUT = (reading * 5.0)/1024.0`:将读取的电压值转换为实际电压。
- `tempCelsius = (VOUT - 0.5) * 100`:将电压值转换为摄氏度。
- `tempF = (tempCelsius * 9.0 / 5.0) + 32.0`:将摄氏度转换为华氏度。
### 1.5 温度转换公式
| 转换类型 | 公式 |
| ---- | ---- |
| 电压转换 | `VOUT = (reading * 5.0)/1024.0` |
| 摄氏度转换 | `Temperature Celsius = (VOUT - 0.5V)*100` |
| 华氏度转换 | `tempF = (tempCelsius * 9.0 / 5.0) + 32.0` |
## 2. 创建草图
### 2.1 网络连接
Intel Galileo 无需特殊的 Arduino 盾牌即可实现网络连接,可以使用以太网电缆、带有 mPCIe 总线的 WiFi 卡(如 Intel Centrino wireless N - 135)或调制解调器卡。由于不使用盾牌,无需编写相关库和代码,可通过简单设置实现连接。
### 2.2 UDP 消息的发送和接收
#### 2.2.1 发送 UDP 消息
以下是发送 UDP 消息的代码:
```cpp
#define WEBSERVER_UDP_PORT 2010 // this port is used to send message events to Node.js
void sendUDPMessage(String protocol)
{
struct sockaddr_in serv_addr;
int sockfd, i, slen=sizeof(serv_addr);
if ((sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
{
printError("socket");
return;
}
bzero(&serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(WEBSERVER_UDP_PORT);
// considering the sketch and the web server run into Galileo
// let's use the loopback address
if (inet_aton("127.0.0.1", &serv_addr.sin_addr)==0)
{
printError("inet_aton() failed\n");
close(sockfd);
return;
}
char send_msg[BUFFERSIZE]; // more than enough
memset((void *)send_msg, sizeof(send_msg), 0);
protocol.toCharArray(send_msg, sizeof(send_msg), 0);
if (sendto(sockfd, send_msg, strlen(send_msg), 0, (struct sockaddr *)&serv_addr, sizeof(serv_addr))==-1)
printError("sendto()");
close(sockfd);
}
```
#### 2.2.2 接收 UDP 消息
以下是接收 UDP 消息的代码:
```cpp
#define SKETCH_UDP_PORT 2000 // this port is used to receive message events from Node.js
int populateUDPServer(void)
{
if ((sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
printError("socket");
else
Serial.println("Server : Socket() successful\n");
bzero(&my_addr, sizeof(my_addr));
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(SKETCH_UDP_PORT);
my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(sockfd, (struct sockaddr* ) &my_addr, sizeof(my_addr))==-1)
printError("bind");
else
Serial.println("Server : bind() successful\n");
memset(msg_buffer, 0, sizeof(msg_buffer));
}
```
为解决 `recvfrom()` 函数的阻塞问题,使用 `select()` 函数实现超时机制。以下是相关代码:
```cpp
void loop() {
if (time0 == 0) time0 = millis();
// clear the set ahead of time
FD_ZERO(&readfds);
FD_SET(sockfd, &readfds);
// wait until either socket has data ready to be recvfrom() (timeout 1000 usecs)
tv.tv_sec = 0;
tv.tv_usec = 1000;
rv = select(sockfd + 1, &readfds, NULL, NULL, &tv);
if(rv==-1)
{
Serial.println("Error in Select!!!");
}
if(rv==0)
{
// TIMEOUT!!!!
if ((millis()-time0) >= 1000)
{
// reached 1 seconds.. let's reads the sensor and send a message!!!
time0 = millis();
String protocol = "";
if (pirState == HIGH)
{
protocol += "*INTRUDER!!!*";
}
else
{
protocol += "*NO DETECTION*";
}
// reading the temperature sensor
int tempC = readTemperatureSensor();
int tempF = convertTempToF(tempC);
char msg[20];
memset(msg, 0, sizeof(msg));
sprintf(msg, "%dC - %dF", tempC, tempF);
protocol += "*";
protocol += msg;
// checking the system status
if (currentStatus == LOCKED)
{
protocol += "*ARMED*";
}
else
{
protocol += "*DISARMED*";
}
sendUDPMessage(protocol);
}
}
// checking if the UDP server received some message from the web page
if (FD_ISSET(sockfd, &readfds))
{
if (recvfrom(sockfd, msg_buffer, BUFFERSIZE, 0, (struct sockaddr*)&cli_addr, &slen)==-1)
{
printError("recvfrom()");
return; // let's abort the loop
}
if (DEBUG)
{
Serial.println("Received packet from %s:%d\nData:");
Serial.println(inet_ntoa(cli_addr.sin_addr));
Serial.println(msg_buffer);
}
String checkResp = msg_buffer;
if (checkResp.lastIndexOf("L1ON", 0) < 0)
{
// There is no L1ON in the string.. let's switch off the relay
digitalWrite(relay1, HIGH);
if (DEBUG) Serial.println("The lamp 1 is
```
0
0
复制全文
相关推荐









