Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SoftwareSerial.h>
- #define DEBUG true
- #include <Servo.h>
- SoftwareSerial esp8266(0,1);
- // Create servo objects
- Servo panServo;
- Servo tiltServo;
- // Center servos
- int tiltVal = 90;
- int panVal =90;
- char letter;
- void setup() {
- letter = 'A';
- panServo.attach(11);
- tiltServo.attach(10);
- Serial.begin(9600); //for monitoring purposes
- Serial.println("Android Sensor Type No: ");
- Serial.println("1- ACCELEROMETER (m/s^2 - X,Y,Z)");
- Serial.println("2- GYROSCOPE (rad/s - X,Y,Z)");
- Serial.flush()
- esp8266.begin(9600); // your esp's baud rate might be different
- ;
- sendCommand("AT+RSTrn",2000,DEBUG); // reset module
- sendCommand("AT+IPR=115200rn",1000,DEBUG);
- sendCommand("AT+CWMODE=1rn",1000,DEBUG); // configure as access point
- sendCommand("AT+CWJAP="ABCDEFG","12345678"rn",3000,DEBUG); //connect to a network with name ABCDEFG with password 12345678
- delay(1000);
- //sendCommand("AT+CIFSRrn",1000,DEBUG); // get ip address
- sendCommand("AT+CIPSTA="192.168.43.16"rn",1000,DEBUG);
- sendCommand("AT+CIPMUX=1rn",1000,DEBUG); // configure for multiple connections
- sendCommand("AT+CIPSERVER=1,6625rn",1000,DEBUG); // turn on server on port 6625
- Serial.println("Server Ready");
- }
- void loop() {
- Serial.flush();
- int inCommand = 0;
- int sensorType = 0;
- unsigned long logCount = 0L;
- char A = ' '; //read serial
- // wait for incoming data
- if (Serial.available() < 1) return; // if serial empty, return to loop().
- // parse incoming command start flag
- A = Serial.read();
- if (A != A) return; // if no command start flag, return to loop().
- // parse incoming pin# and value
- sensorType = Serial.parseInt(); // read sensor typr
- logCount = Serial.parseInt(); // read total logged sensor readings
- value0 = Serial.parseFloat(); // 1st sensor value
- value1 = Serial.parseFloat(); // 2rd sensor value if exists
- value2 = Serial.parseFloat(); // 3rd sensor value if exists
- // send sensoduino readings to serial monitor/terminal
- if (DEBUG) {
- Serial.print("Sensor type: ");
- Serial.println(sensorType);
- Serial.print("Sensor log#: ");
- Serial.println(logCount);
- Serial.print("Val[0]: ");
- Serial.println(value0);
- Serial.print("Val[1]: ");
- Serial.println(value1);
- Serial.print("Val[2]: ");
- Serial.println(value2);
- Serial.println("-----------------------");
- delay(10);
- }
- // Check sensor type. If not for Accelerometer (#1) then ignore readings
- // sensorType 1 is the Accelerometer sensor
- if (sensorType !=1) return;
- panVal = value0; // value0 = X sensor reading
- tiltVal = value1; // value1 = Y sensor reading
- tiltVal = map(tiltVal, 10, -10, 0, 179); // Map Accelerometer Y value to tilt servo angle.
- tiltServo.write(tiltVal);
- delay(10);
- panVal = map(panVal, -10, 10, 0, 179); // Map Accelerometer X value to pan servo angle.
- panServo.write(panVal);
- delay(10);
- }
Add Comment
Please, Sign In to add comment