Created
May 26, 2016 07:01
-
-
Save ludovicfrin/4ef06c268bb541283cdb155f54955eda to your computer and use it in GitHub Desktop.
NodeMcu + DHT22
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Script permettant la lecture régulière de la température et de l'humidité | |
* la lecture s'effectue sur un DHT22 en utilisant une carte NodeMCU | |
*/ | |
#include <ESP8266WiFi.h> | |
#include <DHT.h> | |
#include <ArduinoJson.h> | |
#include <SPI.h> | |
//Paramètres wifi | |
const char* WIFI_SSID = "Ascape"; | |
const char* WIFI_PASSWORD = "q79UCYjRV0z1VDGA"; | |
//Paramètres DHT22 | |
const int DHT_TYPE = DHT22; | |
const int DHT_PIN = D4; | |
DHT dht(DHT_PIN, DHT_TYPE, 15); | |
//Paramètres réseau | |
IPAddress server(192,168,111,101); | |
WiFiClient client; | |
/** | |
* Initialisation des paramètres | |
*/ | |
void setup() { | |
Serial.begin(115200); | |
connectWifi(); | |
dht.begin(); | |
} | |
/** | |
* Boucle permettant la lecture et l'envoi des données | |
*/ | |
void loop() { | |
StaticJsonBuffer<50> jsonBuffer; | |
JsonObject& data = jsonBuffer.createObject(); | |
data["id"] = ESP.getChipId(); | |
//Lecture et envoi des informations | |
readDht22(data); | |
sendData(data); | |
//Mise en veille prolongée | |
ESP.deepSleep(600*1000000, WAKE_RF_DEFAULT); | |
} | |
/** | |
* Réalisation de la connexion wifi | |
*/ | |
void connectWifi () { | |
WiFi.begin(WIFI_SSID, WIFI_PASSWORD); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.println("WiFi connected"); | |
Serial.println("IP address: "); | |
Serial.println(WiFi.localIP()); | |
} | |
/** | |
* Lecture des valeurs sur le DHT22 | |
*/ | |
void readDht22(JsonObject& json) { | |
Serial.println ("Lecture du DHT22 ..."); | |
json["hum"] = dht.readHumidity(); | |
json["tmp"] = dht.readTemperature(); | |
} | |
/** | |
* Envoi des données au serveur à travers une socket | |
*/ | |
void sendData(JsonObject& json) { | |
Serial.println ("Envoi des données"); | |
if (client.connect(server, 8888)) { | |
json.printTo(client); | |
} | |
client.stop(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment