1. 프로젝트 사용한 보드 종류
ESP8266 (esp-12e)
2. 사용한 개발 프로그램명
아두이노 IDE
3. 사용한 센서 모델명
ESP-8266 (esp-12e)
4. 연결한 회로 설명 (또는 이미지)
5. 소스코드 (주석 필수)
#include
#include
#include
#include
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
String apiKey = "KBD1JSZTUKCXJ15V"; // thingspeak에서 주는 값으로 대체했음
const char *ssid = "Homewifi"; // replace with your wifi ssid and wpa2 key
const char *pass = "1q2w3e4r1!";
const char* server = "api.thingspeak.com";
#define LED_BUILTIN 16
#define SENSOR 2
long currentMillis = 0;
long previousMillis = 0;
int interval = 1000;
boolean ledState = LOW;
float calibrationFactor = 4.5;
volatile byte pulseCount;
byte pulse1Sec = 0;
float flowRate;
unsigned long flowMilliLitres;
unsigned int totalMilliLitres;
float flowLitres;
float totalLitres;
void IRAM_ATTR pulseCounter()
{
pulseCount++;
}
WiFiClient client;
void setup()
{
Serial.begin(115200);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //initialize with the I2C addr 0x3C (128x64)
display.clearDisplay();
delay(10);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(SENSOR, INPUT_PULLUP);
pulseCount = 0;
flowRate = 0.0;
flowMilliLitres = 0;
totalMilliLitres = 0;
previousMillis = 0;
attachInterrupt(digitalPinToInterrupt(SENSOR), pulseCounter, FALLING);
}
void loop()
{
currentMillis = millis();
if (currentMillis - previousMillis > interval)
{
pulse1Sec = pulseCount;
pulseCount = 0;
// Because this loop may not complete in exactly 1 second intervals we calculate
// the number of milliseconds that have passed since the last execution and use
// that to scale the output. We also apply the calibrationFactor to scale the output
// based on the number of pulses per second per units of measure (litres/minute in
// this case) coming from the sensor.
flowRate = ((1000.0 / (millis() - previousMillis)) * pulse1Sec) / calibrationFactor;
previousMillis = millis();
// Divide the flow rate in litres/minute by 60 to determine how many litres have
// passed through the sensor in this 1 second interval, then multiply by 1000 to
// convert to millilitres.
flowMilliLitres = (flowRate / 60) * 1000;
flowLitres = (flowRate / 60);
// Add the millilitres passed in this second to the cumulative total
totalMilliLitres += flowMilliLitres;
totalLitres += flowLitres;
// Print the flow rate for this second in litres / minute
Serial.print("Flow rate: ");
Serial.print(float(flowRate)); // Print the integer part of the variable
Serial.print("L/min");
Serial.print("\t"); // Print tab space
display.clearDisplay();
display.setCursor(10,0); //oled display
display.setTextSize(1);
display.setTextColor(WHITE);
display.print("Water Flow Meter");
display.setCursor(0,20); //oled display
display.setTextSize(2);
display.setTextColor(WHITE);
display.print("R:");
display.print(float(flowRate));
display.setCursor(100,28); //oled display
display.setTextSize(1);
display.print("L/M");
// Print the cumulative total of litres flowed since starting
Serial.print("Output Liquid Quantity: ");
Serial.print(totalMilliLitres);
Serial.print("mL / ");
Serial.print(totalLitres);
Serial.println("L");
display.setCursor(0,45); //oled display
display.setTextSize(2);
display.setTextColor(WHITE);
display.print("V:");
display.print(totalLitres);
display.setCursor(100,53); //oled display
display.setTextSize(1);
display.print("L");
display.display();
}
if (client.connect(server, 80)) // "184.106.153.149" or api.thingspeak.com
{
String postStr = apiKey;
postStr += "&field1=";
postStr += String(float(flowRate));
postStr += "&field2=";
postStr += String(totalLitres);
postStr += "\r\n\r\n";
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(postStr.length());
client.print("\n\n");
client.print(postStr);
}
client.stop();
}
6. 문제점 및 에러 내용
컴파일에는 오류가 안뜨는데 칩으로 전송을 하면 다음과 같은 오류가 확인됩니다.
시리얼포트는 툴에서 com4로 잘 뜹니다
아두이노:1.8.15 (Windows 10), 보드:"NodeMCU 1.0 (ESP-12E Module), 80 MHz, Flash, Disabled (new aborts on oom), Disabled, All SSL ciphers (most compatible), 32KB cache + 32KB IRAM (balanced), Use pgm_read macros for IRAM/PROGMEM, 4MB (FS:2MB OTA:~1019KB), 2, v2 Lower Memory, Disabled, None, Only Sketch, 115200"
Executable segment sizes:
ICACHE : 32768 - flash instruction cache
IROM : 258924 - code in flash (default or ICACHE_FLASH_ATTR)
IRAM : 28309 / 32768 - code in IRAM (IRAM_ATTR, ISRs...)
DATA : 1516 ) - initialized variables (global, static) in RAM/HEAP
RODATA : 1252 ) / 81920 - constants (global, static) in RAM/HEAP
BSS : 26624 ) - zeroed variables (global, static) in RAM/HEAP
스케치는 프로그램 저장 공간 290001 바이트(27%)를 사용. 최대 1044464 바이트.
전역 변수는 동적 메모리 29392바이트(35%)를 사용, 52528바이트의 지역변수가 남음. 최대는 81920 바이트.
esptool.py v3.0
Serial port COM4
Connecting........_____....._____....._____....._____....._____....._____.....____Traceback (most recent call last):
File "C:\Users\alsrb\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.0.0/tools/upload.py", line 66, in
esptool.main(cmdline)
File "C:/Users/alsrb/AppData/Local/Arduino15/packages/esp8266/hardware/esp8266/3.0.0/tools/esptool\esptool.py", line 3552, in main
esp.connect(args.before, args.connect_attempts)
File "C:/Users/alsrb/AppData/Local/Arduino15/packages/esp8266/hardware/esp8266/3.0.0/tools/esptool\esptool.py", line 529, in connect
raise FatalError('Failed to connect to %s: %s' % (self.CHIP_NAME, last_error))
esptool.FatalError: Failed to connect to ESP8266: Timed out waiting for packet header
_
선택돤 시리얼 포트_
는 존재하지 않거나 해당 보드가 연결되지 않았습니다.
이 리포트는 파일 -> 환경설정에 "컴파일중 자세한 출력보이기"를 활성화하여 더 많은 정보를 보이게 할 수 있습니다.
|