160 lines
5.0 KiB
C++
160 lines
5.0 KiB
C++
|
|
/* DHT22 temperature and humidity data are uploaded to thingspeak using
|
|
WeMOS ESP8266 MCU.
|
|
|
|
This chip uses I2C bus to communicate, specials pins are required to interface
|
|
Board: SDA SCL Level
|
|
Uno, Mini, Pro, ATmega168, ATmega328..... A4 A5 5v
|
|
Mega2560................................. 20 21 5v
|
|
Due, SAM3X8E............................. 20 21 3.3v
|
|
Leonardo, Micro, ATmega32U4.............. 2 3 5v
|
|
Digistump, Trinket, ATtiny85............. 0/physical pin no.5 2/physical pin no.7 5v
|
|
Blue Pill, STM32F103xxxx boards.......... PB7 PB6 3.3v/5v
|
|
ESP8266 ESP-01........................... GPIO0/D5 GPIO2/D3 3.3v/5v
|
|
NodeMCU 1.0, WeMos D1 Mini............... GPIO4/D2 GPIO5/D1 3.3v/5v
|
|
ESP32.................................... GPIO21/D21 GPIO22/D22 3.3v
|
|
|
|
Details: http://embedded-lab.com/blog/iot-temperature-and-humidity-logger/
|
|
*/
|
|
#include <ESP8266WiFi.h>
|
|
#include <AHT10.h>
|
|
#include <Wire.h>
|
|
|
|
uint8_t readStatus = 0;
|
|
|
|
AHT10 myAHT10(AHT10_ADDRESS_0X38);
|
|
|
|
//#include "Math.h"
|
|
#define DHTTYPE DHT22
|
|
#define DHTPIN 0 // DHT22 data pin connects to D4
|
|
|
|
// Time to sleep (in seconds):
|
|
const int sleepTimeS = 60;
|
|
float humidity = 0;
|
|
float temp = 0;
|
|
|
|
float prevTemp = 0;
|
|
const char* server = "api.thingspeak.com";
|
|
String apiKey = "API Key here";
|
|
const char* MY_SSID = "Livebox-37cc";
|
|
const char* MY_PWD = "8A6060920A8A86896F770F2C47";
|
|
|
|
// Host
|
|
const char* host = "192.168.1.3";
|
|
const int port = 81;
|
|
int idx = 1203; //IDX for this virtual sensor, found in Setup -> Devices
|
|
|
|
int sent = 0;
|
|
|
|
|
|
void setup() {
|
|
Serial.begin(115200);
|
|
Serial.println();
|
|
|
|
int bcl = 0;
|
|
while (myAHT10.begin() != true && bcl < 5)
|
|
{
|
|
bcl ++;
|
|
Serial.println(F("AHT10 not connected or fail to load calibration coefficient")); //(F()) save string to flash & keeps dynamic memory free
|
|
delay(5000);
|
|
}
|
|
Serial.println(F("AHT10 OK"));
|
|
connectWifi();
|
|
|
|
}
|
|
|
|
void loop() {
|
|
|
|
/* DEMO - 1, every temperature or humidity call will read 6 bytes over I2C, total 12 bytes */
|
|
Serial.println(F("DEMO 1: read 12-bytes, show 255 if communication error is occurred"));
|
|
Serial.print(F("Temperature: "));
|
|
temp = myAHT10.readTemperature();
|
|
Serial.print(temp);
|
|
Serial.println(F(" +-0.3C")); //by default "AHT10_FORCE_READ_DATA"
|
|
|
|
Serial.print(F("Humidity...: "));
|
|
humidity = myAHT10.readHumidity();
|
|
Serial.print(humidity);
|
|
|
|
|
|
Serial.println(F(" +-2%")); //by default "AHT10_FORCE_READ_DATA"
|
|
|
|
|
|
// /* DEMO - 2, temperature call will read 6 bytes via I2C, humidity will use same 6 bytes */
|
|
// Serial.println(F("DEMO 2: read 6 byte, show 255 if communication error is occurred"));
|
|
// Serial.print(F("Temperature: ")); Serial.print(myAHT10.readTemperature(AHT10_FORCE_READ_DATA)); Serial.println(F(" +-0.3C"));
|
|
// Serial.print(F("Humidity...: ")); Serial.print(myAHT10.readHumidity(AHT10_USE_READ_DATA)); Serial.println(F(" +-2%"));
|
|
//
|
|
//
|
|
// /* DEMO - 3, same as demo2 but different call procedure */
|
|
// Serial.println(F("DEMO 3: read 6-bytes, show 255 if communication error is occurred"));
|
|
|
|
readStatus = myAHT10.readRawData(); //read 6 bytes from AHT10 over I2C
|
|
|
|
if (readStatus != AHT10_ERROR)
|
|
{
|
|
Serial.print(F("Temperature: "));
|
|
Serial.print(myAHT10.readTemperature(AHT10_USE_READ_DATA));
|
|
Serial.println(F(" +-0.3C"));
|
|
Serial.print(F("Humidity...: "));
|
|
Serial.print(myAHT10.readHumidity(AHT10_USE_READ_DATA));
|
|
Serial.println(F(" +-2%"));
|
|
}
|
|
else
|
|
{
|
|
Serial.print(F("Failed to read - reset: "));
|
|
Serial.println(myAHT10.softReset()); //reset 1-success, 0-failed
|
|
}
|
|
|
|
//delay(10000);
|
|
|
|
sendTemperatureTS(temp, humidity);
|
|
Serial.println("Goto to sleep");
|
|
delay(200);
|
|
ESP.deepSleep(sleepTimeS*1000000, WAKE_RF_DEFAULT); // Sleep for 60 seconds
|
|
}
|
|
|
|
void connectWifi()
|
|
{
|
|
Serial.print("Connecting to " + *MY_SSID);
|
|
WiFi.begin(MY_SSID, MY_PWD);
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
delay(1000);
|
|
Serial.print(".");
|
|
}
|
|
|
|
Serial.println("");
|
|
Serial.println("Connected");
|
|
Serial.println("");
|
|
}//end connect
|
|
|
|
void sendTemperatureTS(float temp1, float temp2)
|
|
{
|
|
WiFiClient client;
|
|
|
|
if (client.connect(host, port)) {
|
|
Serial.println("WiFi Client connected ");
|
|
|
|
client.print("GET /json.htm?type=command¶m=udevice&idx=");
|
|
client.print(idx);
|
|
client.print("&nvalue=0&svalue=");
|
|
client.print(temp);
|
|
client.print(";");
|
|
client.print(humidity);
|
|
client.print(";0"); //Value for HUM_STAT. Can be one of: 0=Normal, 1=Comfortable, 2=Dry, 3=Wet
|
|
client.println(" HTTP/1.1");
|
|
client.print("Host: ");
|
|
client.print(host);
|
|
client.print(":");
|
|
client.println(port);
|
|
client.println("User-Agent: Arduino-ethernet");
|
|
client.println("Connection: close");
|
|
client.println();
|
|
|
|
client.stop();
|
|
|
|
}//end if
|
|
sent++;
|
|
client.stop();
|
|
}//end send
|