Skip to content

Instantly share code, notes, and snippets.

@slavama
Created January 24, 2021 12:44
Show Gist options
  • Save slavama/a743ce9fafa17d7642a21ed92f6f3338 to your computer and use it in GitHub Desktop.
Save slavama/a743ce9fafa17d7642a21ed92f6f3338 to your computer and use it in GitHub Desktop.
esp8266 weather station
#include <ESP8266WiFi.h>
#include <TM1637Display.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266WiFiMulti.h>
#include "DHTesp.h"
const char* ssid = "HUAWEI-E8372-26A6";//put your wifi ssid here
const char* password = "***";//put your wifi password here
const int CLK = D2; //Set the CLK pin connection to the display
const int DIO = D3; //Set the DIO pin connection to the display
TM1637Display display(CLK, DIO); //set up the 4-Digit Display.
WiFiClient client;
HTTPClient http;
ESP8266WiFiMulti WiFiMulti;
DHTesp dht;
void setup_wifi() {
delay(100);
String thisBoard = ARDUINO_BOARD;
Serial.println(thisBoard);
// We start by connecting to a WiFi network
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
randomSeed(micros());
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void setup() {
dht.setup(D5, DHTesp::DHT22);
Serial.begin(115200);
setup_wifi();
display.setBrightness(0x0a); //set the diplay to maximum brightness
}
void loop() {
// wait for WiFi connection
if ((WiFiMulti.run() == WL_CONNECTED)) {
delay(dht.getMinimumSamplingPeriod());
float humidity = dht.getHumidity();
float temperature = dht.getTemperature();
Serial.println(temperature);
Serial.println(humidity);
Serial.print("[HTTP] begin...\n");
if (http.begin(client, "http://api.thingspeak.com/update?api_key=***&field1="+String(temperature)+"&field3="+String(humidity))) { // HTTP
Serial.print("[HTTP] GET...\n");
// start connection and send HTTP header
int httpCode = http.GET();
// httpCode will be negative on error
if (httpCode > 0) {
// HTTP header has been send and Server response header has been handled
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
// file found at server
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
String payload = http.getString();
Serial.println(payload);
display.showNumberDec(payload.toInt(),false,4,0);
}
} else {
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
} else {
Serial.printf("[HTTP} Unable to connect\n");
}
}
delay(10000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment