Ubidots Community

Unable to connect NodeMCU to ubidots

Please help. I am unable to connect NodeMCU to ubidots. No variable data gets displayed on the web. If anyone knows please help.
Thankyou.

#include<DHT.h>
#include “UbidotsMicroESP8266.h”
#define TOKEN “xxx”
#define WIFISSID “xxx”
#define PASSWORD “xxx”
#define DHTPIN 4

const int sensor_pin = A0;
int motor=12;
Ubidots client(TOKEN);
DHT dht(DHTPIN,DHT22);

void setup(){
pinMode(motor,OUTPUT);
Serial.begin(115200);
delay(10);
dht.begin();
client.wifiConnection(WIFISSID, PASSWORD);
}

void loop(){
float moisture_percentage;
float hum = dht.readHumidity();
float temp = dht.readTemperature();
moisture_percentage = analogRead(sensor_pin);
Serial.println(moisture_percentage);
Serial.println(hum);

Serial.println(temp);
moisture_percentage = map(moisture_percentage,0,1023,0,100);
moisture_percentage=100-moisture_percentage;
Serial.println(“moisture_percentage %”);
Serial.println(moisture_percentage);
client.add(“Temperature”, temp);
client.add(“Humidity”,hum);
client.add(“Moisture %”,moisture_percentage);
if(moisture_percentage <= 15)
{
digitalWrite(motor,HIGH);
client.add(“Motor”,1);
}
else
{
digitalWrite(motor,LOW);
client.add(“Motor”,0);
}
client.sendAll(true);
}

NodeMCU keeps disconnecting every 2 seconds.

Hello @IoTGreen ,

For us and our community to be able to help you, can you please:

1. Make sure that you’re using the appropriate URL. For your reference:

  • If you have an Industrial (trial or licensed) or STEM account, the URL is industrial.api.ubidots.com

2. Please make sure that the following parameters are correct:

  • WIFISSID
  • PASSWORD
  • TOKEN

3. Does the code show any error messages in the serial monitor?

If yes, please upload an image of the error so we can identify what could be happening.

Lastly, before we or our community can provide any inputs, check out,

I’ll be attentive to your results.

–Isabel

Hi Isabel Mam (@isalogi). Thank you for replying. I just tried correcting my code with the code present on this (“Connect a NodeMCU ESP8266 to Ubidots over HTTP | Ubidots Help Center”, code for posting variable) page.

#include<DHT.h>
#include “Ubidots.h”
#define TOKEN “BBFF-5fqh05QYD7USL9BkqAxrEbnIclEc1f”
#define WIFISSID “Sar”
#define PASSWORD “g12345”
#define DHTPIN 4
//int motor =17;
const int sensor_pin = A0;
int motor = 12;

Ubidots client(TOKEN,UBI_HTTP);
DHT dht(DHTPIN, DHT22);
void setup() {
pinMode(motor, OUTPUT);
Serial.begin(115201);
delay(100);
dht.begin();
client.wifiConnect(WIFISSID, PASSWORD);
}
void loop() {
float moisture_percentage;
float hum = dht.readHumidity();
float temp = dht.readTemperature();
moisture_percentage = ( 100.00 - ( (analogRead(sensor_pin) / 1023.00) * 100.00 ) );
moisture_percentage = analogRead(sensor_pin);
Serial.println(moisture_percentage);
moisture_percentage = map(moisture_percentage, 0, 1023, 0, 100);
moisture_percentage = 100 - moisture_percentage;
Serial.println(“moisture_percentage %”);
Serial.println(moisture_percentage);

client.add(“Temperature”, temp);
client.add(“Humidity”, hum);
client.add(“Soil Moisture”, moisture_percentage);

if (moisture_percentage <= 15)
{
digitalWrite(motor, HIGH);
client.add(“Motor”, 1);
}
else
{
digitalWrite(motor, LOW);
client.add(“Motor”, 0);
}
bool bufferSent = false;
bufferSent = client.send();
//client.sendAll(true);//

if (bufferSent) {
// Do something if values were sent properly
Serial.println(“Values sent by the device”);
}
delay(5000);
}

When I tried the code mentioned in above webpage, in serial monitor, it created a device and sent data. But when I editted and tried my code it showed the readings but didn’t sent and data. Please check the code. :smiley: :pray:
Once again thank you.

Mam please help.

It started working. Thank you for your support. Have a good day. :partying_face: :hugs: :hugs: :hugs: :hugs: