BASIC4MCU | 질문게시판 | 아두이노 lcd표시문제
페이지 정보
작성자 아두이노공부해요 작성일2021-12-10 14:38 조회14,036회 댓글4건본문
오픈웨더맵에서 위도경도를 넣고 그 지역의 pm10값이랑 pm25값을 찾아서 수치를 lcd에 나타내야하는데 수치뿐아니라 문자까지 나타내서 숫자만 나타나게 하던가 중간에 짜르는건 어떻게 하나요?
------------------------------------------------------
pm25: (수치) um/m3
pm10: (수치) um/m3
------------------------------------------------------ 이렇게 표시하고싶어요
lcd에 표시되는 사진이랑 api로 불러들인 자료사진도 첨부했습니다
코드입니다
#include "WizFi250.h"
#include <SPI.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C LCD
char ssid[] = "NEXT-2204N PLUS"; // your network SSID (name)
char pass[] = "39853985"; // your network password
int status = WL_IDLE_STATUS; // the Wifi radio's status
//Ubidots information
#define lat "35.19" //위도
#define ion "128.1" //경도
#define appid "564900d0968f0885f60f9b7e68482c74"
//Hardware Pin status
char server[] = "api.openweathermap.org";
boolean readingVal;
boolean readingVal1;
boolean viewData;
boolean getIsConnected = false;
String pm25;
String pm10;
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"));
pinMode(7, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP);
pinMode(5, OUTPUT);
lcd.init();
lcd.backlight();
lcd.print(" pm Info System ");
lcd.setCursor(0, 1);
lcd.print(" pm10, pm25 ");
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
while (client.available()) {
digitalWrite(5,HIGH); //led on
char c = client.read();
if ( c != NULL ) {
if (rcvbuf.length() > 80)
rcvbuf = "";
rcvbuf += c;
Serial.write(c);
if (rcvbuf.endsWith("\"pm2_5\":")) {
readingVal = true;
pm25= "";
}
else if (rcvbuf.endsWith("\"pm10\":")) {
readingVal1 = true;
pm10= "";
}
if (readingVal)
dataParser(c, pm25, readingVal);
else if (readingVal1)
dataParser(c, pm10, readingVal1);
}
digitalWrite(5,LOW); //LED OFF
}
//if (millis() - lastConnectionTime > postingInterval) {
if (!digitalRead(7)) {
httpRequest();
}
if (!digitalRead(6)) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Daejeon ");
lcd.setCursor(0, 1);
lcd.print(" pm25 pm10 info ");
delay(5000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("pm25:");
lcd.print(pm25);
lcd.print("ug/m3 ");
lcd.setCursor(0, 1);
lcd.print("pm10:");
lcd.print(pm10);
lcd.print("ug/m3 ");
delay(5000);
}
rcvbuf = "";
}
// 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(F("GET /data/2.5/air_pollution?lat="));
client.print(lat);
client.print(F("&lon="));
client.print(ion);
client.print(F("&appid="));
client.print(appid);
client.print(F(" HTTP/1.1\r\n"));
client.print(F("Host: api.openweathermap.org\r\n"));
client.print(F("Connection: close\r\n"));
client.print(F("\r\n\r\n"));
}
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");
}
void dataParser(char c, String &data, boolean &b) {
if (c != '<') {
if (c != '>')
data += c;
}
else {
b = false;
}
}
댓글 4
조회수 14,036master님의 댓글
master 작성일
void parsing() {
String announce_time; int tm_start= line0.indexOf(F("<tm>")); // "<tm>"문자가 시작되는 인덱스 값('<'의 인덱스)을 반환한다.
int tm_end= line0.indexOf(F("</tm>"));
announce_time = line0.substring(tm_start + 4, tm_end); // +4: "<tm>"스트링의 크기 4바이트, 4칸 이동
https://postpop.tistory.com/86
위 링크를 보면 문자열과 문자열 사이의 문자를 파싱하는 예가 있습니다.
만약 컴마 전까지 파싱하고 싶다면
int tm_end= line0.indexOf(F(","));
이렇게 변경하면 되겠죠
아두이노공부해요님의 댓글
아두이노공부해요
글을 늦게봐서 답변을 늦게 달았습니다.
블로그도 보고 저 line0 부분을 pm25로 바꾸고 해도 잘되지않아 어떻게 고쳐야 하는지 자세히 알려주실수 있을까요?
void parsing() {
String pm25; int tm_start= pm25.indexOf(F("\"pm2_5\":"));
int tm_end= pm25.indexOf(F(","));
String pm10; int hour_start= pm10.indexOf(F("\"pm10\":"));
int hour_end= pm10.indexOf(F(",")); }
고치기는 이렇게 고쳐봤습니다만...
master님의 댓글
master
동일한 경험이 없어서 조언을 드리지 못합니다.
보통 코드를 완전히 이해하면 수정이나 디버깅을 할 수 있게 됩니다.
아두이노공부해요님의 댓글
아두이노공부해요
알겠습니다 제가 아직 초보라 조금더 천천히 보면서 공부해보겠습니다. 알려주셔서 감사합니다