정보나눔

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

날씨 정보를 알려주는 무드램프과 비슷한 프로젝트 진행중입니다
leeshy | 2017-05-29

날씨 정보를 알려주는 무드램프와 비슷한 프로젝트를 진행중입니다.

 

저희는 해당 프로젝트와는 달리 날씨 정보를 받아와서 LED Matrix로 나타내는 동시에 온습도센서를 활용하여 측정값을 LCD Module에 출력하는 프로젝트입니다.

 

현재 각각의 기능들의 구현은 완료된 상태입니다. 

 

그런데 이제 하나의 스케치로 합치려고 하는데 문제가 발생했습니다. 문제의 원인도 어느정도 알게된거 같은데 아마도 delay 함수의 문제로 생각되어집니다. 그래서 저희는 millis() 함수를 이용하여 두개의 기능이 동시에 작동하도록 하고 싶은데 어느정도 진전이 생겼습니다. 

 

그런데 날씨 정보를 받아오는 값이 아래 사진과 같이 계속 0으로 출력됩니다. 그리고 습도 측정 값이 변화가 너무 심합니다.. 

 

 

 

 

어떻게 코드를 수정해야할지 도저히 모르겠습니다.. ㅠㅠㅠ 제발 도와주세요

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

LiquidCrystal_I2C lcd(0x3F, 16, 2);
int pin = 8;
DHT11 dht11(pin);

LedControl lc = LedControl(7, 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";
char pass[] = "leeshy91s";
int status = WL_IDLE_STATUS;

char server[] = "api.openweathermap.org";

unsigned long lastConnectionTime = 0;
const unsigned long postingInterval = 1000L;


boolean readingVal;
boolean getIsConnected = false;

int val, temp;
int weatherNum;

String rcvbuf;

WiFiClient client;

void printWifiStatus();
void httpRequest();

void setup()
{
  lcd.begin();
  Serial.begin(115200);
  Serial.println(F("\r\nSerial Init"));

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

  WiFi.init();

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

}

void weatherLcd() {

  int err;
  float tem, humi;

  /*unsigned long previousMillis = 0;
  unsigned long interval = 1000L;
  unsigned long currentMillis = millis()*/;

  if (millis() - lastConnectionTime > postingInterval) {
    //previousMillis = currentMillis;
    if ((err = dht11.read(humi, tem)) == 0) {
      char humiCharValue[10];
      dtostrf(humi, 4, 1, humiCharValue);

      String humiDisplayStr = "HUMI:     ";
      humiDisplayStr += (String)humiCharValue;
      humiDisplayStr += "%";

      lcd.setCursor(0, 0);
      lcd.print(humiDisplayStr);

      char temCharValue[10];
      dtostrf(tem, 4, 1, temCharValue);

      String temDisplayStr = "TEMP:     ";
      temDisplayStr += (String)temCharValue;
      temDisplayStr += "'C";

      lcd.setCursor(0, 1);
      lcd.print(temDisplayStr);

      Serial.print("HUMI : ");
      Serial.println(humiDisplayStr);
      Serial.print("TEMP : ");
      Serial.println(temDisplayStr);

    }

    else {
      lcd.print("ERROR NO : ");
      lcd.print(err);
    }

  }
}

void wifi_loop() {

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

    if (readingVal) {
      if (c != 'v' ) {
        valString += c;
      }
      else {
        readingVal = false;
        weatherNum = getInt(valString);
        Serial.print("weather code : ");
        Serial.println(weatherNum);
      }
    }
  }

  if (millis() - lastConnectionTime > postingInterval) {
    httpRequest();
  }
  rcvbuf = "";
}

void loop() {

  wifi_loop();
  weatherLcd();

}

void httpRequest() {
  Serial.println();

  client.stop();

  if (client.connect(server, 80)) {
    Serial.println("Connecting...");

    client.print("GET /data/2.5/weather?q=London,gb&appid=");
    client.print(VARID);
    client.println(" HTTP/1.1");
    client.println("Host: api.openweathermap.org");
    client.println("Connection: close");
    client.println();

    lastConnectionTime = millis();
    getIsConnected = true;
  }
  else {
    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];
  input.toCharArray(carray, sizeof(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]);
  }
}

이전글   |    온습도 센서(AM2301) 측정값을 네오픽셀(SY-SL0983)에 띄우기... 2017-05-29
다음글   |    아두이노와 blynk를 사용해서 알람을 오게 하려는데 도움 좀 부탁드릴게요... 2017-05-30