Ubidots Community

[SOLVED] BMP280 + Ubidots + Ethernet Shield

[SOLVED]

replaced analogRead(a0) with bme.readTemperature()

Hello,

I am trying to send data to ubidots from BMP280 sensor using an ethernet shield, and the sad part is my programming is really bad.

so far i have been able to mock up the following some code by editing code available for DHT11 sensor.
(i have replaced my token id and var id to paste the code here) also all the ethernet details are accurate.

#include <Ethernet.h
#include <SPI.h
#include <UbidotsEthernet.h
#include <Wire.h
#include <SPI.h
#include <Adafruit_Sensor.h
#include <Adafruit_BMP280.h

#define BMP_SCK 13
#define BMP_MISO 12
#define BMP_MOSI 11 
#define BMP_CS 10


String tempVarId = "*id here*";
String preVarId = "*id here*";
String sltVarId = "*id here*";
String token = "*id here*";

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
Adafruit_BMP280 bme(BMP_CS, BMP_MOSI, BMP_MISO,  BMP_SCK);
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

// Set the static IP address to use if the DHCP fails to assign
char server[]="things.ubidots.com";
 
EthernetClient client;
IPAddress ip(192, 168, 1, 60); // Arduino IP Add
IPAddress myDns(192, 168, 1, 1);
IPAddress myGateway(192, 168, 1 ,1);


void setup()

{
    Serial.begin(9600);
    Serial.print("Here");
    bme.begin();
    // start the Ethernet connection:
    if (Ethernet.begin(mac) == 0) {
      Serial.println("Failed to configure Ethernet using DHCP");
      // try to congifure using IP address instead of DHCP:
      Ethernet.begin(mac, ip);
    }
    // give the Ethernet shield a second to initialize:
    delay(1000);
}


void loop()
{



float p = bme.readPressure();
float t = bme.readTemperature();
float a = bme.readAltitude(1013.25);

    Serial.print(F("Temperature = "));
    Serial.print(t);
    Serial.println(" *C");
    
    Serial.print(F("Pressure = "));
    Serial.print(p);
    Serial.println(" Pa");

    Serial.print(F("Approx altitude = "));
    Serial.print(a); // this should be adjusted to your local forcase
    Serial.println(" m");
    Serial.println();
    save_value (t, p, a);
    delay(2000);

}

void save_value(float tempValue, float preValue, float altValue)

{

Serial.println("Sending data...");
  // if you get a connection, report back via serial:
  int num = 0;
  delay(2000);

 String varString = "[{\"variable\": \"" + tempVarId + "\", \"value\":" + String(tempValue) + "}";
  varString += ",{\"variable\": \"" + preVarId + "\", \"value\":" + String(preValue) + "}]";
  
  num = varString.length();

if (client.connect(server,80)) {
    //client.println("POST /api/v1.6/variables/"+tempVarId+"/values HTTP/1.1");
    //Serial.println("POST /api/v1.6/variables/"+tempVarId+"/values HTTP/1.1");
    client.println("POST /api/v1.6/collections/values HTTP/1.1");
    Serial.println("POST /api/v1.6/collections/values HTTP/1.1");
    client.println("Content-Type: application/json");
    Serial.println("Content-Type: application/json");
    client.println("Content-Length: "+String(num));
    Serial.println("Content-Length: "+String(num));
    client.println("X-Auth-Token: "+token);
    Serial.println("X-Auth-Token: "+token);
    client.println("Host: things.ubidots.comn");
    Serial.println("Host: things.ubidots.comn");
    client.print(varString);
   Serial.print(varString+"n");
  }

else

{
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }

boolean sta = client.connected();
  Serial.println("Connection ["+String(sta)+"]");
  if (!client.connected())   {
   Serial.println();
   Serial.println("disconnecting.");
   client.stop();
  }

Serial.println("Reading..");
  while (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

client.flush();
  client.stop();
  
  }

i am getting sensor output on the serial console ( inaccurate in this code but accurate using the example sketch for BMP280 (code below))

/***************************************************************************
This is a library for the BMP280 humidity, temperature & pressure sensor

Designed specifically to work with the Adafruit BMEP280 Breakout
----> http://www.adafruit.com/products/2651

These sensors use I2C or SPI to communicate, 2 or 4 pins are required
to interface.

Adafruit invests time and resources providing this open source code,
please support Adafruit andopen-source hardware by purchasing products
from Adafruit!

Written by Limor Fried & Kevin Townsend for Adafruit Industries.
BSD license, all text above must be included in any redistribution
***************************************************************************/

#include <Wire.h
#include <SPI.h
#include <Adafruit_Sensor.h
#include <Adafruit_BMP280.h

#define BMP_SCK 13
#define BMP_MISO 12
#define BMP_MOSI 11 
#define BMP_CS 10

Adafruit_BMP280 bme; // I2C
//Adafruit_BMP280 bme(BMP_CS); // hardware SPI
//Adafruit_BMP280 bme(BMP_CS, BMP_MOSI, BMP_MISO,  BMP_SCK);

void setup() {
  Serial.begin(9600);
  Serial.println(F("BMP280 test"));
  
  if (!bme.begin()) {  
    Serial.println(F("Could not find a valid BMP280 sensor, check wiring!"));
    while (1);
  }
}

void loop() {
    Serial.print(F("Temperature = "));
    Serial.print(bme.readTemperature());
    Serial.println(" *C");
    
    Serial.print(F("Pressure = "));
    Serial.print(bme.readPressure());
    Serial.println(" Pa");

    Serial.print(F("Approx altitude = "));
    Serial.print(bme.readAltitude(1013.25)); // this should be adjusted to your local forcase
    Serial.println(" m");
    
    Serial.println();
    delay(2000);
}

this is the following examplel code from the ubidots library. can someone please tell me how would i use it with my sensor if i have to send 3 values namely temprature, pressure and altiti=ude.

#include <Ethernet.h
#include <SPI.h
#include <UbidotsEthernet.h
#define ID  "Your_variable_ID_here"  // Put here your Ubidots variable ID
#define ID2 "Your_variable_ID2_here"
#define ID3 "Your_variable_ID3_here"
#define ID4 "Your_variable_ID4_here"
#define TOKEN  "Your_token_here"  // Put here your Ubidots TOKEN

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 0, 177);

Ubidots client(TOKEN);

void setup(){
    Serial.begin(9600);
    // start the Ethernet connection:
    if (Ethernet.begin(mac) == 0) {
      Serial.println("Failed to configure Ethernet using DHCP");
      // try to congifure using IP address instead of DHCP:
      Ethernet.begin(mac, ip);
    }
    // give the Ethernet shield a second to initialize:
    delay(1000);
}
void loop(){
    float value = analogRead(A0);
    float value2 = analogRead(A1);
    float value3 = analogRead(A2);
    float value4 = analogRead(A3);
    client.add(ID, value);
    client.add(ID2, value2);
    client.add(ID3, value3);
    client.add(ID4, value4);
    client.sendAll();
}

Thank You Help Appreciated.

Hello @quackquackquack,

First, you have to follow our example to send multiple values to Ubidots once you’re able to send data to Ubidots, I recommend you follow the code structure from Adafruit sensor and integrate it on our example.

Remeber the important’s points to inicializate a sensor. Assign the pins of the sensor, inicializate the sensor, call the reading functions.

At the moment I don’t have a BMP280 for make a test and verify if I’m doing well, but following the structures you should have something like this:

#include <Ethernet.h>
#include <SPI.h>
#include <UbidotsEthernet.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>

#define ID  "Your_variable_ID_here"  // Put here your Ubidots variable ID
#define ID2 "Your_variable_ID2_here"

#define TOKEN  "Your_token_here"  // Put here your Ubidots TOKEN

#define BMP_SCK 13
#define BMP_MISO 12
#define BMP_MOSI 11 
#define BMP_CS 10

Adafruit_BMP280 bme; // I2C
//Adafruit_BMP280 bme(BMP_CS); // hardware SPI
//Adafruit_BMP280 bme(BMP_CS, BMP_MOSI, BMP_MISO,  BMP_SCK);

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 0, 177);

Ubidots client(TOKEN);

void setup(){
    Serial.begin(9600);
    // start the Ethernet connection:
    if (Ethernet.begin(mac) == 0) {
      Serial.println("Failed to configure Ethernet using DHCP");
      // try to congifure using IP address instead of DHCP:
      Ethernet.begin(mac, ip);
    }
    Serial.println(F("BMP280 test"));
  
    if (!bme.begin()) {  
    Serial.println(F("Could not find a valid BMP280 sensor, check wiring!"));
    while (1);
  }
    // give the Ethernet shield a second to initialize:
    delay(1000);
}
void loop(){
    float value = bme.readTemperature();
    float value2 = bme.readPressure();
    client.add(ID, value);
    client.add(ID2, value2);
    client.sendAll();
}

I hope would help you!

Best Regards.
Maria C.

Yes, Thank You! i fiddled around and solved it. i just had to replace analogRead(A0) with bme.readTemprature and it worked.