[SOLVED] Variable decrementation (on/off device)

Hello, i´m trying to configure a system that cont the number of pills that are in the bottle and allways the bootle are open the number of pills are decremented.
I have one variable on ubidots with the nº off pills in the bootle… and i want to decrement the value of his variable (-1) when i press a switch… I have tried with many forms without success.
Anyone can help me and observe my code below, please?
(in this code the variable is changed to -1 but i want do decrement)

#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>

const char* ssid = "Vodafone-D61838";
const char* password = "C4fdsdN";

//Ubidots account data
#define URL    "things.ubidots.com"
#define TOKEN  "A1E-yGxMXh6OuTTNhHMKjQf51s2wslZ343"     // replace with your Ubidots token generated in your profile tab
#define VARID1 "5b2e7372c03f97085e1aefb7"    //batery
#define VARID2 "5b2e979fc03f97280ab3f124"   //counter



int cont = -1;
ADC_MODE(ADC_VCC); //voltage level measurement

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.println();
  Serial.print("connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
  delay(500);
  Serial.print(".");
  }

  
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  delay(500);
  send_data_to_ubidot();

}

void loop() {


}

void send_data_to_ubidot(){
   WiFiClient client;
  float voltage = ESP.getVcc()/1024.0;
  int level = 100/3.6 * voltage;
  
  String payload = "[{\"variable\":\"" VARID1 "\",\"value\":"+ String(level) + "},{\"variable\":\"" VARID2 "\",\"value\":" + int(cont) + "}]";
  String le = String(payload.length());  // How long is the payload


  Serial.print("Connecting to ");
  Serial.println(URL);
  if (client.connect(URL, 80))
  {
    // Build HTTP POST request
    Serial.println("connected");    
    client.print(F("POST /api/v1.6/collections/values/?token="));
    client.print(TOKEN);
    client.println(F(" HTTP/1.1"));
    client.println(F("Content-Type: application/json"));
    client.print(F("Content-Length: "));
    client.println(le);
    client.print(F("Host: "));
    client.println(URL);
    client.println(); 
    client.println(payload);
    client.println(); 
    client.println((char)26); //This terminates the JSON SEND with a carriage return
  }
  else
  {
    Serial.println("connection failed");
  }
  delay(100);
  // if there are incoming bytes available
  // from the server, read them and print them:
  if (client.available())
  {
    char c = client.read();
    Serial.print(c);
  }

  if (!client.available() && !client.connected())
  {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
  }
  
}

Many thks

Dear @jisby,

After review the sample code provided I noted that the desired behavior is no presented in the code. For a better understanding, at this moment you are calling a send_data_to_ubidots() function which is in charge of taking the value for the level variable and in the same request you are trying to send the value which is the one who is going to be decremented. While the expected behavior is that the value decrement just once a switch is pressed, right?

I would like to clarify the behavior expected by you in order to help you in the best way I can! :slight_smile:

All the best,
Maria C.

Hello @mariahernandez, many thanks for your attention!

The code have other functions like obtain the charge of device. Basicly the main objective it´s when i turn on the device (user a switch or not) a value of a variable declared in ubidots it´s decremented.

I create a variable:
int cont = -1;

And send the value for the server to decrement a variable created on ubidots.

The project it´s this:
http://jorgebrandao.pt/idose/

Hello @jisby,

My recommendation is to build a counter in the firmware based on the conditions of the device. For example, if the status of the device is equal to one (device turned on/switch pressed) update a variable with the number of pills - 1 having as result the value decremented. Then, you should post the variable updated at Ubidots.

That makes sense for you?

All the best,
Maria C.

Hello @mariahernandez!

Yes, but its make all sense…
But how can i implement this in my code? I have tried t send the value -1 to the ubidots variable but changes the actual value (30) to -1 :frowning:

Many thks

Dear user,

Firstly, is important have the idea of how our project is going to work. In your case, you expect that the value of the numbers of pills decrement everytime the device is turned on or a switch is pressed, so you should have a variable in your firmware which is reflecting that status.

Now based on the status of the variable you should create a condition which is going to be in charge of decrement the expected value just when the condition is true. As an example:

if (device_status == 1) {
    total_pills = pills - 1;
    // send "total_pill" to ubidots
} 

At this point, you will be posting the end pills value which is the variable who is being decremented. Also, you can post both variables in the same request total pills (decremented value) and pills

I hope this would help you get a better idea of how to build your project!

All the best,
Maria C.

@mariahernandez many thaks for your help and patience…
I want to decrement the value (-1) allways when i put on the microprocessor (wemos d1) ON i think i don´t need the variable to read the status…

But your ideia (obtain the value of the pills and decrement directly on firmware (total_pills = pills - 1)) it´s amazing!!!
I just have some difficult to import to the firmware the value of the variable total_pills (because this variable is set directly on ubidots plataform…

Many thaks one more time!

The device status variable will be in charge of setting the condition to decrement the number of pills. Putting this step by step you should:

  1. Create a flag variable by default in false for the device status.
  2. In the setup of your code set the variable to true. This will be true every time the setup runs so that’s mean every time your device boots up.
  3. In the loop establish the condition mentioned above, and then inside the condition set the flag to false

At this point, the behavior presented should be

  1. Device Turn On
  2. Set variable device-status to true
  3. Verify the status of the device and set condition
  4. If the condition is true (in the first should be always true) decrement the value
  5. Inside the same condition, set the device-status variable to false.

So your device will be a decrement the value just in the first boot because once the condition is true the device-status return to false and the value will be no decremented in the following loops. It will be just decremented in the first one once the device just boots up.

To give you a better understanding of how it should look like, check the code below to build your own:

bool device_status = false;

void setup() {
    device_status = true;
}

void loop() {
    if (device_status) {
        // decrement value
        // send value to ubidots
        device_status = false;
    }
}

I hope this would help you! :smiley:

Regards,
Maria C