MIT App Inventer 2 Button Control

I have a ultrasonic sensor and water pump that connect to Photon board. The reading of ultrasonic sensor will be display on Android phone. I’m using MIT app inventor 2 to done this. Besides, I also make 2 button on the app to turn on/off the water pump. I had completed the sensor reading part. However, i cannot make the button to turn on the pump yet. So, I hope those who had experience on this can give me a help. Thank you.

Below is the sensor reading part that i had done using MIT app inventor 2.

You can create a Particle.function() - https://docs.particle.io/reference/firmware/electron/#particle-function-

Depending on the value you send when you call the function, the pump will turn on/off.

int motorOn = 0;

void setup(){
 Particle.function("controlMotor", ctrlMotor);
}

int ctrlMotor(String command){
 if(command == "on")
   motorOn = 1;
 else if (command == "off")
   motorOn = 0;
else
  return -1;

 return motorOn;
}

The MIT app inventor side should look something like this. (I might be getting the json list wrong)

The idea is to do a POST request and the block BuildRequestData will make it into a form that the API endpoint (https://docs.particle.io/reference/api/#call-a-function) wants. The missing piece is if i got the json part correct or not.

It will want a json with the following:

{
 arg: yourArgument,
 access_token: yourAccessToken
}

The value of yourArgument will be passed to the function call for Particle.function()

Hi Kenneth, in your example, your function name is controlMotor right?
Then how about the argument value? How it work and how to set it?

Maybe for the buttons that you have added, you can set the argument to on and another to off. The next step is to trigger the web POST l.

Let me know if you managed to get it working :slight_smile:

I try on the Particle example apps Web-Connected LED but I cannot make it work as well.

Below is the mit app inventor block that i had build. Does anything wrong there???

The light blue (make a list) is not correct. As mentioned in my previous post, i might not be getting it right.

You might need to try and see how you make a list to pass 2 json argument to Web1.

Follow this link and it will help with making the buttons work - then couple it with the sensor readings to be displayed.
Note the >return 200 value in the photon code doesn't do anything in App inventor.

hope it helps, if not I will post what I have working

2 Likes

Hi Kenneth, i had successful control the LED through App Inventor. Below is the inventor block.
Hope it may help in future.

4 Likes

I had make my LED working. Thanks for your help. :smiley:

2 Likes

Thanks for sharing!