Created
November 9, 2018 17:36
-
-
Save zeroflow/28bf74fd6eda5a932c05f81d964fd381 to your computer and use it in GitHub Desktop.
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
#include <Arduino.h> | |
#include <ESP8266WiFi.h> | |
#include <WiFiClient.h> | |
#include "settings.h" | |
#define MQTT_MAXBUFFERSIZE 250 | |
#include "Adafruit_MQTT.h" | |
#include "Adafruit_MQTT_Client.h" | |
const char* ssid = "myssid"; | |
const char* password = "mypassword"; | |
#define MQTT_SERVER "192.168.0.7" | |
#define MQTT_PORT 1883 | |
#define CYCLE_TIME 300000 | |
#define SLEEP 600 | |
#define SLEEP_MODE WAKE_RF_DEFAULT | |
WiFiClient client; | |
Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, MQTT_PORT, "DHT_"+ESP.getChipId() , ""); | |
void MQTT_connect(); | |
int measureMoisture(void); | |
void setup(void){ | |
Serial.begin(112500); | |
Serial.println(""); | |
WiFi.begin(ssid, password); | |
pinMode(LED_BUILTIN, OUTPUT); | |
pinMode(HYGRO_VCC, OUTPUT); | |
digitalWrite(HYGRO_VCC, LOW); | |
// Wait for connection | |
while (WiFi.status() != WL_CONNECTED) { | |
digitalWrite(LED_BUILTIN, HIGH); | |
delay(100); | |
digitalWrite(LED_BUILTIN, LOW); | |
delay(400); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.print("Connected to "); | |
Serial.println(ssid); | |
Serial.print("IP address: "); | |
Serial.println(WiFi.localIP()); | |
} | |
void loop(void){ | |
MQTT_connect(); | |
char buf[50]; | |
char buf_value[50]; | |
int val = measureMoisture(); | |
Serial.printf("Moisture level: %d\r\n", val); | |
//Humidity | |
String("soil/" + String(ESP.getChipId()) + "/value").toCharArray(buf, 50); | |
String(val).toCharArray(buf_value, 50); | |
Serial.print(buf); | |
Serial.print(": "); | |
Serial.println(buf_value); | |
mqtt.publish(buf, buf_value); | |
Serial.println("Going to sleep"); | |
ESP.deepSleep(SLEEP*1000000, SLEEP_MODE); | |
} | |
int measureMoisture(void){ | |
digitalWrite(HYGRO_VCC, HIGH); | |
delay(10); | |
long sum; | |
for (int i=0;i<10;i++){ | |
sum += analogRead(A0); | |
} | |
sum = sum/10; | |
Serial.printf("Raw value0: %d\r\n", sum); | |
sum = 1024 - sum; | |
Serial.printf("Raw value1: %d\r\n", sum); | |
digitalWrite(HYGRO_VCC, LOW); | |
return constrain(map(sum, HYGRO_LOW, HYGRO_HIGH, 0, 100), 0, 100); | |
} | |
void MQTT_connect() { | |
int8_t ret; | |
// Stop if already connected. | |
if (mqtt.connected()) { | |
return; | |
} | |
Serial.print("Connecting to MQTT... "); | |
uint8_t retries = 3; | |
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected | |
Serial.println(mqtt.connectErrorString(ret)); | |
Serial.println("Retrying MQTT connection in 5 seconds..."); | |
mqtt.disconnect(); | |
delay(5000); // wait 5 seconds | |
retries--; | |
if (retries == 0) { | |
// basically die and wait for WDT to reset me | |
while (1); | |
} | |
} | |
Serial.println("MQTT Connected!"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment