BASIC4MCU | 질문게시판 | 아두이노 메가 하드웨어시리얼 소프트웨어시리얼 동시 제어 질문입니다.
페이지 정보
작성자 다웅 작성일2021-04-08 22:59 조회6,738회 댓글3건
https://www.basic4mcu.com/bbs/board.php?bo_table=gac&wr_id=15051
작성일
작성일
본문
코드 두개를 합쳐서
하나는 소프트웨어 시리얼로 연결하고
하나는 하드웨어 시리얼로 연결했는데
와이파이를 연결한 소프트웨어 시리얼만 작동되고
하드웨어에 연결된 lcd 는 화면만 뜨고 전혀 작동을 하지않습니다. 뭐가 문제일까요ㅜ
혹시 하드웨어시리얼과 소프트웨어시리얼은 동시에 작동이 불가능한가요?
확인해보니 loop 쪽이 제대로 안돌아가더라구요ㅠㅠ
[code]
#include <ESP8266.h>
#include <DHT.h>
#include <PMS.h>
#include <SPI.h>
#include <TFT_22_ILI9225.h>
#include <SoftwareSerial.h>
#include "FreeMonoBold24pt7b.h"
#define TFT_RST 7
#define TFT_RS 8
#define TFT_CS 53 // ss
#define TFT_LED 9
#define TFT_SDI 51 //mosi
#define TFT_CLK 52 //sck
#define DHT_PIN 4
#define BUZZER_PIN 5
SoftwareSerial esp8266(11, 10);
//SoftwareSerial pmsSerial(19, 18);
PMS pms(Serial1);
PMS::DATA data;
TFT_22_ILI9225 tft = TFT_22_ILI9225(TFT_RST, TFT_RS, TFT_CS, TFT_LED, 100);
uint16_t x, y;
boolean flag = false;
uint16_t pm_color[] = {COLOR_BLACK, COLOR_NAVY, COLOR_GREEN, COLOR_ORANGE, COLOR_RED};
String pm_text[] = {"Waiting..", "Good", "Moderate", "Bad", "Very Bad"};
int8_t pm25_status = 0;
int8_t pm100_status = 0;
uint16_t pm25_color = pm_color[0];
uint16_t pm100_color = pm_color[0];
String pm25_text = "Heating..";
String pm100_text = "Plz Wait";
float temp;
float humi;
#define DEBUG true
String cmd = "AT+CIPSTART=\"TCP\",\"api.openweathermap.org\",80\r\n";
String cmd_get="GET /data/2.5/weather?id=1838524&APPID=adb35942d1e456b594096e8749a504fc\r\n";
String recived="";
int position;
char icon[3];
int icon_in;
int data_count=0;
int redPin=22;
int greenPin=23;
int bluePin=24;
DHT dht(DHT_PIN, DHT11);
void setup() {
//Serial.begin(9600);
Serial1.begin(9600);
//pmsSerial.begin(9600);
dht.begin();
tft.begin();
tft.setOrientation(1); // 상하좌우
tft.setFont(Terminal6x8); //글씨체
tft.setBackgroundColor(COLOR_BLACK);
tft.drawText(5 + 100 - 72, 6, "temp: `C", COLOR_WHITE); //텍스트 위치
tft.drawText(5 + 100 + 10, 6, "Humi: %", COLOR_WHITE);
tft.fillRectangle(5, 20 + 2, 5 + 100, 20 + 2 + 74, pm_color[0]);
tft.fillRectangle(5 + 100 + 10 + 0, 20 + 2, 5 + 100 + 10 + 15, 20 + 2 + 22, pm_color[1]);
tft.fillRectangle(5 + 100 + 10 + 16, 20 + 2, 5 + 100 + 10 + 35, 20 + 2 + 22, pm_color[2]);
tft.fillRectangle(5 + 100 + 10 + 36, 20 + 2, 5 + 100 + 10 + 75, 20 + 2 + 22, pm_color[3]);
tft.fillRectangle(5 + 100 + 10 + 76, 20 + 2, 5 + 100 + 10 + 100, 20 + 2 + 22, pm_color[4]);
tft.drawRectangle(5 + 100 + 10 + 0, 20 + 2 + 22 + 25, 5 + 100 + 10 + 100, 20 + 2 + 22 + 25 + 27, COLOR_WHITE);
tft.fillRectangle(5, 20 + 78 + 2, 5 + 100, 20 + 78 + 2 + 74, pm_color[0]);
tft.fillRectangle(5 + 100 + 10 + 0, 20 + 78 + 2, 5 + 100 + 10 + 15, 20 + 78 + 2 + 22, pm_color[1]);
tft.fillRectangle(5 + 100 + 10 + 16, 20 + 78 + 2, 5 + 100 + 10 + 40, 20 + 78 + 2 + 22, pm_color[2]);
tft.fillRectangle(5 + 100 + 10 + 41, 20 + 78 + 2, 5 + 100 + 10 + 75, 20 + 78 + 2 + 22, pm_color[3]);
tft.fillRectangle(5 + 100 + 10 + 76, 20 + 78 + 2, 5 + 100 + 10 + 100, 20 + 78 + 2 + 22, pm_color[4]);
tft.drawRectangle(5 + 100 + 10 + 0, 20 + 78 + 2 + 22 + 25, 5 + 100 + 10 + 100, 20 + 78 + 2 + 22 + 25 + 27, COLOR_WHITE);
tft.setFont(Terminal11x16);
tft.setBackgroundColor(pm25_color);
tft.drawText(5 + 2, 20 + 2 + 2, "pm 2.5", COLOR_WHITE);
tft.drawText(5 + 2, 20 + 78 + 2 + 2, "pm 10.0", COLOR_WHITE);
tft.setGFXFont(&FreeMonoBold24pt7b);
tft.drawGFXText(10, 20 + 2 + 74 - 10, "000", COLOR_WHITE);
tft.drawGFXText(10, 20 + 78 + 2 + 74 - 10, "000", COLOR_WHITE);
tft.setFont(Terminal11x16);
tft.setBackgroundColor(COLOR_BLACK);
tft.drawText(5 + 100 + 10 + 3, 20 + 2 + 22 + 25 + 6, pm25_text, COLOR_WHITE);
tft.drawText(5 + 100 + 10 + 3, 20 + 78 + 2 + 22 + 25 + 6, pm100_text, COLOR_WHITE);
pinMode(BUZZER_PIN, OUTPUT);
unsigned int i=0;
esp8266.begin(9600);
esp8266.setTimeout(5000);
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600);
Serial.println("Open Weather Map API Start\r\n");
sendData("AT+RST\r\n",2000, false);
sendData("AT+CWMODE=1\r\n",1000,false);
sendData("AT+CIPMUX=0\r\n",1000,false);
boolean connected=false;
for(int i=0;i<5;i++) {
if(connectWiFi()) {
connected = true;
break;
}
}
if (!connected){while(1);}
delay(2000);
sendData("AT+CIFSR\r\n",1000,DEBUG);
}
void loop() {
if (pms.read(data)) {
bool temp_change, humi_change;
temp_change = temp != dht.readTemperature();
humi_change = humi != dht.readHumidity();
tft.setFont(Terminal6x8);
tft.setBackgroundColor(COLOR_BLACK);
if (temp_change) {
temp = dht.readTemperature();
tft.fillRectangle(5 + 100 - 72 + 33 + 2, 6, 5 + 100 - 69 + 33 + 21, 6 + 8, COLOR_BLACK);
if ((int)temp < 10) {
tft.drawText(5 + 100 - 72 + 33 + 2 + 6, 6, String(temp, 1), COLOR_WHITE);
} else {
tft.drawText(5 + 100 - 72 + 33 + 2, 6, String(temp, 1), COLOR_WHITE);
}
}
if (humi_change) {
humi = dht.readHumidity();
tft.fillRectangle(5 + 100 + 10 + 33, 6, 5 + 100 + 10 + 33 + 21, 6 + 8, COLOR_BLACK);
if ((int)humi < 10) {
tft.drawText(5 + 100 + 10 + 33 + 6, 6, String(humi, 1), COLOR_WHITE);
} else {
tft.drawText(5 + 100 + 10 + 33, 6, String(humi, 1), COLOR_WHITE);
}
}
Serial1.print("Temperature: ");
Serial1.print(temp);
Serial1.println(" ℃");
Serial1.print("humidity : ");
Serial1.print(humi);
Serial1.println(" %");
Serial1.print("PM2.5 : ");
Serial1.print(data.PM_AE_UG_2_5);
Serial1.println(" ug/m3");
Serial1.print("pm10.0 : ");
Serial1.print(data.PM_AE_UG_10_0);
Serial1.println(" ug/m3");
Serial1.println("");
bool pm25_change, pm100_change;
if (0 == (int) data.PM_AE_UG_2_5) {
pm25_change = pm25_status != 0;
pm25_status = 0;
}
if (0 < (int) data.PM_AE_UG_2_5 && (int) data.PM_AE_UG_2_5 < 16) {
pm25_change = pm25_status != 1;
pm25_status = 1;
}
if (15 < (int) data.PM_AE_UG_2_5 && (int) data.PM_AE_UG_2_5 < 36) {
pm25_change = pm25_status != 2;
pm25_status = 2;
}
if (36 < (int) data.PM_AE_UG_2_5 && (int) data.PM_AE_UG_2_5 < 76) {
pm25_change = pm25_status != 3;
pm25_status = 3;
}
if (75 < (int) data.PM_AE_UG_2_5) {
pm25_change = pm25_status != 4;
pm25_status = 4;
}
if (0 == (int) data.PM_AE_UG_10_0) {
pm100_change = pm100_status != 0;
pm100_status = 0;
}
if (0 < (int) data.PM_AE_UG_10_0 && (int) data.PM_AE_UG_10_0 < 31) {
pm100_change = pm100_status != 1;
pm100_status = 1;
}
if (30 < (int) data.PM_AE_UG_10_0 && (int) data.PM_AE_UG_10_0 < 81) {
pm100_change = pm100_status != 2;
pm100_status = 2;
}
if (80 < (int) data.PM_AE_UG_10_0 && (int) data.PM_AE_UG_10_0 < 151) {
pm100_change = pm100_status != 3;
pm100_status = 3;
}
if (150 < (int) data.PM_AE_UG_10_0) {
pm100_change = pm100_status != 4;
pm100_status = 4;
}
pm25_color = pm_color[pm25_status];
pm25_text = pm_text[pm25_status];
pm100_color = pm_color[pm100_status];
pm100_text = pm_text[pm100_status];
int neo_status = max(pm25_status, pm100_status);
if (pm25_change || pm100_change) {
if (neo_status == 4) {
tone(BUZZER_PIN, 200);
} else {
noTone(BUZZER_PIN);
}
}
int16_t w, h;
tft.setGFXFont(&FreeMonoBold24pt7b);
tft.setBackgroundColor(pm25_color);
if (pm25_change) {
tft.fillRectangle(5, 20 + 2, 5 + 100, 20 + 2 + 74, pm25_color);
tft.setFont(Terminal11x16);
tft.drawText(5 + 2, 20 + 2 + 2, "pm 2.5", COLOR_WHITE);
tft.setBackgroundColor(COLOR_BLACK);
tft.fillRectangle(5 + 100 + 10 + 0 + 1, 20 + 2 + 22 + 25 + 1, 5 + 100 + 10 + 100 - 1, 20 + 2 + 22 + 25 + 27 - 1, COLOR_BLACK);
tft.drawText(5 + 100 + 10 + 0 + (100 - tft.getTextWidth(pm25_text)) / 2, 20 + 2 + 22 + 25 + 6, pm25_text, COLOR_WHITE);
}
tft.getGFXTextExtent(String(constrain((int) data.PM_AE_UG_2_5, 0, 999)), 0, 0, &w, &h);
tft.fillRectangle(10, 20 + 2 + 74 - 10 - 30, 10 + 28 * 3+1, 20 + 2 + 74 - 10 + 1, pm25_color);
if ((int) data.PM_AE_UG_2_5 != 0) {
tft.drawGFXText(5 + (100 - w) / 2, 20 + 2 + 74 - 10, String(constrain((int) data.PM_AE_UG_2_5, 0, 999)), COLOR_WHITE);
} else {
tft.drawGFXText(5 + ( 100 - w) / 2, 20 + 2 + 74 - 10, "-", COLOR_WHITE);
}
tft.setBackgroundColor(pm100_color);
if (pm100_change) {
tft.fillRectangle(5, 20 + 78 + 2, 5 + 100, 20 + 78 + 2 + 74, pm100_color);
tft.setFont(Terminal11x16);
tft.drawText(5 + 2, 20 + 78 + 2 + 2, "pm 10.0", COLOR_WHITE);
tft.setBackgroundColor(COLOR_BLACK);
tft.fillRectangle(5 + 100 + 10 + 0 + 1, 20 + 78 + 2 + 22 + 25 + 1, 5 + 100 + 10 + 100 - 1, 20 + 78 + 2 + 22 + 25 + 27 - 1, COLOR_BLACK);
tft.drawText(5 + 100 + 10 + 0 + (100 - tft.getTextWidth(pm100_text)) / 2, 20 + 78 + 2 + 22 + 25 + 6, pm100_text, COLOR_WHITE);
}
tft.getGFXTextExtent(String(constrain((int) data.PM_AE_UG_10_0, 0, 999)), 0, 0, &w, &h);
tft.fillRectangle(10, 20 + 78 + 2 + 74 - 10 - 30, 10 + 28 * 3+1 , 20 + 78 + 2 + 74 - 10 + 1, pm100_color);
if ((int) data.PM_AE_UG_10_0 != 0) {
tft.drawGFXText(5 + (100 - w) / 2, 20 + 78 + 2 + 74 - 10, String(constrain((int) data.PM_AE_UG_10_0, 0, 999)), COLOR_WHITE);
} else {
tft.drawGFXText(5 + (100 - w) / 2, 20 + 78 + 2 + 74 - 10, "-", COLOR_WHITE);
}
int16_t x;
tft.fillRectangle(5 + 100 + 10 + 0 - 5, 20 + 2 + 22 + 1, 5 + 100 + 10 + 100 + 5, 20 + 2 + 22 + 24, COLOR_BLACK);
x = map(min((int) data.PM_AE_UG_2_5, 100), 0, 100, 0, 100);
if ((int) data.PM_AE_UG_2_5 != 0) {
tft.fillTriangle(5 + 100 + 10 + 0 + 0 + x, 20 + 2 + 22 + 1 + 0, 5 + 100 + 10 + 0 + 5 + x, 20 + 2 + 22 + 10, 5 + 100 + 10 + 0 - 5 + x, 20 + 2 + 22 + 10, COLOR_WHITE);
tft.drawLine(5 + 100 + 10 + 0 + 0 + x, 20 + 2 + 22 + 1, 5 + 100 + 10 + 0 + 0 + x, 20 + 2 + 22 + 24, COLOR_WHITE);
}
tft.fillRectangle(5 + 100 + 10 + 0 - 5, 20 + 78 + 2 + 22 + 1, 5 + 100 + 10 + 100 + 5, 20 + 78 + 2 + 22 + 24, COLOR_BLACK);
x = map(min((int) data.PM_AE_UG_10_0, 200), 0, 200, 0, 100);
if ((int) data.PM_AE_UG_10_0 != 0) {
tft.fillTriangle(5 + 100 + 10 + 0 + 0 + x, 20 + 78 + 2 + 22 + 1 + 0, 5 + 100 + 10 + 0 + 5 + x, 20 + 78 + 2 + 22 + 10, 5 + 100 + 10 + 0 - 5 + x, 20 + 78 + 2 + 22 + 10, COLOR_WHITE);
tft.drawLine(5 + 100 + 10 + 0 + 0 + x, 20 + 78 + 2 + 22 + 1, 5 + 100 + 10 + 0 + 0 + x, 20 + 78 + 2 + 22 + 24, COLOR_WHITE);
}
}
//코드1
int i,j;
String cipsend = "AT+CIPSEND=";
sendData(cmd, 1000, DEBUG);
if(esp8266.find("Error")) return;
cipsend += cmd_get.length();
cipsend += "\r\n";
sendData(cipsend, 0, DEBUG);
if(esp8266.find(">"))
{
Serial.println(">");
}else
{
sendData("AT+CIPCLOSE\r\n", 2000, DEBUG);
delay(1000); return;
}
Serial.print(cmd_get);
recived=sendData(cmd_get, 4000, DEBUG);
position = recived.indexOf("icon");
icon[0]= recived.charAt(position+7);
icon[1]= recived.charAt(position+8);
icon[2]=0; // null
Serial.print("Processing data : ");
Serial.println(data_count,DEC);
data_count=data_count+1;
Serial.print("Text Position : ");
Serial.println(position);
Serial.print("icon : ");
Serial.println(icon);
icon_in = atoi(icon);
Serial.println(icon_in,DEC);
switch (icon_in){
case 1 : setColor(255,0,255); break; //red
case 2 : setColor(255,228,0);break; //yellow
case 3 :setColor(140,140,140); break;//gray
case 4 : setColor(250,250,250); break; // dark gray
case 9 : setColor(1,0,255); break; //dark blue
case 10 : setColor(255,0,255);break; //blue
case 11 : setColor(76,76,76); break; // dark gray
case 13 : setColor(1,0,255); break; //dark blue
case 50 : setColor(128,65,217); break;// puple
default : setColor(255,0,0); break; //red
}
delay(3600000);
//코드2
}
boolean connectWiFi()
{
String cmd="AT+CWJAP?\r\n";
sendData(cmd, 0, DEBUG);
if(esp8266.find("OK"))
{
Serial.println("OK, Connected to WiFi.");
return true;
} else {
Serial.println("Can not connect to the WiFi.");
return false;
}
}
String sendData(String command, const int timeout, boolean debug)
{
String response = "";
esp8266.print(command); // send the read character to the esp8266
long int time = millis();
while( (time+timeout) > millis())
{
while(esp8266.available())
{
char c = esp8266.read(); // read the next character.
response+=c;
}
}
if(debug)
{
Serial.print(response);
}
return response;
}
void setColor(int red, int green, int blue)
{
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}
[/code]
댓글 3
조회수 6,738master님의 댓글
master 작성일
pms.begin(9600);
초기화 코드가 안보입니다.
다웅님의 댓글
다웅
넣어봤는데 오류가 뜹니다ㅠㅠ setup 문에 넣는게 아닌가요?? 소프트웨어 시리얼 날씨정보를 받아오는 루프가 한시간에 한번 동작되도록 설정하였는데 혹시 그것때문에 하드웨어시리얼이 안돌아가는건 아닌가요? ㅜㅜ
master님의 댓글
master
void loop() {
if (pms.read(data)) { <-- 보통은 if (Serial1.available()) { 이렇게 사용하지 않나요?