인터넷 띵!!!


IoT 실험


선구자들이 해 놓은게 많아서

연습하는데 어렵지는 않다 ...


이번에는

ESP8266  모듈중에 가장 작고 저렴한

ESP-01을 이용하여 만든것 ...


이 모듈은 IO 핀이 2개 이나 ...

시리얼 통신을 포기하면 RxD 및 TxD까지 4개가 된다

GPIO 0

GPIO 2

GPIO 1 = TxD

GPIo 3 = RxD


여기서는 송신은 하고 수신은 포기함

따라서 GPIO 3 (RxD)에 온습도 센서를 연결하고

GPIO 0/2에는 OLED LCD를 I2C로 연결한다..


온도를 측정해서 OLED에 표시하고 thingspeak로 데이터 전송


코드...


// ============================================================================================
// https://randomnerdtutorials.com/esp8266-0-96-inch-oled-display-with-arduino-ide/
// ============================================================================================
/*
 * Random Nerd Tutorials - Rui Santos 
 * Complete Project Details http://randomnerdtutorials.com
 * 
 * The MIT License (MIT)
 * Copyright (c) 2016 by Daniel Eichhorn
 */
// ============================================================================================
// ESP8266 보드 설치하기
// https://learn.sparkfun.com/tutorials/esp8266-thing-hookup-guide/installing-the-esp8266-arduino-addon
// To begin, we’ll need to update the board manager with a custom URL. Open up Arduino, 
// then go to the Preferences (File > Preferences). Then, towards the bottom of the window, 
// copy this URL into the “Additional Board Manager URLs” text box:
//
// Hit OK. Then navigate to the Board Manager by going to Tools > Boards > Boards Manager. 
// There should be a couple new entries in addition to the standard Arduino boards. 
// Look for esp8266. Click on that entry, then select Install.
// SSD1306 Lib for ESP8266
// https://randomnerdtutorials.com/esp8266-0-96-inch-oled-display-with-arduino-ide/
// https://github.com/squix78/esp8266-oled-ssd1306/archive/master.zip
// unzip to : C:\Users\turbocrazy\Documents\Arduino\libraries
// other lib not tested
// https://github.com/ThingPulse/esp8266-oled-ssd1306
// ============================================================================================ 
// Include the correct display library
// For a connection via I2C using Wire include
#include 
#include                  // Only needed for Arduino 1.6.5 and earlier
#include "SSD1306.h"              // alias for `#include "SSD1306Wire.h"`
#include 
#define DHTPIN 3                  // what pin we're connected to RxD = GPIO 3
#define DHTTYPE DHT11             // DHT 11
// Initialize the OLED display using Wire library
SSD1306  display(0x3c, 0, 2);     // ESP-01 : GPIO 0 = SDA, GPIO 2 = SCL
// Initialize DHT sensor for normal 16mhz Arduino
DHT dht(DHTPIN, DHTTYPE);
WiFiClient client;
String apiKey = "0--XXXX--XXXXXXXX";            // your ThingSpeak API key
const char* ssid = "iptimeXXXX";               // your ssid
const char* password = "xxxxxxxxxx";            // your password
const char* server = "api.thingspeak.com";
const int recordPeriod = 600;                  // Recording Period (seconds)
// ============================================================================================
void setup(){
  Serial.begin(115200);                       // Arduino monitor
  // Initialising the UI will init the display too.
  display.init();
  display.flipScreenVertically();
  display.setFont(ArialMT_Plain_16);
  display.setTextAlignment(TEXT_ALIGN_LEFT);
  dht.begin(); // initialize dht
  display.clear();
  display.drawString(0, 0, "connecting ..."); 
  display.display();
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) delay(500);
  display.clear();
  display.drawString(0, 0, "Connected!!");
  display.display();  
}
// ============================================================================================
void displayTempHumid(){
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  float t = dht.readTemperature();      // Read temperature as Celsius
  float f = dht.readTemperature(true);  // Read temperature as Fahrenheit

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)){
    display.clear(); // clearing the display
    display.drawString(5,0, "Failed DHT");
    return;
  }
  Serial.println("Humidity: " + String(h) + "  Temp: " + String(t));
  display.clear();
  display.drawString(0, 0, "Humidity: " + String(h) + "%\t"); 
  display.drawString(0, 16, "Temp: " + String(t) + "C"); 
  display.drawString(0, 32, "Temp: " + String(f) + "F"); 
  
  if (client.connect(server,80)) {  
    String postStr = apiKey;
           postStr +="&field1=";
           postStr += String(t, 1);
           postStr +="&field2=";
           postStr += String(h, 1);
    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();
  //ESP.deepSleep(recordPeriod * 1000000 * 1.05);  
}
// ============================================================================================
void loop(){
  displayTempHumid();
  display.display();
  delay(99000);
}
// ============================================================================================


'IoT_ESP8266' 카테고리의 다른 글

ESP-01 : 서버로 LED ON/OFF  (0) 2019.01.01
ESP-8266 : 서버 - LED/Relay 제어  (0) 2019.01.01
ESP-01 with OLED  (1) 2018.12.30
Arduino IDE 이용 ESP8266 웹서버  (0) 2018.08.14
IoT - 사물인터넷 기초 자료  (0) 2017.01.30
블로그 이미지

DIYworld

,