Advertisement
snaggedagge

Arduino coventry

May 22nd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.35 KB | None | 0 0
  1. #include <time.h>
  2. #include <Ticker.h>
  3. #include <SPI.h>
  4. #include <SD.h>
  5. #include <SoftwareSerial.h>
  6. #include <ESP8266WiFi.h>
  7. #include <NTPClient.h>
  8. #include <WiFiUdp.h>
  9. #include "GPIOWrite.h"
  10.  
  11.  
  12. #include <OneWire.h>
  13. #include <DallasTemperature.h>
  14.  
  15. #include "Adafruit_IO_Client.h"
  16.  
  17. #define WIND_SPEED_SENSOR_PIN 4
  18. #define LED_BUILTIN_CUSTOM_PIN 2
  19. #define TEMPERATURE_SENSOR_PIN 0
  20. #define GPS_RX_PIN 16
  21. #define GPS_TX_PIN 5
  22.  
  23.  
  24. // Configure Adafruit IO access.
  25. #define AIO_KEY    "75aa2cfa136d4645802240e566424303"
  26.  
  27. const char* wifiSsid     = "Lewis' hot place";
  28. const char* wifiPassword = "hellom88";
  29. const char READINGS_FILENAME[] = "readings.txt";
  30.  
  31.  
  32.  
  33.  
  34. WiFiClient client;
  35.  
  36.  
  37. Adafruit_IO_Client aio = Adafruit_IO_Client(client, AIO_KEY);
  38.  
  39. Adafruit_IO_Feed temperatureFeed = aio.getFeed("temperature");
  40. Adafruit_IO_Feed windspeedFeed = aio.getFeed("windspeed");
  41. Adafruit_IO_Feed timeFeed = aio.getFeed("timefeed");
  42.  
  43. // Used for temperature sensor
  44. OneWire oneWire(TEMPERATURE_SENSOR_PIN);
  45. DallasTemperature sensors(&oneWire);
  46.  
  47. Ticker measurementTimer;
  48.  
  49. //Used for time syncing
  50. WiFiUDP ntpUDP;
  51. NTPClient timeClient(ntpUDP);
  52.  
  53. GPIOWrite workingLed(LED_BUILTIN_CUSTOM_PIN);
  54.  
  55. SoftwareSerial gps(GPS_RX_PIN, GPS_TX_PIN);
  56.  
  57. int interruptCounter = 0;
  58.  
  59. int lastWindspeed = 0;
  60. float temperature = 0;
  61. time_t timestamp = 0;
  62.  
  63. void setup() {
  64.   Serial.begin(9600);
  65.   gps.begin(9600);
  66.   WiFi.begin(wifiSsid, wifiPassword);
  67.   timeClient.begin(); // MAKE SURE THIS DONT CRASH EVERYTHING
  68.   sensors.begin();
  69.   aio.begin();
  70.   if (!SD.begin(4)) {
  71.     Serial.println("initialization failed!");
  72.     return;
  73.   }
  74.   pinMode(WIND_SPEED_SENSOR_PIN, INPUT_PULLUP);
  75.   measurementTimer.attach(10, measureWindspeed);
  76.   attachInterrupt(digitalPinToInterrupt(WIND_SPEED_SENSOR_PIN), handleInterrupt, FALLING);
  77.  
  78.   Serial.print("Your are connecting to;");
  79.   Serial.println(wifiSsid);
  80. }
  81.  
  82. float getTemperature() {
  83.   sensors.requestTemperatures();
  84.   return (float)sensors.getTempCByIndex(0);
  85. }
  86.  
  87. void measureWindspeed() {
  88.  
  89.   File myFile = SD.open(READINGS_FILENAME, FILE_WRITE);
  90.   if (myFile) {
  91.     char measurementsData[100] = {};
  92.     timestamp = timeClient.getEpochTime() + 3600;
  93.     sprintf(measurementsData, "%s,%d,%.2f\n",ctime(&timestamp), interruptCounter, temperature);
  94.     Serial.println(measurementsData);
  95.     myFile.print(measurementsData);
  96.     myFile.close();
  97.     workingLed.flip();
  98.   } else {
  99.     Serial.println("error opening file");
  100.   }
  101.   lastWindspeed = interruptCounter;
  102.   interruptCounter=0;
  103. }
  104.  
  105. void handleInterrupt() {
  106.   interruptCounter++;
  107. }
  108.  
  109. void loop() {  
  110.  
  111.  
  112. Serial.println(ctime(&timestamp));
  113.  
  114.   temperature = getTemperature();
  115.   if(WiFi.status() == WL_CONNECTED) {
  116.     Serial.println("WiFi is on");
  117.     timeClient.update(); // MAKE SURE THIS DONT CRASH EVERYTHING
  118.  
  119.     if (!temperatureFeed.send(temperature) && !windspeedFeed.send(lastWindspeed) && !timeFeed.send(ctime(&timestamp))) {
  120.       Serial.println("Could not send data");
  121.     }
  122.   }
  123.  
  124.  
  125.   if(gps.available() > 0) {
  126.     Serial.println("GPS");
  127.     File myFile = SD.open(READINGS_FILENAME, FILE_WRITE);
  128.       while (gps.available() > 0){
  129.         byte gpsData = gps.read();
  130.         Serial.write(gpsData);
  131.    
  132.         if (myFile) {
  133.           myFile.println("GPS");
  134.           myFile.print(gpsData);
  135.         }
  136.       }
  137.     myFile.close();
  138.   }
  139. delay(5000);
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement