DC Motor help (two wires)

No rest for the wicked
Haha

I really appreciate the help by the way.
Just wanna learn it. I looked at the Phobot coding but could not figure how to control over the web using the APi
Dave

What’s the problem you can’t get over?
Have you looked at the WebControl.cpp sample?

Been trying, just confusing me
Would this bit here be my variables in the APi url?

void setup() {
Spark.function("control", control);
Spark.function("setmotors", setMotors);
Spark.variable("volts", &volts, DOUBLE);
Spark.variable("distance", &distance, DOUBLE);

So I would use something like, forwards/backwards/stop via the APi

Dave

The two functions setMotors and control are the ones you want to look at.
If you look at the code, you’ll see what command strings you need to send into the functions to have the device do what you want.

1 Like

ok.
Bear with me to see if I’m actually going the right way or the wrong way.
Old code (running but not controlled by the web)

#include "PhoBot/PhoBot.h"

PhoBot p = PhoBot(12.0, 12.0);

void setup() {
}

void loop() {
p.setMotors("M4-B-200");
delay(25000);
p.setMotors("M4-S");
delay(2000);
p.setMotors("M4-f-200");
delay(500);
p.setMotors("M4-S");
delay(5000);
}

New Code

    char motorCommand[12];
uint motorNr;
int speed;

int control(String command) {
    return p.control(command);
}

int setMotors(String command) {
    return p.setMotors(command);
}


  if (speed==0)
    sprintf(motorCommand, "M%u-S", motorNr);
  else
    sprintf(motorCommand, "M%u-%s-%d", motorNr, (speed > 0 ? "F" : "B"), abs(speed));

  p.setMotors(motorCommand);

I assume the p.setMotors("M4-B-200"); will be the same but where does it go, or is that called via the API?

Dave

Uhm :confused: nope.

My code would just illustrate how to build a control string from two numeric values, but the sample I refered won’t need that.
The sample expects you to already send in a well formed command string - however you will have to do that on your remote side.
There is even a curl sample call in the first line of the sample comment, but wrapping your brain round all that is something we can’t do for you.

Push me! This is the actual full code of the sample (with some things commented out) ```cpp /* curl https://api.spark.io/v1/devices/3b001b000447343232363230/setmotors -d access_token=172f237a43e4ed0ee25ddbb1507ebd5061bb19c5 -d params=M3-F-100

Commands are of the format: Motor-Direction-duty
Motor is one of: M1, M2, M3, M4
Direction is one of: F, B, S (forward, back, stop) - reverse has no effect on M1 and M2
Duty is percentage 0 to 100 and controls the motor speed.
M1 and M2 do not have PWM so duty is 0 or 1
To read the battery voltage use:
https://api.spark.io/v1/devices/3b001b000447343232363230/volts?access_token=172f237a43e4ed0ee25ddbb1507ebd5061bb19c5
To read the rangfinder distance in cm use:
https://api.spark.io/v1/devices/3b001b000447343232363230/distance?access_token=172f237a43e4ed0ee25ddbb1507ebd5061bb19c5
*/

#include ā€œPhoBot/PhoBot.hā€

//double volts = 0.0;
//double distance = 0.0;

PhoBot p; // = PhoBot();
//HC_SR04 rangefinder = HC_SR04(p.trigPin, p.echoPin);

int control(String command) {
return p.control(command);
}

int setMotors(String command) {
return p.setMotors(command);
}

void setup() {
Spark.function(ā€œcontrolā€, control);
Spark.function(ā€œsetmotorsā€, setMotors);
//Spark.variable(ā€œvoltsā€, &volts, DOUBLE);
//Spark.variable(ā€œdistanceā€, &distance, DOUBLE);
}

void loop() {
//volts = p.batteryVolts();
//distance = rangefinder.getDistanceCM();
//delay(100);
}

</details>

i think i might be useless

I think i understand that now.

It makes more sense seeing it like the second way… I’m sorry for my density lol

the API string i guess would be
https://api.spark.io/v1/devices/DEVICEID/setMotors?access_token=TOKEN
So if i want to use the following

M4-F-100

Looking at the example code

curl https://api.particle.io/v1/devices/0123456789abcdef/brew \
     -d access_token=123412341234 \
     -d "args=coffee"

How would i then add this to the CURL / API call?
Am i right in thinking i would have an argument. (args?) after the access token with the M4-F-100 within it?

Hoping thats right or the right track.

Yet again, i can’t thank you enough
Dave

Ok shoot me
It’s at the top I think

ok so I’m using

https://api.spark.io/v1/devices/deviceid/setMotors?access_token=token&?params=M4-F-100

but just getting

{
  "error": "Timed out."
}

so i must be getting close

Also using SSH getting the same error

{
  "ok": false,
  "error": "Timed out."
}

:bomb: How about this? :gun:

The way how you do it depends on your remote app (as said earlier too)

So you'd need to disclose your inrentions on this to us too.

its to trigger a device to release a biscuit for my cats… lol

have tried

curl https://api.spark.io/v1/devices/DEVICE ID/setmotors -d access_token=TOKEN -d params=M4-F-100

But thats getting timed out error
Im also trying just over the web as the API url

https://api.spark.io/v1/devices/DEVICE ID/setmotors?access_token=TOKEN&control=M4-F-100

Try this page to test your firmware
http://suda.github.io/particle-web-interface/

Ok calling

M4-F-100

will start the motor moving via that interface
so it must be something I’m calling wrong

is there any any way to see exactly what that Suda web interface page is polling to the Particle?
So i can see my variables

As its working using that like and setting parameters there, but not in the URL or Api call

Dave

Getting somewhere

in SSH using CURL

curl https://api.spark.io/v1/devices/DEVICEID/setmotors -d access_token=TOKEN -d params=M4-F-100

this works.
so how to i call the params as M4-F-100 within the URL from the API call?

Dave

Got is working

:smile:

1 Like

Good tho hear :+1:

Just one point.
If you find a solution please do post it along with your success report for others to benefit from it too.
(or what the problem was that held you back before)

figured out about the GET and POST requests via the API
so have to use a function to be able to get it to run in Javascript

function ONEFEED()
{
 $.post("https://api.spark.io/v1/devices/DEVICE/setmotors?access_token=token",
    {
		args:"M4-B-200"
		
    });
}

using

<button type="button" onclick="ONETREAT()">One Treat</button>

Now i just need to figure it out so it only does one revolution of the motor rather than continually spinning

:wink:

1 Like