weathercube의 코드를 약간 바꿔서 실행했는데 weather code를 받아오는 것 까진 실행이 되는데 LED나 펌프, 가습기 모듈이 동작하지 않습니다. 아래의 코드가 맞는지 봐주시면 감사하겠습니다.
#include <SPI.h>
#include <WiFi.h>
#include <Adafruit_NeoPixel.h>
#define PIN 6 // 네오픽셀 할당 핀(Digital 6)
#define N_LEDS 30 // 사용 LED 개수
Adafruit_NeoPixel strip = Adafruit_NeoPixel(N_LEDS, PIN, NEO_GRB + NEO_KHZ800);
char ssid[] = "AndroidHotspot3374"; // your network SSID (name)
char pass[] = "52840005"; // your network password
char ON, OFF;
int keyIndex = 0; // your network key Index number (needed only for WEP)
int temp=0;
int status = WL_IDLE_STATUS;
// Initialize the Wifi client library
WiFiClient client;
// server address:
char server[] = "api.openweathermap.org";
//IPAddress server(64,131,82,241);
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
const unsigned long postingInterval = 10L * 1000L; // delay between updates, in milliseconds
String location = "Busan,KR";
String currentLine = "";
String weatherString = "";
boolean readingWeather = false;
int weather;
int PUMP = 2;
int Cloud = 7;
uint8_t ret;
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue:
while (true);
}
String fv = WiFi.firmwareVersion();
if ( fv != "1.1.0" )
Serial.println("Please upgrade the firmware");
// attempt to connect to Wifi network:
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
// you're connected now, so print out the status:
printWifiStatus();
// 기타 초기 설정
pinMode(Cloud,OUTPUT);// 가습기 핀 출력 설정
pinMode(PUMP,OUTPUT); // 펌프 핀 출력 설정
strip.begin(); // 네오픽셀 초기 값
strip.show(); // 네오픽셀 출력 함수
Rain_PUMP(0); // 펌프 출력 LOW
Cloud_PUMP(0); // 가습기(구름) 출력 LOW
}
void loop()
{
Data_Phasing(); // 와이파이로 부터 데이터 파싱 함수
Animation(); // 파싱받은 데이터에 따라 이펙트 출력 함수
}
void Animation() // 파싱해오는 weather값에 따라 이펙트 출력 함수
{
/*Lightening Rain*/
if(weather <= 232 && weather >= 200)
{
Rain_PUMP(1);
Lightening_LED();
Cloud_PUMP(1);//Cloud Effect
}
/*Light Rain*/
else if(weather <= 321 && weather >= 300)
{
Rain_PUMP(1);
Light_Rainy_LED();
Cloud_PUMP(0);
}
/*White Snow*/
else if(weather <= 622 && weather >= 600)
{
Rain_PUMP(1);
Snowy_LED();
}
/*Fog*/
else if(weather <= 721 && weather >= 701 || (weather == 741))
{
Rain_PUMP(0);
Cloud_PUMP(1);//Cloud Effect
//No LED
}
/*Dark Cloudy*/
else if((weather == 731) || (weather == 751) || (weather == 761) || (weather == 781))
{
Rain_PUMP(0);
Cloudy_LED();
Cloud_PUMP(1);// Cloud Effect
}
/*General Cloud*/
else if(weather <= 804 && weather >= 800)
{
Rain_PUMP(0);
Fine_LED();
Cloud_PUMP(1);// Cloud Effect
}
// Extreme Weather ---> Storm : Fast Dimming LED and Rain
}
void Data_Phasing() {
// if there's incoming data from the net connection.
// send it out the serial port. This is for debugging
// purposes only:
while (client.available()) {
char inChar = client.read();
//Serial.write(inChar);
currentLine += inChar;
if (inChar == '\n') {
//Serial.print("clientReadLine = ");
//Serial.println(currentLine);
currentLine = "";
}
if ( currentLine.endsWith("<weather number=")) {
readingWeather = true;
weatherString = "";
}
if (readingWeather) {
if (inChar != 'v') {
weatherString += inChar;
}
else {
readingWeather = false;
int weather = getInt(weatherString);
Serial.print("- weathercode: ");
Serial.print(weather);
}
}
}
// if ten seconds have passed since your last connection,
// then connect again and send data:
if (millis() - lastConnectionTime > postingInterval) {
httpRequest();
}
}
// this method makes a HTTP connection to the server:
void httpRequest() {
// 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.println("GET /data/2.5/weather?q="+location+"&mode=xml&APPID=8eae77cc884c4c9fd092b3f7cafc8623");
client.println("HOST: api.openweathermap.org\n");
client.println("User-Agent: ArduinoWiFi/1.1");
client.println("Connection: close");
client.println();
// note the time that the connection was made:
lastConnectionTime = millis();
}
else {
// if you couldn't make a connection:
Serial.println("connection failed");
}
}
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)
{
int i = 2;
while(input[i] != '"')
{
i++;
}
input = input.substring(2,i);
char carray[20];
input.toCharArray(carray, sizeof(carray));
temp = atoi(carray);
return temp;
}
void Cloud_PUMP(byte state) // 구름(가습기)구동 함수
{
if(state == 1){digitalWrite(Cloud,HIGH);}
else if(state == 0){digitalWrite(Cloud,LOW);}
}
void Rain_PUMP(byte state) // 펌프 구동 함수
{
if(state == 0){digitalWrite(PUMP,HIGH);}
else if(state == 1){digitalWrite(PUMP,LOW);}
}
void Fine_LED() // '맑은 날' LED 출력
{
int i;
for(i=0;i<201;i++)
{
LED_ON(strip.Color(255,i,0));
}
for(i=0;i<47;i++)
{
LED_ON(strip.Color(255,200,i));
}
for(i=0;i<56;i++)
{
LED_ON(strip.Color(255,200+i,46));
}
for(i=0;i<210;i++)
{
LED_ON(strip.Color(255,255,46+i));
}
for(i=0;i<256;i++)
{
LED_ON(strip.Color(255,255-i,255-i));
}
}
void Cloudy_LED() // '구름 낀 날' LED 출력
{
int i,j;
for(j=30;j<255;j++)
{
LED_ON(strip.Color(j, j, j));
}
for(j=255;j>30;j--)
{
LED_ON(strip.Color(j, j, j));
}
delay(3000);
}
void Snowy_LED() // '눈 오는 날' LED 출력
{
LED_ON(strip.Color(150, 150, 150));
delay(2000);
LED_ON(strip.Color(0,0,0));
delay(10);
LED_ON(strip.Color(150, 150, 150));
}
void Light_Rainy_LED() // '약한 비' LED 출력
{
int i;
for(i=0;i<201;i++)
{
LED_ON(strip.Color(0,234-i,234-i));
}
for(i=0;i<201;i++)
{
LED_ON(strip.Color(0,34+i,34+i));
}
}
void Lightening_LED() // '뇌우' LED 출력
{
int i,j,k;
for(i=0;i<15;i++)
{
LED_ON(strip.Color(80, 80, 250));
delay(10);
LED_ON(strip.Color(0, 0, 0));
delay(5);
}
for(i=0;i<5;i++)
{
LED_ON(strip.Color(80, 80, 250));
delay(500);
LED_ON(strip.Color(0, 0, 0));
delay(100);
}
for(i=0;i<8;i++)
{
LED_ON(strip.Color(80, 80, 250));
delay(1000);
LED_ON(strip.Color(0, 0, 0));
delay(50);
}
}
void LED_ON(uint32_t c) // Strip LED 출력 함수
{
for(uint16_t i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, c);
strip.show();
}
}
|