Advertisement
Guest User

Untitled

a guest
Dec 17th, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.34 KB | None | 0 0
  1. #include "I2Cdev.h"
  2. #include "MPU6050_6Axis_MotionApps20.h"
  3. #include <NeoPixelBus.h>
  4. #include <WiFi.h>
  5. #include <PubSubClient.h>
  6.  
  7. #define colorSaturation 5
  8. #define OUTPUT_READABLE_YAWPITCHROLL
  9. #define INTERRUPT_PIN 27 // use pin 2 on Arduino Uno & most boards
  10. #define LED_PIN 2 // (Arduino is 13, Teensy is 11, Teensy++ is 6)
  11.  
  12. enum mode { DEFAULT_MODE, PLAY_MODE, IMAGE_MODE };
  13.  
  14. const uint16_t PixelCount = 64; // this example assumes 4 pixels, making it smaller will cause a failure
  15. const uint8_t PixelPin = 26; // make sure to set this to the correct pin, ignored for Esp8266
  16. bool dmpReady = false; // set true if DMP init was successful
  17. uint8_t mpuIntStatus; // holds actual interrupt status byte from MPU
  18. uint8_t devStatus; // return status after each device operation (0 = success, !0 = error)
  19. uint16_t packetSize; // expected DMP packet size (default is 42 bytes)
  20. uint16_t fifoCount; // count of all bytes currently in FIFO
  21. uint8_t fifoBuffer[128]; // FIFO storage buffer
  22. float ypr[3]; // [yaw, pitch, roll] yaw/pitch/roll container and gravity vector
  23. volatile bool mpuInterrupt = false; // indicates whether MPU interrupt pin has gone high
  24. Quaternion q;
  25. VectorFloat gravity; // [x, y, z] gravity vector
  26. MPU6050 mpu;
  27.  
  28. //const uint16_t PixelCount = 64; // this example assumes 4 pixels, making it smaller will cause a failure
  29. //bool blinkState = false;
  30. char* ssid = "504";
  31. char* password = "20142020";
  32. const char* mqttServer = "m15.cloudmqtt.com";
  33. const int mqttPort = 15861;
  34. const char* mqttUser = "llaoendv";
  35. const char* mqttPassword = "jULqWpRt3vRx";
  36.  
  37.  
  38. int n = 2;
  39.  
  40. NeoPixelBus<NeoGrbFeature, Neo800KbpsMethod> strip(PixelCount, PixelPin);
  41. WiFiClient espClient;
  42. PubSubClient client(espClient);
  43.  
  44. int foo;
  45.  
  46. void dmpDataReady() {
  47. mpuInterrupt = true;
  48. }
  49.  
  50.  
  51.  
  52. void show_array(byte* Data) {
  53. RgbColor Colors[8][8];
  54. int k = 0;
  55.  
  56. if (Data[0] == 0) {
  57. for (int i = 0; i < 8; i++) {
  58. for (int j = 0; j < 8; j++) {
  59. Colors[i][j] = RgbColor(Data[k + 1], Data[k + 2], Data[k + 3]);
  60. strip.SetPixelColor(i * 8 + j, Colors[i][j]);
  61. k += 3;
  62. }
  63. }
  64. } else {}
  65. strip.Show();
  66. }
  67.  
  68. void callback(char* topic, byte* payload, unsigned int length) {
  69. char message[256] = "";
  70. byte Data[256];
  71.  
  72. for (int i = 0; i < 256; i++) {
  73. if (i < length) {
  74. Data[i] = payload[i];
  75. message[i] = (char)payload[i];
  76. } else {
  77. Data[i] = 0;
  78. }
  79. }
  80. show_array(Data);
  81. Serial.println(message);
  82. Serial.println("-----------------------");
  83. n = 2;
  84. }
  85.  
  86. void setSensorConnect() {
  87. #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
  88. Wire.begin();
  89. Wire.setClock(400000);
  90. #elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
  91. Fastwire::setup(400, true);
  92. #endif
  93.  
  94. Serial.println(F("Initializing I2C devices..."));
  95. mpu.initialize();
  96. pinMode(INTERRUPT_PIN, INPUT);
  97.  
  98. Serial.println(F("Testing device connections..."));
  99. Serial.println(mpu.testConnection() ? F("MPU6050 connection successful") : F("MPU6050 connection failed"));
  100.  
  101. Serial.println(F("Initializing DMP..."));
  102. devStatus = mpu.dmpInitialize();
  103.  
  104. mpu.setXGyroOffset(220);
  105. mpu.setYGyroOffset(76);
  106. mpu.setZGyroOffset(-85);
  107. mpu.setZAccelOffset(1788);
  108.  
  109. if (devStatus == 0) {
  110. Serial.println(F("Enabling DMP..."));
  111. mpu.setDMPEnabled(true);
  112.  
  113. Serial.print(F("Enabling interrupt detection (Arduino external interrupt "));
  114. Serial.print(digitalPinToInterrupt(INTERRUPT_PIN));
  115. Serial.println(F(")..."));
  116. attachInterrupt(digitalPinToInterrupt(INTERRUPT_PIN), dmpDataReady, RISING);
  117. mpuIntStatus = mpu.getIntStatus();
  118.  
  119. Serial.println(F("DMP ready! Waiting for first interrupt..."));
  120. dmpReady = true;
  121. packetSize = mpu.dmpGetFIFOPacketSize();
  122. } else {
  123. Serial.print(F("DMP Initialization failed (code "));
  124. Serial.print(devStatus);
  125. Serial.println(F(")"));
  126. }
  127. }
  128.  
  129.  
  130. void setWifiConnect() {
  131. WiFi.begin(ssid, password);
  132. while (WiFi.status() != WL_CONNECTED) {
  133. Serial.println("Connecting to WiFi..");
  134. delay(1000);
  135. }
  136. Serial.println("Connected to the WiFi network");
  137. }
  138.  
  139.  
  140. void setMqttConnect() {
  141. client.setServer(mqttServer, mqttPort);
  142. client.setCallback(callback);
  143.  
  144. while (!client.connected()) {
  145. Serial.println("Connecting to MQTT...");
  146. if (client.connect("ESP32Client", mqttUser, mqttPassword )) {
  147. Serial.println("connected");
  148. } else {
  149. Serial.print("failed with state ");
  150. Serial.print(client.state());
  151. delay(2000);
  152. }
  153. }
  154. client.subscribe("esp/test");
  155. client.publish("esp/test_hi", "Hello from ESP32");
  156. }
  157.  
  158. void setup() {
  159. Serial.begin(115200);
  160. while (!Serial);
  161.  
  162. //setSensorConnect();
  163. setWifiConnect();
  164. setMqttConnect();
  165.  
  166. strip.Begin();
  167.  
  168. for (int i = 0; i < PixelCount; i++) {
  169. strip.SetPixelColor(i, RgbColor(0, 0, 0));
  170. }
  171.  
  172.  
  173. switch (n) {
  174. case DEFAULT_MODE:
  175. break;
  176.  
  177. case PLAY_MODE:
  178. foo = generateFood();
  179. strip.SetPixelColor(foo, RgbColor(50, 0, 0));
  180. break;
  181.  
  182. case IMAGE_MODE:
  183. break;
  184. }
  185.  
  186. strip.Show();
  187. pinMode(LED_PIN, OUTPUT);
  188. }
  189.  
  190.  
  191. int generateFood() {
  192. return random(1, 64);
  193. }
  194.  
  195.  
  196. void playLoop() {
  197. if (!dmpReady) return;
  198.  
  199. while (!mpuInterrupt && fifoCount < packetSize) {
  200. if (mpuInterrupt && fifoCount < packetSize) {
  201. fifoCount = mpu.getFIFOCount();
  202. }
  203. }
  204.  
  205. mpuInterrupt = false;
  206. mpuIntStatus = mpu.getIntStatus();
  207. fifoCount = mpu.getFIFOCount();
  208.  
  209. if ((mpuIntStatus & _BV(MPU6050_INTERRUPT_FIFO_OFLOW_BIT)) || fifoCount >= 1024) {
  210. mpu.resetFIFO();
  211. fifoCount = mpu.getFIFOCount();
  212. Serial.println(F("FIFO overflow!"));
  213. } else if (mpuIntStatus & _BV(MPU6050_INTERRUPT_DMP_INT_BIT)) {
  214. while (fifoCount < packetSize) {
  215. fifoCount = mpu.getFIFOCount();
  216. }
  217.  
  218. mpu.getFIFOBytes(fifoBuffer, packetSize);
  219. fifoCount -= packetSize;
  220.  
  221. #ifdef OUTPUT_READABLE_YAWPITCHROLL
  222.  
  223. mpu.dmpGetQuaternion(&q, fifoBuffer);
  224. mpu.dmpGetGravity(&gravity, &q);
  225. mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);
  226.  
  227. #endif
  228.  
  229.  
  230. for (int i = 0; i < PixelCount; i++) {
  231. if (i != foo)
  232. strip.SetPixelColor(i, RgbColor(0, 0, 0));
  233. }
  234.  
  235. int pxl1 = (ypr[1] * 180/M_PI);
  236. pxl1 = (pxl1 > 0) ? (pxl1 - 180) : (pxl1 + 180);
  237. pxl1 /= 10;
  238. pxl1 *= -8;
  239. pxl1 += 32;
  240.  
  241. int pxl2 = (ypr[2] * 180/M_PI);
  242. pxl2 = (pxl2 > 0) ? (pxl2 - 180) : (pxl2 + 180);
  243. pxl2 /= 10;
  244. pxl2 += 4;
  245.  
  246. if ((pxl2 >= 0)
  247. && (pxl2 <= 7)
  248. && (pxl1 + pxl2 >= 0)
  249. && (pxl1 + pxl2 <= 63)) {
  250.  
  251. strip.SetPixelColor(pxl1 + pxl2, RgbColor(0, colorSaturation, 0));
  252. if (pxl1 + pxl2 == foo) {
  253. foo = generateFood();
  254. strip.SetPixelColor(foo, RgbColor(50, 0, 0));
  255. }
  256. } else {
  257. for (int i = 0; i < PixelCount; i++) {
  258. strip.SetPixelColor(i, RgbColor(20, 0, 0));
  259. }
  260. }
  261. }
  262.  
  263. strip.Show();
  264. }
  265.  
  266. void loop() {
  267. switch (n) {
  268. case DEFAULT_MODE:
  269. break;
  270.  
  271. case PLAY_MODE:
  272. playLoop();
  273. break;
  274.  
  275. case IMAGE_MODE:
  276. client.loop();
  277. break;
  278. }
  279. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement