Управление двигателями через интернет (Требуется помощь с кодом)
- Войдите на сайт для отправки комментариев
Чт, 25/04/2019 - 22:34
Доброго времени суток! Хочу собрать машинку с IP камерой и управлять ей по Wifi (Офисная игрушка, буду разыгрывать колег))). Использую Sheild L293D, интернет модуль hr91105a, ардуину мегу и вай фай роутер. Должен создаваться сервер и открываться сайт с кнопочками, для управления двигателями. Код компилируется без проблем, даже открывается окно для управления, но двигатели не подчиняются.
#include <EtherCard.h> #include <AFMotor.h> static byte mymac[] = {0x5A, 0x5A, 0x5A, 0x5A, 0x5A, 0x5A}; byte Ethernet::buffer[900]; BufferFiller bfill; AF_DCMotor motor1(1);// Подключаем моторы к клеммникам M1 AF_DCMotor motor2(2);// Подключаем моторы к клеммникам M2 AF_DCMotor motor3(3);// Подключаем моторы к клеммникам M3 AF_DCMotor motor4(4);// Подключаем моторы к клеммникам M4 int i; boolean PinStatus[] = {1, 2, 3, 4, 5, 6, 7, 8}; const char http_OK[] PROGMEM = "HTTP/1.0 200 OK\r\n" "Content-Type: text/html\r\n" "Pragma: no-cache\r\n\r\n"; const char http_Found[] PROGMEM = "HTTP/1.0 302 Found\r\n" "Location: /\r\n\r\n"; const char http_Unauthorized[] PROGMEM = "HTTP/1.0 401 Unauthorized\r\n" "Content-Type: text/html\r\n\r\n" "<h1>401 Unauthorized</h1>"; void homePage() { bfill.emit_p(PSTR("$F" "<title>ArduinoPIN Webserver</title>" "Relay 1: <a href=\"?ArduinoPIN1=$F\">$F</a><br />" "Relay 2: <a href=\"?ArduinoPIN2=$F\">$F</a><br />" "Relay 3: <a href=\"?ArduinoPIN3=$F\">$F</a><br />" "Relay 4: <a href=\"?ArduinoPIN4=$F\">$F</a><br />" "Relay 5: <a href=\"?ArduinoPIN5=$F\">$F</a><br />" "Relay 6: <a href=\"?ArduinoPIN6=$F\">$F</a><br />" "Relay 7: <a href=\"?ArduinoPIN7=$F\">$F</a><br />" "Relay 8: <a href=\"?ArduinoPIN8=$F\">$F</a>"), http_OK, PinStatus[1] ? PSTR("off") : PSTR("on"), PinStatus[1] ? PSTR("<font color=\"green\"><b>ON</b></font>") : PSTR("<font color=\"red\">OFF</font>"), PinStatus[2] ? PSTR("off") : PSTR("on"), PinStatus[2] ? PSTR("<font color=\"green\"><b>ON</b></font>") : PSTR("<font color=\"red\">OFF</font>"), PinStatus[3] ? PSTR("off") : PSTR("on"), PinStatus[3] ? PSTR("<font color=\"green\"><b>ON</b></font>") : PSTR("<font color=\"red\">OFF</font>"), PinStatus[4] ? PSTR("off") : PSTR("on"), PinStatus[4] ? PSTR("<font color=\"green\"><b>ON</b></font>") : PSTR("<font color=\"red\">OFF</font>"), PinStatus[5] ? PSTR("off") : PSTR("on"), PinStatus[5] ? PSTR("<font color=\"green\"><b>ON</b></font>") : PSTR("<font color=\"red\">OFF</font>"), PinStatus[6] ? PSTR("off") : PSTR("on"), PinStatus[6] ? PSTR("<font color=\"green\"><b>ON</b></font>") : PSTR("<font color=\"red\">OFF</font>"), PinStatus[7] ? PSTR("off") : PSTR("on"), PinStatus[7] ? PSTR("<font color=\"green\"><b>ON</b></font>") : PSTR("<font color=\"red\">OFF</font>"), PinStatus[8] ? PSTR("off") : PSTR("on"), PinStatus[8] ? PSTR("<font color=\"green\"><b>ON</b></font>") : PSTR("<font color=\"red\">OFF</font>")); } void setup() { Serial.begin(9600); if (ether.begin(sizeof Ethernet::buffer, mymac) == 0); if (!ether.dhcpSetup()); ether.printIp("My Router IP: ", ether.myip); for (int i = 0; i <= 8; i++) { motor1.setSpeed(255);// Задаем максимальную скорость вращения моторов motor1.run(RELEASE); if (PinStatus[1] = true){ motor1.run(FORWARD); } else if (PinStatus[1] = false){ motor1.run(RELEASE); } if (PinStatus[2] = true){ motor2.run(FORWARD); } else if (PinStatus[2] = false){ motor2.run(RELEASE); } if (PinStatus[3] = true){ motor3.run(FORWARD); } else if (PinStatus[3] = false){ motor3.run(RELEASE); } if (PinStatus[4] = true){ motor4.run(FORWARD); } else if (PinStatus[4] = false){ motor4.run(RELEASE); } if (PinStatus[5] = true){ motor1.run(FORWARD); } else if (PinStatus[5] = false){ motor1.run(RELEASE); } if (PinStatus[6] = true){ motor2.run(FORWARD); } else if (PinStatus[6] = false){ motor2.run(RELEASE); } if (PinStatus[7] = true){ motor3.run(FORWARD); } else if (PinStatus[7] = false){ motor3.run(RELEASE); } if (PinStatus[8] = true){ motor4.run(FORWARD); } else if (PinStatus[8] = false){ motor4.run(RELEASE); } } } void loop() { delay(1); word len = ether.packetReceive(); // check for ethernet packet word pos = ether.packetLoop(len); // check for tcp packet if (pos) { bfill = ether.tcpOffset(); char *data = (char *) Ethernet::buffer + pos; if (strncmp("GET /", data, 5) != 0) { bfill.emit_p(http_Unauthorized); } else { data += 5; if (data[0] == ' ') { homePage(); // Return home page } else if (strncmp("?ArduinoPIN1=on ", data, 16) == 0) { PinStatus[1] = true; bfill.emit_p(http_Found); } else if (strncmp("?ArduinoPIN2=on ", data, 16) == 0) { PinStatus[2] = true; bfill.emit_p(http_Found); } else if (strncmp("?ArduinoPIN3=on ", data, 16) == 0) { PinStatus[3] = true; bfill.emit_p(http_Found); } else if (strncmp("?ArduinoPIN4=on ", data, 16) == 0) { PinStatus[4] = true; bfill.emit_p(http_Found); } else if (strncmp("?ArduinoPIN5=on ", data, 16) == 0) { PinStatus[5] = true; bfill.emit_p(http_Found); } else if (strncmp("?ArduinoPIN6=on ", data, 16) == 0) { PinStatus[6] = true; bfill.emit_p(http_Found); } else if (strncmp("?ArduinoPIN7=on ", data, 16) == 0) { PinStatus[7] = true; bfill.emit_p(http_Found); } else if (strncmp("?ArduinoPIN8=on ", data, 16) == 0) { PinStatus[8] = true; bfill.emit_p(http_Found); } else if (strncmp("?ArduinoPIN1=off ", data, 17) == 0) { PinStatus[1] = false; bfill.emit_p(http_Found); } else if (strncmp("?ArduinoPIN2=off ", data, 17) == 0) { PinStatus[2] = false; bfill.emit_p(http_Found); } else if (strncmp("?ArduinoPIN3=off ", data, 17) == 0) { PinStatus[3] = false; bfill.emit_p(http_Found); } else if (strncmp("?ArduinoPIN4=off ", data, 17) == 0) { PinStatus[4] = false; bfill.emit_p(http_Found); } else if (strncmp("?ArduinoPIN5=off ", data, 17) == 0) { PinStatus[5] = false; bfill.emit_p(http_Found); } else if (strncmp("?ArduinoPIN6=off ", data, 17) == 0) { PinStatus[6] = false; bfill.emit_p(http_Found); } else if (strncmp("?ArduinoPIN7=off ", data, 17) == 0) { PinStatus[7] = false; bfill.emit_p(http_Found); } else if (strncmp("?ArduinoPIN8=off ", data, 17) == 0) { PinStatus[8] = false; bfill.emit_p(http_Found); } else { // Page not found bfill.emit_p(http_Unauthorized); } } ether.httpServerReply(bfill.position()); // send http response } }
Понимаю, что код можно уменьшить циклами и тд. Но хотелось бы сначала проверить работоспособность данной схемы. Буду рад любой помощи)
Понял, что нужно прописать скорость вращения для всех моторов, но от этого ничего работать не стало...
Может сначала на кнопочках запустите, без веб-сервера?
Пробовал, всё работает. Поэтому и решил усложнить схему
Вы ее как-то не так усложняете.
Понимаете, есть такое понятие, как структурное программирование. Сильно помогает отлаживать код. (ООП еще больше помогает, но к нему еще дольше привыкать)
Так вот, и в setup() и в loop() должно остаться по 3-4 строчки. А весь остальной код должен быть вынесен в функции. Часть функций - работа с мотороами, часть - с кнопками. А в loop() они только объединяются.
Когда все это заработает, просто вместо кнопочек пишете то же самое по сети. Заменяете кнопки на сеть, содержимое loop() при этом должно остаться неизменным. И, собственно, - все работает.