IoT_lover
Published © GPL3+

Control DIY DC Motor Car via Web

Without web programming, we can control DC motor car via web.

BeginnerFull instructions provided1 hour4,113
Control DIY DC Motor Car via Web

Things used in this project

Story

Read more

Schematics

wiring

1. Assembly car
2. Stack PHPoC Shield on Arduino
3. Stack DC motor controller on PHPoC Shield
4. Connect DC motors to DC motor controller

Code

WebCar.ino

Arduino
#include <Phpoc.h>
#include <PhpocExpansion.h>

PhpocServer server(80);
byte expansionId = 1;
ExpansionDCMotor dcm1(expansionId, 1);
ExpansionDCMotor dcm2(expansionId, 2);

void setup() {
  Serial.begin(9600);
  while(!Serial)
    ;

  // initialize PHPoC [WiFi] Shield:
  Phpoc.begin(PF_LOG_SPI | PF_LOG_NET);
  //Phpoc.begin();
  Expansion.begin();

  // start WebSocket server
  server.beginWebSocket("remote_push");

  // print IP address of PHPoC [WiFi] Shield to serial monitor:
  Serial.print("WebSocket server address : ");
  Serial.println(Phpoc.localIP());

  dcm1.setPolarity(1);
  dcm2.setPolarity(-1);
  // set PWM period
  dcm1.setPeriod(10000);
  dcm2.setPeriod(10000);
}

void loop() {
  // wait for a new client:
  PhpocClient client = server.available();

  if (client) {
    if (client.available() > 0) {
      // read a byte incoming from the client:
      char thisChar = client.read();

      // when an user presses a button on the web apps, the web app sends an
      // uppercase character corresponding with the name of button to Arduino.
      // When an user releases a button on the web apps, the web app sends a
      // lowercase character corresponding with the name of button to Arduino.

      if(thisChar == 'B')
      {
         Serial.println("Move forward");
         dcm1.setDirection(1);
         dcm2.setDirection(1);
         dcm1.setWidth(5000);
         dcm2.setWidth(5000);
      }
      if(thisChar == 'b')
      {
         Serial.println("Stop");
         dcm1.setWidth(0);
         dcm2.setWidth(0);
      }
      if(thisChar == 'D')
      {
         Serial.println("Turn left");
         dcm2.setDirection(1);
         dcm2.setWidth(5000);
      }
      if(thisChar == 'd')
      {
         Serial.println("Stop");
         dcm1.setWidth(0);
         dcm2.setWidth(0);
      }
      if(thisChar == 'F')
      {
         Serial.println("turn right");
         dcm1.setDirection(1);
         dcm1.setWidth(5000);
      }
      if(thisChar == 'f')
      {
         Serial.println("Stop");
         dcm1.setWidth(0);
         dcm2.setWidth(0);
      }
      if(thisChar == 'H')
      {
         Serial.println("Move backward");
         dcm1.setDirection(-1);
         dcm2.setDirection(-1);
         dcm1.setWidth(5000);
         dcm2.setWidth(5000);
      }
      if(thisChar == 'h')
      {
         Serial.println("Stop");
         dcm1.setWidth(0);
         dcm2.setWidth(0);
      }
    }
  }
}

Credits

IoT_lover

IoT_lover

10 projects • 190 followers

Comments