Skip to content

Instantly share code, notes, and snippets.

@ubi-gists
Created June 10, 2016 21:49
Show Gist options
  • Save ubi-gists/dafcad2e1f7ae5e9962d3615d964ce50 to your computer and use it in GitHub Desktop.
Save ubi-gists/dafcad2e1f7ae5e9962d3615d964ce50 to your computer and use it in GitHub Desktop.
// This example allow send data using UDP protocol
#include "Ubidots/Ubidots.h"
#define TOKEN "CCN8FrVulRYGulPTkbxxxxxxxxxxxxx" // Put here your Ubidots TOKEN
#define LED D0
#define VARIABLE_NAME "DUST"
#define DATA_SOURCE_LABEL "waveshare-dust-sensor"
#define DATA_SOURCE_NAME "Waveshare Dust Sensor"
#define COV_RATIO 0.2 //ug/mmm / mv
#define NO_DUST_VOLTAGE 400 //mv
#define SYS_VOLTAGE 3300
Ubidots ubidots(TOKEN);
float density, voltage;
int adcvalue;
void setup() {
Serial.begin(115200);
pinMode(LED, OUTPUT);
digitalWrite(LED, LOW);
ubidots.setDatasourceName(DATA_SOURCE_NAME);
ubidots.setDatasourceTag(DATA_SOURCE_LABEL);
}
void loop() {
digitalWrite(LED, HIGH);
delayMicroseconds(280);
int dust = analogRead(A0);
digitalWrite(LED, LOW);
adcvalue = Filter(dust);
/*
covert voltage (mv)
*/
voltage = (0.8 * adcvalue * 11);
/*
voltage to density
*/
if (voltage >= NO_DUST_VOLTAGE) {
voltage -= NO_DUST_VOLTAGE;
density = voltage * COV_RATIO;
}
else
density = 0;
ubidots.add(VARIABLE_NAME, density);
ubidots.sendAll();
delay(5000);
}
int Filter(int m)
{
static int flag_first = 0, _buff[10], sum;
const int _buff_max = 10;
int i;
if(flag_first == 0)
{
flag_first = 1;
for(i = 0, sum = 0; i < _buff_max; i++)
{
_buff[i] = m;
sum += _buff[i];
}
return m;
}
else
{
sum -= _buff[0];
for(i = 0; i < (_buff_max - 1); i++)
{
_buff[i] = _buff[i + 1];
}
_buff[9] = m;
sum += _buff[9];
i = sum / 10.0;
return i;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment