Forked from frenchguycooking/alex-pasta-data-collection.ino
Last active
July 2, 2022 18:01
-
-
Save JanisHuser/90c13b9212ac0e907c7f7337feb44283 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> | |
//CUSTOM TYPES | |
typedef struct | |
{ | |
int humidity; | |
int temperature; | |
void print() | |
{ | |
Serial.print(temperature); | |
Serial.print("°C, Humidity: "); | |
Serial.print(humidity); | |
Serial.println("%"); | |
} | |
} SensorData; | |
// FUNCTION DEFINITIONS | |
// should be placed in a header file | |
void setupWifi (void); | |
void setupSensors (void); | |
void setupSerial (void); | |
void setupLeds (void); | |
void setWifiStatus(bool status); | |
void setDataComStatus(bool status); | |
void setTimeComStatus(bool status); | |
void checkWifiStatus (void); | |
SensorData readSensorData(DHT dht); | |
//Constants | |
#define DHTPIN2 6 // what pin we're connected to | |
#define DHTTYPE2 DHT22 // DHT 22 (AM2302) | |
#define DHTPIN 7 // what pin we're connected to | |
#define DHTTYPE DHT22 // DHT 22 (AM2302) | |
#define LED_WIFI 13 | |
#define LED_DATA_COM 12 | |
#define LED_TIME_COM 11 | |
// CONSTANTS (prefered way, as defines can be mended into unwanted other variables) | |
const char ssid[] = "xxx"; // your network SSID (name) | |
const char pass[] = "yyy"; // your network password | |
const char server[] = "www.hostname.com"; | |
const unsigned long intervalTime = 60; // time between two data collection | |
// SENSOR Configurations, Proper naming prefered | |
// WHAT is Dht / Dht2 | |
DHT dht(DHTPIN, DHTTYPE); //// Initialize DHT sensor for normal 16mhz Arduino | |
DHT dht2(DHTPIN2, DHTTYPE2); //// Initialize DHT sensor for normal 16mhz Arduino | |
// VARIABLES | |
unsigned long lastCollectionTime = 0; | |
// 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 | |
void setup() { | |
setupSerial(); | |
setupSensors(); | |
setupLeds(); | |
setupWifi(); | |
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(LED_DATA_COM); | |
SensorData sensor1Data = readSensorData(dht); | |
SensorData sensor2Data = readSensorData(dht2); | |
//Print temp and humidity values to serial monitor | |
Serial.print("DHT22 Sensor #1: Reading Temperature: "); | |
sensor1Data.print(); | |
//Print temp and humidity values to serial monitor | |
Serial.print("DHT22 Sensor #2: Reading Temperature: "); | |
sensor2Data.print(); | |
// time to TRY to send data to the server | |
Serial.println("Trying to send this data to the server..."); | |
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)) | |
{ | |
String request = "GET /arduino/data.php"; | |
request += "&fi" + dataFileName; | |
request += "&ti=" + t1; | |
request += "&te=" + sensor1Data.temperature; | |
request += "&hu=" + sensor1Data.humidity; | |
request += "&te2=" + sensor2Data.temperature; | |
request += "&hu2=" + sensor2Data.humidity; | |
request += " HTTP/1.1 \r\n"; | |
request += "Host" + String(server) + "\r\n"; | |
request += "Connection: close\r\n"; | |
// Make a HTTP request: | |
Serial.println("***DEBUG GET REQUEST START"); | |
Serial.println(request); | |
Serial.println("***DEBUG GET REQUEST END"); | |
client.print(request); | |
Serial.println("Request sent"); | |
Serial.println("--------------------------------"); | |
setTimeComStatus(true); | |
} | |
else{ | |
// connection to php file failed | |
Serial.println("(!) Connection to php file failed"); | |
Serial.println("(!) Disconnecting Client"); | |
client.stop(); | |
setTimeComStatus(false); | |
} | |
} | |
} | |
//************************************************************************************************************************** | |
// 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){ | |
for (int j=0; j < 4; j++) { | |
digitalWrite(i, HIGH); | |
delay(100); | |
digitalWrite(i, LOW); | |
delay(100); | |
} | |
} | |
void setupSerial (void) | |
{ | |
Serial.begin(9600); | |
//wait for serial to be ready | |
while (!Serial); | |
Serial.println("*********************************************"); | |
Serial.println("*******THE-PASTA-DRYING-DATA-LOGGER**********"); | |
Serial.println("*********************************************"); | |
} | |
void setupSensors (void) | |
{ | |
// DHT22 Sensor 1 setup | |
dht.begin(); | |
// DHT22 Sensor 2 setup | |
dht2.begin(); | |
} | |
void setupLeds (void) | |
{ | |
// 13 is the blue led used for WIFI ON OFF | |
pinMode(LED_WIFI, OUTPUT); | |
// 12 is the white led used for DATA COM | |
pinMode(LED_DATA_COM, OUTPUT); | |
// 11 is the green LED use for TIME COM | |
pinMode(LED_TIME_COM, OUTPUT); | |
// SAY HELLO | |
setWifiStatus(true); | |
setDataComStatus(true); | |
setTimeComStatus(true); | |
delay(1000); | |
setWifiStatus(false); | |
setDataComStatus(false); | |
setTimeComStatus(false); | |
} | |
void setupWifi (void) | |
{ | |
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 | |
} | |
} | |
void setWifiStatus(bool status) | |
{ | |
digitalWrite(LED_WIFI, status); | |
} | |
void setDataComStatus(bool status) | |
{ | |
digitalWrite(LED_DATA_COM, status); | |
} | |
void setTimeComStatus(bool status) | |
{ | |
digitalWrite(LED_TIME_COM, status); | |
} | |
void checkWifiStatus (void) | |
{ | |
// 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"); | |
return; | |
case 4: | |
Serial.println("Connection Failed"); | |
break; | |
case 5: | |
Serial.println("Connection Lost"); | |
break; | |
case 6: | |
Serial.println("Disconnected"); | |
break; | |
case 255: | |
Serial.println("No Shield"); | |
break; | |
default: | |
Serial.println("???"); | |
break; | |
} | |
status = WL_IDLE_STATUS; | |
WiFi.disconnect(); | |
setWifiStatus(false); | |
} | |
SensorData readSensor(DHT dht) | |
{ | |
SensorData data; | |
data.humidity = 100*dht.readHumidity(); | |
data.temperature = 100*dht.readTemperature(); | |
return data; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment