Skip to content

Instantly share code, notes, and snippets.

@ItKindaWorks
Last active January 3, 2023 18:13
Show Gist options
  • Save ItKindaWorks/adca36681ea4650303b9de0f6fe551fc to your computer and use it in GitHub Desktop.
Save ItKindaWorks/adca36681ea4650303b9de0f6fe551fc to your computer and use it in GitHub Desktop.
A small ESP8266 Arduino program for using getting averaged data from a moisture sensor and sending it via mqtt
/*
moistureSense.ino
Copyright (c) 2018 ItKindaWorks All right reserved.
github.com/ItKindaWorks
moistureSense.ino is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
moistureSense.ino is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with moistureSense.ino. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Metro.h"
#include <ESP8266WiFi.h>
#include "ESPHelper.h"
//ESPHelper info
netInfo homeNet = { .mqttHost = "YOUR MQTT-IP", //can be blank if not using MQTT
.mqttUser = "YOUR MQTT USERNAME", //can be blank
.mqttPass = "YOUR MQTT PASSWORD", //can be blank
.mqttPort = 1883, //default port for MQTT is 1883 - only chance if needed.
.ssid = "YOUR SSID",
.pass = "YOUR NETWORK PASS"};
ESPHelper myESP(&homeNet);
//timer for how often to send the data to MQTT (in ms)
Metro sendTimer = Metro(15000);
void setup() {
//initially disconnect from wifi to prevent issues with ESP8266 ADC
WiFi.disconnect();
//start serial line for debugging
Serial.begin(115200);
//set the ADC pin to be an input
pinMode(A0, INPUT);
}
//variable to hold the averaged data - this could be a local variable if the code were tweaked just a bit
int avgCounter = 0;
void loop(){
//if the send timer has gone off, send the most recent reading to MQTT
if(sendTimer.check()){
//start ESPHelper
myESP.begin();
//loop until connected
while(myESP.loop() != FULL_CONNECTION){
delay(10);
}
delay(100);
//convert the raw value to a percent and post that to mqtt
int percentMoist = map(avgCounter, 0, 1023, 100, 0);
//convert int data to char[] to be sent through mqtt
char toPrint[5];
String forConvert = String(percentMoist);
forConvert.toCharArray(toPrint, sizeof(toPrint));
//send the data
myESP.publish("/home/test", toPrint);
//wait for data to be sent before shutting off wifi again
delay(250);
//shut off wifi and reset the timer
myESP.end();
WiFi.disconnect();
delay(1000);
sendTimer.reset();
}
//get and average the sensor data
avgCounter = 0;
//loop 10 times for averaging
for(int i = 0; i < 10; i++){
avgCounter+= analogRead(A0);
delay(10);
}
//divide by 10 to get the average
avgCounter /= 10;
//print that data to the serial line
Serial.println(avgCounter);
//delay a small bit because thats always a good idea :)
delay(10);
}
@rlajos
Copy link

rlajos commented May 19, 2019

Thank you for the great contribution first!
I was able to upload and run the code, but serial communication freezes after some minutes, and also the MQTT sending is stoping after some minutes later. Did you have experienced this kind a behaviour?

Best,
Lajos

@alfredorico009
Copy link

Hi Lajos,
I had a similar problem with different code, while sending info to ThingSpeak and I solved in a not elegant way but works anyway.
I'm using the ESP32 (my favorite SOC) and when I send the info, I check the sent code to find out if it went through or not. In case it failed, I call ESP.restart(); Once my SOC resets, it works again for some time. Something between 5 to 25 min. Then it resets again.
Don't know why the problem happens, but this keeps it working while I find the root cause.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment