#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include <Arduino.h> // this is needed for FT6206
#include <Adafruit_FT6206.h>
#include <Adafruit_SSD1306.h>
#include "DHTesp.h"
#include <WiFi.h>
#include "ThingSpeak.h" // always include thingspeak header file after other header files and custom macros
#include <U8g2_for_Adafruit_GFX.h>
Adafruit_FT6206 ctp = Adafruit_FT6206();
//------------------------------Thingspeak and wifi
#define SECRET_SSID "Wokwi-GUEST" // replace MySSID with your WiFi network name
#define SECRET_PASS "" // replace MyPassword with your WiFi password
#define SECRET_CH_ID 2355394 // replace 0000000 with your channel number
#define SECRET_WRITE_APIKEY "KK1OGBWEJRL2ZH8S" // replace XYZ with your channel write API Key
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password
int keyIndex = 0; // your network key Index number (needed only for WEP)
WiFiClient client;
unsigned long myChannelNumber = SECRET_CH_ID;
const char * myWriteAPIKey = SECRET_WRITE_APIKEY;
String myStatus = "";
//------------------------------
#define TFT_DC 6
#define TFT_CS 7
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
DHTesp dhtSensor;
#define BOXSIZE 40
//------------------------OLED
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define NUMFLAKES 10 // Number of snowflakes in the animation example
//-------------------------
//-------------------------------------------------計時
unsigned long previousMillis = 0;
unsigned long previousMillis_Thingspeak = 0;
const long interval = 1000;
const long interval_Thingspeak = 20000;
//-------------------------------------------------
const int DHT_PIN = 15;
int led = 39;
int Count = 0;
int r = 38;
int g = 37;
int b = 36;
//--------------------------------LGB bar
const int ledCount = 10; // the number of LEDs in the bar graph
int ledPins[] = {
1, 2, 42, 41, 40, 35, 0, 45, 48, 47
}; // an array of pin numbers to which LEDs are attached
//--------------------------------
U8G2_FOR_ADAFRUIT_GFX u8g2_for_adafruit_gfx;
void setup() {
Serial.begin(115200);
u8g2_for_adafruit_gfx.begin(display);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo native USB port only
}
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client); // Initialize ThingSpeak
pinMode(led, OUTPUT);
pinMode(r, OUTPUT);
pinMode(g, OUTPUT);
pinMode(b, OUTPUT);
// loop over the pin array and set them all to output:
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
pinMode(ledPins[thisLed], OUTPUT);
}
digitalWrite(r, LOW);
digitalWrite(g, LOW);
digitalWrite(b, LOW);
//Wire.setPins(10, 8);
tft.begin();
if (! ctp.begin(40)) { // pass in 'sensitivity' coefficient
Serial.println("Couldn't start FT6206 touchscreen controller");
while (1);
}
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3c)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
tft.setCursor(26, 120);
tft.setTextColor(ILI9341_RED);
tft.setTextSize(3);
tft.println("Hello, TFT!");
tft.setCursor(20, 160);
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(2);
tft.println("I can has colors?");
//delay(2000);
tft.fillScreen(ILI9341_BLACK);
tft.fillRect(0, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
tft.setCursor(13, 18);
tft.setTextColor(ILI9341_RED);
tft.setTextSize(1);
tft.println("ON!");
tft.fillRect(BOXSIZE+3, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
tft.setCursor(53, 18);
tft.setTextColor(ILI9341_RED);
tft.setTextSize(1);
tft.println("OFF!");
tft.fillRect((BOXSIZE*2)+6, 0, 80, BOXSIZE, ILI9341_WHITE);
tft.setCursor(93, 18);
tft.setTextColor(ILI9341_RED);
tft.setTextSize(1);
tft.println("Temp ON/OFF");
tft.fillRect(0, 43, BOXSIZE, BOXSIZE, ILI9341_WHITE);
tft.setCursor(13, 58);
tft.setTextColor(ILI9341_RED);
tft.setTextSize(1);
tft.println("Red");
tft.fillRect(BOXSIZE+3, 43, BOXSIZE, BOXSIZE, ILI9341_WHITE);
tft.setCursor(50, 58);
tft.setTextColor(ILI9341_RED);
tft.setTextSize(1);
tft.println("Green");
tft.fillRect((BOXSIZE*2)+6, 43, BOXSIZE, BOXSIZE, ILI9341_WHITE);
tft.setCursor(93, 58);
tft.setTextColor(ILI9341_RED);
tft.setTextSize(1);
tft.println("Blue");
}
void loop() {
// Connect or reconnect to WiFi
if(WiFi.status() != WL_CONNECTED){
Serial.print("Attempting to connect to SSID: ");
Serial.println(SECRET_SSID);
while(WiFi.status() != WL_CONNECTED){
WiFi.begin(ssid, pass); // Connect to WPA/WPA2 network. Change this line if using open or WEP network
Serial.print(".");
delay(5000);
}
Serial.println("\nConnected.");
}
Touch();
if(Count == 1)
{
Temp();
}
else{
display.clearDisplay();
// display.setTextSize(1);
// display.setTextColor(1);
// display.setCursor(0, 0);
// display.println("Temp:OFF");
u8g2_for_adafruit_gfx.setFontMode(1);
u8g2_for_adafruit_gfx.setFontDirection(0);
u8g2_for_adafruit_gfx.setForegroundColor(WHITE);
u8g2_for_adafruit_gfx.setFont(u8g2_font_wqy13_t_chinese1);
u8g2_for_adafruit_gfx.setCursor(54,32);
u8g2_for_adafruit_gfx.print("OFF");
display.display();
for (int i = 0; i <= 9; i++){
digitalWrite(ledPins[i], LOW);
}
}
//delay(90);
}
void Temp(){
TempAndHumidity data = dhtSensor.getTempAndHumidity();
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
int level = map(data.temperature, -40, 80, 0, 10);
Serial.println("Temp: " + String(data.temperature, 2) + "°C");
Serial.println("Humidity: " + String(data.humidity, 1) + "%");
Serial.println("---");
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
// if the array element's index is less than ledLevel,
// turn the pin for this element on:
if (thisLed < level) {
digitalWrite(ledPins[thisLed], HIGH);
}
// turn off all pins higher than the ledLevel:
else {
digitalWrite(ledPins[thisLed], LOW);
}
}
// display.clearDisplay();
// display.setTextSize(1);
// display.setTextColor(1);
// display.setCursor(0, 0);
// display.println("Temp:ON");
// display.println("Temp:" + String(data.temperature, 2) + "C");
// display.println("Humidity:" + String(data.humidity, 1) + "%");
// display.display();
display.clearDisplay(); // clear the graphcis buffer
u8g2_for_adafruit_gfx.setFontMode(1); // use u8g2 transparent mode (this is default)
u8g2_for_adafruit_gfx.setFontDirection(0); // left to right (this is default)
u8g2_for_adafruit_gfx.setForegroundColor(WHITE); // apply Adafruit GFX color
u8g2_for_adafruit_gfx.setFont(u8g2_font_wqy13_t_chinese1); // select u8g2 font from here: https://github.com/olikraus/u8g2/wiki/fntlistall
u8g2_for_adafruit_gfx.setCursor(0,20); // start writing at this position
u8g2_for_adafruit_gfx.print("Temp:" + String(data.temperature, 2) + "度");
u8g2_for_adafruit_gfx.setCursor(0,40); // start writing at this position
u8g2_for_adafruit_gfx.print("Humidity:" + String(data.humidity, 1) + "%"); // UTF-8 string with german umlaut chars
display.display(); // make everything visible
}
unsigned long currentMillis_Thingspeak = millis();
if (currentMillis_Thingspeak - previousMillis_Thingspeak >= interval_Thingspeak) {
previousMillis_Thingspeak = currentMillis_Thingspeak;
// set the fields with the values
ThingSpeak.setField(1, data.temperature);
ThingSpeak.setField(2, data.humidity);
// set the status
ThingSpeak.setStatus(myStatus);
// write to the ThingSpeak channel
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));
}
}
}
void Touch(){
delay(10);
// Wait for a touch
if (! ctp.touched()) {
return;
}
TS_Point p = ctp.getPoint();
p.x = map(p.x, 0, 240, 240, 0);
p.y = map(p.y, 0, 320, 320, 0);
Serial.print("("); Serial.print(p.x);
Serial.print(", "); Serial.print(p.y);
Serial.println(")");
if (p.y < BOXSIZE){
if (p.x < 40){
digitalWrite(led, HIGH);
}
else if (p.x < 40*2){
digitalWrite(led, LOW);
}
else if (p.x < 40*3 || p.x < 40*4){
Count++;
if(Count > 1)
Count = 0;
}
}
else if (p.y < BOXSIZE*2)
{
if (p.x < BOXSIZE){
digitalWrite(r, HIGH);
digitalWrite(g, LOW);
digitalWrite(b, LOW);
}
else if (p.x < BOXSIZE*2){
digitalWrite(r, LOW);
digitalWrite(g, HIGH);
digitalWrite(b, LOW);
}
else if (p.x < BOXSIZE*3){
digitalWrite(r, LOW);
digitalWrite(g, LOW);
digitalWrite(b, HIGH);
}
}
}