I am trying to use a capacitive soil moisture sensor with a nodemcu but I'm only getting 2 values, 1023 when dry or 0 when wet. How do I get this to read in the middle?
Sensor:
Sketch:
void setup() {
Serial.begin(115200);
}
void loop() {
int val;
val = analogRead(D1);
Serial.println(val);
delay(500);
}
I have Vcc to Vcc. Gnd to Gnd. Analog out to D0,
pert
March 13, 2019, 9:04pm
2
You can only use amalogRead() on the analog pin of your board.
pert:
You can only use amalogRead() on the analog pin of your board.
I can't believe I did that.
So now I get actual analog data however I am having trouble getting it to my mqtt server
/*
Basic ESP8266 MQTT publish client example for a moisture sensor
*/
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
// Update these with values suitable for your network.
const char* ssid = "REMOVED";
const char* password = "REMOVED";
const char* mqtt_server = "REMOVED";
WiFiClient espClient;
PubSubClient client(espClient);
void setup_wifi() {
// Connecting to a WiFi network
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void reconnect() {
// Loop until we're reconnected
Serial.println("In reconnect...");
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("Arduino_Moisture")) {
Serial.println("connected");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
void setup() {
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
}
void loop() {
char msg[10];
if (!client.connected()) {
reconnect();
}
float maxv=1023;
int val=analogRead(A0);
Serial.print("analog value: ");
Serial.println(val);
float SoilMoisture=val/maxv*100;
Serial.print("Percentage: ");
Serial.println(SoilMoisture);
sprintf(msg,"%i",SoilMoisture); //if I replace "SoilMoisture" with "val" I get actual readings
client.publish("Sensor", msg);
delay(500);
}
Through serial I am getting good results:
analog value: 818
Percentage: 79.96
analog value: 821
Percentage: 80.25
However when it is sent though mqtt i am getting out a random gargle of numbers:
1610612736
-2147483648
1073741824
1610612736
-536870912
0
0
1610612736
536870912
-2147483648
If I replace "SoilMoisture" with "val", the mqtt messages are actual readings just not in the format that I need:
814
814
814
816
816
816
pert
March 16, 2019, 2:25am
4
sprintf's %i specifier is for use with a signed integer type, but you're passing it a float. Try casting SoilMoisture to the int type:
sprintf(msg,"%i",(int)SoilMoisture);
Any one knows where to find the graph : moist level (%) to output voltage (V or mV) for this sensor ?
tanx in advance !
The moist level (%) to output voltage (V or mV) will be different from one sensor model/manufacturer to another. This kind of information is available in the manufacturer datasheet of that particular sensor that you have. You can find the datasheet by doing a google search or, most of the time, directly on the distributor website where you got that sensor.