zaterdag 24 oktober 2020

Weerstation met BME280 en Oled i2c

 /*

 * Arduino with SSD1306 OLED display (128x64 Pixel) and BME280 sensor.

 * BME280 is barometric pressure, temperature and humidity sensor.

 */


#include <Wire.h>              // include Arduino wire library (required for I2C devices)

#include <Adafruit_GFX.h>      // include Adafruit graphics library

#include <Adafruit_SSD1306.h>  // include Adafruit SSD1306 OLED display driver

#include <Adafruit_BME280.h>   // include Adafruit BME280 sensor library

 

#define OLED_RESET  4    // define display reset pin


#define SCREEN_WIDTH 128 // OLED display width, in pixels

#define SCREEN_HEIGHT 64 // OLED display height, in pixels


#define SEALEVELPRESSURE_HPA (1013.25)


// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

 

// define device I2C address: 0x76 or 0x77 (0x77 is library default address)

#define BME280_I2C_ADDRESS  0x76

 

Adafruit_BME280  bme280;  // initialize Adafruit BME280 library

 

void setup(void)

{

  delay(1000);  // wait a second

 

  // initialize the SSD1306 OLED display with I2C address = 0x3D

  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);

 

  // clear the display buffer.

  display.clearDisplay();

 

  display.setTextSize(1);   // text size = 1

  display.setTextColor(WHITE, BLACK);  // set text color to white and black background


  display.setTextSize(1);

   display.invertDisplay(true);

  display.setTextColor(WHITE);

  display.setCursor(20, 4);

  display.println("Arduino Projects");

  display.drawRoundRect(0, 0, 128, 16, 4, WHITE); //drawRect(x, y, width, height, color) 

    display.setCursor(20, 20);

  display.setTextSize(2);

      display.setCursor(20, 20);

    display.println("By");

      display.setTextSize(3);

          display.setCursor(10, 40);

      display.println("Spike");

     

  


  

 

  // initialize the BME280 sensor

  if( bme280.begin(BME280_I2C_ADDRESS) == 0 )

  {  // connection error or device address wrong!

    display.setCursor(34, 23);

    display.print("Connection");

    display.setCursor(49, 33);

    display.print("Error");

    display.display();        // update the display

    while(1);                 // stay here

  }

 


  display.display();        // update the display

 delay(2000);

  display.invertDisplay(false);

}

 

char _buffer[12];

 

void loop()

{    

  display.clearDisplay();

  display.setTextColor(WHITE, BLACK); 

   display.setTextSize(1); 

  

  // read temperature, humidity and pressure from the BME280 sensor

  float temp = bme280.readTemperature();    // get temperature in degree Celsius

  float humi = bme280.readHumidity();       // get humidity in rH%

  float pres = bme280.readPressure();       // get pressure in Pa

  float alt = bme280.readAltitude((SEALEVELPRESSURE_HPA)); 


  display.setCursor(30, 3);

  // Display static text

  display.println("Weer Station");

//  display.drawRect(10, 10, 50, 30, WHITE);  //drawRect(x, y, width, height, color) 

display.drawRoundRect(0, 0, 120, 16, 4, WHITE);





 

  // print all data on the display

  display.setCursor(0, 20);

  display.print("TEMP:");


  display.setCursor(50, 20);

  display.print(temp);

  // print degree symbols ( ° )

  display.drawRect(85, 20, 3, 3, WHITE);

    

   display.setCursor(0, 30);

  display.print("HUMI:");

  display.setCursor(50, 30);

  display.print(humi);

display.print(" %");



   display.setCursor(0, 40);

  display.print("Luchtd:");

  display.setCursor(50, 40);

  display.print((pres/100));

  display.print(" hPa");


   display.setCursor(0, 50);

  display.print("Alt");

  display.setCursor(50, 50);

  display.print((alt));

  display.print(" m");

 

  // update the display

  display.display();

 

  delay(3000);  // wait a second








//Temp weergave

 display.clearDisplay();

   display.setCursor(20, 4);

  // Display static text

  display.println("Temperatuur");

display.drawRoundRect(0, 0, 120, 16, 4, WHITE); 

 display.setTextSize(2);   // text size = 1

  display.setCursor(0, 20);

  display.print("TEMP:");

  display.setCursor(20, 45);

  display.print(temp);

  display.drawRect(90, 45, 5, 5, WHITE);//graden symbool

  display.display();


  delay(3000);  // wait a second


//Humi weergave

 display.clearDisplay();

   display.setCursor(20, 4);

display.setTextSize(1);

  display.println("Rel Vochtigheid");

display.drawRoundRect(0, 0, 120, 16, 4, WHITE); 

 display.setTextSize(2);   // text size = 1

  display.setCursor(0, 20);

  display.print("Rel Vocht:");

  display.setCursor(20, 45);

  display.print(humi);

    display.print(" %");

  display.display();


  delay(3000);  // wait a second


//Luchtdruk weergave

 display.clearDisplay();

   display.setCursor(20, 4);

display.setTextSize(1);

  display.println("Luchtdruk");

display.drawRoundRect(0, 0, 120, 16, 4, WHITE); 

 display.setTextSize(2);   

  display.setCursor(0, 20);

  display.print("Luchtdruk:");

  display.setCursor(0, 45);

  display.print(pres/100);

    display.print("hPa");

  display.display();


  delay(3000);  // wait a second




// Big screen temp

//  display.setTextColor(BLACK,WHITE);

//display.fillRoundRect(0, 0, 128, 96, 2, WHITE);


 display.invertDisplay(true);

 

  display.clearDisplay(); 

  display.display();

  display.setCursor(30, 4);

  display.setTextSize(1);

  display.println("Temperatuur");

  display.setTextSize(3); 

  display.setCursor(10, 30);

  display.println(temp);

    // print degree symbols ( ° )

  display.drawRect(105, 30, 5, 5, WHITE);

  display.display();

  delay (2000);

  

// Big screen vocht

 display.clearDisplay();

 display.setCursor(30, 4);

 display.setTextSize(1);

 display.println("Vochtigheid %");

 display.setTextSize(3); 

 display.setCursor(10, 30);

 display.println(humi);

 display.display();

 delay(2000);

 

// Big screen luchtdruk

 display.clearDisplay();

 display.setCursor(20, 4);

 display.setTextSize(1);

 display.println("Luchtdruk in hPa");

 display.setTextSize(3); 

 display.setCursor(0, 30);

 display.println(pres/100);

 display.display();

 delay(2000);


 display.invertDisplay(false);

 

}

 

// end of code.

zondag 18 oktober 2020

i2c scanner code

 


//i found the scanning script at CircuitMagic.com so some credits go to them

#include <Wire.h>
#include <SPI.h>
//#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for SSD1306 display connected using software SPI (default case):
#define OLED_MOSI   9   // SDA op display
#define OLED_CLK   10   // SCL op display
#define OLED_DC    11   // DC op display
#define OLED_CS    12   // (niet op deze display aanwezig 
#define OLED_RESET 13   // RES op diplay
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT,
  OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
  
void setup() {
   Wire.begin(); //i2c start
  Serial.begin(9600);
 
   // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if(!display.begin(SSD1306_SWITCHCAPVCC)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
 }

  display.clearDisplay();
  display.setTextSize(3);             // Normal 1:1 pixel scale
  display.setTextColor(SSD1306_BLACK, SSD1306_WHITE); // Draw 'inverse' text
  display.setCursor(0,0);             // Start at top-left corner
  display.println(" Spike ");
  display.setTextSize(2);
  display.setTextColor(SSD1306_WHITE);        // Draw white text
  display.setCursor(10,30);             // Start at top-left corner
  display.println("Arduino");
  display.setCursor(10,50);             // Start at top-left corner
  display.println("Projects");
  display.display();// – call this method for the changes to make effect
  delay(2000);
  
}

void loop() {
 byte error, address;
  int nDevices;
  Serial.println("Scanning I2C bus...");
  display.clearDisplay();
  display.setTextSize(2);             // Normal 1:1 pixel scale
  display.setTextColor(SSD1306_WHITE);        // Draw white text
  display.setCursor(0,0);             // Start at top-left corner
  display.println("Scanning ");
  display.setCursor(20,20);             // Start at top-left corner
  display.println("I2C Bus");
  display.display();// – call this method for the changes to make effect

  nDevices = 0;
  for(address = 1; address < 127; address++ ) 
  {
    // The i2c_scanner uses the return value of
    // the Write.endTransmisstion to see if
    // a device did acknowledge to the address.
    Wire.beginTransmission(address);
    error = Wire.endTransmission();

    if (error == 0)
    {
      Serial.print("I2C device found at address 0x");
      if (address<16) 
        Serial.print("0");
      Serial.print(address,HEX);
      Serial.println("  !");

  display.setTextSize(3);             // Normal 1:1 pixel scale
  display.setCursor(30,40);             // Start at top-left corner
  display.print("0x");
  display.print(address,HEX);
  display.display();//

delay(1000);
 
       nDevices++;
    }
    else if (error==4) 
    {
      Serial.print("Unknow error at address 0x");
      if (address<16) 
        Serial.print("0");
      Serial.println(address,HEX);
    }    
  }
  if (nDevices == 0){
    Serial.println("No I2C devices \n");


  display.setTextSize(1);             // Normal 1:1 pixel scale
  display.setTextColor(SSD1306_WHITE);        // Draw white text
  display.setCursor(0,40);             // Start at top-left corner
  display.println("No I2C devices found");
  display.display();// – call this method for the changes to make effect
  }
 
  else
    Serial.println("done\n");
    display.clearDisplay();

  delay(1000); // wait 1 second for next scan
}

zaterdag 10 oktober 2020

Code voor tankinhoud met 2 displays

 


#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space

#include <SPI.h>

#include <ESP8266WiFi.h>

#include <BlynkSimpleEsp8266.h>

#include <SimpleTimer.h>

#include <DHT.h>                      //Temp sensor

#include <Wire.h>

#include <LiquidCrystal_I2C.h>   //LCD display

#include <Adafruit_GFX.h>         //Nokia Display

#include <Adafruit_PCD8544.h>     //Nokia display


// Nokia dispay:

// pin 7 - Serial clock out (SCLK)

// pin 6 - Serial data out (DIN)

// pin 5 - Data/Command select (D/C)

// pin 4 - LCD chip select (CS)

// pin 3 - LCD reset (RST)

//Adafruit_PCD8544 display = Adafruit_PCD8544(7, 6, 5, 4, 3);


Adafruit_PCD8544 display = Adafruit_PCD8544(16,0, 13, 15, 3);


//Adafruit_PCD8544 display = Adafruit_PCD8544(16=D0, 0= D3, 13=D7, 15=D8, 3=RX);





// Configuration for the display  i2c op lolin pin D1=SCL  D2=SDA

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);


// Define Trig and Echo pin for ultrasonic sensor

#define trigPin 14  //(d05 op lolin)

#define echoPin 12  //(do06 op lolin)

// Define variables:

long duration;

int distance;

int inhoud;   




// You should get Auth Token in the Blynk App.

// Go to the Project Settings (nut icon).

char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxX"; //Enter the Auth code which was send by Blink


// Your WiFi credentials.

// Set password to "" for open networks.

char ssid[] = "xxxxxxxe";  //Enter your WIFI Name

char pass[] = "xxxxxxxe22";  //Enter your WIFI Password


#define DHTPIN 2          // Digital pin D4 op lolin voor temp sensor


// Uncomment whatever type you're using!

//#define DHTTYPE DHT11     // DHT 11

#define DHTTYPE DHT22   // DHT 22, AM2302, AM2321

//#define DHTTYPE DHT21   // DHT 21, AM2301


DHT dht(DHTPIN, DHTTYPE);

SimpleTimer timer;


// This function sends Arduino's up time every second to Virtual Pin (5).

// In the app, Widget's reading frequency should be set to PUSH. This means

// that you define how often to send data to Blynk App.



void sendSensor()

{

  float h = dht.readHumidity();

  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit


  if (isnan(h) || isnan(t)) {

    Serial.println("Failed to read from DHT sensor!");

    return;

  }


//ultrasone sensor

  digitalWrite(trigPin, LOW);

  delayMicroseconds(1000);

 // Trigger the sensor by setting the trigPin high for 10 microseconds:

  digitalWrite(trigPin, HIGH);

  delayMicroseconds(2000);

  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);

   // Calculate the distance:

  distance = duration*0.034/2;

inhoud= map(distance,200,30, 0,5000);

  

  //Blink settings

  // You can send any value at any time.

  // Please don't send more that 10 values per second.

  Blynk.virtualWrite(V5, h);  //V5 is for Humidity

  Blynk.virtualWrite(V6, t);  //V6 is for Temperature

  Blynk.virtualWrite(V7, distance);  //V6 is for Temperature

Blynk.virtualWrite(V8, inhoud);  //V6 is for Temperature


// some serial info 

  Serial.println(t);

  Serial.println(h);

    Serial.println(distance);  

    Serial.println(inhoud);


//tekst on display lcd

  lcd.setCursor ( 0, 0 );                 

  lcd.print("Afstand : "); //distance

  lcd.print(distance);

  lcd.print (" cm  ");

  lcd.setCursor ( 0, 1 );  

  lcd.print("Inhoud : ");

  lcd.setCursor ( 8, 1 ); 

  lcd.print (inhoud);

  lcd.setCursor ( 13, 1 ); 

  lcd.print ("L");



//Nokia display

  display.clearDisplay();

  display.setTextSize(1);

 // display.setTextColor(WHITE, BLACK); // 'inverted' text

  display.setCursor(0,0);

  display.print("Temp  = ");

  display.print(t);

    display.setCursor(0,10);

    display.print("Vocht = ");

   display.println(h);


  display.setTextColor(BLACK);

  display.setCursor(0,22);

 display.print(distance);

   display.print(" cm");


 display.setCursor(0,35);

display.print("Inh ");

 display.setCursor(20,30);

   display.setTextSize(2);

 display.print(inhoud);

 display.print("L");

  display.display();






    

  

}


void setup()

{

  Serial.begin(115200); // See the connection status in Serial Monitor


  // Define inputs and outputs

  

  pinMode(trigPin, OUTPUT);

  pinMode(echoPin, INPUT);


//*Just some text for testing the display

  lcd.begin(16,2);               // initialize the lcd  

  lcd.home ();                   // go home

  lcd.setCursor ( 0, 0 );        // go to the next line

  lcd.print("Tank inhoud ");  

  lcd.setCursor ( 0, 1 );        // go to the next line

  lcd.print (" Ultra sonic    ");

  delay(2000);

  lcd.clear();

//



  

  Blynk.begin(auth, ssid, pass);


  dht.begin();


  // Setup a function to be called every second

  timer.setInterval(1000L, sendSensor);




  display.begin();

  // init done


  // you can change the contrast around to adapt the display

  // for the best viewing!

  display.setContrast(60);


  display.display(); // show splashscreen

  delay(500);

  display.clearDisplay();   // clears the screen and buffer



  display.clearDisplay();

  display.setTextSize(2);

  display.setTextColor(WHITE, BLACK); // 'inverted' text

  display.setCursor(0,5);

  display.println(" Spike ");

  display.setTextSize(1);

    display.setTextSize(1);

  display.setTextColor(BLACK);

  display.setCursor(10,25);

 display.println("Arduino");

 display.setCursor(30,35);

 display.println("projects");

  display.display();


delay(500);

  

}


void loop()

{

  Blynk.run(); // Initiates Blynk

  timer.run(); // Initiates SimpleTimer

  

}

pzem module op Wemos D1 ESP easy

pzem module voorzien van R 1K tussen rx en 5v RX van pzem op D8 TX van pzem op D7 Wemos Config esp easy RX: GPIO-13 (D7) TX: GPIO-15 (D8) ht...