BASIC4MCU | 질문게시판 | 아두이노 api 질문
페이지 정보
작성자 리니어 작성일2022-05-26 19:19 조회272회 댓글1건첨부파일
본문
esp8266 d1 mini와 lcd디스플레이를 사용하여 비트코인 시세 모니터를 만들고 있습니다.
코드를 실행시키면 통신이 되지 않는지 디스플레이에 Connecting..., Complete! 만 출력이 되고 그 이후는 출력이 전혀되지 않습니다. 이것은 어떻게 해결하나요? (WiFi 연결 했습니다, 코드에 에러는 없습니다.)
디스플레이 영상 첨부합니다.
코드
#include <ESP8266WiFi.h> #include <ESP8266HTTPClient.h> #include <Wire.h> #include <LiquidCrystal_I2C.h> WiFiClient client; LiquidCrystal_I2C lcd(0x27, 16, 2); HTTPClient http; String payload; int start_idx; int end_idx ; String prc; int price; float percent; int old_price=13000000; int key_blink = 0;; int thehour; void setup() { Serial.begin(115200); lcd.init(); lcd.backlight(); lcd.clear(); lcd.setCursor(0, 0); lcd.print("Connecting..."); WiFi.waitForConnectResult(); lcd.setCursor(0, 1); lcd.print("Complete!"); delay(500); lcd.clear(); } void loop() { WiFi.waitForConnectResult(); String hour=getTime().substring(17,19); thehour=hour.toInt(); if(thehour<16){ thehour=thehour+9; char buf[30]={0}; getTime().toCharArray(buf,30); lcd.setCursor(0, 0); lcd.print(thehour); lcd.setCursor(2, 0); lcd.print(getTime().substring(19, 22)); } else{ char buf[30]={0}; getTime().toCharArray(buf,30); lcd.setCursor(0,0); lcd.println(getTime().substring(17, 22)); } http.begin(client, "http://api.coindesk.com/v1/bpi/currentprice/KRW.json"); int httpCode = http.GET(); if (httpCode == HTTP_CODE_OK) { payload = http.getString(); start_idx = payload.lastIndexOf("rate_float") + 12; end_idx = payload.lastIndexOf("}}}"); prc = payload.substring(start_idx, end_idx); price = (float)prc.toInt()*1.0807; lcd.setCursor(13, 1); lcd.print(key_blink == 0?"/":"|"); key_blink = 1 - key_blink; if (old_price != price) { if(price > old_price){ lcd.setCursor(9, 0); lcd.print("+"); float x = price - old_price; float y = x/float(old_price); float percent = 100 * y; lcd.setCursor(10, 0); lcd.print(percent); lcd.setCursor(15, 0); lcd.print("%"); Serial.println(price); Serial.println(old_price); Serial.println(percent); } if(price < old_price){ lcd.setCursor(9, 0); lcd.print("-"); float x = old_price - price; float y = x/float(old_price); float percent = 100 * y; lcd.setCursor(10, 0); lcd.print(percent); lcd.setCursor(15, 0); lcd.print("%"); Serial.println(price); Serial.println(old_price); Serial.println(percent); } lcd.setCursor(0, 1); lcd.print(" "); lcd.setCursor(0, 1); lcd.print("KRW"); lcd.setCursor(4, 1); lcd.print(price); old_price = price; } } else { lcd.setCursor(15, 0); lcd.print("ERR"); } delay(1500); } String getTime() { while (!client.connect("google.com", 80)) {} client.print("HEAD / HTTP/1.1\r\n\r\n"); while(!client.available()) {} while(client.available()){ if (client.read() == '\n') { if (client.read() == 'D') { if (client.read() == 'a') { if (client.read() == 't') { if (client.read() == 'e') { if (client.read() == ':') { client.read(); String timeData = client.readStringUntil('\r'); client.stop(); return timeData; } } } } } } } }
댓글 1
조회수 272master님의 댓글
master 작성일
https://ieatt.tistory.com/51
와이파이가 외부 인터넷에 접속하기 위해선 공유기 설정을 해야 하는 것 아닐까요?