實踐大學聯網感測實作
開發環境This project is maintained by yazelin
積土成山,風雨興焉;積水成淵,蛟龍生焉;積善成德,而神明自得,聖心備焉。故不積蹞步,無以至千里;不積小流,無以成江海。騏驥一躍,不能十步;駑馬十駕,功在不舍。
// including the library
#include<SPI.h>
#include<Ethernet.h>
#include<PubSubClient.h>
#include<dht11.h>
// Module variable
dht11 DhtSensor;
int PsSensor;
// including the library
#include<SPI.h>
#include<Ethernet.h>
#include<PubSubClient.h>
#include<dht11.h>
// Ethernet variable
byte Mac[] = {0xDE ,0xAD ,0xBE ,0xEF ,0xFE ,0xED};
byte Ip[] = {192 ,168 ,1 ,200};
EthernetClient client;
// Module variable
dht11 DhtSensor;
int PsSensor;
// initial
void setup(){
// Serial Baudrate
Serial.begin(115200);
// Ethernet communication
Ethernet.begin(Mac ,Ip);
// Setting I/O mode
pinMode(5 ,OUTPUT);
pinMode(6 ,OUTPUT);
pinMode(7 ,OUTPUT);
digitalWrite(5 ,LOW);
digitalWrite(6 ,LOW);
digitalWrite(7 ,LOW);
}
// initial
void setup(){
// Serial Baudrate
Serial.begin(115200);
// Ethernet communication
Ethernet.begin(Mac ,Ip);
// Setting I/O mode
pinMode(5 ,OUTPUT);
pinMode(6 ,OUTPUT);
pinMode(7 ,OUTPUT);
digitalWrite(5 ,LOW);
digitalWrite(6 ,LOW);
digitalWrite(7 ,LOW);
// Show local IP
Serial.print("Local IP : ");
Serial.println(Ethernet.localIP());
}
// including the library
#include<SPI.h>
#include<Ethernet.h>
#include<PubSubClient.h>
#include<dht11.h>
// Ethernet variable
byte Mac[] = {0xDE ,0xAD ,0xBE ,0xEF ,0xFE ,0xED};
byte Ip[] = {192 ,168 ,1 ,200};
EthernetClient client;
// MQTT variable
int ServerPort = 1883;
byte MqttServer[] = {192 ,168 ,1 ,252};
PubSubClient MqttClient(client);
// Module variable
dht11 DhtSensor;
int PsSensor;
// initial
void setup(){
// Serial Baudrate
Serial.begin(115200);
// Ethernet communication
Ethernet.begin(Mac ,Ip);
// MQTT communication
MqttClient.setServer(MqttServer ,ServerPort);
// Setting I/O mode
pinMode(5 ,OUTPUT);
pinMode(6 ,OUTPUT);
pinMode(7 ,OUTPUT);
digitalWrite(5 ,LOW);
digitalWrite(6 ,LOW);
digitalWrite(7 ,LOW);
// Show local IP
Serial.print("Local IP : ");
Serial.println(Ethernet.localIP());
}
// main loop
void loop(){
char msg[100] = " ";
// Running at connect server
if(MqttClient.connect("SensorClient")){
// Read sensor data
DhtSensor.read(A0);
PsSensor = analogRead(A1);
// Show value
Serial.print("Humidity : ");
Serial.println(DhtSensor.humidity);
sprintf(msg ,"{\"Humidity\":%4d}" ,DhtSensor.humidity);
MqttClient.publish("Sensor",msg);
Serial.print("Temperature : ");
Serial.println(DhtSensor.temperature);
sprintf(msg ,"{\"Temperature\":%4d}" ,DhtSensor.temperature);
MqttClient.publish("Sensor",msg);
Serial.print("Pressure : ");
Serial.println(PsSensor);
sprintf(msg ,"{\"Pressure\":%4d}" ,PsSensor);
MqttClient.publish("Sensor",msg);
// Test output
if(DhtSensor.humidity <= 30){
digitalWrite(7 ,HIGH);
// Alarm msg
Serial.println("Humidity Alarm. ");
sprintf(msg ,"{\"Humidity\":%4d}" ,DhtSensor.humidity);
MqttClient.publish("Alarm",msg);
}else{
digitalWrite(7 ,LOW);
}
if(DhtSensor.temperature <= 25){
digitalWrite(6 ,HIGH);
// Alarm msg
Serial.println("Temperature Alarm. ");
sprintf(msg ,"{\"Temperature\":%4d}" ,DhtSensor.temperature);
MqttClient.publish("Alarm",msg);
}else{
digitalWrite(6 ,LOW);
}
if(PsSensor >= 10){
digitalWrite(5 ,HIGH);
// Alarm msg
Serial.println("Pressure Alarm. ");
sprintf(msg ,"{\"Pressure\":%4d}" ,PsSensor);
MqttClient.publish("Alarm",msg);
}else{
digitalWrite(5 ,LOW);
}
}
Serial.println("");
// Every second run one turn
delay(1000);
}
將課堂做完的成果錄影後貼至FB社團內