#include <Adafruit_NeoPixel.h>
#include <avr/power.h>
#include <SPI.h>
#include "WizFi250.h"
#define PIN 10
Adafruit_NeoPixel strip = Adafruit_NeoPixel(10, PIN, NEO_GRB + NEO_KHZ800); //neopixel LED의 개수 설정 및 선언
int getInt(String input);
#define VARID "API KEY" // openweather map 가입 시 지급되는 API KEY를 입력해주세요.
char ssid[] = "SSID"; // 사용할 Wi_Fi 네트워크의 SSID를 입력해주세요
char pass[] = "Password"; // 사용할 Wi_Fi 네트워크의 Password를 입력해주세요
int status = WL_IDLE_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 redVal; //RGB LED red 값 변수
int greenVal; //RGB LED green 값 변수
int blueVal; //RGB LED blue 값 변수
float weatherNum;
int weatherButton = 9; //날씨 데이터 파싱 동작을 위한 버튼을 9번에 연결
int powerButton = 8; //조명 on, off 동작을 위한 버튼을 8번에 연결
//on, off 버튼 토글 동작을 위한 변수
int oneTimeFlag;
boolean onOffStatus;
String rcvbuf;
// Initialize the Ethernet client object
WiFiClient client;
uint32_t Wheel(byte WheelPos) {
WheelPos = 255 - WheelPos;
if (WheelPos < 85) {
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
} else if (WheelPos < 170) {
WheelPos -= 85;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
} else {
WheelPos -= 170;
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}
}
// 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&mode=xml&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();
}
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) {
char carray[20];
//Serial.println(input);
input.toCharArray(carray, sizeof(carray));
//Serial.println(carray);
temp = atoi(carray);
return temp;
}
void dimming(int color) { //if 1=red, 2=green, 3=blue, 4=yellow, 5=white
uint16_t i, j;
for (int k = 0; k < 3; k++) {
for (j = 0; j < 256; j++) {
for (i = 0; i < strip.numPixels(); i++) {
if (color == 1) strip.setPixelColor(i, strip.Color(j, 0, 0));
else if (color == 2) strip.setPixelColor(i, strip.Color(0, j, 0));
else if (color == 3) strip.setPixelColor(i, strip.Color(0, 0, j));
else if (color == 4) strip.setPixelColor(i, strip.Color(j, j, 0));
else if (color == 5) strip.setPixelColor(i, strip.Color(j, j, j));
}
delay(8);
strip.show();
}
}
}
void theaterChase(uint32_t c, uint8_t wait) {
for (int j = 0; j < 10; j++) { //do 10 cycles of chasing
for (int q = 0; q < 3; q++) {
for (int i = 0; i < strip.numPixels(); i = i + 3) {
strip.setPixelColor(i + q, c); //turn every third pixel on
}
strip.show();
delay(wait);
for (int i = 0; i < strip.numPixels(); i = i + 3) {
strip.setPixelColor(i + q, 0); //turn every third pixel off
}
}
}
}
void rainbow() {
uint16_t i, j;
for (j = 0; j < 256; j++) {
for (i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel((i + j) & 255));
}
strip.show();
delay(20);
}
}
void setup()
{
// initialize serial for debugging
Serial.begin(115200);
Serial.println(F("\r\nSerial Init"));
pinMode(powerButton, INPUT_PULLUP);
pinMode(weatherButton, INPUT_PULLUP);
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();
}
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("<weather number=\"")) {
readingVal = true;
valString = "";
}
char c = client.read();
if ( c != NULL ) {
if (rcvbuf.length() > 100)
rcvbuf = "";
rcvbuf += c;
// Serial.write(c);
}
if (readingVal) {
if (c != '\"' ) {
valString += c;
//Serial.write(c);
}
//Serial.println(valString);
else { //파싱 끝
readingVal = false;
weatherNum = getInt(valString);
Serial.println(weatherNum);
if (weatherNum > 801 && weatherNum < 805) {
Serial.println("cloudy");
dimming(5); //white
}
else if (weatherNum > 800) {
Serial.println("sunny");
rainbow();
}
else if (weatherNum > 700) {
dimming(5); //white
Serial.println("mist");
}
else if (weatherNum >= 600) {
dimming(5); //white
theaterChase(strip.Color( 255, 255, 255), 50); // white
Serial.println("snow");
}
else if (weatherNum >= 300) {
Serial.println("rain");
dimming(4); //yellow
theaterChase(strip.Color( 255, 255, 255), 50); // white
}
}
}
}
if (digitalRead(powerButton) == LOW) { //power 버튼이 눌러지면 onOffstatus의 상태값 토글
if (oneTimeFlag == 0) {
oneTimeFlag = 1;
onOffStatus = !onOffStatus;
}
}
else {
oneTimeFlag = 0;
}
if (onOffStatus == 0) { //onOffStatus의 상태가 0이면
for (int i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, 0, 0, 0);
strip.show();
}
}
else {
if (!digitalRead(weatherButton)) { //날씨 버튼이 눌렸을때, 데이터 요청
httpRequest();
}
rcvbuf = "";
redVal = map(analogRead(A0), 0, 1023, 0, 255);
greenVal = map(analogRead(A1), 0, 1023, 0, 255);
blueVal = map(analogRead(A2), 0, 1023, 0, 255);
for (int i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, redVal, greenVal, blueVal);
strip.show();
}
}
}
이걸 지금 제작하는중인데요 이번에올려주신 무드등인데
버튼없게만들구 싶은데 버튼 부분을 어디서부터 어디뺄지 알려주실수 있으신가요?
ws2801 led제품 쓰는데 올려진 네오픽셀로 그냥 사용가능한가요?
|