728x90
아두이노 우노에서 ESP8266 와이파이 통신과 HC06을 동시에 SoftwareSerial을 사용할 경우 원할히 동작하지 않습니다.
그래서 ESP8266은 Software 시리얼 / HC-06 블루통신은 HardwareSerial을 사용합니다.
우노 빅보드의 자석 점퍼선을 사용하면 잘못된 회로 연결을 쉽게 찾을 수 있습니다.
프로그래밍
블루투스 하드웨어 시리얼을 사용하도록 정의합니다.
하드웨어 시리얼 Serial 을 myBluetooth 로 별칭을 사용합니다.
소프트웨어 시리얼 이든 하드웨어 시리얼이든 동일한 별칭으로 사용하여 블루투스 송 수신 하면 됩니다.
소스코드
// #include <SoftwareSerial.h>
#include <Servo.h>
#define USE_NETWORK 1
#define USE_BLUETOOTH 1
#define USE_BLUETOOTH_HW 1
#if USE_BLUETOOTH_HW
#define DEBUG 0
#else
#define DEBUG 1
#endif
/////////////////////////////////////////////////////
#include "WiFiEsp.h"
// Emulate Serial1 on pins 6/7 if not present
#ifndef HAVE_HWSERIAL1
#include "SoftwareSerial.h"
SoftwareSerial Serial1(7, 6); // RX, TX
#endif
char ssid[] = "ZEROWIN"; // your network SSID (name)
char pass[] = "zzzzzzzz"; // your network password
int status = WL_IDLE_STATUS; // the Wifi radio's status
WiFiEspServer server(400);
#if (USE_BLUETOOTH_HW == 0)
// 블루투스 라이브러리
#include <SoftwareSerial.h>
#define bt_tx 10 // 스마트폰->블루투스 송신 -> 아두이노
#define bt_rx 11 // 아두이노 -> 블루투스 수신-> 스마트폰
SoftwareSerial myBluetooth(bt_tx,bt_rx);
#else
// ARDUINO 0 - HC06 TX
// ARDUINO 1 - HC06 RX
HardwareSerial & myBluetooth = Serial;
#endif
//=====================================================================
#include <DHT.h>
#include <DHT_U.h>
#define DHTPIN 4
#define DHTTYPE DHT11
#define SERVOPIN 2
int angle = 0;
Servo servo;
#define LIGHTPIN 5
#define FAN_PIN 3
#define WATER_PUMP_PIN 8
DHT dht(DHTPIN, DHTTYPE);
// ?占쏙옙? ???? ?????? ???
float temperature, humidity;
// LCD ????占쏙옙??
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
//=====================================================================
bool bConnected = false;
LiquidCrystal_I2C lcd(0x39, 16, 2);
void printLCD(int col, int row , unsigned long value) {
char str[21] = { 0x00, };
sprintf(str, "%lu", value);
for(int i=0 ; i < strlen(str) ; i++){
lcd.setCursor(col+i , row);
lcd.print(str[i]);
}
}
void printLCD(int col, int row , char *str) {
for(int i=0 ; i < strlen(str) ; i++){
lcd.setCursor(col+i , row);
lcd.print(str[i]);
}
}
void printLCD_M(int col, int row , char * str) { // MIDDLE
int i = 0;
i = (20-col-strlen(str)) / 2;
int j = 0;
for( ; j < strlen(str) ; j++){
lcd.setCursor(col+i+j , row);
lcd.print(str[j]);
}
}
void printWifiStatus()
{
#if DEBUG
// print the SSID of the network you're attached to
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
#endif
// print your WiFi shield's IP address
IPAddress ip = WiFi.localIP();
#if DEBUG
Serial.print("IP Address: ");
Serial.println(ip);
#endif
char ipno2[26] ;
sprintf(ipno2, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
printLCD(0, 1, ipno2);
// print the received signal strength
long rssi = WiFi.RSSI();
#if DEBUG
Serial.print("Signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
#endif
}
void setup() {
// put your setup code here, to run once:
#if (USE_BLUETOOTH_HW == 0)
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
#endif
myBluetooth.begin(9600);
lcd.begin();
printLCD(0, 0, "UNO BIGBOARD");
printLCD(0, 1, "NETWORKING...");
#if USE_NETWORK
// initialize serial for ESP module
Serial1.begin(9600);
// initialize ESP module
WiFi.init(&Serial1);
// check for the presence of the shield
if (WiFi.status() == WL_NO_SHIELD) {
#if DEBUG
Serial.println("WiFi shield not present");
#endif
// don't continue
while (true);
}
// attempt to connect to WiFi network
while ( status != WL_CONNECTED) {
#if DEBUG
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
#endif
// Connect to WPA/WPA2 network
status = WiFi.begin(ssid, pass);
}
#if DEBUG
Serial.println("You're connected to the network");
#endif
printWifiStatus(); // display IP address on LCD
delay(2000);
server.begin();
#endif
// printLCD(0, 1, "SMARTFARM");
dht.begin();
// servo.attach(SERVOPIN);
pinMode(LIGHTPIN, OUTPUT);
pinMode(FAN_PIN, OUTPUT);
pinMode(WATER_PUMP_PIN, OUTPUT);
#if DEBUG
Serial.println("START");
#endif
}
int cdcValue = 0;
int waterValue = 0;
int lightOutput = 0;
int fanOutput = 0;
int waterPumpPin = 0;
int timeout = 0;
char sData[256] = { 0x00, };
#define R_MAXNUM 64
char rData[R_MAXNUM] = { 0x00, };
int rPos = 0;
char bData[R_MAXNUM] = { 0x00, }; // 블루투스 데이터 수신
int bPos = 0;
void loop() {
timeout += 1;
// put your main code here, to run repeatedly:
if(timeout % 10 == 0) {
cdcValue = analogRead(0);
cdcValue /= 10;
//Serial.print(cdcValue); Serial.print(",");
waterValue = analogRead(1);
waterValue /= 10;
//Serial.print(waterValue); Serial.print(",");
humidity = dht.readHumidity();
temperature = dht.readTemperature();
sprintf(sData, "{ \"temp\":%02d,\"humidity\":%02d,\"cdc\":%-04d,\"water\":%-04d }",
(int)temperature, (int)humidity,
cdcValue, waterValue);
// Serial.println(sData);
/*
angle += 10;
if(90 < angle) angle = 0;
servo.write(angle);
lightOutput = !lightOutput;
digitalWrite(LIGHTPIN, lightOutput);
fanOutput = !fanOutput;
digitalWrite(FAN_PIN, fanOutput);
waterPumpPin = !waterPumpPin;
digitalWrite(WATER_PUMP_PIN, waterPumpPin); */
myBluetooth.println(sData);
}
while(0 < myBluetooth.available()) {
char ch = myBluetooth.read();
bData[bPos] = ch; bPos += 1;
Serial.print(ch);
if(ch == '\n')
{
#if DEBUG
Serial.print("bPos=");
Serial.print(bPos);
Serial.print(" ");
Serial.println(bData);
#endif
if(memcmp(bData, "C_S-", 4) == 0)
{
if(bData[4] == '0') angle = 0;
else angle = 180;
servo.attach(SERVOPIN);
servo.write(angle);
delay(500);
servo.detach();
#if DEBUG
Serial.print("SERVER_MOTOR=");
Serial.println(angle);
#endif
}
if(memcmp(bData, "C_F-", 4) == 0)
{
if(bData[4] == '0') digitalWrite(FAN_PIN, 0);
else digitalWrite(FAN_PIN, 1);
#if DEBUG
Serial.print("FAN=");
Serial.println(bData[4]);
#endif
}
if(memcmp(bData, "C_L-", 4) == 0)
{
if(bData[4] == '0') digitalWrite(LIGHTPIN, 0);
else digitalWrite(LIGHTPIN, 1);
#if DEBUG
Serial.print("LIGHT=");
Serial.println(bData[4]);
#endif
}
if(memcmp(bData, "C_W-", 4) == 0)
{
if(bData[4] == '0') digitalWrite(WATER_PUMP_PIN, 0);
else digitalWrite(WATER_PUMP_PIN, 1);
#if DEBUG
Serial.print("WATER=");
Serial.println(bData[4]);
#endif
}
bPos = 0;
memset(bData, 0x00, R_MAXNUM);
break;
}
delay(10);
}
#if USE_NETWORK
WiFiEspClient c = server.available();
if(c) {
#if DEBUG
Serial.println("RECV: ");
#endif
boolean bDataRead = false;
while (0 < c.available()) {
char ch = c.read();
#if DEBUG
Serial.write(ch);
#endif
bDataRead = true;
rData[rPos] = ch; rPos += 1;
}
if(bDataRead == true) {
#if DEBUG
Serial.println();
#endif
// sprintf(sData, "timeout %d", timeout);
c.print(sData);
if(5 <= rPos)
{
#if DEBUG
Serial.println(rData);
#endif
if(memcmp(rData, "C_S-", 4) == 0)
{
if(rData[4] == '0') angle = 0;
else angle = 180;
servo.attach(SERVOPIN);
servo.write(angle);
delay(500);
servo.detach();
#if DEBUG
Serial.print("SERVER_MOTOR=");
Serial.println(angle);
#endif
}
if(memcmp(rData, "C_F-", 4) == 0)
{
if(rData[4] == '0') digitalWrite(FAN_PIN, 0);
else digitalWrite(FAN_PIN, 1);
#if DEBUG
Serial.print("FAN=");
Serial.println(rData[4]);
#endif
}
if(memcmp(rData, "C_L-", 4) == 0)
{
if(rData[4] == '0') digitalWrite(LIGHTPIN, 0);
else digitalWrite(LIGHTPIN, 1);
#if DEBUG
Serial.print("LIGHT=");
Serial.println(rData[4]);
#endif
}
if(memcmp(rData, "C_W-", 4) == 0)
{
if(rData[4] == '0') digitalWrite(WATER_PUMP_PIN, 0);
else digitalWrite(WATER_PUMP_PIN, 1);
#if DEBUG
Serial.print("WATER=");
Serial.println(rData[4]);
#endif
}
rPos = 0;
memset(rData, 0x00, R_MAXNUM);
}
}
delay(10);
// close the connection:
c.stop();
// Serial.println("Client disconnected");
}
#endif
delay(100);
}
테스트영상
우노빅보드에서 와이파이 와 블루투스를 동시에 동작합니다.
Node-RED 웹서버 기능을
스마트폰 앱에서 블루투스 통신으로 데이터 표시 와 보드 제어를 구현합니다.
'Node-RED 우노빅보드 스마트팜' 카테고리의 다른 글
ESP8266 + ARDUINO UDP SERVER / 전압 & 전류 데이터 (0) | 2020.08.12 |
---|