Skip to content

Instantly share code, notes, and snippets.

@zeroflow
Created November 9, 2018 17:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeroflow/922eb01906bae95ee5492621f4f94772 to your computer and use it in GitHub Desktop.
Save zeroflow/922eb01906bae95ee5492621f4f94772 to your computer and use it in GitHub Desktop.
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include "ADS1115.h"
//#define ADS1115_SERIAL_DEBUG
#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();
ADS1115 adc0(ADS1115_DEFAULT_ADDRESS);
// Wire ADS1115 ALERT/RDY pin to Arduino pin 2
const int alertReadyPin = D6;
const int VCCPin = D5;
void setup() {
//I2Cdev::begin(); // join I2C bus
Wire.begin(D3, D2);
Serial.begin(115200); // initialize serial communication
Serial.println("");
Serial.printf("ESP_%d speaking", ESP.getChipId());
pinMode(VCCPin, OUTPUT);
WiFi.begin(ssid, password);
Serial.println("Testing device connections...");
Serial.println(adc0.testConnection() ? "ADS1115 connection successful" : "ADS1115 connection failed");
adc0.initialize(); // initialize ADS1115 16 bit A/D chip
// We're going to do single shot sampling
adc0.setMode(ADS1115_MODE_SINGLESHOT);
// Slow things down so that we can see that the "poll for conversion" code works
adc0.setRate(ADS1115_RATE_128);
// Set the gain (PGA) +/- 6.144v
// Note that any analog input must be higher than –0.3V and less than VDD +0.3
adc0.setGain(ADS1115_PGA_6P144);
// ALERT/RDY pin will indicate when conversion is ready
pinMode(alertReadyPin,INPUT_PULLUP);
adc0.setConversionReadyPinMode();
// To get output from this method, you'll need to turn on the
//#define ADS1115_SERIAL_DEBUG // in the ADS1115.h file
#ifdef ADS1115_SERIAL_DEBUG
adc0.showConfigRegister();
Serial.print("HighThreshold="); Serial.println(adc0.getHighThreshold(),BIN);
Serial.print("LowThreshold="); Serial.println(adc0.getLowThreshold(),BIN);
#endif
// 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());
}
/** Poll the assigned pin for conversion status
*/
void pollAlertReadyPin() {
unsigned long millis_begin = millis();
while (millis() - millis_begin < 100){
if (!digitalRead(alertReadyPin)) {
#ifdef ADS1115_SERIAL_DEBUG
Serial.printf("Alert after %dms\r\n", millis()-millis_begin);
#endif
return;
}
}
Serial.println("Failed to wait for AlertReadyPin in 100ms, it's stuck high!");
}
int mvToPercent(int mv){
int hum = map(mv, 1000, 3200, 100, 0);
if (hum > 130){
return 0;
}
return constrain(hum, 0, 100);
}
void loop() {
MQTT_connect();
char buf[50];
char buf_value[50];
digitalWrite(VCCPin, HIGH);
delay(100);
int a0, a1, a2, a3;
// The below method sets the mux and gets a reading.
adc0.setMultiplexer(ADS1115_MUX_P0_NG);
adc0.triggerConversion();
pollAlertReadyPin();
a0 = adc0.getMilliVolts(false);
adc0.setMultiplexer(ADS1115_MUX_P1_NG);
adc0.triggerConversion();
pollAlertReadyPin();
a1 = adc0.getMilliVolts(false);
adc0.setMultiplexer(ADS1115_MUX_P2_NG);
adc0.triggerConversion();
pollAlertReadyPin();
a2 = adc0.getMilliVolts(false);
adc0.setMultiplexer(ADS1115_MUX_P3_NG);
// Do conversion polling via I2C on this last reading:
a3 = adc0.getMilliVolts(true);
Serial.printf("Raw: %d\t%d\t%d\t%d\n",a0,a1,a2,a3);
Serial.printf("Percent: %d\t%d\t%d\t%d\n",mvToPercent(a0),mvToPercent(a1),mvToPercent(a2),mvToPercent(a3));
digitalWrite(VCCPin, LOW);
//Humidity
String("soil/" + String(ESP.getChipId()) + "/value0").toCharArray(buf, 50);
String(mvToPercent(a0)).toCharArray(buf_value, 50);
mqtt.publish(buf, buf_value);
String("soil/" + String(ESP.getChipId()) + "/value1").toCharArray(buf, 50);
String(mvToPercent(a1)).toCharArray(buf_value, 50);
mqtt.publish(buf, buf_value);
String("soil/" + String(ESP.getChipId()) + "/value2").toCharArray(buf, 50);
String(mvToPercent(a2)).toCharArray(buf_value, 50);
mqtt.publish(buf, buf_value);
String("soil/" + String(ESP.getChipId()) + "/value3").toCharArray(buf, 50);
String(mvToPercent(a3)).toCharArray(buf_value, 50);
mqtt.publish(buf, buf_value);
Serial.println("Going to sleep");
ESP.deepSleep(SLEEP*1000000, SLEEP_MODE);
delay(2000);
}
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