Stacja pogodowa NodeMCU i HD44780 i2c
Chcesz znać aktualną pogodę? W tym artykule dowiesz się, jak zbudować prostą stację pogodową z płytą NODEmcu i wyświetlaczem HD44780 i2c. Płytka NodeMCU korzysta z układu ESP8266 w celu połączenia się z internetem i będziemy ją programować za pomocą Arduino IDE. Projekt pobiera dane pogodowe ze strony openweathermap.org i wyświetla je na wyświetlaczu. Wyświetlacz jest podłączony do Nodemcu poprzez moduł i2c, więc połączenie jest bardzo proste. Potrzebujemy tylko adresu modułu.
Podłączenie
NODEmcu -> Arduino:
- D1 -> SCL
- D2 -> SDA
- Vin -> Vcc
- GND -> GND
Pliki:
Instalacja płytki ESP8266 w Arduino IDE
Skopiuj link do dodatkowych adresów URL menedżera płytek (plik Arduino IDE -> Preferencje)
1 |
http://arduino.esp8266.com/stable/package_esp8266com_index.json |

Teraz dodaj płytkę NODEmcu do Arduino IDE. Otwórz menadżer płytek (Arduino IDE -> Narzędzia -> Płytki: „……….” -> Menadżer płytek)


Biblioteka:
OK, Twoja płytka jest już dodana. Teraz musimy dodać biblioteki potrzebne w programie. Dodaj Liquid crystal_i2c, otwórz Narzędzia -> Biblioteki menedżera i wpisz LiquidCrystal_i2c w wyszukiwarkę i znajdź bibliotekę „LiquidCrystal_i2c autorstwa franka de Brabandera”.

Potrzebujesz jeszcze biblioteki JsonArduino, Otwórz Adruino IDE -> Szkic -> Dołącz bibliotekę -> Dodaj .ZIP Library …. I wybierz plik ArduinoJson-6.x.zip

Moduł adresowy i2c:
Jeśli nie znasz adresu swojego modułu, wgraj poniższy kod na płytkę i otwórz Serial Monitor tam pojawi się adres. Mój to 0x3F.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
// ---------------------------------------------------------------- // // Arduino I2C Scanner // Re-writed by Arbi Abdul Jabbaar // Using Arduino IDE 1.8.7 // Using GY-87 module for the target // Tested on 10 September 2019 // This sketch tests the standard 7-bit addresses // Devices with higher bit address might not be seen properly. // ---------------------------------------------------------------- // #include <Wire.h> //include Wire.h library void setup() { Wire.begin(); // Wire communication begin Serial.begin(9600); // The baudrate of Serial monitor is set in 9600 while (!Serial); // Waiting for Serial Monitor Serial.println("\nI2C Scanner"); } void loop() { byte error, address; //variable for error and I2C address int nDevices; Serial.println("Scanning..."); nDevices = 0; for (address = 1; address < 127; address++ ) { // The i2c_scanner uses the return value of // the Write.endTransmisstion to see if // a device did acknowledge to the address. Wire.beginTransmission(address); error = Wire.endTransmission(); if (error == 0) { Serial.print("I2C device found at address 0x"); if (address < 16) Serial.print("0"); Serial.print(address, HEX); Serial.println(" !"); nDevices++; } else if (error == 4) { Serial.print("Unknown error at address 0x"); if (address < 16) Serial.print("0"); Serial.println(address, HEX); } } if (nDevices == 0) Serial.println("No I2C devices found\n"); else Serial.println("done\n"); delay(5000); // wait 5 seconds for the next I2C scan } |
Skonfiguruj stację pogodową:
Teraz otwórz szkic Weather_Station_i2c_NODEmcu.ino i przejdź do sekcji konfiguracji.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
// ---------------- CONFIGURE ------------------------------ // initialize the library LiquidCrystal_I2C lcd(ADDRESS MODULE,2,16); const char* ssid = "NAME NETWORK"; // SSID of Wifi network const char* password = "PASSWORD NETWORK"; // Password on network String APIKEY = "APIKEY"; String CityID = "NUMBER CITY"; // London, GB. 2643743 // ------------------ END ------------------------------------- |
Wpisz swój moduł adresowy i2c:
// zainicjuj bibliotekę
LiquidCrystal_I2C lcd( address module i2c ,2,16);
Wpisz nazwę swojej sieci:
const char* ssid = „NAME NETWORK”;
Wprowadź hasło sieciowe:
const char* password = „PASSWORD NETWORK”;
Teraz wejdź na www.openweathermap.org i zarejestruj się na stronie, przejdź do zakładki cen i kliknij BEZPŁATNIE BEZPŁATNIE „pobierz KLUCZ API i zacznij”. Wejdź na home.openweathermap.org i otwórz APIKEY i skopiuj go.
Następnie wyszukaj swoje miasto i z linku skopiuj numer miasta do skech https://openweathermap.org/city/2643743 numer miasta 2643743.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
/* * ForbiddenBit.com * * http://arduino.esp8266.com/stable/package_esp8266com_index.json */ #include <ESP8266WiFi.h> #include <LiquidCrystal_I2C.h> #include <ArduinoJson.h> // ---------------- CONFIGURE ------------------------------ // initialize the library LiquidCrystal_I2C lcd(ADDRESS MODULE,2,16); const char* ssid = "NAME NETWORK"; // SSID of Wifi network const char* password = "PASSWORD NETWORK"; // Password on network String APIKEY = "APIKEY"; String CityID = "NUMBER CITY"; // London, GB. 2643743 // ------------------ END ------------------------------------- WiFiClient client; char servername[]="api.openweathermap.org"; // remote server we will connect to String result; int counter = 60; String weatherDescription =""; String weatherLocation = ""; String Country; float Temperature; float Humidity; float Pressure; void setup() { Serial.begin(115200); int cursorPosition=0; //display initialization lcd.init(); //turn on the backlight lcd.backlight(); lcd.print(" Connecting"); Serial.println("Connecting"); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); lcd.setCursor(cursorPosition,2); lcd.print("."); cursorPosition++; } lcd.clear(); lcd.print(" Connected!"); Serial.println("Connected"); delay(1000); } void loop() { if(counter == 60) //Get new data every 10 minutes { counter = 0; displayGettingData(); delay(1000); getWeatherData(); }else { counter++; displayWeather(weatherLocation,weatherDescription); delay(5000); displayConditions(Temperature,Humidity,Pressure); delay(5000); } } void getWeatherData() //client function to send/receive GET request data. { if (client.connect(servername, 80)) { //starts client connection, checks for connection client.println("GET /data/2.5/weather?id="+CityID+"&units=metric&APPID="+APIKEY); client.println("Host: api.openweathermap.org"); client.println("User-Agent: ArduinoWiFi/1.1"); client.println("Connection: close"); client.println(); } else { Serial.println("connection failed"); //error message if no client connect Serial.println(); } while(client.connected() && !client.available()) delay(1); //waits for data while (client.connected() || client.available()) { //connected or data available char c = client.read(); //gets byte from ethernet buffer result = result+c; } client.stop(); //stop client result.replace('[', ' '); result.replace(']', ' '); Serial.println(result); char jsonArray [result.length()+1]; result.toCharArray(jsonArray,sizeof(jsonArray)); jsonArray[result.length() + 1] = '\0'; StaticJsonDocument<1024> doc; /* DeserializationError error = deserializeJson(doc, json); if (error) { Serial.print("deserializeJson() failed"); Serial.println(error.c_str()); return; } */ String location = doc["name"]; String country = doc["sys"]["country"]; float temperature = doc["main"]["temp"]; float humidity = doc["main"]["humidity"]; String weather = doc["weather"]["main"]; String description = doc["weather"]["description"]; float pressure = doc["main"]["pressure"]; weatherDescription = description; weatherLocation = location; Country = country; Temperature = temperature; Humidity = humidity; Pressure = pressure; } void displayWeather(String location,String description) { lcd.clear(); lcd.setCursor(0,0); lcd.print(location); lcd.print(", "); lcd.print(Country); lcd.setCursor(0,1); lcd.print(description); } void displayConditions(float Temperature,float Humidity, float Pressure) { lcd.clear(); lcd.print("T:"); lcd.print(Temperature,1); lcd.print((char)223); lcd.print("C "); //Printing Humidity lcd.print(" H:"); lcd.print(Humidity,0); lcd.print(" %"); //Printing Pressure lcd.setCursor(0,1); lcd.print("P: "); lcd.print(Pressure,1); lcd.print(" hPa"); } void displayGettingData() { lcd.clear(); lcd.print("Getting data"); } |