Advertisement
pleasedontcode

Pet Tracking

Nov 17th, 2024
452
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 1.96 KB | Source Code | 0 0
  1. #include <Wire.h>
  2. #include <Adafruit_Sensor.h>
  3. #include <DHT.h>
  4. #include <TinyGPS++.h>
  5. #include <WiFi.h>
  6.  
  7. // Define pin numbers
  8. #define DHTPIN 4
  9. #define DHTTYPE DHT22
  10. DHT dht(DHTPIN, DHTTYPE);
  11.  
  12. WiFiClient client;
  13. TinyGPSPlus gps;
  14.  
  15. const char* ssid = "your_network_name";
  16. const char* password = "your_network_password";
  17. const char* server = "your_cloud_server.com"; // e.g., ThingSpeak or Firebase
  18.  
  19. void setup() {
  20.   Serial.begin(115200);
  21.   WiFi.begin(ssid, password);
  22.  
  23.   while (WiFi.status() != WL_CONNECTED) {
  24.     delay(1000);
  25.     Serial.println("Connecting to WiFi...");
  26.   }
  27.   Serial.println("Connected to WiFi");
  28.  
  29.   dht.begin();
  30. }
  31.  
  32. void loop() {
  33.   // GPS data reading
  34.   while (Serial.available() > 0) {
  35.     gps.encode(Serial.read());
  36.   }
  37.  
  38.   // Get data from GPS
  39.   if (gps.location.isUpdated()) {
  40.     float latitude = gps.location.lat();
  41.     float longitude = gps.location.lng();
  42.     Serial.print("Latitude= "); Serial.print(latitude, 6);
  43.     Serial.print(" Longitude= "); Serial.println(longitude, 6);
  44.   }
  45.  
  46.   // Get pet activity from accelerometer (simplified)
  47.   int x = analogRead(A0);  // Just a placeholder, use accelerometer API here
  48.   int y = analogRead(A1);
  49.   int z = analogRead(A2);
  50.  
  51.   // Get temperature and humidity data
  52.   float temp = dht.readTemperature();
  53.   float humidity = dht.readHumidity();
  54.  
  55.   // Send data to the cloud (simplified)
  56.   if (client.connect(server, 80)) {
  57.     client.print("GET /update?latitude=");
  58.     client.print(gps.location.lat());
  59.     client.print("&longitude=");
  60.     client.print(gps.location.lng());
  61.     client.print("&temperature=");
  62.     client.print(temp);
  63.     client.print("&humidity=");
  64.     client.print(humidity);
  65.     client.print("&activity_x=");
  66.     client.print(x);
  67.     client.print("&activity_y=");
  68.     client.print(y);
  69.     client.print("&activity_z=");
  70.     client.print(z);
  71.     client.println(" HTTP/1.1");
  72.   }
  73.  
  74.   delay(1000); // Adjust delay based on the sampling rate you want
  75. }
  76.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement