Forked from frenchguycooking/alex-pasta-data-collection.ino
Last active
June 9, 2022 14:50
-
-
Save luisfcorreia/7f019c1379595bfd552f78562ba65294 to your computer and use it in GitHub Desktop.
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
//************************************************************************************************************************** | |
//************************************************************************************************************************** | |
// ALEX-PASTA-DATA-COLLECTION V6 | |
//************************************************************************************************************************** | |
//************************************************************************************************************************** | |
// Wifi libraries | |
#include <SPI.h> | |
#include <WiFiNINA.h> | |
// TIME through Wifi libraries | |
//#include <WiFiUdp.h> | |
//#include <TimeLib.h> | |
// DHT22 sensor library | |
#include <DHT.h> | |
//////////////////////////////////////////////////// | |
// WIFI SPECIFIC VARIABLES | |
//////////////////////////////////////////////////// | |
char ssid[] = "xxx"; // your network SSID (name) | |
char pass[] = "yyy"; // your network password | |
char server[] = "www.hostname.com"; | |
// WIFI status can be either : WL_IDLE_STATUS or WL_CONNECTED when connected | |
int status = WL_IDLE_STATUS; | |
// Initialize the Wifi client library | |
// with the IP address and port of the server | |
// that you want to connect to (port 80 is default for HTTP): | |
WiFiClient client; | |
String dataFileName = "data"; // data collection filename = "data" + random number, the "creation date" is created by the php/server anyways | |
//////////////////////////////////////////////////// | |
// SENSOR SPECIFIC VARIABLES | |
//////////////////////////////////////////////////// | |
//Constants | |
#define DHTPIN 7 // what pin we're connected to | |
#define DHTTYPE DHT22 // DHT 22 (AM2302) | |
DHT dht(DHTPIN, DHTTYPE); //// Initialize DHT sensor for normal 16mhz Arduino | |
//Variables for sensor 1 | |
int hu; //Stores humidity value | |
int te; //Stores temperature value | |
//Constants | |
#define DHTPIN2 6 // what pin we're connected to | |
#define DHTTYPE2 DHT22 // DHT 22 (AM2302) | |
DHT dht2(DHTPIN2, DHTTYPE2); //// Initialize DHT sensor for normal 16mhz Arduino | |
//Variables for sensor 2 | |
int hu2; //Stores humidity value | |
int te2; //Stores temperature value | |
// warning MILLIS() can't use INT variabbles, or it will spit out negative values sometimes | |
unsigned long lastCollectionTime = 0; | |
unsigned long intervalTime = 60; // time between two data collection | |
//************************************************************************************************************************** | |
// SETUP | |
//************************************************************************************************************************** | |
void setup() { | |
Serial.begin(9600); | |
//wait for serial to be ready | |
while (!Serial); | |
Serial.println("*********************************************"); | |
Serial.println("*******THE-PASTA-DRYING-DATA-LOGGER**********"); | |
Serial.println("*********************************************"); | |
/////////////////////////////////////////////////////////////// | |
// SETUP : SENSORS | |
/////////////////////////////////////////////////////////////// | |
// DHT22 Sensor 1 setup | |
dht.begin(); | |
// DHT22 Sensor 2 setup | |
dht2.begin(); | |
/////////////////////////////////////////////////////////////// | |
// SETUP : LED | |
/////////////////////////////////////////////////////////////// | |
// 13 is the blue led used for WIFI ON OFF | |
pinMode(13, OUTPUT); | |
// 12 is the white led used for DATA COM | |
pinMode(12, OUTPUT); | |
// 11 is the green LED use for TIME COM | |
pinMode(11, OUTPUT); | |
// SAY HELLO | |
digitalWrite(13, HIGH);//blue | |
digitalWrite(12, HIGH);//white | |
digitalWrite(11, HIGH);//green | |
delay(1000); | |
digitalWrite(13, LOW);//blue | |
digitalWrite(12, LOW);//white | |
digitalWrite(11, LOW);//green | |
/////////////////////////////////////////////////////////////// | |
// SETUP : WIFI | |
/////////////////////////////////////////////////////////////// | |
while (status != WL_CONNECTED) { | |
printTimeElapsed(); | |
Serial.print("Trying to connect to : "); | |
Serial.println(ssid); | |
status = WiFi.begin(ssid, pass); | |
delay(5000); //wait 5s for connection | |
} | |
printTimeElapsed(); | |
Serial.println("Connected to Wifi"); | |
digitalWrite(13, HIGH);//blue | |
/////////////////////////////////////////////////////////////// | |
// SETUP : Display WIFI INFORMATION | |
/////////////////////////////////////////////////////////////// | |
String fv = WiFi.firmwareVersion(); | |
if (fv < WIFI_FIRMWARE_LATEST_VERSION) { | |
Serial.println("Firmware : Please upgrade"); | |
}else{ | |
Serial.println("Firmware : Up to date"); | |
} | |
printCurrentNet(); | |
printWiFiData(); | |
printTimeElapsed(); | |
/////////////////////////////////////////////////////////////// | |
// SETUP : Create data filename using TIME | |
/////////////////////////////////////////////////////////////// | |
Serial.println("Generating Random Data Filename..."); | |
long randNumber; | |
randomSeed(analogRead(0)); | |
randNumber = random(10000,100000); | |
//Serial.print("Random number : "); | |
//Serial.println(randNumber); | |
dataFileName = dataFileName + randNumber; | |
Serial.print("dataFileName= "); | |
Serial.println(dataFileName); | |
printTimeElapsed(); | |
} | |
//************************************************************************************************************************** | |
// LOOP | |
//************************************************************************************************************************** | |
void loop() { | |
/////////////////////////////////// | |
// LOOP :COLLECT DATA USING SENSOR | |
/////////////////////////////////// | |
//delay(2000); // at least 2000ms between two readings of sensor | |
unsigned long t1; | |
t1 = millis()/1000; // this is the time at which data will be collected, so t1 needs to be sent va GET | |
unsigned long t2; | |
t2 = lastCollectionTime + intervalTime; //collect data every XXs | |
if(t1 >= t2){ //is it time to collect data ? | |
// Update the lastCollectionTime | |
lastCollectionTime = t1; | |
Serial.println("-"); | |
Serial.println("TIME TO COLLECT DATA !"); | |
printTimeElapsed(); | |
blinkLed(12); | |
//Read data and multiply by 100 and store it to variables hu and te | |
hu = 100*dht.readHumidity(); | |
te = 100*dht.readTemperature(); | |
//Print temp and humidity values to serial monitor | |
Serial.print("DHT22 Sensor #1: Reading Temperature: "); | |
Serial.print(te); | |
Serial.print("°C, Humidity: "); | |
Serial.print(hu); | |
Serial.println("%"); | |
//Read data and multiply by 100 and store it to variables hu and te | |
hu2 = 100*dht2.readHumidity(); | |
te2 = 100*dht2.readTemperature(); | |
//Print temp and humidity values to serial monitor | |
Serial.print("DHT22 Sensor #2: Reading Temperature: "); | |
Serial.print(te2); | |
Serial.print("°C, Humidity: "); | |
Serial.print(hu2); | |
Serial.println("%"); | |
// time to TRY to send data to the server | |
Serial.println("Trying to send this data to the server..."); | |
// wifi check first | |
Serial.print(">> CHECKING WiFi.status() = "); | |
Serial.println(WiFi.status()); | |
Serial.print(">> WiFi.status() MEANING : "); | |
switch (WiFi.status()) { | |
case 3: | |
Serial.println("Connected"); | |
break; | |
case 4: | |
Serial.println("Connection Failed"); | |
status = WL_IDLE_STATUS; | |
WiFi.disconnect(); | |
digitalWrite(13, LOW);//blue | |
break; | |
case 5: | |
Serial.println("Connection Lost"); | |
status = WL_IDLE_STATUS; | |
WiFi.disconnect(); | |
digitalWrite(13, LOW);//blue | |
break; | |
case 6: | |
Serial.println("Disconnected"); | |
status = WL_IDLE_STATUS; | |
WiFi.disconnect(); | |
digitalWrite(13, LOW);//blue | |
break; | |
case 255: | |
Serial.println("No Shield"); | |
status = WL_IDLE_STATUS; | |
WiFi.disconnect(); | |
digitalWrite(13, LOW);//blue | |
break; | |
default: | |
Serial.println("???"); | |
status = WL_IDLE_STATUS; | |
WiFi.disconnect(); | |
digitalWrite(13, LOW);//blue | |
break; | |
} | |
while (status != WL_CONNECTED) { | |
// TRYING TO RECONNECT | |
Serial.println("(!) WiFi Connection is failing !"); | |
Serial.print("(!) Trying to reconnect to : "); | |
Serial.println(ssid); | |
status = WiFi.begin(ssid, pass); | |
delay(5000); //wait 5s for connection | |
} | |
Serial.println("***Connected to Wifi***"); | |
digitalWrite(13, HIGH);//blue | |
Serial.println("Connecting to server..."); | |
// if you get a connection, report back via serial: | |
if (client.connectSSL(server, 443)) { | |
Serial.println("Success ! SSL-Connected to server"); | |
// Make a HTTP request: | |
dataToSend = "GET /arduino/data.php?fi=" + dataFileName + "&ti=" + String(t1) + "&te=" + String(te) + "&hu=" + String(hu) + "&te2=" + String(te2) + "&hu2=" + String(hu2); | |
Serial.println("***DEBUG GET REQUEST START"); | |
Serial.println(dataToSend); | |
Serial.println("***DEBUG GET REQUEST END"); | |
client.print(dataToSend); | |
client.println(" HTTP/1.1"); | |
client.println("Host: www.hostname.com"); | |
client.println("Connection: close"); | |
client.println(); | |
Serial.println("Request sent"); | |
Serial.println("--------------------------------"); | |
// Wait from response and then stop client, hopefully this closes connection properly | |
while (client.available()){ | |
client.read(); | |
} | |
client.stop(); | |
digitalWrite(11, HIGH);//green | |
} | |
else{ | |
// connection to php file failed | |
Serial.println("(!) Connection to php file failed"); | |
Serial.println("(!) Disconnecting Client"); | |
client.stop(); | |
digitalWrite(11, LOW);//green | |
} | |
} | |
} | |
//************************************************************************************************************************** | |
// FUNCTIONS | |
//************************************************************************************************************************** | |
/////////////////////////////////////////////////////////////// | |
// TIME FUNCTIONS | |
/////////////////////////////////////////////////////////////// | |
void printTimeElapsed(){ | |
unsigned long myTime; | |
Serial.print(">> Time elapsed since program started : "); | |
myTime = millis()/1000; | |
Serial.println(myTime); // prints time since program started | |
} | |
/////////////////////////////////////////////////////////////// | |
// WIFI FUNCTIONS | |
/////////////////////////////////////////////////////////////// | |
void printWiFiData() { | |
// print your board's IP address: | |
IPAddress ip = WiFi.localIP(); | |
Serial.print("IP address : "); | |
Serial.println(ip); | |
Serial.print("Subnet mask: "); | |
Serial.println((IPAddress)WiFi.subnetMask()); | |
Serial.print("Gateway IP : "); | |
Serial.println((IPAddress)WiFi.gatewayIP()); | |
// print your MAC address: | |
byte mac[6]; | |
WiFi.macAddress(mac); | |
Serial.print("MAC address: "); | |
printMacAddress(mac); | |
} | |
void printCurrentNet() { | |
// print the SSID of the network you're attached to: | |
Serial.print("SSID: "); | |
Serial.println(WiFi.SSID()); | |
// print the MAC address of the router you're attached to: | |
byte bssid[6]; | |
WiFi.BSSID(bssid); | |
Serial.print("BSSID: "); | |
printMacAddress(bssid); | |
// print the received signal strength: | |
long rssi = WiFi.RSSI(); | |
Serial.print("signal strength (RSSI): "); | |
Serial.println(rssi); | |
// print the encryption type: | |
byte encryption = WiFi.encryptionType(); | |
Serial.print("Encryption Type: "); | |
Serial.println(encryption, HEX); | |
Serial.println(); | |
} | |
void printMacAddress(byte mac[]) { | |
for (int i = 5; i >= 0; i--) { | |
if (mac[i] < 16) { | |
Serial.print("0"); | |
} | |
Serial.print(mac[i], HEX); | |
if (i > 0) { | |
Serial.print(":"); | |
} | |
} | |
Serial.println(); | |
} | |
/////////////////////////////////////////////////////////////// | |
// LED WAIT FUNCTIONS | |
/////////////////////////////////////////////////////////////// | |
void blinkLed(int i){ | |
digitalWrite(i, HIGH); | |
delay(100); | |
digitalWrite(i, LOW); | |
delay(100); | |
digitalWrite(i, HIGH); | |
delay(100); | |
digitalWrite(i, LOW); | |
delay(100); | |
digitalWrite(i, HIGH); | |
delay(100); | |
digitalWrite(i, LOW); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment