Вы находитесь на странице: 1из 3

#include "DHT.

h"
#include <PubSubClient.h>
#include <ESP8266WiFi.h>
#include <Adafruit_Sensor.h>
#include "location.h"

#define WIFI_AP "Google Fiber" //replace "AP"


#define WIFI_PASSWORD "zxc12345678" //replace "PASSWORD"
#define TOKEN "IC1oTPMtKzcE3u4ZYkvb" //replace "TOKEN"

// DHT
int measurePin = A0;
int ledPower = D1;
#define DHTPIN D2 //replace D3 to your choice of datapin for DHT
#define DHTTYPE DHT22
int samplingTime = 280;
int deltaTime = 40;
int sleepTime = 9680;
float voMeasured = 0;
float calcVoltage = 0;
float dustDensity = 0;
float lat = 0;
float longi = 0;
char thingsboardServer[] = "demo.thingsboard.io";

WiFiClient wifiClient;

// Initialize DHT sensor.


DHT dht(DHTPIN, DHTTYPE);

PubSubClient client(wifiClient);

int status = WL_IDLE_STATUS;


unsigned long lastSend;

String geolocationKey = "AIzaSyDetHpzOz9I5nkJVfz25_b86XL5RH4z5bc";


String geocodingKey = "AIzaSyBfeUoRbNEnF8A3XIzHy3OMu-B3JFcQDYA";

int MCC = 502;


int MNC = 10;
String carrier = "Digi";

Location myLocation;

void setup()
{
Serial.begin(115200);
dht.begin();
delay(10);
InitWiFi();
pinMode(ledPower,OUTPUT);
myLocation.begin(geolocationKey, geocodingKey, MCC, MNC, carrier);
client.setServer( thingsboardServer, 1883 );
lastSend = 0;
}

void loop()
{
if ( !client.connected() ) {
reconnect();
}
if ( millis() - lastSend > 30000 ) {
digitalWrite(ledPower,LOW); // power on the LED
delayMicroseconds(samplingTime);
float h = dht.readHumidity();
float t = dht.readTemperature();
myLocation.getLocation();
voMeasured = analogRead(measurePin); // read the dust value
delayMicroseconds(deltaTime);
digitalWrite(ledPower,HIGH); // turn the LED off
delayMicroseconds(sleepTime);
calcVoltage = voMeasured * (3.3 / 1024);
dustDensity = ((0.17 * calcVoltage) - 0.1)*1000;
Serial.print("Latitude = "); Serial.println(myLocation.latitude,7);
Serial.print("Longitude = "); Serial.println(myLocation.longitude,7);
Serial.print("Accuracy = "); Serial.println(myLocation.accuracy);
Serial.print("Raw Signal Value (0-1023): ");
Serial.print(voMeasured);
Serial.print(" Voltage: ");
Serial.print(calcVoltage);
Serial.print(" Dust Density [ug/m3] : ");
Serial.println(dustDensity);
Serial.print("Value is: ");
Serial.println(lat);
Serial.println(longi);
String temperature = String(t);
String humidity = String(h);
String (lat) = String (myLocation.latitude);
String (longi) = String (myLocation.longitude);
String payload = "{";
payload += "\"dustdensity\":"; payload += dustDensity;payload += ",";
payload += "\"temperature\":"; payload += temperature;payload += ",";
payload += "\"humidity\":"; payload += humidity;payload += ",";
payload += "\"latitude\":"; payload += lat;payload += ",";
payload += "\"longitude\":"; payload += longi;payload += "}";
char attributes[100];
payload.toCharArray( attributes, 100 );
client.publish( "v1/devices/me/telemetry", attributes );
Serial.println( attributes );
//delay(10000);
lastSend = millis();
}
client.loop();
}
void InitWiFi()
{
Serial.println("Connecting to AP ...");
// attempt to connect to WiFi network

WiFi.begin(WIFI_AP, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected to AP");
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
status = WiFi.status();
if ( status != WL_CONNECTED) {
WiFi.begin(WIFI_AP, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected to AP");
}
Serial.print("Connecting to Thingsboard node ...");
// Attempt to connect (clientId, username, password)
if ( client.connect("gps", TOKEN, NULL) ) { //replace "Arduino Uno Demo
Device" with name of device in dashboard created
Serial.println( "[DONE]" );
} else {
Serial.print( "[FAILED] [ rc = " );
Serial.print( client.state() );
Serial.println( " : retrying in 5 seconds]" );
// Wait 5 seconds before retrying
delay( 3000 );
}
}
}