Merge branch 'main' of gitlab.fdmci.hva.nl:propedeuse-hbo-ict/onderwijs/2023-2024/out-a-se-ti/blok-3/qaajeeqiinii59

This commit is contained in:
Bram Barbieri
2024-02-27 20:22:35 +01:00
6 changed files with 618 additions and 22 deletions

Binary file not shown.

View File

@@ -0,0 +1,40 @@
#include <SPI.h>
#include "Adafruit_GFX.h"
#include "Adafruit_ST7796S_kbv.h"
#define TFT_CS 14
#define TFT_DC 13
#define TFT_RST 12 // RST can be set to -1 if you tie it to Arduino's reset
#define MOSI 11
#define SCK 10
#define MISO 9
//simplify the name of the library
Adafruit_ST7796S_kbv tft = Adafruit_ST7796S_kbv(TFT_CS, TFT_DC, MOSI, SCK, TFT_RST, MISO);
void setup() {
tft.begin(); // Initialize the display
tft.setRotation(3); // Set the rotation
tft.fillScreen(ST7796S_BLACK); // Fill the screen with black color
tft.setTextColor(ST7796S_WHITE); // Set the text color to white
tft.setTextSize(2); // Set the text size to 2
}
void loop() {
tft.setTextSize(5); // Set textsize
tft.setCursor(0, 0); // Set the cursor to the top left corner
//the cursor is where it types the text or shape on the screen, 0,0 is the topleft
tft.println("Hello, world!"); // Print the text
delay(2000); // Wait for 2 seconds
tft.fillScreen(ST7796S_BLACK); // Clear the screen
tft.setCursor(0, 0); // Set the cursor to the top left corner
tft.println("New text!"); // Print the new text
delay(2000); // Wait for 2 seconds
tft.fillScreen(ST7796S_BLACK); // Clear the screen
}

View File

@@ -0,0 +1,327 @@
/***************************************************
This is our library for the Adafruit HX8357D Breakout
----> http://www.adafruit.com/products/2050
Check out the links above for our tutorials and wiring diagrams
These displays use SPI to communicate, 4 or 5 pins are required to
interface (RST is optional)
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries.
MIT license, all text above must be included in any redistribution
****************************************************/
#include <SPI.h>
#include "Adafruit_GFX.h"
#include "Adafruit_ST7796S_kbv.h"
// These are 'flexible' lines that can be changed
#define TFT_CS 14
#define TFT_DC 13
#define TFT_RST 12 // RST can be set to -1 if you tie it to Arduino's reset
#define MOSI 11
#define SCK 10
#define MISO 9
// Use hardware SPI (on Uno, #13, #12, #11) and the above for CS/DC
//Adafruit_ST7796S_kbv tft = Adafruit_ST7796S_kbv(TFT_CS, TFT_DC, TFT_RST);
// SoftSPI - note that on some processors this might be *faster* than hardware SPI!
Adafruit_ST7796S_kbv tft = Adafruit_ST7796S_kbv(TFT_CS, TFT_DC, MOSI, SCK, TFT_RST, MISO);
void setup() {
Serial.begin(9600);
Serial.println("ST7796S_kbv Test!");
tft.begin();
// read diagnostics (optional but can help debug problems)
uint8_t x = tft.readcommand8(ST7796S_RDMODE);
Serial.print("Display Power Mode: 0x"); Serial.println(x, HEX);
x = tft.readcommand8(ST7796S_RDMADCTL);
Serial.print("MADCTL Mode: 0x"); Serial.println(x, HEX);
x = tft.readcommand8(ST7796S_RDPIXFMT);
Serial.print("Pixel Format: 0x"); Serial.println(x, HEX);
x = tft.readcommand8(ST7796S_RDIMGFMT);
Serial.print("Image Format: 0x"); Serial.println(x, HEX);
x = tft.readcommand8(ST7796S_RDSELFDIAG);
Serial.print("Self Diagnostic: 0x"); Serial.println(x, HEX);
Serial.println(F("Benchmark Time (microseconds)"));
tft.setRotation(1);
Serial.print(F("Text "));
Serial.println(testText());
delay(500);
Serial.print(F("Lines "));
Serial.println(testLines(ST7796S_CYAN));
delay(500);
Serial.print(F("Rectangles (outline) "));
Serial.println(testRects(ST7796S_GREEN));
delay(500);
tft.fillScreen(ST7796S_BLACK);
Serial.print(F("Circles (outline) "));
Serial.println(testCircles(10, ST7796S_RED));
delay(500);
Serial.print(F("Triangles (outline) "));
Serial.println(testTriangles());
delay(500);
Serial.print(F("Triangles (filled) "));
Serial.println(testFilledTriangles());
delay(500);
Serial.print(F("Rounded rects (outline) "));
Serial.println(testRoundRects());
delay(500);
Serial.print(F("Rounded rects (filled) "));
Serial.println(testFilledRoundRects());
delay(500);
Serial.println(F("Done!"));
}
void loop(void) {
for(uint8_t rotation=0; rotation<4; rotation++) {
tft.setRotation(rotation);
testText();
delay(1000);
}
}
unsigned long testFillScreen() {
unsigned long start = micros();
tft.fillScreen(ST7796S_RED);
tft.fillScreen(ST7796S_GREEN);
tft.fillScreen(ST7796S_BLUE);
tft.fillScreen(ST7796S_WHITE);
return micros() - start;
}
unsigned long testText() {
tft.fillScreen(ST7796S_BLACK);
unsigned long start = micros();
tft.setCursor(0, 0);
tft.setTextColor(ST7796S_WHITE); tft.setTextSize(1);
tft.println("Hello World!");
tft.setTextColor(ST7796S_YELLOW); tft.setTextSize(2);
tft.println(1234.56);
tft.setTextColor(ST7796S_RED); tft.setTextSize(3);
tft.println(0xDEADBEEF, HEX);
tft.println();
tft.setTextColor(ST7796S_GREEN);
tft.setTextSize(5);
tft.println("Groop");
tft.setTextSize(2);
tft.println("I implore thee,");
tft.setTextSize(1);
tft.println("my foonting turlingdromes.");
tft.println("And hooptiously drangle me");
tft.println("with crinkly bindlewurdles,");
tft.println("Or I will rend thee");
tft.println("in the gobberwarts");
tft.println("with my blurglecruncheon,");
tft.println("see if I don't!");
tft.setTextColor(ST7796S_WHITE);
tft.println(F("Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do: once or twice she had peeped into the book her sister was reading, but it had no pictures or conversations in it, 'and what is the use of a book,' thought Alice 'without pictures or conversations?'"));
tft.println(F("So she was considering in her own mind (as well as she could, for the hot day made her feel very sleepy and stupid), whether the pleasure of making a daisy-chain would be worth the trouble of getting up and picking the daisies, when suddenly a White Rabbit with pink eyes ran close by her."));
tft.println(F("There was nothing so very remarkable in that; nor did Alice think it so very much out of the way to hear the Rabbit say to itself, 'Oh dear! Oh dear! I shall be late!' (when she thought it over afterwards, it occurred to her that she ought to have wondered at this, but at the time it all seemed quite natural); but when the Rabbit actually took a watch out of its waistcoat-pocket, and looked at it, and then hurried on, Alice started to her feet, for it flashed across her mind that she had never before seen a rabbit with either a waistcoat-pocket, or a watch to take out of it, and burning with curiosity, she ran across the field after it, and fortunately was just in time to see it pop down a large rabbit-hole under the hedge."));
tft.println(F("In another moment down went Alice after it, never once considering how in the world she was to get out again."));
tft.println(F("The rabbit-hole went straight on like a tunnel for some way, and then dipped suddenly down, so suddenly that Alice had not a moment to think about stopping herself before she found herself falling down a very deep well."));
tft.println(F("Either the well was very deep, or she fell very slowly, for she had plenty of time as she went down to look about her and to wonder what was going to happen next. First, she tried to look down and make out what she was coming to, but it was too dark to see anything; then she looked at the sides of the well, and noticed that they were filled with cupboards and book-shelves; here and there she saw maps and pictures hung upon pegs. She took down a jar from one of the shelves as she passed; it was labelled 'ORANGE MARMALADE', but to her great disappointment it was empty: she did not like to drop the jar for fear of killing somebody, so managed to put it into one of the cupboards as she fell past it."));
return micros() - start;
}
unsigned long testLines(uint16_t color) {
unsigned long start, t;
int x1, y1, x2, y2,
w = tft.width(),
h = tft.height();
tft.fillScreen(ST7796S_BLACK);
x1 = y1 = 0;
y2 = h - 1;
start = micros();
for(x2=0; x2<w; x2+=6) tft.drawLine(x1, y1, x2, y2, color);
x2 = w - 1;
for(y2=0; y2<h; y2+=6) tft.drawLine(x1, y1, x2, y2, color);
t = micros() - start; // fillScreen doesn't count against timing
return micros() - start;
}
unsigned long testFastLines(uint16_t color1, uint16_t color2) {
unsigned long start;
int x, y, w = tft.width(), h = tft.height();
tft.fillScreen(ST7796S_BLACK);
start = micros();
for(y=0; y<h; y+=5) tft.drawFastHLine(0, y, w, color1);
for(x=0; x<w; x+=5) tft.drawFastVLine(x, 0, h, color2);
return micros() - start;
}
unsigned long testRects(uint16_t color) {
unsigned long start;
int n, i, i2,
cx = tft.width() / 2,
cy = tft.height() / 2;
tft.fillScreen(ST7796S_BLACK);
n = min(tft.width(), tft.height());
start = micros();
for(i=2; i<n; i+=6) {
i2 = i / 2;
tft.drawRect(cx-i2, cy-i2, i, i, color);
}
return micros() - start;
}
unsigned long testFilledRects(uint16_t color1, uint16_t color2) {
unsigned long start, t = 0;
int n, i, i2,
cx = tft.width() / 2 - 1,
cy = tft.height() / 2 - 1;
tft.fillScreen(ST7796S_BLACK);
n = min(tft.width(), tft.height());
for(i=n; i>0; i-=6) {
i2 = i / 2;
start = micros();
tft.fillRect(cx-i2, cy-i2, i, i, color1);
t += micros() - start;
// Outlines are not included in timing results
tft.drawRect(cx-i2, cy-i2, i, i, color2);
}
return t;
}
unsigned long testFilledCircles(uint8_t radius, uint16_t color) {
unsigned long start;
int x, y, w = tft.width(), h = tft.height(), r2 = radius * 2;
tft.fillScreen(ST7796S_BLACK);
start = micros();
for(x=radius; x<w; x+=r2) {
for(y=radius; y<h; y+=r2) {
tft.fillCircle(x, y, radius, color);
}
}
return micros() - start;
}
unsigned long testCircles(uint8_t radius, uint16_t color) {
unsigned long start;
int x, y, r2 = radius * 2,
w = tft.width() + radius,
h = tft.height() + radius;
// Screen is not cleared for this one -- this is
// intentional and does not affect the reported time.
start = micros();
for(x=0; x<w; x+=r2) {
for(y=0; y<h; y+=r2) {
tft.drawCircle(x, y, radius, color);
}
}
return micros() - start;
}
unsigned long testTriangles() {
unsigned long start;
int n, i, cx = tft.width() / 2 - 1,
cy = tft.height() / 2 - 1;
tft.fillScreen(ST7796S_BLACK);
n = min(cx, cy);
start = micros();
for(i=0; i<n; i+=5) {
tft.drawTriangle(
cx , cy - i, // peak
cx - i, cy + i, // bottom left
cx + i, cy + i, // bottom right
tft.color565(200, 20, i));
}
return micros() - start;
}
unsigned long testFilledTriangles() {
unsigned long start, t = 0;
int i, cx = tft.width() / 2 - 1,
cy = tft.height() / 2 - 1;
tft.fillScreen(ST7796S_BLACK);
start = micros();
for(i=min(cx,cy); i>10; i-=5) {
start = micros();
tft.fillTriangle(cx, cy - i, cx - i, cy + i, cx + i, cy + i,
tft.color565(0, i, i));
t += micros() - start;
tft.drawTriangle(cx, cy - i, cx - i, cy + i, cx + i, cy + i,
tft.color565(i, i, 0));
}
return t;
}
unsigned long testRoundRects() {
unsigned long start;
int w, i, i2,
cx = tft.width() / 2 ,
cy = tft.height() / 2 ;
tft.fillScreen(ST7796S_BLACK);
w = min(tft.width(), tft.height());
start = micros();
for(i=0; i<w; i+=8) {
i2 = i / 2 - 2;
tft.drawRoundRect(cx-i2, cy-i2, i, i, i/8, tft.color565(i, 100, 100));
}
return micros() - start;
}
unsigned long testFilledRoundRects() {
unsigned long start;
int i, i2,
cx = tft.width() / 2 + 10,
cy = tft.height() / 2 + 10;
tft.fillScreen(ST7796S_BLACK);
start = micros();
for(i=min(tft.width(), tft.height()) - 20; i>25; i-=6) {
i2 = i / 2;
tft.fillRoundRect(cx-i2, cy-i2, i-20, i-20, i/8, tft.color565(100, i/2, 100));
}
return micros() - start;
}

View File

@@ -1,10 +1,13 @@
// Sietse Jonker & Dano Bosch
// 13/02/2024
// 27/02/2024
#include <Wire.h>
#include <Adafruit_SH110X.h>
#include <Adafruit_SGP30.h>
#include <DHT.h>
#include <WiFiMulti.h>
#include <WiFi.h>
#include <WebSocketsClient.h>
#define MICPIN 6
#define DHTPIN 7
@@ -14,12 +17,16 @@
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define i2c_adress 0x3c
#define USE_SERIAL Serial
// make new objects
Adafruit_SH1106G display = Adafruit_SH1106G(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire);
DHT dht(DHTPIN, DHTTYPE);
WiFiMulti WiFiMulti;
Adafruit_SGP30 sgp;
WebSocketsClient webSocket;
// define variables
uint16_t TVOC_base, eCO2_base;
uint16_t counter = 0;
float temperature = 0;
@@ -27,7 +34,79 @@ float humidity = 0;
uint16_t eCO2 = 0;
uint16_t TVOC = 0;
bool noise = false;
unsigned long currentCounter;
unsigned long lastCounter;
uint16_t interval = 5000;
bool errorSGP30 = false;
bool errorDHT11 = false;
// hexdump function for websockets binary handler
void hexdump(const void *mem, uint32_t len, uint8_t cols = 16) {
const uint8_t* src = (const uint8_t*) mem;
USE_SERIAL.printf("\n[HEXDUMP] Address: 0x%08X len: 0x%X (%d)", (ptrdiff_t)src, len, len);
for(uint32_t i = 0; i < len; i++) {
if(i % cols == 0) {
USE_SERIAL.printf("\n[0x%08X] 0x%08X: ", (ptrdiff_t)src, i);
}
USE_SERIAL.printf("%02X ", *src);
src++;
}
USE_SERIAL.printf("\n");
}
// handler for websocket events
void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) {
switch(type) {
case WStype_DISCONNECTED:
USE_SERIAL.printf("[WSc] Disconnected!\n");
break;
case WStype_CONNECTED:
USE_SERIAL.printf("[WSc] Connected to url: %s\n", payload);
// send message to server when Connected
webSocket.sendTXT("Connected");
break;
case WStype_TEXT:
USE_SERIAL.printf("[WSc] get text: %s\n", payload);
// send message to server
// webSocket.sendTXT("message here");
break;
case WStype_BIN:
USE_SERIAL.printf("[WSc] get binary length: %u\n", length);
hexdump(payload, length);
// send data to server
// webSocket.sendBIN(payload, length);
break;
case WStype_ERROR:
case WStype_FRAGMENT_TEXT_START:
case WStype_FRAGMENT_BIN_START:
case WStype_FRAGMENT:
case WStype_FRAGMENT_FIN:
break;
}
}
// special function to setup websocket
void websocketSetup(){
WiFiMulti.addAP("iotroam", "vbK9gbDBIB");
while(WiFiMulti.run() != WL_CONNECTED) {
delay(100);
}
// server address, port and URL
webSocket.begin("145.92.8.114", 80, "/ws");
// event handler
webSocket.onEvent(webSocketEvent);
// try ever 500 again if connection has failed
webSocket.setReconnectInterval(500);
}
// fucntion to reset the values if needed
void resetValues() {
TVOC_base;
eCO2_base;
@@ -37,8 +116,48 @@ void resetValues() {
eCO2 = 0;
TVOC = 0;
noise = false;
lastCounter = 0;
}
// function to check for errors in sensors
bool checkForError(){
if (!sgp.IAQmeasure()) {
Serial.println("SGP30: BAD");
errorSGP30 = true;
} else {
Serial.println("SGP30: OK");
errorSGP30 = false;
}
if (isnan(temperature) || isnan(humidity)){
Serial.println("DHT11: BAD");
} else {
Serial.println("DHT11: OK");
errorDHT11 = false;
}
}
// function to update when interval is met (see intervalCounter variable)
void update(){
webSocket.sendTXT("Temp: " + String(temperature));
webSocket.sendTXT("Humi: " + String(humidity));
webSocket.sendTXT("eCO2: " + String(sgp.eCO2));
webSocket.sendTXT("TVOC: " + String(sgp.TVOC));
sgp.getIAQBaseline(&eCO2_base, &TVOC_base);
// if sensor doesnt work print in serialmonitor
// read dht11 sensor
temperature = float(dht.readTemperature());
humidity = float(dht.readHumidity());
// display sensordata on oled screen
displayData();
}
// function to display data on oled screen
void displayData() {
// clear display for new info
display.clearDisplay();
@@ -57,6 +176,7 @@ void displayData() {
display.display();
}
// setup function
void setup() {
Serial.begin(115200);
@@ -70,30 +190,19 @@ void setup() {
pinMode(MICPIN, INPUT);
pinMode(DHTPIN, INPUT);
websocketSetup();
resetValues();
}
// loop function
void loop() {
// if sensor doesnt work print in serialmonitor
if (!sgp.IAQmeasure()) {
Serial.println("SGP30: BAD");
return;
} else {
Serial.println("SGP30: OK");
webSocket.loop();
if (currentCounter - lastCounter >= interval){
lastCounter = millis();
update();
}
// read dht11 sensor
temperature = float(dht.readTemperature());
humidity = float(dht.readHumidity());
// display sensordata on oled screen
displayData();
// get new baseline every 30 seconds
if (counter == 30) {
sgp.getIAQBaseline(&eCO2_base, &TVOC_base);
}
delay(1000);
counter++;
currentCounter = millis();
}

View File

@@ -0,0 +1,112 @@
/*
* WebSocketClient.ino
*
* Created on: 24.05.2015
*
*/
#include <Arduino.h>
#include <WiFi.h>
#include <WiFiMulti.h>
#include <WiFiClientSecure.h>
#include <WebSocketsClient.h>
WiFiMulti WiFiMulti;
WebSocketsClient webSocket;
#define USE_SERIAL Serial1
void hexdump(const void *mem, uint32_t len, uint8_t cols = 16) {
const uint8_t* src = (const uint8_t*) mem;
USE_SERIAL.printf("\n[HEXDUMP] Address: 0x%08X len: 0x%X (%d)", (ptrdiff_t)src, len, len);
for(uint32_t i = 0; i < len; i++) {
if(i % cols == 0) {
USE_SERIAL.printf("\n[0x%08X] 0x%08X: ", (ptrdiff_t)src, i);
}
USE_SERIAL.printf("%02X ", *src);
src++;
}
USE_SERIAL.printf("\n");
}
void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) {
switch(type) {
case WStype_DISCONNECTED:
USE_SERIAL.printf("[WSc] Disconnected!\n");
break;
case WStype_CONNECTED:
USE_SERIAL.printf("[WSc] Connected to url: %s\n", payload);
// send message to server when Connected
webSocket.sendTXT("Connected");
break;
case WStype_TEXT:
USE_SERIAL.printf("[WSc] get text: %s\n", payload);
// send message to server
// webSocket.sendTXT("message here");
break;
case WStype_BIN:
USE_SERIAL.printf("[WSc] get binary length: %u\n", length);
hexdump(payload, length);
// send data to server
// webSocket.sendBIN(payload, length);
break;
case WStype_ERROR:
case WStype_FRAGMENT_TEXT_START:
case WStype_FRAGMENT_BIN_START:
case WStype_FRAGMENT:
case WStype_FRAGMENT_FIN:
break;
}
}
void setup() {
// USE_SERIAL.begin(921600);
USE_SERIAL.begin(115200);
//Serial.setDebugOutput(true);
USE_SERIAL.setDebugOutput(true);
USE_SERIAL.println();
USE_SERIAL.println();
USE_SERIAL.println();
for(uint8_t t = 4; t > 0; t--) {
USE_SERIAL.printf("[SETUP] BOOT WAIT %d...\n", t);
USE_SERIAL.flush();
delay(1000);
}
WiFiMulti.addAP("iotroam", "vbK9gbDBIB");
//WiFi.disconnect();
while(WiFiMulti.run() != WL_CONNECTED) {
delay(100);
}
// server address, port and URL
webSocket.begin("145.92.8.114", 80, "/ws");
// event handler
webSocket.onEvent(webSocketEvent);
// use HTTP Basic Authorization this is optional remove if not needed
// try ever 5000 again if connection has failed
webSocket.setReconnectInterval(5000);
}
void loop() {
webSocket.loop();
webSocket.sendTXT("test websocket 45050");
}

View File

@@ -7,6 +7,14 @@ Afspraken documenten delen Gitlab, discord
Geen spaties in gitlab documenten/mappen
Procedure bij niet-nakomen afspraken Communicatie is sleutel
Definition of done:
- Code is geschreven
- Code is getest
- Code is gereviewd
- Code is gedocumenteerd
- Code Werkt
- Code is geïntegreerd
Taakverdeling
Veranderd elke sprint