Sending data from MKR WiFi 1010

Hey, folks, I found a Ubidots article written by Isabel Lopez. It details the process for sending data from the Arduino MKR WiFi 1010 devise to Ubidots. It works just fine however I am trying to integrate the necessary code to send a value coming from an attached Pressure Sensor.

Any help would be great.
Here is the article URL for referring to Isabel’s code.

Next Here is both her code and my code for constructing the data to send. I only need to send pressure_inhg at this time.

void loop(){

//ISABELS CODE START
char payload[200];
char str_val_1[30];

/4 is the total lenght of number,maximun number accepted is 99.99/
float value = analogRead(A0);
dtostrf(value, 4, 2, str_val_1);
sprintf(payload, “%s”,"");
sprintf(payload, “{”");
sprintf(payload, “%s%s”:%s", payload, VARIABLE_LABEL_1, str_val_1);
sprintf(payload, “%s}”, payload);
//ISABELS CODE END

//MY CODE START
int sensorVal=analogRead(A1);
float voltage = (sensorVal5.0)/1024.0;
float pressure_pascal = (3.0
((float)voltage-0.47))1000000.0;
float pressure_bar = pressure_pascal/10e5;
//FOR NOW I ONLY NEED TO SEND THE FOLLOWING VALUE - pressure_inhg
float pressure_inhg = pressure_bar
29.53;
float pressure_mpa = pressure_inhg/295;
//MY CODE END

//Send the payload to Ubidots
sendData(payload);
delay(5000);
}

Hello @kbnetguy

Here’s the appropriate loop to send your pressure value:

void loop() {
  char payload[200];
  char str_val_1[30];

  int sensorVal = analogRead(A1);
  float voltage = (float) sensorVal * 5.0 / 1024.0;
  float pressure_pascal = (voltage - 0.47) * 1000000.0 * 3.0;
  float pressure_bar = pressure_pascal / 10e5;

  // FOR NOW I ONLY NEED TO SEND THE FOLLOWING VALUE: pressure_inhg
  float pressure_inhg = pressure_bar * 29.53;

  /* 4 is the total lenght of number,maximun number accepted is 99.99 */
  dtostrf(pressure_inhg, 4, 2, str_val_1);
  sprintf(payload, "%s", "");
  sprintf(payload, "{\"");
  sprintf(payload, "%s%s\":%s", payload, VARIABLE_LABEL_1, str_val_1);
  sprintf(payload, "%s}", payload);

  //Send the payload to Ubidots
  sendData(payload);
  delay(5000);
}

That will get you sending the pressure_inhg variables to Ubidots.

–David

DSR Thanks for your help I have a new challenge that I was hoping you could assist with. I would like to send multiple variables for Example Sound, Humidity, Temperature and Distance. All Separate Ubidots Variables coming from the one single MKR wifi 1010 devise. Here is my code for generating each of the variables

void loop() {

char payload[200];
char str_val_1[30];
    delay(2000); // Delay so DHT-22 sensor can stabalize
  
    hum = dht.readHumidity();     // Get humidity value
    temp = dht.readTemperature(); // Get temperature value
    
    // Calculate the Speed of Sound in M/S
    soundsp = 331.4 + (0.606 * temp) + (0.0124 * hum);
    
    // Convert to cm/ms
    soundcm = soundsp / 10000;
    
  duration = sonar.ping_median(iterations);
  
  // Calculate the distance
  distance = (duration / 2) * soundcm;
    
  // Send results to Seria Monitor
  
  // Variables to send to UBIDOTS
    //Sound (soundsp), Humid (hum), Temp (temp), Distance (distance)
  
    Serial.print("Sound: ");
    Serial.print(soundsp);
    Serial.print(" m/s, ");
    Serial.print("Humid: ");
    Serial.print(hum);
    Serial.print(" %, Temp: ");
    Serial.print(temp);
    Serial.print(" C, ");
    Serial.print("Distance: ");
    
    if (distance >= 400 || distance <=2) {
      Serial.print("Out of Range");
    }
    else {
      Serial.print(distance);
      Serial.print(" cm");
      delay(500);
    }
  Serial.println(" ");

  sendData(payload);

  //delay(5000);

}

Hi @kbnetguy

You need to add the new variables to the payload. For example, I’m adding 2 variables using the same code I sent before:

 void loop() {
  char payload[200];
  char str_val_1[30];
  char str_val_2[30];

  int sensorVal = analogRead(A1);
  float voltage = (float) sensorVal * 5.0 / 1024.0;
  float pressure_pascal = (voltage - 0.47) * 1000000.0 * 3.0;
  float pressure_bar = pressure_pascal / 10e5;

  // FOR NOW I ONLY NEED TO SEND THE FOLLOWING VALUE: pressure_inhg
  float pressure_inhg = pressure_bar * 29.53;
  float value2 = 15.4;

  /* 4 is the total lenght of number,maximun number accepted is 99.99 */
  dtostrf(pressure_inhg, 4, 2, str_val_1);
  dtostrf(value2, 5, 2, str_val_2);
  sprintf(payload, "%s", "");
  sprintf(payload, "{\"");
  sprintf(payload, "%s%s\":%s,", payload, VARIABLE_LABEL_1, str_val_1);
  sprintf(payload, "%s\"%s\":%s", payload, VARIABLE_LABEL_2, str_val_2);
  sprintf(payload, "%s}", payload);

  //Send the payload to Ubidots
  sendData(payload);
  delay(5000);
}

You’d need to add the other variables to the JSON payload as I did with the second variable.

Furthermore, you might want to use our library for the Arduino MKR WiFi 1010 found at https://github.com/ubidots/ubidots-ArduinoMKR

–David

1 Like