/**********************************
*Project : CEIS490 IoT data collection
*Institution : DeVry University
*Module : 3
*Program Purpose: Sends data to ThingSpeak
*Date created : 2024.06.11
**********************************/
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <DHT.h>
#include <ThingSpeak.h>
#include <WiFi.h>
#define DHTTYPE DHT22 //Using DHT22 sensor
const char* ssid = "Wokwi-GUEST"; // your network SSID (name)
const char* password = ""; // your network password
WiFiClient client;
String studentName = "XXXXX"; // Replace xxxxx with your name
String programEnrolled = "XXXXX"; //Replace xxxxx with your program Use your major for example: Engineering Technology
unsigned long myChannelNumber = 2568494; //Put the correct channel number
const char * myWriteAPIKey = "2FAASIFFLK5MMJOK"; // Use correct Write API Key
unsigned long lastTime = 0;
unsigned long timerDelay = 10000;
const int DHT_PIN = 15;
DHT dht(DHT_PIN, DHTTYPE);
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET 4
#define SCREEN_ADDRESS 0x3C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
int sensorPin = A0;
void setup() {
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client); // Initialize ThingSpeak
// Connect or reconnect to WiFi
if(WiFi.status() != WL_CONNECTED){
Serial.print("Attempting to connect");
while(WiFi.status() != WL_CONNECTED){
WiFi.begin(ssid, password);
delay(5000);
}
Serial.println("\nConnected.");
}
Serial.begin(115200);
// Checked the OLED display
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
display.display();
delay(2000);
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(2);
Serial.println("CEIS490 Module 3 Project ");
Serial.println(String(__DATE__) + "\t" + String(__TIME__));
Serial.println("Name: " + studentName);
Serial.println("DeVry Program: " + programEnrolled); //Replace xxxxx with your program Use your major for example: Engineering Technolog
Serial.println("Setup done");
}
unsigned long t=0;
unsigned long interval = 20000; //the update interval should be at least 30s
bool tempScale = false ; // selects temperature scale. false = Celcius true = Fahrenheit
void loop() {
float temp = dht.readTemperature(tempScale); //the tempScale selects the reading temperature scale
float hum = dht.readHumidity();
//Serial.println(analogRead(sensorPin));
Serial.println("Temp: " + String(temp, 2) + "°C");
Serial.println("Humidity: " + String(hum, 1) + "%");
Serial.println("---");
delay(3000); // Wait for a new reading from the sensor (DHT22 has ~0.5Hz sample rate)
display.clearDisplay();
display.setTextSize(1.8);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println(studentName);
display.setCursor(0, 15);
display.println("Temp: " +String(temp, 2) + " C");
display.setCursor(0, 30);
display.println("Humidity: " +String(hum, 1) + " %");
display.display();
if ( (millis()-t) > interval){ //Writing to ThingSpeak if the log time has elapsed.
ThingSpeak.setField(1, temp);
ThingSpeak.setField(2, hum);
int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if(x == 200){
Serial.println("Channel update successful.");
}
else{
Serial.println("Problem updating channel. HTTP error code " + String(x));
}
t = millis();
}
}
Loading
ssd1306
ssd1306