@peekay123 Thank you for reviewing my code and you suggestions.
I updated the program by slim fasten setup routine, implementation of the circular buffer and the optimized update_time function. You will find the program following. Do you think I will solve the problem? What is the relationship to the wrong values coming from MCP9808?
// OLED
#include "Adafruit_mfGFX.h"
#include "Adafruit_SSD1306_mfGFX.h"
#define OLED_RESET D4
Adafruit_SSD1306 display(OLED_RESET);
// MCP9808
#include "MCP9808.h"
MCP9808 mcp = MCP9808();
#define temp_iterations 12
float temp_avarage = 0;
float temp_values[temp_iterations] = {0};
int temp_value_pointer = 0;
// TCP client
#include "HttpClient.h"
HttpClient http;
// Headers currently need to be set at init, useful for API keys etc.
http_header_t headers[] = {
{ "Content-Type" , "application/x-www-form-urlencoded" },
{ "Accept" , "*/*"},
{ NULL, NULL } // NOTE: Always terminate headers will NULL
};
http_request_t request;
http_response_t response;
// Data Server Configuration
#define data_server "data.xxx"
#define data_port 80
#define data_path "/v1/"
#define data_feedID "temp_office"
#define transmit_interval 150000UL // 150000UL = 2,5min
// send_data notification (position on screen)
#define y_offset 3 // 3 for top
unsigned int x_offset = 62; // 124 for right
// SETUP
void setup() {
Serial.begin(9600);
Time.zone(+1);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3D (for the 128x64)
display.clearDisplay(); // clears the screen and buffer
display.setTextColor(WHITE);
while(! mcp.begin()){
display.println("MCP9808 not found");
display.display();
delay(500);
}
mcp.setResolution(MCP9808_SLOWEST);
display.println("MCP9808 OK");
display.display();
// initialize temp value array with current Temperature
float temp = round(mcp.getTemperature()*10)/10;
for (byte i=0; i < temp_iterations; i++) {
temp_values[i] = temp;
}
delay(500);
display.clearDisplay(); // clears the screen and buffer
display.display();
}
// LOOP
void loop() {
update_wifi();
update_time();
update_temp();
send_data();
}
void update_wifi() {
static unsigned long previousMillis_update_wifi;
if ((unsigned long) (millis() - previousMillis_update_wifi) > 5000UL) {
previousMillis_update_wifi = millis();
display.fillRect(0,0,100,10,BLACK); // clear area of signal streght and network
// checking if connected
if (WiFi.ready()){
// display singal strength
int wifi_rssi = WiFi.RSSI();
int wifi_quality;
wifi_quality = map(wifi_rssi, -1, -127, 100, 0);
if (wifi_quality > 0) {
display.drawFastVLine( 0, 6, 2, WHITE);
display.drawFastVLine( 1, 6, 2, WHITE);
display.setCursor(5,0);
}
if (wifi_quality >= 20) {
display.drawFastVLine( 3, 5, 3, WHITE);
display.drawFastVLine( 4, 5, 3, WHITE);
display.setCursor(8,0);
}
if (wifi_quality >= 40) {
display.drawFastVLine( 6, 4, 4, WHITE);
display.drawFastVLine( 7, 4, 4, WHITE);
display.setCursor(11,0);
}
if (wifi_quality >= 60) {
display.drawFastVLine( 9, 3, 5, WHITE);
display.drawFastVLine(10, 3, 5, WHITE);
display.setCursor(14,0);
}
if (wifi_quality >= 80) {
display.drawFastVLine(12, 2, 6, WHITE);
display.drawFastVLine(13, 2, 6, WHITE);
display.setCursor(17,0);
}
display.setFont(SansSerif_6);
display.print(WiFi.SSID());
}
else {
display.setCursor(0,0);
display.setFont(SansSerif_6);
display.print("no WiFi");
}
display.display();
}
}
void update_time() {
static unsigned long previousMillis_update_time;
static boolean second_blink;
if ((unsigned long) (millis() - previousMillis_update_time) > 1000UL) {
previousMillis_update_time = millis();
display.setFont(SansSerif_6);
String time_out = Time.format(Time.now(), "%H:%M");
// elaborate the width of the time_out
int time_out_width = 0;
for(int i = 0; time_out[i] != '\0'; i++) {
time_out_width += display.charWidth(time_out[i]);
}
// set data send notification before time
x_offset = 120 - time_out_width;
display.fillRect(100,0,128,10,BLACK); // clear area of time
display.setCursor(127 - time_out_width,0);
display.print(time_out);
display.display();
}
}
void update_temp(){
static unsigned long previousMillis_update_temp;
if ((unsigned long) (millis() - previousMillis_update_temp) > 5000UL) {
previousMillis_update_temp = millis();
// store current temperature in circular buffer for averaging
temp_value_pointer ++;
if (temp_value_pointer == temp_iterations-1) {
temp_value_pointer = 0;
}
temp_values[temp_value_pointer] = round(mcp.getTemperature()*10)/10;
// calculate avarage
temp_avarage = 0;
for (byte x=0; x < temp_iterations; x++){
temp_avarage = temp_avarage + temp_values[x];
}
temp_avarage = round((temp_avarage / temp_iterations)*10)/10;
display.fillRect(0,10,128,55,BLACK);
display.setFont(SansSerif_24);
// elaborate the width of the Temp. output for horizontal centering
String temp_out(temp_avarage, 1);
temp_out = temp_out.replace(".", ",");
int text_width = 0;
for(int i = 0; temp_out[i] != '\0'; i++) {
text_width += display.charWidth(temp_out[i]);
}
String temp_out2 = "~C";
int text_width2 = 0;
for(int i = 0; temp_out[i] != '\0'; i++) {
text_width2 = text_width2 + display.charWidth(temp_out2[i]);
}
text_width2 = text_width2 - 7; // fix for wrong width of °
int text_width_complete = text_width + text_width2 + 4; // // add a extra space between value and °C
display.setCursor( (128 - text_width_complete)/2 ,24);
display.print(temp_out);
display.setCursor( ((128 - text_width_complete)/2)+text_width+4 ,24);
display.print(temp_out2);
display.display();
}
}
void send_data() {
static unsigned long previousMillis_send_time;
if ((unsigned long) (millis() - previousMillis_send_time) > transmit_interval) {
previousMillis_send_time = millis();
display.drawCircle(x_offset, y_offset, 3, WHITE); display.display();
request.hostname = data_server;
request.port = data_port;
request.path = data_path;
request.body = "feedID=";
request.body += data_feedID;
request.body += "&value1=";
request.body += String(temp_avarage,1);
request.body += "\r\n";
http.post(request, response, headers); // POST request
if (response.status == 200){
display.fillCircle(x_offset, y_offset, 3, WHITE); display.display(); // draw dot
delay(500);
display.fillCircle(x_offset, y_offset, 3, BLACK); display.display(); // clear dot
}
else {
display.fillCircle(x_offset, y_offset, 3, BLACK); // clear dot
display.drawLine(x_offset -3, y_offset +3, x_offset +3, y_offset, WHITE); //x1=121 x2=127
display.drawLine(x_offset -3, y_offset, x_offset +3, y_offset+6, WHITE);
display.display();
delay(500);
display.fillRect(x_offset -3, 0, x_offset +3, 9,BLACK); //x1=110 x2=128
display.display();
}
}
}