Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some suggestions #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
268 changes: 127 additions & 141 deletions HiGrowESP32MQTT/HiGrowESP32MQTT.ino
Original file line number Diff line number Diff line change
@@ -1,142 +1,128 @@
/*
HiGrowESP32MQTT
(c) Claus Kuehnel 2018-03-18 [email protected]

The ESP32 reads data from HiGrow device and publishes these via MQTT

based on https://github.com/LilyGO/higrowopen/tree/master/HiGrowEsp32
*/

#include <WiFi.h>
#include <PubSubClient.h>
#include "DHT.h"
#include "credentials.h"

#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
//#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
#define uS_TO_S_FACTOR 1000000

//int DEEPSLEEP_SECONDS = 1800; // 30 min
int DEEPSLEEP_SECONDS = 180; // 3 min

/* create an instance of PubSubClient client */
WiFiClient espClient;
PubSubClient client(espClient);

uint64_t chipid;

const int dhtpin = 22;
const int soilpin = 32;
const int POWER_PIN = 34;
const int LIGHT_PIN = 33;

// Initialize DHT sensor.
DHT dht(dhtpin, DHTTYPE);

// Temporary variables
static char celsiusTemp[7];
static char humidityTemp[7];
char msg[20];

/* topics */
#define TEMP_TOPIC "HiGrow/temp"
#define HUMI_TOPIC "HiGrow/humi"
#define SOIL_TOPIC "HiGrow/soil"
#define LIGHT_TOPIC "HiGrow/light"

// Client variables
char linebuf[80];
int charcount=0;

char deviceid[21];

void setup()
{
dht.begin();

Serial.begin(115200);
delay(2000); // wait for monitor

esp_sleep_enable_timer_wakeup(DEEPSLEEP_SECONDS * uS_TO_S_FACTOR);

pinMode(16, OUTPUT); // blue LED
pinMode(POWER_PIN, INPUT);
digitalWrite(16, LOW);

chipid = ESP.getEfuseMac();
sprintf(deviceid, "%" PRIu64, chipid);
Serial.print("DeviceId: ");
Serial.println(deviceid);

connectWiFi();
configureMQTT();
}

void loop()
{
char body[1024];
digitalWrite(16, LOW); //switched on

/* if client was disconnected then try to reconnect again */
if (!client.connected()) {
mqttconnect();
}

sensorsData(body);
delay(500);
WiFi.disconnect(true);
Serial.println("Going to Deep Sleep..."); esp_deep_sleep_start(); // uncomment for deep sleep
delay(5000); // used for test
}

void sensorsData(char* body)
{
//This section reads all sensors

int waterlevel = analogRead(soilpin);
int lightlevel = analogRead(LIGHT_PIN);

waterlevel = map(waterlevel, 0, 4095, 0, 1023);
waterlevel = constrain(waterlevel, 0, 1023);
if (!isnan(waterlevel))
{
snprintf (msg, 20, "%d", waterlevel);
/* publish the message */
client.publish(SOIL_TOPIC, msg);
}

lightlevel = map(lightlevel, 0, 4095, 0, 1023);
lightlevel = constrain(lightlevel, 0, 1023);
if (!isnan(lightlevel))
{
snprintf (msg, 20, "%d", lightlevel);
/* publish the message */
client.publish(LIGHT_TOPIC, msg);
}

// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float humidity = dht.readHumidity();
if (!isnan(humidity))
{
snprintf (msg, 20, "%5.1f", humidity);
/* publish the message */
client.publish(HUMI_TOPIC, msg);
}

// Read temperature as Celsius (the default)
float temperature = dht.readTemperature();
if (!isnan(temperature))
{
snprintf (msg, 20, "%5.1f", temperature);
/* publish the message */
client.publish(TEMP_TOPIC, msg);
}

Serial.print("DevideId: "); Serial.println(deviceid);
Serial.print("Temperature: "); Serial.print(temperature); Serial.println(" *C");
Serial.print("Humidity: "); Serial.print(humidity); Serial.println(" %rF");
Serial.print("Soil: "); Serial.println(waterlevel);
Serial.print("Light: "); Serial.println(lightlevel);
}
/*
HiGrowESP32MQTT
(c) Claus Kuehnel 2018-03-18 [email protected]

The ESP32 reads data from HiGrow device and publishes these via MQTT

based on https://github.com/LilyGO/higrowopen/tree/master/HiGrowEsp32
*/

#include <WiFi.h>
#include <PubSubClient.h>
#include "DHT.h"
#include "credentials.h"

/* create an instance of PubSubClient client */
WiFiClient espClient;
PubSubClient client(espClient);

uint64_t chipid;

const int dhtpin = 22;
const int soilpin = 32;
const int POWER_PIN = 34;
const int LIGHT_PIN = 33;

// Initialize DHT sensor.
DHT dht(dhtpin, DHTTYPE);

// Temporary variables
static char celsiusTemp[7];
static char humidityTemp[7];
char msg[20];

// Client variables
char linebuf[80];
int charcount=0;

char deviceid[21];

void setup()
{
dht.begin();

Serial.begin(115200);
delay(2000); // wait for monitor

esp_sleep_enable_timer_wakeup(DEEPSLEEP_SECONDS * uS_TO_S_FACTOR);

pinMode(16, OUTPUT); // blue LED
pinMode(POWER_PIN, INPUT);
digitalWrite(16, LOW);

chipid = ESP.getEfuseMac();
sprintf(deviceid, "%" PRIu64, chipid);
Serial.print("DeviceId: ");
Serial.println(deviceid);

connectWiFi();
configureMQTT();
}

void loop()
{
char body[1024];
digitalWrite(16, LOW); //switched on

/* if client was disconnected then try to reconnect again */
if (!client.connected()) {
mqttconnect();
}

sensorsData(body);
delay(500);
WiFi.disconnect(true);
Serial.println("Going to Deep Sleep..."); esp_deep_sleep_start(); // uncomment for deep sleep
delay(5000); // used for test
}

void sensorsData(char* body)
{
//This section reads all sensors

int waterlevel = analogRead(soilpin);
int lightlevel = analogRead(LIGHT_PIN);

waterlevel = map(waterlevel, 0, 4095, 0, 1023);
waterlevel = constrain(waterlevel, 0, 1023);
if (!isnan(waterlevel))
{
snprintf (msg, 20, "%d", waterlevel);
/* publish the message */
client.publish(SOIL_TOPIC, msg);
}

lightlevel = map(lightlevel, 0, 4095, 0, 1023);
lightlevel = constrain(lightlevel, 0, 1023);
if (!isnan(lightlevel))
{
snprintf (msg, 20, "%d", lightlevel);
/* publish the message */
client.publish(LIGHT_TOPIC, msg);
}

// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float humidity = dht.readHumidity();
if (!isnan(humidity))
{
snprintf (msg, 20, "%5.1f", humidity);
/* publish the message */
client.publish(HUMI_TOPIC, msg);
}

// Read temperature as Celsius (the default)
float temperature = dht.readTemperature();
if (!isnan(temperature))
{
snprintf (msg, 20, "%5.1f", temperature);
/* publish the message */
client.publish(TEMP_TOPIC, msg);
}

Serial.print("DevideId: "); Serial.println(deviceid);
Serial.print("Temperature: "); Serial.print(temperature); Serial.println(" *C");
Serial.print("Humidity: "); Serial.print(humidity); Serial.println(" %rF");
Serial.print("Soil: "); Serial.println(waterlevel);
Serial.print("Light: "); Serial.println(lightlevel);
}

30 changes: 30 additions & 0 deletions HiGrowESP32MQTT/credentials.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,39 @@
const char* mySSID = "<Your SSID>";
const char* myPW = "<Your Password>";

#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
//#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
#define uS_TO_S_FACTOR 1000000

//maximum is 400 days
//uint64_t DEEPSLEEP_SECONDS = 1800; // 30 min
//uint64_t DEEPSLEEP_SECONDS = 180; // 3 min
uint64_t DEEPSLEEP_SECONDS = 3600; // 60 min number to big
// uint64_t DEEPSLEEP_SECONDS = 3600*12; // twice a day
// uint64_t DEEPSLEEP_SECONDS = 3600*24; // once a day


// MQTT (CloudMQTT)
const char* BROKER = "m20.cloudmqtt.com";
uint16_t BRPORT = 12394;
const char* BRUSER = "<Your Username>";
const char* BRPWD = "<Your Password>";
const char* CLIENTID = "HiGrow";

/* topics */
#define WOHNEN_BENJAMINI

#ifdef BUERO_BOGENHANF
#define TEMP_TOPIC "Büro/Bogenhanf/Temperatur"
#define HUMI_TOPIC "Büro/Bogenhanf/Luft_Feuchtigkeit"
#define SOIL_TOPIC "Büro/Bogenhanf/Boden_Feuchtigkeit"
#define LIGHT_TOPIC "Büro/Bogenhanf/Licht"

#elif defined WOHNEN_BENJAMINI
#define TEMP_TOPIC "Wohnen/Benjamini/Temperatur"
#define HUMI_TOPIC "Wohnen/Benjamini/Luft_Feuchtigkeit"
#define SOIL_TOPIC "Wohnen/Benjamini/Boden_Feuchtigkeit"
#define LIGHT_TOPIC "Wohnen/Benjamini/Licht"
//....
#endif