아두이노 스케치 (wizfi250 기준)
PubSubClient 라이브러리 설치 후 아래의 소스코드 업로드
#include <SPI.h>
#include <WizFi250.h>
#include <PubSubClient.h>
// Update these with values suitable for your network.
const char* ssid = "SK_WiFiGIGAF5E0";
const char* password = "----";
const char* mqtt_server = "192.168.---";
WiFiClient WizFi250Client;
PubSubClient client(WizFi250Client);
long lastMsg = 0;
char msg[50];
int value = 0;
void setup_wifi();
void callback(char* topic, byte* payload, unsigned int length);
void reconnect();
void setup() {
pinMode(7, OUTPUT);
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.init();
WiFi.begin((char*)ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* payload, unsigned int length) {
int i = 0;
Serial.println("Message arrived: topic: " + String(topic));
Serial.println("Length: "+ String(length,DEC));
//create character buffer with ending null terminator (string)
for(i=0; i<length; i++){
msg[i] = payload[i];
}
msg[i]= '\0';
String msgString = String(msg);
Serial.println("Payload: "+ msgString);
int state = digitalRead(7);
//전송된 메시가 "led"이면 LED를 받을 때마다 켜고 끈다.(토글)
if (msgString == "led_on"){
digitalWrite(7, HIGH);
Serial.println("LED ON...");
}
else if (msgString == "led_off"){
digitalWrite(7, LOW);
Serial.println("LED OFF...");
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("WizFi250Client")) { // 클라이언트가 2대 이상일 경우 이름을 다르게 적어준다.
Serial.println("connected");
// Once connected, publish an announcement...
client.publish("iot", "WizFi250 hello world"); // iot/room1 ... iot/room2 등으로 구분 가능
// ... and resubscribe
client.subscribe("iot");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
long now = millis();
}
// DHT11 센서값 json, db저장
#include <SPI.h>
#include <WizFi250.h>
#include <PubSubClient.h>
#include <DHT11.h>
#include <SimpleTimer.h>
#define DHTPIN 8
SimpleTimer timer;
DHT11 dht11(DHTPIN);
float temperature, humidity;
int err;
int setTemp;
char ch[60] = {0};
char ch2[60] = {0};
const char* ssid = "SK_WiFiGIGAF5E0";
const char* password = "1611010239";
const char* mqtt_server = "192.168.35.163";
WiFiClient WizFi250Client;
WiFiClient client;
PubSubClient client2(WizFi250Client);
long lastMsg = 0;
char msg[50];
int value = 0;
void setup_wifi();
void callback(char* topic, byte* payload, unsigned int length);
void reconnect();
void setup() {
pinMode(7, OUTPUT);
Serial.begin(115200);
setup_wifi();
client2.setServer(mqtt_server, 1883);
client2.setCallback(callback);
timer.setInterval(15000, notify);
}
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.init();
WiFi.begin((char*)ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* payload, unsigned int length) {
int i = 0;
Serial.println("Message arrived: topic: " + String(topic));
Serial.println("Length: "+ String(length,DEC));
//create character buffer with ending null terminator (string)
for(i=0; i<length; i++){
msg[i] = payload[i];
}
msg[i]= '\0';
String msgString = String(msg);
Serial.println("Payload: "+ msgString);
int state = digitalRead(7);
//전송된 메시가 "led"이면 LED를 받을 때마다 켜고 끈다.(토글)
if (msgString == "led1_on"){
digitalWrite(7, HIGH);
Serial.println("LED ON...");
}
else if (msgString == "led1_off"){
digitalWrite(7, LOW);
Serial.println("LED OFF...");
}
if ( msgString == "temp_humidity#stat") {
if ( (err=dht11.read(humidity, temperature) ) == 0 ) {
String str_temp(temperature);
String str_hum(humidity);
//str = "ROOM1#TEMP#" + str;
//str.toCharArray(ch, str.length());
String sensor_id = "\"Dummy-1\"";
// Make JSON data
String msg = "{\"Sensor_ID\": ";
msg += sensor_id;
msg += ", \"Humidity\": ";
msg += str_hum;
msg += "}";
msg.toCharArray(ch, msg.length() + 1);
client2.publish("Home/BedRoom/DHT11/Humidity", ch);
// Make JSON data
String msg2 = "{\"Sensor_ID\": ";
msg2 += sensor_id;
msg2 += ", \"Temperature\": ";
msg2 += str_temp;
msg2 += "}";
msg2.toCharArray(ch2, msg2.length() + 1);
client2.publish("Home/BedRoom/DHT11/Temperature", ch2);
}
}
}
void reconnect() {
// Loop until we're reconnected
while (!client2.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client2.connect("WizFi250Client0")) {
Serial.println("connected");
// Once connected, publish an announcement...
// ... and resubscribe
client2.subscribe("iot/room1");
client2.subscribe("iot/temp");
client2.subscribe("Home/BedRoom/DHT11/Humidity");
client2.subscribe("Home/BedRoom/DHT11/Temperature");
} else {
Serial.print("failed, rc=");
Serial.print(client2.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void loop() {
if (!client2.connected()) {
reconnect();
}
client2.loop();
timer.run();
long now = millis();
}
void notify() {
// 일정 주기마다 실행
if ( client2.state() == 0 ) {
if ( (err=dht11.read(humidity, temperature) ) == 0 ) {
String str_temp(temperature);
String str_hum(humidity);
//str = "ROOM1#TEMP#" + str;
//str.toCharArray(ch, str.length());
String sensor_id = "\"Dummy-1\"";
// Make JSON data
String msg = "{\"Sensor_ID\": ";
msg += sensor_id;
msg += ", \"Humidity\": ";
msg += str_hum;
msg += "}";
msg.toCharArray(ch, msg.length() + 1);
client2.publish("Home/BedRoom/DHT11/Humidity", ch);
// Make JSON data
String msg2 = "{\"Sensor_ID\": ";
msg2 += sensor_id;
msg2 += ", \"Temperature\": ";
msg2 += str_temp;
msg2 += "}";
msg2.toCharArray(ch2, msg2.length() + 1);
client2.publish("Home/BedRoom/DHT11/Temperature", ch2);
}
}
}
'Study > Embedded' 카테고리의 다른 글
라즈베리파이에서 nginx, php, php-sqlite 설치 (0) | 2017.08.10 |
---|---|
라즈베리파이3, mosquitto (0) | 2017.07.31 |
라즈베리 HTTP 서버 구축 (0) | 2017.06.27 |
GCM (0) | 2017.06.27 |
라즈베리파이, Pi4J를 통한 GPIO 제어 (0) | 2017.06.20 |