Rebooting...
ets Jun 8 2016 00:22:57
rst:0xc (SW_CPU_RESET),boot:0x17 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0030,len:1324
ho 0 tail 12 room 4
load:0x40078000,len:13508
load:0x40080400,len:3604
entry 0x400805f0
DHTxx test!
assert failed: tcpip_send_msg_wait_sem IDF/components/lwip/lwip/src/api/tcpip.c:455 (Invalid mbox)
Backtrace:0x400832ad:0x3ffb24f00x4008dd45:0x3ffb2510 0x40092f6d:0x3ffb2530 0x400f7855:0x3ffb2660 0x40107845:0x3ffb2690 0x401078a5:0x3ffb26b0 0x400f7031:0x3ffb2700 0x400d771a:0x3ffb2720 0x400d77b5:0x3ffb2760 0x40167ef5:0x3ffb2790 0x400d8063:0x3ffb27b0 0x400d2f12:0x3ffb27d0 0x400ddade:0x3ffb2820
#include "DHT.h"
#include "FS.h"
#include "SD.h"
#include <WiFi.h>
#include <Wire.h>
#include <TinyGPS++.h>
#include <NTPClient.h>
//#include <WiFiUdp.h>
#include <SPI.h>
#include <HTTPClient.h>
#define pi 3.14159265358979323846
#define RXD2 14
#define TXD2 12
#define DHTPIN 2
#define LDR 34
#define DHTTYPE DHT22
#define SECRET_SSID "S***" // replace MySSID with your WiFi network name
#define SECRET_PASS "S*****" // replace MyPassword with your WiFi password
#define SD_CS 5
String dataMessage;
HardwareSerial neogps(1);
String GOOGLE_SCRIPT_ID = ""; // Type your App Script id
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS;
float h1=0;
float t1=0;
double latt1;
double lang1;
double latt;
double lang;
int dark1=100;
String formattedDate;
String dayStamp;
String timeStamp;
TinyGPSPlus gps;
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);
DHT dht(DHTPIN, DHTTYPE);
void setup() {
// put your setup code here, to run once:
Serial.begin(115200); //Initialize serial
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo native USB port only
}
Serial.println(F("DHTxx test!"));
dht.begin();
delay(1000);
//neogps.begin(9600, SERIAL_8N1, RXD2, TXD2);
delay(1000);
timeClient.begin();
delay(1000);
timeClient.setTimeOffset(19800);
delay(1000);
// Initialize SD card
SD.begin(SD_CS);
if(!SD.begin(SD_CS)) {
Serial.println("Card Mount Failed");
return;
}
uint8_t cardType = SD.cardType();
if(cardType == CARD_NONE) {
Serial.println("No SD card attached");
return;
}
Serial.println("Initializing SD card...");
if (!SD.begin(SD_CS)) {
Serial.println("ERROR - SD card initialization failed!");
return; // init failed
}
File file = SD.open("/data.csv");
if(!file) {
Serial.println("File doens't exist");
Serial.println("Creating file...");
writeFile(SD, "/data.csv", "Date, Time, Temperature, Humidity , lattitude , longitude , darkness \r\n");
}
else {
Serial.println("File already exists");
}
file.close();
}
void loop()
{
/// ldr
float dark ;
int ldr = analogRead(LDR) ;
dark= map(ldr,0,4095,0,100);
Serial.println(dark);
// finction for connecting the wifi
if(WiFi.status() != WL_CONNECTED){
Serial.print("Attempting to connect to SSID: ");
Serial.println(SECRET_SSID);
while(WiFi.status() != WL_CONNECTED){
WiFi.begin(ssid, pass); // Connect to WPA/WPA2 network. Change this line if using open or WEP network
Serial.print(".");
delay(5000);
}
Serial.println("\nConnected.");
}
delay (10000);
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
if (isnan(h) || isnan(t) || isnan(f))
{
Serial.println(F("Failed to read from DHT sensor!"));
//return ;
}
Serial.println(h);
Serial.println(t);
/// function to get lat long .
boolean newData = false;
for (unsigned long start = millis(); millis() - start < 1000;)
{
while (neogps.available())
{
if (gps.encode(neogps.read()))
{
newData = true;
}
}
}
//If newData is true
if(newData == true)
{
newData = false;
latt =(gps.location.lat());
lang =(gps.location.lng());
//double sat=gps.satellites.value();
//Serial.print(latt);
Serial.println(gps.satellites.value());
Serial.println(gps.location.lng(),15);
Serial.println(gps.location.lat(),15);
}
else
{
Serial.println("no data");
}
double d= distance(latt1,lang1,latt,lang);
if((abs(h-h1)>=2) || (abs (t-t1)>=.5)||(abs(d)>=1) || abs(dark-dark1)>=10 )
{
Serial.print(F("Temperature "));
Serial.print(t);
Serial.println(F("°C "));
Serial.print(F("Humidity: "));
Serial.print(h);
Serial.println(F("%"));
t1=t;
h1=h;
latt1=latt;
lang1=lang;
dark1=dark;
getTimeStamp();
// for writting in google sheet
write_google_sheet( "value1="+ String(t)+"&value2="+String(h)+"&value3="+String(latt)+"&value4="+String(lang)+"&value5="+String(dark));
logSDCard( String(t)+","+String(h)+","+String(latt)+","+String(lang)+","+String(dark));
}
}
double deg2rad(double);
double rad2deg(double);
double distance(double lat1, double lon1, double lat2, double lon2) {
double theta, dist;
if ((lat1 == lat2) && (lon1 == lon2)) {
return 0;
}
else {
theta = lon1 - lon2;
dist = sin(deg2rad(lat1)) * sin(deg2rad(lat2)) + cos(deg2rad(lat1)) * cos(deg2rad(lat2)) * cos(deg2rad(theta));
dist = acos(dist);
dist = rad2deg(dist);
dist = dist * 60 * 1.1515;
dist = dist * 1.609344;
return (dist);
}
}
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
/*:: This function converts decimal degrees to radians :*/
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
double deg2rad(double deg) {
return (deg * pi / 180);
}
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
/*:: This function converts radians to decimal degrees :*/
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
double rad2deg(double rad) {
return (rad * 180 / pi);
}
void write_google_sheet(String params) {
HTTPClient http;
String url="https://script.google.com/macros/s/"+GOOGLE_SCRIPT_ID+"/exec?"+params;
Serial.println(url);
//Serial.println("Updating Temperature & Humidity Status");
http.begin(url.c_str());
http.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);
int httpCode = http.GET();
Serial.print("HTTP Status Code: ");
Serial.println(httpCode);
String payload;
if (httpCode > 0) {
payload = http.getString();
Serial.println("Payload: "+payload);
}
http.end();
}
void getTimeStamp() {
while(!timeClient.update()) {
timeClient.forceUpdate();
}
formattedDate = timeClient.getFormattedTime();
//Date = date;
// Serial.println(Date)
Serial.println(formattedDate);
int splitT = formattedDate.indexOf("T");
dayStamp = formattedDate.substring(0, splitT);
Serial.println(dayStamp);
// Extract time
timeStamp = formattedDate.substring(splitT+1, formattedDate.length()-1);
Serial.println(timeStamp);
}
void logSDCard(String Data) {
dataMessage = String(timeStamp) + "," + Data + "\r\n";
Serial.print("Save data: ");
Serial.println(dataMessage);
appendFile(SD, "/data.csv", dataMessage.c_str());
}
// Write to the SD card (DON'T MODIFY THIS FUNCTION)
void writeFile(fs::FS &fs, const char * path, const char * message) {
Serial.printf("Writing file: %s\n", path);
File file = fs.open(path, FILE_WRITE);
if(!file) {
Serial.println("Failed to open file for writing");
return;
}
if(file.print(message)) {
Serial.println("File written");
} else {
Serial.println("Write failed");
}
file.close();
}
// Append data to the SD card (DON'T MODIFY THIS FUNCTION)
void appendFile(fs::FS &fs, const char * path, const char * message) {
Serial.printf("Appending to file: %s\n", path);
File file = fs.open(path, FILE_APPEND);
if(!file) {
Serial.println("Failed to open file for appending");
return;
}
if(file.print(message)) {
Serial.println("Message appended");
} else {
Serial.println("Append failed");
}
file.close();
}