The Project:
A neopixel watch that update intern display and thingspeak with temperature and time from RTC. Also reading and sending values to thingspeak from an extern DHT11 sensor (temp/humidity).
My goal:
I have tried to get the readings from DHT11 to behave just as the reading from RTC-temp. I know how to get the reading from the sensor done...but i dont know how to make something good of it. Lets say that I make a function updateTempHum(), as in row 279, that read the values from the sensor DHT11. How do I get the values out of there and into, for example, sendThingspeak() function. As of now I have to write the whole process in sendThingspeak() to get the (t) and (h) values in there. It works! But I want to be able to use the values in a lot of processes. So my big problem is to understand how I can make values float and then fetch them when I want to use them...for example in an other functionX() that starts a fan or something. Hope u understand my problem. Thanks for all help I can get
//Include the libraries
#include "DHT.h"
#include <Adafruit_NeoPixel.h>
#include <Adafruit_TiCoServo.h>
#include <RtcDS3231.h>
#include <SoftwareSerial.h>
#include <SSD1306AsciiWire.h>
#include <WiFiEsp.h>
#include <Wire.h>
//Declare and initialise global constants for Neopixel settings
const byte neoPixels = 24;
const byte neoPin = 10;
//Declare and initialise global arrays for WiFi settings
char ssid[] = "Telia-68651D";
char pass[] = "xxMyPaSSxx";
// Declare and initialise global variables/arrays for Thingspeak connection
const char server[] = "thingspeak.com";
const char thingspeakAPIKey[] = "xxxAPIpaSSxxx";
const int postingInterval = 60;
//Create new display object
SSD1306AsciiWire oled;
//Create new Neopixel object
Adafruit_NeoPixel ring = Adafruit_NeoPixel(neoPixels, neoPin, NEO_GRB);
//Create new RTC module object
RtcDS3231 rtcModule;
//Create new DHT object
#define DHTPIN 4 // what digital pin we're connected to
// Uncomment whatever type you're using!
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
//Initialise DHT-sensor
DHT dht(DHTPIN, DHTTYPE);
//Create new client object
WiFiEspClient client;
//Create new servo object
Adafruit_TiCoServo servo;
//Create WiFi module object on GPIO pin 6 (RX) and 7 (TX)
SoftwareSerial Serial1(6, 7);
//Declare and initialise variable for radio status
int status = WL_IDLE_STATUS;
//Declare global variables for time and temperature in RTC-module
int hours;
int minutes;
int seconds;
float temp;
//Declare global variables/arrays for timing
int oldMinute;
char lastSent[20];
void setup() {
// Enable I2C communication
Wire.begin();
// Start display on adress 0x3C
oled.begin(&Adafruit128x64, 0x3C);
oled.setFont(utf8font10x16);
oled.clear();
oled.print("Startar");
// Initialise serial for debugging
Serial.begin(115200);
Serial.println("Debugging är igång!");
dht.begin();
Serial.println("DHT11 är igång!");
servo.attach(9);
Serial.println("Servo är monterat!");
// Start RTC module
rtcModule.Begin();
Serial.println("RTC-modul är startad!");
// Start Neopixel ring
ring.begin();
ring.setBrightness(80);
ring.show();
Serial.println("NeoPixel Ring är startad!");
// add dot to start up animation
oled.print(".");
// Initialise serial for ESP module
Serial1.begin(9600);
Serial.println("WiFi-port öppen!");
// Initialise ESP module
WiFi.init(&Serial1);
Serial.println("WiFi är initierat!");
//Add dot to start up animation
oled.print(".");
// Check for the presence of the shield
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// Dont continue
while (true);
}
// Attempt to connect to WiFi network
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
status = WiFi.begin(ssid, pass);
Serial.println("WiFi är igång!");
// Add dot to start up animation
oled.print(".");
}
printWifiStatus();
printOled();
updateTemp();
//updateTempHum(); //Doesnt work, cant get the values out to thingspeak()
}
void loop() {
updateTime();
updateNeoRing();
// Update channel on Thingspeak (every postingInterval)
if (seconds % postingInterval == 0) {
updateTemp();
//updateTempHum();//Does not work, cant get the values out to sendthingspeak
sendThingspeak(); //Deleted value (temp) from inside the () so that I can get all data to thingspeak
}
client.flush();
client.stop();
delay(500);
}
void sendThingspeak() { //Deleted (float value) from inside the () so that I can get all data to thingspeak
//Read Temperature and Humidity
float t = dht.readTemperature();
float h = dht.readHumidity();
//Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t)) {
Serial.println("Kunde inte läsa från DHT-sensorn!");
return;
}
//Compute Heat Index
float hic = dht.computeHeatIndex(t, h, false);
Serial.println("");
Serial.print("Humiditet: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperatur: ");
Serial.print(t);
Serial.print(" *C ");
Serial.print("Värme index: ");
Serial.print(hic);
Serial.print(" *C ");
Serial.println("");
if (client.connectSSL(server, 443)) {
Serial.println("Ansluten till server.");
client.println("GET /update?api_key=" + String(thingspeakAPIKey) +
"&field1=" + String(temp) + "&field2=" + String(t) + "&field3=" + String(h) + " HTTP/1.1");
client.println("Host: api.thingspeak.com");
client.println("Connection: close");
client.println();
Serial.println("Information skickad till server.");
// Save sent time as char-array
sprintf(lastSent, "Sent: %02d:%02d:%02d", hours, minutes, seconds);
// Update time on display
printOled();
delay(1000);
}
}
void updateTime() {
oldMinute = minutes;
RtcDateTime now = rtcModule.GetDateTime();
// Save time in variables
hours = now.Hour();
minutes = now.Minute();
seconds = now.Second();
// Update screen on minute update
if (minutes != oldMinute) {
printOled();
}
// Update time on Neopixel ring
updateNeoRing();
}
void printOled() {
// Format and print time on display
oled.clear();
oled.setFont(lcdnums14x24);
oled.setCol(20);
char timeString[6];
sprintf(timeString, "%02d:%02d", hours, minutes);
oled.println(timeString);
// Format and print temperature on display
oled.setFont(utf8font10x16);
oled.setRow(6);
oled.setCol(100);
oled.print(int(temp));
oled.write(176);
// Format and print last sent time on display
oled.setCol(0);
oled.print(lastSent);
}
// Display time on Neopixel ring
void updateNeoRing() {
// Map minutes and hours to 24 pixels
int neoMin = round(minutes / 2.5 - 1);
int neoHour = hours % 12 * 2 - 1;
// Declare variables for number of blue and red pixels
int blue;
int red;
// Set colors on Neopixel according to time
for (int i = 0; i <= neoPixels; i++) {
if (i <= neoMin) {
blue = 255;
} else {
blue = 0;
}
if (i <= neoHour) {
red = 255;
} else {
red = 0;
}
ring.setPixelColor(i, ring.Color(red, 0, blue));
}
// Update Neopixel ring
ring.show();
}
void updateTemp() {
// Read temperature
RtcTemperature rtcTemp = rtcModule.GetTemperature();
temp = rtcTemp.AsFloat();
// Map temperature to servo
int tempPointer = map(temp, 18, 28, 30, 140);
// Turn servo to temperature
servo.write(tempPointer);
}
void updateTempHum() {
//Read Temperature and Humidity
float t = dht.readTemperature();
float h = dht.readHumidity();
//Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t)) {
Serial.println("Kunde inte läsa från DHT-sensorn!");
return;
//Compute Heat Index
float hic = dht.computeHeatIndex(t, h, false);
Serial.print("Humiditet: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperatur: ");
Serial.print(t);
Serial.print(" *C ");
Serial.print("Heat index: ");
Serial.print(hic);
Serial.print(" *C ");
}
}
void printWifiStatus() {
// Print the SSID of the network
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// Print the IP address
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
}