[NOOB] Blynk + DH11 + Servo

Hi All!

I have a small “issue” with my little test project, i want to move a servo when the temp. of the DH11 is above or equal to 26 degrees.
But when the sensor gives a value of 23 of 24 the servo also turns now and then

Can someone tell me what’s wrong with my code?

[code]/**************************************************************

  • Blynk is a platform with iOS and Android apps to control
  • Arduino, Raspberry Pi and the likes over the Internet.
  • You can easily build graphic interfaces for all your
  • projects by simply dragging and dropping widgets.
  • Downloads, docs, tutorials: http://www.blynk.cc
  • Blynk community: http://community.blynk.cc
  • Social networks: http://www.fb.com/blynkapp
  •                           http://twitter.com/blynk_app
    
  • Blynk library is licensed under MIT license
  • This example code is in public domain.

  • This example shows how to use ESP8266 Shield via Hardware Serial
  • (on Mega, Leonardo, Micro…) to connect your project to Blynk.
  • Note: Ensure a stable serial connection to ESP8266!
  •   Firmware version 1.0.0 (AT v0.22) or later is needed.
    
  •   You can change ESP baud rate. Connect to AT console and call:
    
  •       AT+UART_DEF=115200,8,1,0,0
    
  • Change WiFi ssid, pass, and Blynk auth token to run :slight_smile:
  • Feel free to apply it to any other example. It’s simple!

**************************************************************/
//#define BLYNK_DEBUG
#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#include <ESP8266_HardSer.h>
#include <BlynkSimpleShieldEsp8266_HardSer.h>
#include <Servo.h>
#include <DHT.h>
#include <SimpleTimer.h>
#define DHTPIN 12 //Set PIN for DH11
#define DHTTYPE DHT11 // DHT 22 Change this if you have a DHT11
DHT dht(DHTPIN, DHTTYPE);
// Set ESP8266 Serial object
#define EspSerial Serial

SimpleTimer timer;

// Set ESP8266 Serial object
#define EspSerial Serial

ESP8266 wifi(EspSerial);
Servo servo;

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = “xxx”;

void setup()
{
// Set console baud rate
Serial.begin(9600);
delay(10);
// Set ESP8266 baud rate
EspSerial.begin(9600);
delay(10);

Blynk.begin(auth, wifi, “xxx”, “xxx”);
servo.attach(11);
delay(500);

timer.setInterval(5000, sendData);

}

BLYNK_WRITE(V3)
{
servo.write(param.asInt());
}

void sendData()
{
//Read the Temp and Humidity from DHT
float h = dht.readHumidity();
float t = dht.readTemperature();
//Write values to V04 and V05
Blynk.virtualWrite(4, h);
Blynk.virtualWrite(5, t);
}

void loop()
{
Blynk.run();
timer.run();

if (dht.readTemperature() > 25)
{
servo.write(0);
delay(1000);
servo.write(120);
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
Blynk.notify("Temperatuur is boven de 25 graden!);
}

}

[/code]

It could be an electrical issue. Also your code is not 100% ok. Try removing everything from the loop() void and use the SimpleTimer and/or PushData example. The delays in the loop will probably cause issues too.

I’m doing something similar (I assume you want to do a greenhouse-thing):

/***************************************************************************
 *                  O    O                                        
 *                +12 GND (D4)                                 
 * O GND (D5)                                              
 * O +12                                                
 *                                                  +5  GND
 *                         #   #   #                  # #
 * #  #  #  #  #  #  #  #  #   #   #                  # #
 *                                                    # #
 * D5 D4 D3 D2 D6 D7 Rx Tx D8  D9 D10
 * 
 *                                                              O GND IN
 *                                                              O +12 IN
 * 
 * 
 * 
 * 12v Fan                  D5
 * 12v Lights               D4
 * Servo 1                  D3
 * DHT22                    D2
 * 
 * Serial Debug (Software)  D12 / D13
 * 
 * V1 = manual override
 * V2 = manual window slider
 ***************************************************************************/

#include <SoftwareSerial.h>
SoftwareSerial DebugSerial(12, 13); // RX, TX

#define BLYNK_PRINT DebugSerial
#include <ESP8266_HardSer.h>
#include <BlynkSimpleShieldEsp8266_HardSer.h>

#include "DHT.h"
#include <Servo.h>
#include <SimpleTimer.h>

#define DHTPIN 6
#define DHTTYPE DHT22
#define fanPin 5
#define lightPin 4
#define servoPin 7

#define EspSerial Serial
ESP8266 wifi(EspSerial);

DHT dht(DHTPIN, DHTTYPE);
Servo myservo;

SimpleTimer timer;

char auth[] = "insert token";

WidgetLED light(28);
WidgetLED fan(29);

void sendValues();
void dhtStuff();
void servoStuff();
void fanStuff();
void lightStuff();

byte timerDoStuffSendValues     = timer.setInterval(5000, sendValues);
byte timerDoStuffDHT            = timer.setInterval(5000, dhtStuff);
byte timerDoStuffServo          = timer.setInterval(5000, servoStuff);
byte timerDoStuffFan            = timer.setInterval(5000, fanStuff);
byte timerDoStuffLight          = timer.setInterval(5000, lightStuff);

float h;
float c;

BLYNK_WRITE(V1)
{
  int v1 = param.asInt();

  if(v1 == 1)
  {
    timer.disable(timerDoStuffDHT);
    timer.disable(timerDoStuffServo);
    timer.disable(timerDoStuffFan);
    timer.disable(timerDoStuffLight);
  }
  else
  {
    timer.enable(timerDoStuffDHT);
    timer.enable(timerDoStuffServo);
    timer.enable(timerDoStuffFan);
    timer.enable(timerDoStuffLight);
  }
}

BLYNK_WRITE(V2)
{
  int air = param.asInt();
  myservo.write(air);
}

BLYNK_WRITE(V6)
{
  int v6 = param.asInt();

  if(v6 == 1)
  {
    digitalWrite(lightPin, HIGH);
    light.on();
  }
  else
  {
    digitalWrite(lightPin, LOW);
    light.off();
  }
}

BLYNK_WRITE(V7)
{
  int v7 = param.asInt();

  if(v7 == 1)
  {
    digitalWrite(fanPin, HIGH);
    fan.on();
  }
  else
  {
    digitalWrite(fanPin, LOW);
    fan.off();
  }
}

void setup() 
{
  pinMode(lightPin, OUTPUT);
  pinMode(fanPin, OUTPUT);
  
  digitalWrite(lightPin, LOW); // Lights off
  digitalWrite(fanPin, LOW); // Fan off
  
  DebugSerial.begin(9600);
  EspSerial.begin(115200);
  delay(10);
  Blynk.begin(auth, wifi, "ssid", "password", "192.168.0.53", 8442);
  myservo.attach(servoPin);
  dht.begin();
}

void loop() 
{
  Blynk.run();
  timer.run();
}

void sendValues()
{
  h = dht.readHumidity();
  c = dht.readTemperature();
  
  Blynk.virtualWrite(V30, h);
  Blynk.virtualWrite(V31, c);
}

void dhtStuff()
{
  h = dht.readHumidity();
  c = dht.readTemperature();
  DebugSerial.println(c);
  DebugSerial.println(h);
}

void servoStuff()
{
  if(c < 25)
  {
    // Close
    myservo.write(0);
  }
  else
  {
    // Open
    myservo.write(120);
  }
  delay(15);
}

void fanStuff()
{
  if(h >60)
  {
    digitalWrite(fanPin, HIGH);
    fan.on();
  }
  else
  {
    digitalWrite(fanPin, LOW);
    fan.off();
  }
}

void lightStuff()
{
  if(c < 25)
  {
    digitalWrite(lightPin, HIGH);
    light.on();
  }
  else
  {
    digitalWrite(lightPin, LOW);
    light.off();
  }
}

maybe this can get you on the right track :slight_smile:

(ow, one more tip, try to have your comments in English, it makes for a better read for other users to understand your code and help you! :slight_smile: I do understand, but then again, I’m dutch…)

@Lichtsignaal,
You need something like that on the roof to do it perfect :wink:
https://www.adafruit.com/products/1733