Advertisement
D0cEvil

Arduino - Meteostation

Dec 27th, 2022
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.24 KB | Source Code | 0 0
  1. #include <LiquidCrystal.h>
  2. #include <Wire.h>
  3. #include <Adafruit_BMP085.h>
  4. #include <SPI.h>
  5. #include <Ethernet.h>
  6. #include <HTTPClient.h>
  7. #include <Cosm.h>
  8. #include <DHT.h>
  9.  
  10. //  Ethernet Variables
  11.  
  12. byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEF };  // MAC address for the ethernet controller.
  13. byte server[] = { 173,203,98,29 };                     // address of the server you want to connect to (pachube.com):
  14. EthernetClient client;
  15.  
  16. //DHT22 Variables
  17.  
  18. #define DHTPIN 2
  19. #define DHTTYPE DHT22
  20.  
  21. // Pachube Variables
  22.  
  23. #define API_KEY     "API ключ"
  24. #define FEED_ID Здесь FEED ID
  25. char cosmKey[] = "ключ Cosm";
  26.  
  27. unsigned long lastConnectionTime = 0;                // last time we connected to Cosm
  28. const unsigned long connectionInterval = 15000;      // delay between connecting to Cosm in milliseconds
  29.  
  30. // Initialize the Cosm library
  31.  
  32. // Define the string for our datastream ID
  33.  
  34. char sensorId[] = "Temperature";
  35. char bufferId[] = "Pressure";
  36. char sensorId2[] = "Humidity";
  37.  
  38. //String stringId("random_string");
  39.  
  40. const int bufferSize = 140;
  41. char bufferValue[bufferSize]; // enough space to store the string we're going to send
  42.  
  43. CosmDatastream datastreams[] = {
  44.   CosmDatastream(sensorId, strlen(sensorId), DATASTREAM_FLOAT),
  45.   CosmDatastream(bufferId, strlen(bufferId), DATASTREAM_FLOAT),
  46.   CosmDatastream(sensorId2, strlen(sensorId2), DATASTREAM_FLOAT),
  47. };
  48.  
  49. // Wrap the datastream into a feed
  50. //CosmFeed feed(FEED_ID, datastreams, 1 /* number of datastreams */);
  51.  
  52. CosmFeed feed(FEED_ID, datastreams, 3 /* number of datastreams */);
  53. CosmClient cosmclient(client);
  54.  
  55. Adafruit_BMP085 bmp;
  56. DHT dht(DHTPIN, DHTTYPE);
  57.  
  58. int a = 0; // из float делаем int далее в коде
  59. int i = 0; // счетчик циклов
  60.  
  61. byte man_with_hat[8] =
  62. {
  63.   B01100,
  64.   B10010,
  65.   B10010,
  66.   B01100,
  67.   B00000,
  68.   B00000,
  69.   B00000,
  70.   B00000,
  71. };
  72.  
  73. void setup()
  74. {
  75.  
  76.   Serial.begin(9600);
  77.   bmp.begin();
  78.   dht.begin();
  79.   Serial.println("Initializing network");
  80.   while (Ethernet.begin(mac) != 1)
  81.   {
  82.     Serial.println("Error getting IP address via DHCP, trying again...");
  83.     delay(15000);
  84.   }
  85.    Serial.println("Network initialized");
  86.    Serial.println();
  87. }
  88.  
  89. void loop()
  90. {
  91.   float h = dht.readHumidity();
  92.   float t = dht.readTemperature();
  93.  
  94.   // check if returns are valid, if they are NaN (not a number) then something went wrong!
  95.   if (isnan(t) || isnan(h)) {
  96.     Serial.println("Failed to read from DHT");
  97.   } else {
  98.     Serial.print("Humidity: ");
  99.     Serial.print(h);
  100.     Serial.print(" %\t");
  101.     Serial.print("Temperature: ");
  102.     Serial.print(t);
  103.     Serial.println(" *C");
  104.   }
  105.   i = i + 1;
  106.   LiquidCrystal lcd(4, 5, 9, 10, 11, 12);
  107.   lcd.createChar(1, man_with_hat);
  108.   lcd.begin(20, 4);
  109.   lcd.print("t = ");
  110.   lcd.print(bmp.readTemperature());
  111.   lcd.print(" \1");
  112.   lcd.print("C");
  113.   lcd.setCursor(0, 1);
  114.   lcd.print("P = ");
  115.   lcd.print((bmp.readPressure()/133.3));
  116.   lcd.print(" mm Hg");
  117.   lcd.setCursor(0, 2);
  118.  
  119.   //АЛЬТИМЕТР (НУЖДАЕТСЯ В КОРРЕКЦИИ)
  120.  
  121.   /*
  122.   lcd.print("Alt = ");
  123.   lcd.print(bmp.readAltitude(101500));
  124.   lcd.print(" m");
  125.   */
  126.  
  127.   //ВЫВОД НА ЭКРАН ЗНАЧЕНИЯ ВЛАЖНОСТИ
  128.   lcd.print("Hum = ");
  129.   lcd.print(h);
  130.   lcd.print(" %");
  131.   /*
  132.  
  133.   //ВЫВОД НОМЕРА ИТЕРАЦИИ
  134.  
  135.   lcd.setCursor(0, 3);
  136.   lcd.print("i = ");
  137.   lcd.print(i);
  138.   Serial.println(i);
  139.   */
  140.   delay(10000);
  141.    
  142.   if (i == 5) // задержка отправки данных на Cosm
  143.   {
  144.     lcd.setCursor(0, 3);
  145.     lcd.print("Uploading it to Cosm");
  146.    
  147.     datastreams[0].setFloat(bmp.readTemperature());
  148.  
  149.     Serial.print("Read sensor value ");
  150.     Serial.println(datastreams[0].getFloat());
  151.    
  152.     a = (bmp.readPressure()/133.3);
  153.  
  154.     datastreams[1].setFloat(a);
  155.     datastreams[2].setFloat(h);
  156.  
  157.     Serial.print("Read sensor value ");
  158.     Serial.println(datastreams[1].getFloat());
  159.     Serial.println("Uploading it to Cosm");
  160.     int ret = cosmclient.put(feed, cosmKey);
  161.     Serial.print("cosmclient.put returned ");
  162.     Serial.println(ret);
  163.     Serial.println();
  164.     i = 0;
  165.   }
  166.  
  167.   /*  
  168.   Время задержки в мс
  169.   1 сек = 1000 мс
  170.   1 мин = 6000 мс
  171.   */
  172. //  delay(15000);
  173. }
Tags: Arduino
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement