SparkFun Forums 

Where electronics enthusiasts find answers.

User avatar
By mahathirhe
#204120
I want to ask for some help from you regarding of my final year project. I'm doing a simple SMART HOME project by using Arduino wemos. While compiling the function of the smart home, i've encountered some problem to compile it. Actually this is my first time using arduino and i don't really understand about the arduino ide syntax. Actually, there is no error in my coding. Basically, there are 5 functions that I want to put in that smart home. First, to control the lights,curtains and fans using ON/OFF Button, and security system only by using TSOP and IR, and lastly controlling the roof for the clothesline. So, I've problem with controlling the roof for the clothesline. The roof for the clothesline works when the rain sensor detects water/rain and it actually functioning when I do it separately. But, when I compile the code with the other functions, the roof is actually functioned, but it works only if I switch ON/OFF the lights/fans/curtains.

So, here I attached the source code of my project with four functions, which are for the lights, fans, curtains and roof.

Code: Select all
#include <ESP8266WiFi.h>
#include <Servo.h>

#define RAIN_SENSOR A0
#define SERVO1_PIN  D2
#define SERVO2_PIN  D3
#define LED_PIN D4
#define INA  D6
#define INB  D7
#define SERVO3_PIN  D5

Servo myservo1; // Create servo object to control a servo
Servo myservo2;
Servo curtain;

int rainAdc = 0;
boolean rainState = false;
boolean previousRainState = false;
boolean changeState = false;
boolean moveServo = false;
int servoLoop = 0;
int pos1 = 0;
int pos2 = 0;

long currentMillis = 0;
long previousMillis = 0;
int interval = 2000;
WiFiServer server(80); //Initialize the server on Port 80

void setup() {

  WiFi.mode(WIFI_AP); //Our ESP8266-12E is an AccessPoint
  WiFi.softAP("SmartHome", "123456789"); // Provide the (SSID, password); .
  server.begin(); // Start the HTTP Server

  IPAddress HTTPS_ServerIP = WiFi.softAPIP(); // Obtain the IP of the Server
  Serial.print("Server IP is: "); // Print the IP to the monitor window
  Serial.println(HTTPS_ServerIP);

  pinMode(LED_PIN, OUTPUT); 
  digitalWrite(LED_PIN, HIGH); 

  pinMode(RAIN_SENSOR, INPUT_PULLUP);

  pinMode(INA,OUTPUT); 
  pinMode(INB,OUTPUT);

  Serial.begin(115200);

  myservo1.attach(SERVO1_PIN);
  myservo2.attach(SERVO2_PIN);
  
  curtain.attach(SERVO3_PIN);

}

void loop() {
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
  
  Serial.println("Somebody has connected :)");

  // Read what the browser has sent into a String class and print the request to the monitor
  String request = client.readString();
  
  Serial.println(request);

  // To Handle the Request

  if (request.indexOf("/OFF") != -1) {
    digitalWrite(LED_PIN, LOW);
  }
  if (request.indexOf("/ON") != -1) {
    digitalWrite(LED_PIN, HIGH);
  }
  if (request.indexOf("/FanON") != -1) {
    analogWrite(INA,255);
    digitalWrite(INB,LOW);
  }
  if (request.indexOf("/FanOFF") != -1) {
    digitalWrite(INA,LOW);
    digitalWrite(INB,LOW);
  }
  if (request.indexOf("/CurtainON") != -1) {
    curtain.write(180);
    delay(15);
  }
  if (request.indexOf("/CurtainOFF") != -1) {
    curtain.write(0);
    delay(15);
  }


// This is for the Servo Motor and Rain Sensor for the roof 

  rainAdc = analogRead(RAIN_SENSOR);
  Serial.print("Rain Meter = ");
  Serial.println(rainAdc);
  if (rainAdc < 1000) {
    Serial.println();
    if (rainState == false && changeState == false) {
      changeState = true;
      previousMillis = millis();
    }
    else if (rainState == true && changeState == true) {
      changeState = false;
    }
  }
  else {
    if (rainState == true && changeState == false) {
      changeState = true;
      previousMillis = millis();
    }
    else if (rainState == false && changeState == true) {
      changeState = false;
    }
  }

  if (changeState == false) {
    previousMillis = millis();
  }

  if (changeState == true) {
    currentMillis = millis();
    if (currentMillis - previousMillis > interval) {
      
      changeState == false;
      
      if (rainState == false) {
        rainState = true;
        moveServo = true;
      }
      else {
        rainState = false;
        moveServo = true;
      }
    }
  }

  if (moveServo == true) {
    if (rainState == true) {
      pos1 = 0;
      pos2 = 90;
      for (servoLoop = 0; servoLoop < 90; servoLoop++) {
        myservo1.write(pos1++);
        myservo2.write(pos2--);
        delay(30);
      }
      myservo1.write(103);
      myservo2.write(0);

      moveServo = false;
    }

    else {
      pos1 = 103;
      pos2 = 0;
      for (servoLoop = 0; servoLoop < 90; servoLoop++) {
        myservo1.write(pos1--);
        myservo2.write(pos2++);
        delay(30);
      }
      myservo1.write(0);
      myservo2.write(90);

      moveServo = false;
    }
  }
  
  
  // the HTML document to respond and add buttons:

  String s = "HTTP/1.1 200 OK\r\n";

  s += "Content-Type: text/html\r\n\r\n";

  s += "<!DOCTYPE HTML>\r\n<html>\r\n";

  s += "<br><input type=\"button\" name=\"b1\" value=\"TURN LIGHTS ON\" onclick=\"location.href='/ON'\">";

  s += "<br><br><br>";

  s += "<input type=\"button\" name=\"bi\" value=\"TURN LIGHTS OFF\" onclick=\"location.href='/OFF'\">";

  s += "<br><br><br>";
  
  s += "<br><input type=\"button\" name=\"b2\" value=\"TURN FANS ON\" onclick=\"location.href='/FanON'\">";

  s += "<br><br><br>";

  s += "<br><input type=\"button\" name=\"b3\" value=\"TURN FANS OFF\" onclick=\"location.href='/FanOFF'\">";

  s += "<br><br><br>";

  s += "<br><input type=\"button\" name=\"b5\" value=\"TURN CURTAIN ON\" onclick=\"location.href='/CurtainON'\">";

  s += "<br><br><br>";

  s += "<br><input type=\"button\" name=\"b4\" value=\"TURN CURTAIN OFF\" onclick=\"location.href='/CurtainOFF'\">";

  s += "<br><br><br>";

  s += "</html>\n";

  //Serve the HTML document to the browser.

  client.flush ();

  //clear previous info in the stream

  client.print (s); // Send the response to the client

  delay(1);

  Serial.println("Client disonnected" );

}
User avatar
By Santa_Impersonator
#205256
Unfortunately, we don't assist with these types of things since the grade for your project is probably primarily based on your execution. Maybe, someone from the community might chime in and give you more direct assistance. That being said, I did notice a few things that might help.

Hardware wise, it seems like you are using separate pins so unless you are overdrawing power, my guess is that it is probably not a hardware issue. Especially, since you have the functions working separately. This, however, brings me to this claim:

Actually, there is no error in my coding.
...maybe it is a code issue. Based on the fact that you could get functions working separately, but not after you tied them all together leads me to believe that when you are bringing the functions together it is creating that conflict. I hope this helps you narrow down your search for the issue.