This post will cover how to control a servo remotely from the Internet. This is a very easy project that can be done with a few lines of codes. Will help to know how to define input parameters to the device, and how to define functions without parameters.
Hardware
In this case I will be using an Arduino Nano + Adafruit CC3000
for connecting the chip to the Internet. You can use any other MCU as the code for controlling the servo will be exactly the same. The servo is a small cheap one called Tower Pro Micro Servo 9g (SG90)
. You can find this one in eBay or AliExpress easily. The servo is attached to digital pin 9, while the Adafruit CC3000
is connected to the Arduino Nano using SPI (with the default configuration for the Adafruit Library).
Software
This how-to will use Arduino IDE with the thinger.io library installed. Click on this link if you do not have the library installed yet. We also need the Servo library that should be installed by default in the Arduino IDE.
Picture
Arduino Sketch
#include <Adafruit_CC3000.h>
#include <SPI.h>
#include <ccspi.h>
#include <ThingerCC3000.h>
#include <Servo.h>
#define USERNAME "your_username"
#define DEVICE_ID "your_device_id"
#define DEVICE_CREDENTIAL "your_device_credential"
#define SSID "your_ssid"
#define SSID_PASSWORD "your_ssid_password"
#define SERVO_PIN 9
ThingerCC3000 thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);
Servo servo;
void setup() {
servo.attach(SERVO_PIN);
// configure wifi network
thing.add_wifi(SSID, SSID_PASSWORD);
// controlling servo position
thing["servo"]["pos"] << [](pson& in){
servo.write((int)in);
};
// reseting servo position
thing["servo"]["reset"] = [](){
servo.write(0);
};
}
void loop() {
thing.handle();
}
Web Control
Once you enter in your device API you will see two different methods for controlling the servo. One is for setting an absolute position in degrees, and the second one is for resetting the servo position. You can try changing the servo position from 0 to 180 degrees. The servo should move as you click on the Run
button.