정보나눔

오픈소스하드웨어 프로젝트에 대한 다양한 정보를 나누는 공간입니다.

조언해주신 내용으로 코드를 수정해봤습니다
leeshy | 2017-05-22

말씀해주신대로 저희가 DHT11의 PIN을 2번에 할당하고 있었습니다. 그래서 일단은 7번으로 변경해둔 상태입니다.

수정한 스케치로 업로딩 한 결과 여전히 사이트에 접속이 되지 않았고, DHT11은 정상 작동하여 LCD 모듈에 데이터 값을 정상적으로 출력하고 있었습니다. 혹시나 하는 마음으로 저희 스케치에 있는 delay 값을 지워보니 이번엔 사이트에 제대로 접속이 됐고, 데이터를 받아왔으며 LED MATRIX까지 정상 작동되었습니다. 그런데 DHT11에서 측정한 데이터 값이 시리얼 모니터에는 출력이 되고 있는데, LCD 모듈에는 ERROR라는 메시지가 출력되고 있습니다.

 

아마도 말씀해주신 것처럼 I2C 통신과 SPI 통신이 겹치는 문제라고 생각이 되는데 공부를 통해 각각의 통신의 개념과 특징에 대해서 어느정도 이해를 한 상태인데, 이제 실제로 스케치에 있는 코드들을 수정해보려니 어디서부터 어떻게 시작을 해야될지 도저히 감이 잡히질 않습니다. 

 

어떠한 방식으로 수정을 해야하는지 다시 한번 조언 주시면 감사하겠습니다.

변경된 코드는 아래에 첨부하겠습니다.

 

#include <SPI.h>
#include <WizFi250.h>
#include "LedControl.h"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT11.h>

LedControl lc = LedControl(4, 6, 5, 2); // Pins: DIN,CLK,CS, # of Display connected
LiquidCrystal_I2C lcd(0x3F, 16, 2);
int pin = 7;
DHT11 dht11(pin);

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);

  lcd.begin();


}

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 = "";

  int err;
  float temper, humi;
  if ((err = dht11.read(humi, temper)) == 0)
  {
    lcd.backlight();
    lcd.display();
    lcd.print("TEMP:     ");
    lcd.print(temper);
    lcd.setCursor(0, 1);
    lcd.print("HUMIDITY: ");
    lcd.print(humi);

    Serial.print("temp: ");
    Serial.print(temper);
    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();
}

// 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]);
  }
}

 

프로필사진

수박쨈 2017-05-25 17:12:40

I2C와 SPI통신은 별개로 사용 가능합니다.

 

I2C는 아날로그 4, 5번 핀을 사용하고

SPI는 보통 디지털 10, 11, 12, 13번 핀을 사용합니다. 그렇기에 두 통신을 사용한다 해서 겹쳐지는 일은 없습니다.

다만 SPI와 SPI를 동시에 사용할 경우 문제가 됩니다. 그럴때는 CS(chipSelect)핀을 이용하여 통신할 모듈을 지정하여 통신할 수 있습니다.

 

근데 LedControl이 뭔지 모르겠네요ㅎ

프로필사진

leeshy 2017-05-25 17:42:47

그럼 제 코드의 문제는 대체 무엇일까요 ㅜㅜ 

전자의 문제가 아니라면 후자의 방법으로 한번 시도 해봐야겠네요...

아 그리구 LedControl은 제가 사용하고 있는 Max7219 led dot matrix의 라이브러리입니다!

이전글   |    아두이노와 터치스크린을 이용하여 그림판 만들려고하는데요 ... 2017-05-22
다음글   |    라즈베리파이3를 이용하여 화재감지기를 만들어볼려하는데 센서 2개 연결이 가능한가요?... 2017-05-24