1. Adafruit MCU 보드에 다음과 같이 프로그램한다.
==============< Adafruit father M0 보드 프로그램 >===============================
#include <SPI.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include <Adafruit_WINC1500.h>
/************************* WiFI Setup *****************************/
#define WINC_CS 8
#define WINC_IRQ 7
#define WINC_RST 4
#define WINC_EN 2 // or, tie EN to VCC
Adafruit_WINC1500 WiFi(WINC_CS, WINC_IRQ, WINC_RST);
char ssid[] = "AHDream"; // your network SSID (name)
char pass[] = "awasoft12"; // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0; // your network key Index number (needed only for WEP)
int status = WL_IDLE_STATUS;
/************************* Adafruit.io Setup *********************************/
#define AIO_SERVER "192.168.10.59" // 192.168.10.59=라즈베리파이3보드 IP임
#define AIO_SERVERPORT 1883
#define AIO_USERNAME "adafruitiousername" // Adafruit에서 라즈베리로보내줄 때 내 장치 이름
#define AIO_KEY "adafruitiokey"
/************ Global State (you don't need to change this!) ******************/
//Set up the wifi client
Adafruit_WINC1500Client client;
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
// You don't need to change anything below this line!
#define halt(s) { Serial.println(F( s )); while(1); }
/****************************** Feeds ***************************************/
// Setup a feed called 'photocell' for publishing.
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
Adafruit_MQTT_Publish photocell = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/photocell"); //서버에서 읽어가는 경로
// Setup a feed called 'onoff' for subscribing to changes.
Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/onoff"); //서버에서 써 넣는 경로
/*************************** Sketch Code ************************************/
#define LEDPIN 13
void setup() {
#ifdef WINC_EN
pinMode(WINC_EN, OUTPUT);
pinMode(6, INPUT); //*********************2018/06/12
digitalWrite(WINC_EN, HIGH);
#endif
while (!Serial);
Serial.begin(115200);
Serial.println(F("Adafruit MQTT demo for WINC1500"));
// Initialise the Client
Serial.print(F("\nInit the WiFi module..."));
// check for the presence of the breakout
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WINC1500 not present");
// don't continue:
while (true);
}
Serial.println("ATWINC OK!");
pinMode(LEDPIN, OUTPUT);
mqtt.subscribe(&onoffbutton);
}
uint32_t x=0;
void loop() {
// Ensure the connection to the MQTT server is alive (this will make the first
// connection and automatically reconnect when disconnected). See the MQTT_connect
// function definition further below.
MQTT_connect();
// this is our 'wait for incoming subscription packets' busy subloop
int DI6_val = digitalRead(6); // *************************** 2018/6/12
Adafruit_MQTT_Subscribe *subscription;
while ((subscription = mqtt.readSubscription(100))) { // 전송주기 5000ms마다 한번
if (subscription == &onoffbutton) {
Serial.print(F("Got: "));
Serial.println((char *)onoffbutton.lastread);
if (0 == strcmp((char *)onoffbutton.lastread, "OFF")) {
digitalWrite(LEDPIN, LOW);
int aaa = digitalRead(6);
if ( aaa = 0){
Serial.println(aaa);
photocell.publish("0");
}
else
Serial.println(aaa);
photocell.publish("1");
//photocell.publish(aaa);
//photocell.publish("LED status : OFF");
}
if (0 == strcmp((char *)onoffbutton.lastread, "ON")) {
digitalWrite(LEDPIN, HIGH);
int bbb = digitalRead(6);
if ( bbb = 1){
Serial.println(bbb);
photocell.publish("1");
}
else
Serial.println(bbb);
photocell.publish("0");
//Serial.println(inString.toInt(bbb));
//photocell.publish(bbb);
//photocell.publish("LED status : ON");
}
}
}
// Now we can publish stuff!
Serial.print(F("\nSending photocell val "));
Serial.print(x);
Serial.print(" Digital channel 6=");
Serial.print(DI6_val);
Serial.print("...");
if (! photocell.publish(x++)) {
Serial.println(F("Failed"));
} else {
Serial.println(F("OK!"));
}
// ================Digital data 전송 프로그램==========
if ( DI6_val >= 1){
Serial.println("DI6_val = 1");
photocell.publish("DI6_val = 1");
}
else
{
Serial.println("DI6_val = 0");
photocell.publish("DI6_val = 0");
}
// ================Digital data 전송 프로그램 끝==========
}
// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care if connecting.
void MQTT_connect() {
int8_t ret;
// attempt to connect to Wifi network:
while (WiFi.status() != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
uint8_t timeout = 10;
while (timeout && (WiFi.status() != WL_CONNECTED)) {
timeout--;
delay(1000);
}
}
// Stop if already connected.
if (mqtt.connected()) {
return;
}
Serial.print("Connecting to MQTT... ");
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
Serial.println(mqtt.connectErrorString(ret));
Serial.println("Retrying MQTT connection in 5 seconds...");
mqtt.disconnect();
delay(5000); // wait 5 seconds
}
Serial.println("MQTT Connected!");
}
================<Adafruit father M0 보드 프로그램 끝 >=============================
2. MQTT 서버 클라이언트를 라즈베리파이 3에서 실행한다.
(사전에 MQTT 를 라즈베리파이3에 설치가 되어 있어야 한다. MQTT 서버 클라이언트 설치편 참조하여 설치할것)
1) 라즈베맆파이의 Terminal을 실행한다. (서버(publish)용 과 클라이언트(Subscribe) 용 따로 구동할 것
2) 서버용에 다음과 같이 입력하고 엔터를 친다. (수신만한다.)
구문 ~#mosquitto_sub -h localhost -t "adafruitiousername/feeds/photocell"
하고 엔터를 친다.
여기서 adafruitiousername=AIO_USERNAME 이다.
"feeds/photocell" 은 서버쪽으로 보내고자 하는 경로를 Adafruit MCU 보드에 명시 해 놓았으므로 이것이 토픽이된다.
localhost는 라즈베리파이(MQTT 서버가 설치되어 있는 장치)의 IP 또는 localhost 또는 127.0.0.1 이 3개 중 하나기록
3) 또 하나의 터미널에서는
구문 ~#mosquitto_pub -h localhost -t "adafruitiousername/feeds/photocell" -m "ON"
하고 엔터를 친다.
그러면 LED가 "ON" 된다.
~#mosquitto_pub -h localhost -t "adafruitiousername/feeds/photocell" -m "OFF"
하고 엔터를 치면 "OFF"된다.
여기서 Adafruit father M0보드에 "LED보드"를 13번 체널에 연결을 하고 6번체널에 상태를 읽어 들이는 형태로
Digital Output 및 Digital Input를 하드웨어 적으로 연결하였음을 알려 드립니다.
또한
~#mosquitto_pub -h localhost -t "adafruitiousername/feeds/photocell" -1
엔터를 치면
연속적으로 명령을 내 보낼 수 있어서
ON 엔터
OFF 엔터
이렇게 하면 연속 명령을 내보내어서 LED를 제어가능하다.
이것이 IOT 시스템의 첫걸음이다.
첫댓글 아 이것은 또한 Adafruit father M0 는 Wifi로 Wifi허브로 연결하는 형식으로 구성하였다.