Forked from frenchguycooking/gist:d368ba94bb9754faecaa10d747e0c635
Last active
June 14, 2022 23:27
-
-
Save davidstosik/a13fb2e7ff6ce302fe49c2acf9a629aa to your computer and use it in GitHub Desktop.
This file contains hidden or 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
//************************************************************************************************************************** | |
//************************************************************************************************************************** | |
// CLEANER-STABLER-DATA-COLLECTION | |
//************************************************************************************************************************** | |
//************************************************************************************************************************** | |
// Wifi libraries | |
#include <SPI.h> | |
#include <WiFiNINA.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"; | |
int port = 443; | |
// 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; | |
// In the past it was a "String object" now it is just a string, works better with sprintf | |
char dataFileName[12]; // data collection filename | |
//////////////////////////////////////////////////// | |
// TRYING TO COUNTER THE FACT THE LOOP STOPS AFTER 380 LOOPS | |
//////////////////////////////////////////////////// | |
// Declare a string format for the whole request content, as a constant. | |
const char requestFormat[] = "GET /arduino/data.php?fi=%s&ti=%ld&te=%d&hu=%d&te2=%d&hu2=%d HTTP/1.1\n\ | |
Host: %s:%d\n\ | |
Connection: keep-alive\n"; | |
// And declare a buffer meant to receive the actual request string. | |
// 250 is the estimated size of the formatted request, with real values replacing place holders. | |
// If the number is too low it will cause problems! | |
char requestBuffer[250]; | |
//////////////////////////////////////////////////// | |
// SENSOR SPECIFIC VARIABLES | |
//////////////////////////////////////////////////// | |
// TOP SENSOR IS CONNECTED TO 7 | |
//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 | |
// BOTTOM SENSOR IS CONNECTED TO 6 | |
//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 = 5; // 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(100000,1000000); | |
Serial.print("Random number : "); | |
Serial.println(randNumber); | |
sprintf(dataFileName,"data%lu",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..."); | |
// Make sure the HTTP/SSL connection is still available | |
if ensureClientConnected() { | |
// This uses requestFormat as format, | |
// replaces all placeholders by the values passed as arguments (dataFileName, t1, etc), | |
// then places the result in requestBuffer. | |
// About placeholders: | |
// %d receive an integers | |
// %s receives a string | |
// %ld receives an unsigned long | |
// About sprintf: https://www.programmingelectronics.com/sprintf-arduino/ | |
sprintf(requestBuffer, requestFormat, dataFileName, t1, te, hu, te2, hu2, server, port); | |
Serial.println(requestBuffer); | |
// Pass the whole request to the client, as a single string. | |
client.println(requestBuffer); | |
// Flush response, we don't need it. | |
while(client.available()) { | |
client.read(); | |
} | |
} | |
} | |
} | |
//************************************************************************************************************************** | |
// 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(); | |
} | |
bool ensureClientConnected() { | |
// if client is already connected, we're good | |
if (client.connected()) { | |
return true; | |
} | |
// try to connect | |
if (client.connectSSL(server, port)) { | |
digitalWrite(11, HIGH); //green | |
Serial.println("Success ! SSL-Connected to server"); | |
return true; | |
} else { | |
// if the connection failed, provide some feedback | |
digitalWrite(11, LOW); //green | |
Serial.println("SSL connection to server failed..."); | |
return false; | |
} | |
} | |
/////////////////////////////////////////////////////////////// | |
// 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