loop문에서 thischar ==E 일때를 주석처리하고 실행하면 F일때 기상청에서 데이터값을 받아오고 반대로 F를 주석처리하고 E일때 실행하면 미세먼지값을 받아옵니다. 그러나 주석처리 없이 E나 F값을 받아오려고하면 사용 가능한 메모리 부족, 안정성에 문제가 있다면서 값을 받아오지 못하는것 같습니다. 메모리를 쭐일수있는 방법이 없을까요?..
#include <SPI.h>
#include <Phpoc.h>
PhpocClient client;
PhpocServer server(80);
String pm10="";
String wt_temp="";
String wt_wfKor="";
String wt_wfEn="";
String value="30";
const int dirPin = 2;
const int stepPin = 3;
const int stepsPerRevolution = 1000;
int k=0;
void setup() {
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
Serial.begin(9600);
while(!Serial);
}
void push(){
Phpoc.begin(PF_LOG_SPI | PF_LOG_NET);
//Phpoc.begin();
server.beginWebSocket("remote_push");
Serial.print("WebSocket server address : ");
Serial.println(Phpoc.localIP());
}
void motorleft(){
if(k==1){
int y=0;
int z=0;
digitalWrite(dirPin, HIGH);
for(int x = 0; x < stepsPerRevolution; x++)
{
y= y+3;
digitalWrite(stepPin, HIGH);
delayMicroseconds(3000-y);
digitalWrite(stepPin, LOW);
delayMicroseconds(3000-y);
}
delay(10);
digitalWrite(dirPin, HIGH);
for(int x = 0; x < stepsPerRevolution; x++)
{
z= z+3;
digitalWrite(stepPin, HIGH);
delayMicroseconds(0+z);
digitalWrite(stepPin, LOW);
delayMicroseconds(0+z);
}
}
}
void motorright(){
if(k==2){
digitalWrite(dirPin, LOW);
for(int x = 0; x < stepsPerRevolution; x++)
{
digitalWrite(stepPin, HIGH);
delayMicroseconds(500);
digitalWrite(stepPin, LOW);
delayMicroseconds(500);
}
delay(1000);
}
}
void pm(){
char server_name[] = "openapi.airkorea.or.kr";
Phpoc.begin(PF_LOG_SPI | PF_LOG_NET);
//Phpoc.begin();
Serial.println("Sending GET request to web server");
if(client.connect(server_name, 80))
{
Serial.println("Connected! Making HTTP request to openapi.airkorea.co.kr");
client.println("GET /openapi/services/rest/ArpltnInforInqireSvc/getMsrstnAcctoRltmMesureDnsty?serviceKey=deiuNSWVM7akLUOeI2UMvtLlFp%2BFFgxqGsJSSvV8TO50vGjdUueQiTS0U0k5XTLO0PzTJukKH9iCHnekONK00A%3D%3D&numOfRows=1&pageNo=1&stationName=봉산동&dataTerm=DAILY&ver=1.3 HTTP/1.1");
client.println("HOST: openapi.airkorea.or.kr");
client.println("Connection: close");
client.println();
Serial.print("pm10 information for ");
}
else
Serial.println("connection failed");
}
void pmparsing(){
while (client.connected()) {
if (client.available()) {
//라인을 기준으로 문자열을 저장한다.
String line=client.readStringUntil('\n');
int i= line.indexOf("</pm10Value>");
if(i>0){
String tmp_str="<pm10Value>";
String pm10 = line.substring(line.indexOf(tmp_str)+tmp_str.length(),i);
Serial.print(" : ");
Serial.print(pm10);
Serial.print(" ug/m^3 ");
if(value>pm10){
//기준값 미세먼지보다 대기 미세먼지가 낮으므로 창문 열림
if(k==0)
{
k=1;
}
motorleft();
k=2;
Serial.println("letf");
}
else if(value<pm10){ //기준값 미세먼지보다 대기 미세먼지가 높으므로 창문 닫힘
if(k==0)
{
k=2;
}
motorright();
k=1;
Serial.println("right");
}
client.stop();
}
}
}
}
void rss(){
char host_name[] = "www.kma.go.kr";
Phpoc.begin(PF_LOG_SPI | PF_LOG_NET);
//Phpoc.begin();
Serial.println("Sending GET request to web server");
if(client.connect(host_name, 80))
{
Serial.println("Connected! Making HTTP request to www.kma.go.kr");
client.println("GET /wid/queryDFSRSS.jsp?zone=4155051000 HTTP/1.1");
//위에 지정된 주소와 연결한다.
client.println("HOST: www.kma.go.kr");
//client.println("User-Agent: launchpad-wifi");
client.println("Connection: close");
client.println();
Serial.println("Weather information for ");
}
else
Serial.println("connection failed");
}
void rssparsing(){
while (client.connected()) {
while (client.available()) {
//라인을 기준으로 문자열을 저장한다.
String line=client.readStringUntil('\n');
//시간
int temp11= line.indexOf("</hour>");
if(temp11>0) {
String tmp_str="<hour>";
String wt_hour = line.substring(line.indexOf(tmp_str)+tmp_str.length(),temp11);
Serial.print("hour : ");
Serial.println(wt_hour);
}
//온도
int temp= line.indexOf("</temp>");
if(temp>0) {
String tmp_str="<temp>";
String wt_temp = line.substring(line.indexOf(tmp_str)+tmp_str.length(),temp);
Serial.print("temperature : ");
Serial.println(wt_temp);
}
//날씨 정보
int wfEn= line.indexOf("</wfEn>");
if(wfEn>0) {
String tmp_str="<wfEn>";
String wt_twfEn = line.substring(line.indexOf(tmp_str)+tmp_str.length(),wfEn);
Serial.print("weather : ");
Serial.println(wt_twfEn);
Serial.println("=====================");
delay(200);
}
}
}
delay(2000);
client.stop();
}
void loop() {
push();
PhpocClient client = server.available();
if (client) {
if (client.available() > 0) {
// read the bytes incoming from the client:
while(1){
char thisChar = client.read();
if(thisChar == 'A') {
Serial.println("button A release");
while(1){
char thisChar = client.read();
if(thisChar == 'B') {
Serial.println("button b release");
k=2;
motorright();
}
else if(thisChar == 'C') {
Serial.println("button c release");
k=1;
motorleft();
}
else if (thisChar == 'D') {
Serial.println("button d release");
break;
}
}
}
else if (thisChar == 'E'){
Serial.println("button e release");
delay(6000);
pm();
delay(3000);
pmparsing();
delay(7000);
}
else if (thisChar == 'F') {
Serial.println("button f release");
delay(3000);
rss();
delay(3000);
rssparsing();
delay(3000);
}
}
}
}delay(6000);
}
|