Monday, November 7, 2016

nodemcu cloudmqtt servo

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <Servo.h>
#include<EEPROM.h>

long lastReconnectAttempt = 0;
int servoPin = D4;
bool statusBall;
const char *statusBall2;
bool statusBall3;
int addressEpp = 0;
Servo servo;
const char *statusServo;
const char *ssid =  "ssid";   // cannot be longer than 32 characters!
const char *pass =  "pass";   //

//Prepare Mqtt connection data
const char *MQTTBroker = "cloudmqtt.com";
const char *MQTTuser = "";
const char *MQTTpassword = "";
//const char *Topic = "/kranstatus";
//const char *Topicid = "/kran";
uint16_t port = 12728;


void cekMem(){
    statusBall=EEPROM.read(1);
 // Serial.println(" memory ");
 // Serial.println(statusBall);
  if(statusBall == 1){
    statusBall2 = "on";
    servoOn();
  }
  if(statusBall == 0){
    statusBall2 ="off";
    servoOff();
  }
 // Serial.println("memory part");
   //Serial.println(statusBall2);
}

void servoOn(){
    servo.attach(servoPin);
    servo.write(86);
    delay(800);
    servo.detach();
   // Serial.println("servo on");
    statusBall2 = "on";
    EEPROM.write(1, 1);
}
void servoOff(){
    servo.attach(servoPin);
    servo.write(250);
    delay(800);
    servo.detach();
    //Serial.println("servo off");
    statusBall2 = "off";
    EEPROM.write(1, 0);
}

//PubSubClient mqtt(MQTTBroker,port);
WiFiClient wclient;
PubSubClient client(wclient, MQTTBroker, port);

void callback(const MQTT::Publish& pub){
  //Serial.println(pub.payload_string());
    if(pub.payload_string()=="on" && statusBall2 == "off" ){
    servoOn();
    //Serial.println("call back on");
  }
  if(pub.payload_string()=="off" && statusBall2 == "on"){
    servoOff();
    //Serial.println("call back off");
  }
}
void WIFI_Connect()
{
  WiFi.disconnect(1);
  Serial.println("Booting Sketch...");
  delay(2000);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, pass);
    int Attempt = 0;
    while (WiFi.status() == 6) {
    delay(100);
    Attempt++;
      if (Attempt == 300) {
        ESP.restart();
      }
    }
 
  client.set_callback(callback);  //Set Callback function

  if (client.connect(MQTT::Connect("kran").set_auth(MQTTuser,MQTTpassword))){
    client.publish("kranstatus", statusBall2);  //publish
    client.subscribe("kranstatus");    //subscribe
  }
   // Serial.print("Wifi connect");
    //Serial.println(WiFi.status());
}
void setup()
{
  statusBall=EEPROM.read(1);
  //Serial.print(" memory --");
  //Serial.println(statusBall);
  if(statusBall == 1){
    statusBall2 = "on";
    servoOn();
  }
  if(statusBall == 0){
    statusBall2 ="off";
    servoOff();
  }
  //Serial.print("memory status");
   //Serial.println(statusBall2);
  //Serial.begin(9600);

  WIFI_Connect();
  //client.set_callback(callback);  //Set Callback function

  //if (client.connect(MQTT::Connect("kran").set_auth(MQTTuser,MQTTpassword))){
  //  client.publish("kranstatus", statusBall2);  //publish
  //  client.subscribe("kranstatus");    //subscribe
  //}

}


void loop()
{

      //delay(1000);
      //client.subscribe("kranstatus");
      //client.publish("kranstatus", statusBall2);  //publish
    //Serial.print("status loop ");
    //Serial.println(WiFi.status());
    delay(2000);
    if (WiFi.status() == 6)
    {
      //Serial.println("putus koneksi");
      WIFI_Connect();
    }
    else
    {
      if(!client.subscribe("kranstatus")){
        WIFI_Connect();
      }
      if(!client.publish("kranstatus", statusBall2)){
        WIFI_Connect();
      }

      //delay(1000);
      //client.subscribe("kranstatus");
      //client.publish("kranstatus", statusBall2);  //publish
    }

client.loop();

 
}

No comments:

Post a Comment