Guest User

Untitled

a guest
Jan 12th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.03 KB | None | 0 0
  1. //Knihovny:
  2.  
  3. #include <Adafruit_BMP085.h>
  4. #include <Adafruit_Sensor.h>
  5. #include <DHT.h>
  6. #include <DHT_U.h>
  7. #include <BH1750.h>
  8. #include <ESP8266WiFi.h>
  9. #include <PubSubClient.h>
  10. #include <Wire.h>
  11.  
  12. //Inicializace Wi-Fi pripojeni:
  13.  
  14. #define WIFI_NAZEV "XXXXX"
  15. #define WIFI_KLIC "XXXXX"
  16.  
  17. WiFiClient WiFiklient;
  18. PubSubClient klient(WiFiklient);
  19. int status = WL_IDLE_STATUS;
  20. unsigned long lastSend;
  21.  
  22. //Inicializace pripojeni k ThingsBoard:
  23.  
  24. #define TOKEN "XXXXX"
  25. char ThingsBoardServer [] = "demo.thingsboard.io";
  26.  
  27. //Inicializace senzoru:
  28.  
  29. #define DHTTYPE DHT22
  30. #define DHTPIN 3
  31. DHT teplomer(DHTPIN, DHTTYPE);
  32.  
  33. Adafruit_BMP085 tlakomer;
  34.  
  35. BH1750 luxmetr;
  36.  
  37. void setup(){
  38.   Serial.begin(115200);
  39.   teplomer.begin();
  40.   tlakomer.begin();
  41.   luxmetr.begin();
  42.   delay(10);
  43.  
  44.   Serial.println("Pripojovani...");
  45.   WiFi.begin(WIFI_NAZEV, WIFI_KLIC);
  46.   while(WiFi.status() != WL_CONNECTED){
  47.     delay(500);
  48.     Serial.println(".");
  49.   }
  50.   Serial.print("Pripojeno k: ");
  51.   Serial.println(WIFI_NAZEV);
  52.   Serial.print("IP adresa: ");
  53.   Serial.println(WiFi.localIP());
  54.   Serial.println("MAC adresa: ");
  55.  
  56.   klient.setServer(ThingsBoardServer, 1883);
  57.   lastSend = 0;
  58. }
  59.  
  60. void loop(){
  61.   if(!klient.connected()){
  62.     while(!klient.connected()){
  63.       status = WiFi.status();
  64.       if(status != WL_CONNECTED){
  65.         WiFi.begin(WIFI_NAZEV, WIFI_KLIC);
  66.         while(WiFi.status() != WL_CONNECTED){
  67.           delay(500);
  68.           Serial.print(".");
  69.         }
  70.         Serial.println("Pripojeno");
  71.       }
  72.       Serial.print("Pripojovani k ThingsBoardu...");
  73.       if(klient.connect("MOPSIKOSTANICE #1", TOKEN, NULL)){
  74.         Serial.println("Pripojeno");
  75.       }else{
  76.         Serial.print("Chyba: ");
  77.         Serial.print(klient.state());
  78.         Serial.println("Opakuji za 5 sekund...");
  79.         delay(5000);
  80.       }
  81.     }
  82.   }
  83.  
  84.   if(millis() - lastSend > 1000){
  85.     float teplota = teplomer.readTemperature();
  86.     float vlhkost = teplomer.readHumidity();
  87.     float tlak = tlakomer.readPressure();
  88.     float tlak_hpa;
  89.     uint16_t svetlo = luxmetr.readLightLevel();
  90.  
  91.     Serial.println("Teplota vzduchu: "+String(teplota)+" st. C");
  92.     Serial.println("Relativni vlhkost vzduchu: "+String(vlhkost)+" %");
  93.     tlak_hpa = tlak/100;
  94.     Serial.println("Atmosfericky tlak: "+String(tlak_hpa)+" hPa");
  95.     Serial.println("Intenzita svetla: "+String(svetlo)+" lx");
  96.  
  97.     String Teplota = String(teplota);
  98.     String Vlhkost = String(vlhkost);
  99.     String Tlak = String(tlak_hpa);
  100.     String Svetlo = String(svetlo);
  101.  
  102.     String payload = "{";
  103.     payload += "\"Teplota\":"; payload += Teplota; payload += ",";
  104.     payload += "\"Vlhkost\":"; payload += Vlhkost; payload += ",";
  105.     payload += "\"Tlak\":"; payload += Tlak; payload += ",";
  106.     payload += "\"Svetlo\":"; payload += Svetlo;
  107.     payload += "}";
  108.  
  109.     char attributes[100];
  110.     payload.toCharArray(attributes, 100);
  111.     klient.publish("vl/devices/me/telemetry", attributes);
  112.     Serial.println(attributes);
  113.    
  114.     lastSend = millis();
  115.   }
  116.   klient.loop();
  117. }
Advertisement
Add Comment
Please, Sign In to add comment