Hi
Gayuh
I connect the sensor to A0 of the Nodemcu. It works well with below code.
int sensorPin = A0<span style="box-sizing: inherit;" int sensorValue = 0<span style="box-sizing: inherit;"void setup() {
Serial.begin(9600);
}
void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
Serial.print("Moisture = " );
Serial.println(sensorValue);
delay(1000);
}
For the NTP, you can refer to https://github.com/arduino-libraries/NTPClient, here is my code.
#include
// change next line to use with another board/shield
#include
//#include // for WiFi shield
//#include // for WiFi 101 shield or MKR1000
#include
const char *ssid = "seeed";
const char *password = "*******";
WiFiUDP ntpUDP;
// You can specify the time server pool and the offset (in seconds, can be
// changed later with setTimeOffset() ). Additionaly you can specify the
// update interval (in milliseconds, can be changed using setUpdateInterval() ).
NTPClient timeClient(ntpUDP, "cn.pool.ntp.org", 28800, 60000);
const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
int sensorValue = 0; // value read from the pot
void setup(){
Serial.begin(115200);
WiFi.begin(ssid, password);
while ( WiFi.status() != WL_CONNECTED ) {
delay ( 500 );
Serial.print ( "." );
}
timeClient.begin();
}
void loop() {
timeClient.update();
Serial.print(timeClient.getFormattedTime());
Serial.print(": ");
sensorValue = analogRead(analogInPin);
Serial.print("sensor = ");
Serial.println(sensorValue);
// wait ten seconds before asking for the time again
delay(1000);
}
// change next line to use with another board/shield
#include
//#include // for WiFi shield
//#include // for WiFi 101 shield or MKR1000
#include
const char *ssid = "seeed";
const char *password = "*******";
WiFiUDP ntpUDP;
// You can specify the time server pool and the offset (in seconds, can be
// changed later with setTimeOffset() ). Additionaly you can specify the
// update interval (in milliseconds, can be changed using setUpdateInterval() ).
NTPClient timeClient(ntpUDP, "cn.pool.ntp.org", 28800, 60000);
const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
int sensorValue = 0; // value read from the pot
void setup(){
Serial.begin(115200);
WiFi.begin(ssid, password);
while ( WiFi.status() != WL_CONNECTED ) {
delay ( 500 );
Serial.print ( "." );
}
timeClient.begin();
}
void loop() {
timeClient.update();
Serial.print(timeClient.getFormattedTime());
Serial.print(": ");
sensorValue = analogRead(analogInPin);
Serial.print("sensor = ");
Serial.println(sensorValue);
// wait ten seconds before asking for the time again
delay(1000);
}
here is the output:
15:02:24: sensor = 9
15:02:25: sensor = 9
15:02:26: sensor = 9
15:02:27: sensor = 9
15:02:28: sensor = 9
15:02:29: sensor = 15
15:02:30: sensor = 15
15:02:31: sensor = 15
15:02:32: sensor = 9
15:02:33: sensor = 9
15:02:34: sensor = 15
15:02:35: sensor = 15
15:02:36: sensor = 9
15:02:25: sensor = 9
15:02:26: sensor = 9
15:02:27: sensor = 9
15:02:28: sensor = 9
15:02:29: sensor = 15
15:02:30: sensor = 15
15:02:31: sensor = 15
15:02:32: sensor = 9
15:02:33: sensor = 9
15:02:34: sensor = 15
15:02:35: sensor = 15
15:02:36: sensor = 9
Thanks
best rgds
Bill