Created
January 27, 2017 20:59
-
-
Save RZRZR/43cb83bb76abaa263034fc33b49d6f8a to your computer and use it in GitHub Desktop.
van_monitor.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <TinyGPS++.h> // Tiny GPS Plus Library | |
#include <SoftwareSerial.h> // for GPS | |
//#include <SPI.h> | |
#include <ESP8266WiFi.h> | |
#include <ThingerWifi.h> | |
//#include <Adafruit_Sensor.h> | |
//#include <math.h> | |
#include "DHT.h" | |
//#include <Adafruit_NeoPixel.h> | |
//#include <SD.h> | |
// thinger.io details | |
#define USERNAME "" | |
#define DEVICE_ID "van_mk_2" | |
#define DEVICE_CREDENTIAL "" | |
// Wifi details | |
#define SSID "" | |
#define SSID_PASSWORD "" | |
// dht config | |
#define DHTPIN D1 | |
#define DHTTYPE DHT22 | |
DHT dht(DHTPIN, DHTTYPE); | |
// RGB LED | |
//#define LEDPIN D2 | |
//Adafruit_NeoPixel pixels = Adafruit_NeoPixel(1, LEDPIN, NEO_GRB + NEO_KHZ800); | |
// SD card | |
//const int chipSelect = D8; | |
// GPS | |
static const int RXPin = D4, TXPin = D3; // Ublox 6m GPS module to pins D4 and D3 | |
static const uint32_t GPSBaud = 9600; // Ublox GPS default Baud Rate is 9600 | |
const double Home_LAT = 52.00000; // Your Home Latitude | |
const double Home_LNG = 0.00000; // Your Home Longitude | |
TinyGPSPlus gps; // Create an Instance of the TinyGPS++ object called gps | |
SoftwareSerial ss(RXPin, TXPin); // The serial connection to the GPS device | |
//voltage divider set-up | |
int analogInput = A0; | |
float volt_reading = 0.0; | |
int volt_value = 0.0; | |
ThingerWifi thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL); | |
unsigned long previousMillis = 0; // to know when to post the temperature | |
unsigned int minutes_interval = 60; // variable in minutes for posting the temperature | |
void call_temperature_endpoint(){ | |
digitalWrite(BUILTIN_LED, LOW); | |
// pson notification_content; | |
// notification_content["value1"] = dht.readTemperature(); | |
// notification_content["value2"] = dht.readHumidity(); | |
// thing.call_endpoint("temp_notification", notification_content); | |
// pixels.setPixelColor(0, pixels.Color(0, 255, 0)); | |
// led_notification(); | |
digitalWrite(BUILTIN_LED, HIGH); | |
} | |
void call_voltage_endpoint(){ | |
digitalWrite(BUILTIN_LED, LOW); | |
// pson notification_content; | |
// notification_content["value1"] = readVolts(); | |
// thing.call_endpoint("volt_notification", notification_content); | |
// pixels.setPixelColor(0, pixels.Color(255, 0, 0)); | |
// led_notification(); | |
digitalWrite(BUILTIN_LED, HIGH); | |
} | |
void call_gps_endpoint(){ | |
digitalWrite(BUILTIN_LED, LOW); | |
// pson notification_content; | |
// notification_content["value1"] = gps.location.lat(); | |
// notification_content["value2"] = gps.location.lng(); | |
// thing.call_endpoint("volt_notification", notification_content); | |
// pixels.setPixelColor(0, pixels.Color(0, 0, 255)); | |
// led_notification(); | |
digitalWrite(BUILTIN_LED, HIGH); | |
} | |
//void led_notification(){ | |
// pixels.setBrightness(255); | |
// pixels.show(); | |
// delay(500); | |
// pixels.setBrightness(0); | |
// pixels.show(); | |
//} | |
//void sd_card_begin(){ | |
// Serial.print("Initializing SD card..."); | |
// if (!SD.begin(chipSelect)) { | |
// Serial.println("Card failed, or not present"); | |
// don't do anything more: | |
// return; | |
// } | |
// Serial.println("card initialized."); | |
//} | |
//void sd_card_write(){ | |
// make a string for assembling the data to log: | |
// String dataString = ""; | |
// String seperate = ";"; | |
// String temp = String(dht.readTemperature()); | |
// String humidity = String(dht.readHumidity()); | |
// String volts = String(readVolts()); | |
// dataString += String(temp + seperate + humidity + seperate + volts + seperate); | |
// open the file. note that only one file can be open at a time, | |
// so you have to close this one before opening another. | |
// File dataFile = SD.open("test.csv", FILE_WRITE); | |
// if the file is available, write to it: | |
// if (dataFile) { | |
// dataFile.println(dataString); | |
// dataFile.close(); | |
// print to the serial port too: | |
// Serial.println(dataString); | |
// } | |
// if the file isn't open, pop up an error: | |
// else { | |
// Serial.println("error opening test.txt"); | |
// } | |
//} | |
float read_gps(){ | |
Serial.print("Latitude : "); | |
Serial.println(gps.location.lat(), 5); | |
Serial.print("Longitude : "); | |
Serial.println(gps.location.lng(), 4); | |
Serial.print("Satellites: "); | |
Serial.println(gps.satellites.value()); | |
Serial.print("Elevation : "); | |
Serial.print(gps.altitude.feet()); | |
Serial.println("ft"); | |
Serial.print("Time UTC : "); | |
Serial.print(gps.time.hour()); // GPS time UTC | |
Serial.print(":"); | |
Serial.print(gps.time.minute()); // Minutes | |
Serial.print(":"); | |
Serial.println(gps.time.second()); // Seconds | |
Serial.print("Heading : "); | |
Serial.println(gps.course.deg()); | |
Serial.print("Speed MPH : "); | |
Serial.println(gps.speed.mph()); | |
Serial.println("---"); | |
delay(200); | |
smartDelay(500); | |
if (millis() > 5000 && gps.charsProcessed() < 10) | |
Serial.println(F("No GPS data received: check wiring")); | |
} | |
float status_update(){ | |
Serial.print("Analog input :"); | |
Serial.println(analogRead(analogInput)); | |
Serial.print("Leisure batt :"); | |
Serial.println(readVolts()); | |
Serial.print("Temperature :"); | |
Serial.println(dht.readTemperature()); | |
Serial.print("Humdity :"); | |
Serial.println(dht.readHumidity()); | |
} | |
void setup() { | |
Serial.begin(115200); | |
delay(1500); // Pause 1.5 seconds | |
ss.begin(GPSBaud); // Set Software Serial Comm Speed to 9600 | |
delay(200); | |
dht.begin(); | |
// pixels.begin(); | |
//sd_card_begin(); | |
// pinMode(BUILTIN_LED, OUTPUT); | |
// pinMode(analogInput, INPUT); | |
// turn off led | |
// digitalWrite(BUILTIN_LED, HIGH); | |
thing.add_wifi(SSID, SSID_PASSWORD); | |
// resource for reading sensor temperature from API | |
thing["dht22"] >> [](pson& out){ | |
out["humidity"] = dht.readHumidity(); | |
out["celsius"] = dht.readTemperature(); | |
}; | |
// resource for reading sensor temperature from API | |
thing["voltage_sensor"] >> [](pson& out){ | |
out["voltage"] = readVolts(); | |
}; | |
// resource for reading sensor temperature from API | |
thing["gps"] >> [](pson& out){ | |
out["Latitude"] = gps.location.lat(); | |
out["Longitude"] = gps.location.lng(); | |
}; | |
// resource that will allow changing the tweet interval | |
thing["notification_interval"] << [](pson& in){ | |
if(in.is_empty()) in = minutes_interval; | |
else minutes_interval = (unsigned int) in; | |
}; | |
// this resource will allow sending the tweet as requested | |
thing["notification_test"] = [](){ | |
call_temperature_endpoint(); | |
call_voltage_endpoint(); | |
}; | |
} | |
void loop() { | |
Serial.println(read_gps()); | |
Serial.println(status_update()); | |
// actual action | |
//sd_card_write(); | |
thing.handle(); | |
unsigned long currentMillis = millis(); | |
if(minutes_interval>0 && currentMillis - previousMillis >= minutes_interval * 5000) { | |
previousMillis = currentMillis; | |
call_temperature_endpoint(); | |
call_voltage_endpoint(); | |
call_gps_endpoint(); | |
} | |
} | |
float readVolts(){ | |
volt_value = analogRead(analogInput); | |
volt_reading = volt_value * 0.017096; // ratio of my voltage divider | |
return volt_reading; | |
} | |
static void smartDelay(unsigned long ms) // This custom version of delay() ensures that the gps object is being "fed". | |
{ | |
unsigned long start = millis(); | |
do | |
{ | |
while (ss.available()) | |
gps.encode(ss.read()); | |
} while (millis() - start < ms); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment