im also can
t send data to ubidots.
Using ESP32 a a code really similar of the one mariahernandez desccribe.
/****************************************
* Include Libraries
****************************************/
#include <WiFi.h>
#include <PubSubClient.h>
#include <ESPmDNS.h>
// BME280 PARAMETERS
#include <Adafruit_BME280.h>
#define I2C_SDA 21
#define I2C_SCL 22
#define BME280_ADD 0x76
#define SEALEVELPRESSURE_HPA (1026.0)
Adafruit_BME280 bme(I2C_SDA, I2C_SCL);
#include <Adafruit_Sensor.h>
#include <Adafruit_TSL2561_U.h>
Adafruit_TSL2561_Unified tsl = Adafruit_TSL2561_Unified(TSL2561_ADDR_FLOAT, 12345);
void displaySensorDetails(void)
{
sensor_t sensor;
tsl.getSensor(&sensor);
Serial.println("------------------------------------");
Serial.print ("Sensor: "); Serial.println(sensor.name);
Serial.print ("Driver Ver: "); Serial.println(sensor.version);
Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id);
Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println(" lux");
Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println(" lux");
Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println(" lux");
Serial.println("------------------------------------");
Serial.println("");
delay(500);
}
void configureSensor(void)
{
/* You can also manually set the gain or enable auto-gain support */
// tsl.setGain(TSL2561_GAIN_1X); /* No gain ... use in bright light to avoid sensor saturation */
// tsl.setGain(TSL2561_GAIN_16X); /* 16x gain ... use in low light to boost sensitivity */
tsl.enableAutoRange(true); /* Auto-gain ... switches automatically between 1x and 16x */
/* Changing the integration time gives you better sensor resolution (402ms = 16-bit data) */
//tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_13MS); /* fast but low resolution */
tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_101MS); /* medium resolution and speed */
//tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_402MS); /* 16-bit data but slowest conversions */
/* Update these values depending on what you've set above! */
Serial.println("------------------------------------");
Serial.print ("Gain: "); Serial.println("Auto");
Serial.print ("Timing: "); Serial.println("101 ms");
Serial.println("------------------------------------");
}
#define ANALOG_PIN_0 34
#define ANALOG_PIN_1 35
#define co2Zero 40 //calibrated CO2 0 level
int co2ppm = 0;
int pino_sensor = 35;
int valor_sensor = 0;
int posicao;
String UV_index = "0";
String UV = "0";
//declare variables
float temperature;
float pressure;
float altitude;
float humidity;
float co2;
float uvindexx;
float light;
/****************************************
* Define Constants
****************************************/
namespace {
const char * WIFISSID = "N9124G"; // Put your WifiSSID here
const char * PASSWORD = "13031976"; // Put your wifi password here
const char * TOKEN = "A1E-bpaq4yuQvKspTNWr8o29pyYZFKdAi9"; // Put your Ubidots' TOKEN
const char * MQTT_CLIENT_NAME = "43de1354LAm"; // MQTT client Name, please enter your own 8-12 alphanumeric character ASCII string;
const char * DEVICE_LABEL = "esp32"; // My Device Label
const char * VARIABLE_LABEL_1 = "temperature"; // My Variable Label
const char * VARIABLE_LABEL_2 = "humidity";
const char * VARIABLE_LABEL_3 = "pressure"; // Assing the variable label
const char * VARIABLE_LABEL_4 = "altitude"; // Assing the variable label
const char * VARIABLE_LABEL_5 = "co2"; // Assing the variable label
const char * VARIABLE_LABEL_6 = "uv"; // Assing the variable label
const char * VARIABLE_LABEL_7 = "luz"; // Assing the variable label
const char * MQTT_BROKER = "things.ubidots.com";
}
/* Space to store the request */
char payload[300];
char topic[50];
/* Space to store values to send */
char str_sensor[10];
char str_tempSensor[10];
char str_humSensor[10];
char str_presSensor[10];
char str_altSensor[10];
char str_co2Sensor[10];
char str_uvSensor[10];
char str_lightSensor[10];
unsigned long previousMillis_one = 0; // store the last time updated
long one_minute_interval = 1*60*1000UL; // interval desired (millisenconds)
/****************************************
* Auxiliar Functions
****************************************/
WiFiClient ubidots;
PubSubClient client(ubidots);
void callback(char* topic, byte* payload, unsigned int length) {
char p[length + 1];
memcpy(p, payload, length);
p[length] = NULL;
String message(p);
Serial.write(payload, length);
Serial.println(topic);
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.println("Attempting MQTT connection...");
// Attemp to connect
if (client.connect(MQTT_CLIENT_NAME, TOKEN, "")) {
Serial.println("Connected");
} else {
Serial.print("Failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 2 seconds");
// Wait 2 seconds before retrying
delay(2000);
}
}
}
/****************************************
* Main Functions
****************************************/
void setup() {
Serial.begin(9600);
WiFi.begin(WIFISSID, PASSWORD);
/* Assign the PINS as INPUT/OUTPUT */
pinMode(ANALOG_PIN_0,INPUT); //MQ135 analog feed set for input
pinMode(pino_sensor, INPUT); //VM30A nalog feed set for input
Serial.println();
Serial.print("Wait for WiFi...");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.println("WiFi Connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
client.setServer(MQTT_BROKER, 1883);
client.setCallback(callback);
}
void loop() {
if (!client.connected()) {
reconnect();
}
/* Reading sensors */
unsigned long currentMillis = millis();
getCO2();
getValuesBME280();
int stringLength = 0;
UV = Calcula_nivel_UV();
float uvindexx = UV.toFloat();
getTSL2561();
//* 4 is mininum width, 2 is precision; float value is copied onto str_sensor*/
dtostrf(temperature, 4, 2, str_tempSensor);
dtostrf(humidity, 3, 2, str_humSensor);
dtostrf(pressure, 4, 2, str_presSensor);
dtostrf(altitude, 4, 2, str_altSensor);
dtostrf(co2, 1, 0, str_co2Sensor);
dtostrf(uvindexx, 1, 0, str_uvSensor);
dtostrf(light, 1, 0, str_lightSensor);
sprintf(topic, "%s%s", "/v1.6/devices/", DEVICE_LABEL);
if (currentMillis - previousMillis_one > one_minute_interval) {
previousMillis_one = currentMillis;
Serial.println("One minute interval");
sprintf(payload, "{\"");
sprintf(payload, "%s%s\":%s", payload, VARIABLE_LABEL_1, str_tempSensor); // Adds the variable label
sprintf(payload, "%s,\"%s\":%s", payload, VARIABLE_LABEL_2, str_humSensor); // Adds the variable label
sprintf(payload, "%s,\"%s\":%s", payload, VARIABLE_LABEL_3, str_presSensor); // Adds the variable label
sprintf(payload, "%s,\"%s\":%s", payload, VARIABLE_LABEL_4, str_altSensor); // Adds the variable label
sprintf(payload, "%s}", payload);
Serial.println();
Serial.println(topic);
Serial.println(payload);
client.publish(topic, payload);
Serial.println();
delay(5000);
sprintf(topic, "%s%s", "/v1.6/devices/", DEVICE_LABEL);
sprintf(payload, "%s", ""); // Cleans the payload
sprintf(payload, "{\"");
sprintf(payload, "%s%s\":%s", payload, VARIABLE_LABEL_5, str_co2Sensor); // Adds the variable label
sprintf(payload, "%s,\"%s\":%s", payload, VARIABLE_LABEL_6, str_uvSensor); // Adds the variable label
sprintf(payload, "%s,\"%s\":%s", payload, VARIABLE_LABEL_7, str_lightSensor); // Adds the variable label
sprintf(payload, "%s}", payload);
Serial.println();
Serial.println(topic);
Serial.println(payload);
client.publish(topic, payload);
Serial.println();
client.loop();
}
}
void getCO2()
{
int co2now[10]; //int array for co2 readings
int co2raw = 0; //int for raw value of co2
int co2comp = 0; //int for compensated co2
int co2ppm = 0; //int for calculated ppm
int zzz = 0; //int for averaging
for (int x = 0;x<10;x++){ //samplpe co2 10x over 2 seconds
co2now[x]=analogRead(ANALOG_PIN_0);
}
for (int x = 0;x<10;x++){ //add samples together
zzz=zzz + co2now[x];
}
co2raw = zzz/10; //divide samples by 10
co2comp = co2raw - co2Zero; //get compensated value
co2ppm = map(co2comp,0,1023,400,5000); //map value for atmospheric levels
Serial.print("CO2 ppm value : ");
Serial.print(co2ppm); //print co2 ppm
co2 = co2ppm;
Serial.println(" PPM"); //print units
Serial.println();
}
void getValuesBME280()
{
bme.begin(BME280_ADD);
temperature = bme.readTemperature();
pressure = bme.readPressure() / 100.0F;
altitude = bme.readAltitude(SEALEVELPRESSURE_HPA);
humidity = bme.readHumidity();
Serial.print("Temperature = ");
Serial.print(temperature);
Serial.println(" ℃");
Serial.print("Pressure = ");
Serial.print(pressure);
Serial.println(" hPa");
Serial.print("Approx. Altitude = ");
Serial.print(altitude);
Serial.println(" m");
Serial.print("Humidity = ");
Serial.print(humidity);
Serial.println(" %");
Serial.println();
}
String Calcula_nivel_UV()
{
String uvindex = "0";
int valor_sensor = 0;
valor_sensor = analogRead(pino_sensor);
//Calcula tensao em milivolts
int tensao = (valor_sensor * (5.0 / 4095.0)) * 1000; //precisa checar
//Compara com valores tabela UV_Index
if (tensao < 50)
{
uvindex = "0";
}
else if (tensao > 50 && tensao <= 227)
{
uvindex = "0";
}
else if (tensao > 227 && tensao <= 318)
{
uvindex = "1";
}
else if (tensao > 318 && tensao <= 408)
{
uvindex = "2";
}
else if (tensao > 408 && tensao <= 503)
{
uvindex = "3";
}
else if (tensao > 503 && tensao <= 606)
{
uvindex = "4";
}
else if (tensao > 606 && tensao <= 696)
{
uvindex = "5";
}
else if (tensao > 696 && tensao <= 795)
{
uvindex = "6";
}
else if (tensao > 795 && tensao <= 881)
{
uvindex = "7";
}
else if (tensao > 881 && tensao <= 976)
{
uvindex = "8";
}
else if (tensao > 976 && tensao <= 1079)
{
uvindex = "9";
}
else if (tensao > 1079 && tensao <= 1170)
{
uvindex = "10";
}
else if (tensao > 1170)
{
uvindex = "11";
}
Serial.println(uvindex);
Serial.println (analogRead(pino_sensor));
Serial.println();
return uvindex;
}
void getTSL2561()
{
sensors_event_t event;
tsl.getEvent(&event);
light = event.light;
Serial.print(event.light); Serial.println(" lux");
Serial.println();
}
Already tried set payload of all variables in one time, one by one, nothing works.
Please help appreciated.