두개의 스케치를 하나로 합치려고 하는데 제대로 작동이 안되서 질문 드립니다
먼저 첫번째 스케치는, 코코아팹 프로젝트를 응용한건데 날씨 데이터를 받아와서 해당 날씨를 LED Matrix에 표현해주는 스케치입니다
#include <SPI.h>
#include <WizFi250.h>
#include "LedControl.h"
LedControl lc = LedControl(4, 6, 5, 2); // Pins: DIN,CLK,CS, # of Display connected
unsigned long delayTime = 200; // Delay between Frames
int getInt(String input);
#define VARID "7b96e29a687be49d95d2cf812701e9cd"
char ssid[] = "Leeshy"; // your network SSID (name)
char pass[] = "leeshy91s"; // your network password
int status = WL_IDLE_STATUS; // the Wifi radio's status
char server[] = "api.openweathermap.org";
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
const unsigned long postingInterval = 1000L; // delay between updates, in milliseconds
boolean readingVal;
boolean getIsConnected = false;
//String valString;
int val, temp;
int weatherNum;
String rcvbuf;
// Initialize the Ethernet client object
WiFiClient client;
void httpRequest();
void printWifiStatus();
void setup()
{
// initialize serial for debugging
Serial.begin(115200);
Serial.println(F("\r\nSerial Init"));
WiFi.init();
// check for the presence of the shield
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue
while (true);
}
// attempt to connect to WiFi network
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network
status = WiFi.begin(ssid, pass);
}
Serial.println("You're connected to the network");
printWifiStatus();
lc.shutdown(0, false); // Wake up displays
lc.shutdown(1, false);
lc.setIntensity(0, 5); // Set intensity levels
lc.setIntensity(1, 5);
lc.clearDisplay(0); // Clear Displays
lc.clearDisplay(1);
}
void loop() {
// if there's incoming data from the net connection send it out the serial port
// this is for debugging purposes only
String valString;
while (client.available()) {
if ( rcvbuf.endsWith("\"id\":")) {
readingVal = true;
valString = "";
}
char c = client.read();
if ( c != '\n' ) {
if (rcvbuf.length() > 30)
rcvbuf = "";
rcvbuf += c;
//Serial.write(c);
}
if (readingVal) {
if (c != 'v' ) {
valString += c;
//Serial.write(c);
}
else {
readingVal = false;
weatherNum = getInt(valString);
Serial.print("weather code : ");
Serial.println(weatherNum);
}
}
}
if (millis() - lastConnectionTime > postingInterval) {
httpRequest();
}
rcvbuf = "";
}
// this method makes a HTTP connection to the server
void httpRequest() {
Serial.println();
// close any connection before send a new request
// this will free the socket on the WiFi shield
client.stop();
// if there's a successful connection
if (client.connect(server, 80)) {
Serial.println("Connecting...");
// send the HTTP PUT request
client.print("GET /data/2.5/weather?q=Seoul,kr&appid=");
client.print(VARID);
client.println(" HTTP/1.1");
client.println("Host: api.openweathermap.org");
client.println("Connection: close");
client.println();
// note the time that the connection was made
lastConnectionTime = millis();
getIsConnected = true;
}
else {
// if you couldn't make a connection
Serial.println("Connection failed");
getIsConnected = false;
}
if (weatherNum > 799 && weatherNum < 802) { //맑음
smatrix1(), smatrix2();
}
else;
if (weatherNum > 700 && weatherNum < 782) { //흐림
smatrix3(), smatrix4();
}
else;
if (weatherNum > 801 && weatherNum < 805) { //흐림
smatrix3(), smatrix4();
}
else;
if (weatherNum > 199 && weatherNum < 532) { //눈,결정
smatrix7(), smatrix8();
}
}
void printWifiStatus() {
// print the SSID of the network you're attached to
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength
long rssi = WiFi.RSSI();
Serial.print("Signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
int getInt(String input) {
char carray[20];
//Serial.println(input);
input.toCharArray(carray, sizeof(carray));
//Serial.println(carray);
temp = atoi(carray);
return temp;
}
byte matrix1[] = { //맑
B00110100,
B00110110,
B00110100,
B00000000,
B00111100,
B00010100,
B00100100,
B00110100
};
byte matrix2[] = { //음
B00011000,
B00111100,
B00011000,
B00000000,
B01111110,
B00000000,
B00111100,
B00111100
};
byte matrix3[] = { //흐
B00011000,
B01111110,
B00011000,
B00100100,
B00011000,
B00000000,
B11111111,
B00000000
};
byte matrix4[] = { //림
B01111010,
B00001010,
B01111010,
B01000010,
B01111010,
B00000000,
B00011110,
B00011110
};
byte matrix5[] = { //눈
B00100000,
B00100000,
B00111100,
B00000000,
B01111110,
B00001000,
B00100000,
B00111100
};
byte matrix6[] = { //결정
B00001000,
B00101010,
B00011100,
B01111111,
B00011100,
B00101010,
B00001000,
B00000000
};
byte matrix7[] = { //비
B00000010,
B10001010,
B10001010,
B11111010,
B10001010,
B10001010,
B11111010,
B00000010
};
byte matrix8[] = { //우산
B00011000,
B00111100,
B01111110,
B11111111,
B00001000,
B00001000,
B00101000,
B00011000
};
void smatrix1() {
for (int i = 0; i < 8; i++) {
lc.setRow(0, i, matrix1[i]);
}
}
void smatrix2() {
for (int i = 0; i < 8; i++) {
lc.setRow(1, i, matrix2[i]);
}
}
void smatrix3() {
for (int i = 0; i < 8; i++) {
lc.setRow(0, i, matrix3[i]);
}
}
void smatrix4() {
for (int i = 0; i < 8; i++) {
lc.setRow(1, i, matrix4[i]);
}
}
void smatrix5() {
for (int i = 0; i < 8; i++) {
lc.setRow(0, i, matrix5[i]);
}
}
void smatrix6() {
for (int i = 0; i < 8; i++) {
lc.setRow(1, i, matrix6[i]);
}
}
void smatrix7() {
for (int i = 0; i < 8; i++) {
lc.setRow(0, i, matrix7[i]);
}
}
void smatrix8() {
for (int i = 0; i < 8; i++) {
lc.setRow(1, i, matrix8[i]);
}
}
그리고 이 스케치와 합치려는 것도 코코아팹에 있는 dht11와 lcd 모듈을 이용한 프로젝트입니다
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT11.h>
//LCD 설정
LiquidCrystal_I2C lcd(0x3F,16,2);
//온습도센서 연결설정
int pin=2;
DHT11 dht11(pin);
void setup()
{
lcd.init();
Serial.begin(9600);
}
void loop()
{
int err;
float temp, humi;
if((err=dht11.read(humi, temp))==0)
{
lcd.backlight();
lcd.display();
lcd.print("TEMP: ");
lcd.print(temp);
lcd.setCursor(0,1);
lcd.print("HUMIDITY: ");
lcd.print(humi);
Serial.print("temp: ");
Serial.print(temp);
Serial.print("humi: ");
Serial.print(humi);
Serial.println();
}
else
{
lcd.backlight();
lcd.display();
lcd.print("ERROR NO.: ");
lcd.print(err);
}
delay(100000); //10초마다 Refresh
lcd.clear();
}
이렇게 두개의 스케치를 합치려고 하는데 문제가 발생합니다...
일단 와이파이에 접속이 안됩니다. 시리얼 모니터를 보니깐 timeout이 뜨고 그 후에 fail이 뜹니다
그리고 두번째 문제는 dht11을 통해 얻은 온습도 값이 lcd모듈에 처음에는 정상적으로 출력됐다가. 그 후에 0.00으로 초기화되고 그 상태가 지속됩니다.
검색을 해보니 setup -> setup, loop -> loop 끼리 합쳐주고, 중복된 변수명이나 함수명이 없는지 확인하면 된다는데 이러한 방법으로 계속해서 시도해도 똑같은 결과뿐입니다
어떻게해야하될지 조언 주시면 감사하겠습니다
|