Experiment 6 Servo Motor Control Using IOT Plaform
Experiment 6 Servo Motor Control Using IOT Plaform
Exp: 5
AIM
APPARATUS REQUIRED:
S.No
Name of the item Type Range Quantity
.
1. NodeMcu Wifi Module ESP8266 - 1
2. Power supply - 1
3. Android device - - 1
4. Servo motor MG996 - 1
5. Connecting Wires - - -
PROCEDURE:
/***************************************************
Adafruit MQTT Library ESP8266 Example
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT,
AIO_USERNAME, AIO_USERNAME, AIO_KEY);
void setup() {
// pinMode(LED, OUTPUT);
// pinMode(PWMOUT, OUTPUT);
servo.attach(2);
Serial.begin(115200);
delay(10);
WiFi.begin(WLAN_SSID, WLAN_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("WiFi connected");
Serial.println("IP address: "); Serial.println(WiFi.localIP());
// Setup MQTT subscription for onoff & slider feed.
mqtt.subscribe(&onoffbutton);
mqtt.subscribe(&slider);
}
uint32_t x=0;
void loop() {
// Ensure the connection to the MQTT server is alive (this will make the first
// connection and automatically reconnect when disconnected). See the MQTT_connect
// function definition further below.
MQTT_connect();
Adafruit_MQTT_Subscribe *subscription;
while ((subscription = mqtt.readSubscription(5000))) {
// Check if its the onoff button feed
//if (subscription == &onoffbutton) {
// Serial.print(F("On-Off button: "));
//Serial.println((char *)onoffbutton.lastread);
uint8_t retries = 3;
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
Serial.println(mqtt.connectErrorString(ret));
Serial.println("Retrying MQTT connection in 5 seconds...");
mqtt.disconnect();
delay(5000); // wait 5 seconds
retries--;
if (retries == 0) {
// basically die and wait for WDT to reset me
while (1);
}
}
Serial.println("MQTT Connected!");
}
Inference:
Result: