Guest User

Untitled

a guest
Apr 23rd, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. #include <SoftwareSerial.h>
  2. #define DEBUG true
  3.  
  4. #include <Servo.h>
  5.  
  6. SoftwareSerial esp8266(0,1);
  7.  
  8.  
  9. // Create servo objects
  10. Servo panServo;
  11. Servo tiltServo;
  12.  
  13. // Center servos
  14. int tiltVal = 90;
  15. int panVal =90;
  16.  
  17. char letter;
  18.  
  19.  
  20.  
  21.  
  22. void setup() {
  23.  
  24. letter = 'A';
  25. panServo.attach(11);
  26. tiltServo.attach(10);
  27.  
  28.  
  29. Serial.begin(9600); //for monitoring purposes
  30.  
  31. Serial.println("Android Sensor Type No: ");
  32. Serial.println("1- ACCELEROMETER (m/s^2 - X,Y,Z)");
  33. Serial.println("2- GYROSCOPE (rad/s - X,Y,Z)");
  34. Serial.flush()
  35. esp8266.begin(9600); // your esp's baud rate might be different
  36.  
  37. ;
  38.  
  39.  
  40. sendCommand("AT+RSTrn",2000,DEBUG); // reset module
  41. sendCommand("AT+IPR=115200rn",1000,DEBUG);
  42. sendCommand("AT+CWMODE=1rn",1000,DEBUG); // configure as access point
  43. sendCommand("AT+CWJAP="ABCDEFG","12345678"rn",3000,DEBUG); //connect to a network with name ABCDEFG with password 12345678
  44. delay(1000);
  45. //sendCommand("AT+CIFSRrn",1000,DEBUG); // get ip address
  46. sendCommand("AT+CIPSTA="192.168.43.16"rn",1000,DEBUG);
  47. sendCommand("AT+CIPMUX=1rn",1000,DEBUG); // configure for multiple connections
  48. sendCommand("AT+CIPSERVER=1,6625rn",1000,DEBUG); // turn on server on port 6625
  49. Serial.println("Server Ready");
  50.  
  51. }
  52.  
  53. void loop() {
  54. Serial.flush();
  55. int inCommand = 0;
  56. int sensorType = 0;
  57. unsigned long logCount = 0L;
  58.  
  59. char A = ' '; //read serial
  60.  
  61. // wait for incoming data
  62. if (Serial.available() < 1) return; // if serial empty, return to loop().
  63.  
  64. // parse incoming command start flag
  65. A = Serial.read();
  66. if (A != A) return; // if no command start flag, return to loop().
  67.  
  68. // parse incoming pin# and value
  69. sensorType = Serial.parseInt(); // read sensor typr
  70. logCount = Serial.parseInt(); // read total logged sensor readings
  71. value0 = Serial.parseFloat(); // 1st sensor value
  72. value1 = Serial.parseFloat(); // 2rd sensor value if exists
  73. value2 = Serial.parseFloat(); // 3rd sensor value if exists
  74.  
  75. // send sensoduino readings to serial monitor/terminal
  76. if (DEBUG) {
  77. Serial.print("Sensor type: ");
  78. Serial.println(sensorType);
  79. Serial.print("Sensor log#: ");
  80. Serial.println(logCount);
  81. Serial.print("Val[0]: ");
  82. Serial.println(value0);
  83. Serial.print("Val[1]: ");
  84. Serial.println(value1);
  85. Serial.print("Val[2]: ");
  86. Serial.println(value2);
  87. Serial.println("-----------------------");
  88. delay(10);
  89. }
  90.  
  91. // Check sensor type. If not for Accelerometer (#1) then ignore readings
  92. // sensorType 1 is the Accelerometer sensor
  93.  
  94. if (sensorType !=1) return;
  95.  
  96. panVal = value0; // value0 = X sensor reading
  97. tiltVal = value1; // value1 = Y sensor reading
  98.  
  99. tiltVal = map(tiltVal, 10, -10, 0, 179); // Map Accelerometer Y value to tilt servo angle.
  100. tiltServo.write(tiltVal);
  101. delay(10);
  102.  
  103. panVal = map(panVal, -10, 10, 0, 179); // Map Accelerometer X value to pan servo angle.
  104. panServo.write(panVal);
  105. delay(10);
  106.  
  107. }
Add Comment
Please, Sign In to add comment