De echte Halfzware uit Friesland!
Weerstation

Prototype Arduino Weerstation

Proto

Prototype op een breadboard

Als geheugensteuntje voor me zelf…

De uiteindelijke uitvoering zoals het moet worden…

Wat hebben we nodig voor deze klus:

1 X Arduino Uno
1 X ESP8266 Wifi-module
1 X DHT22 temperatuur en luchtvochtigheidssensor
1 X Barometer-sensor
2 x LED Display – 7-segment module
2 X Blauwe LED
2 X Weerstand 100 Ω
1 X 3,3 volt spanningsregelaar (voor de ESP8266)


Het arduino weerstation is online…

Hier de tijdelijke code voor de Arduino:

#include<stdlib.h>                 //voor strings
#include <Arduino.h>
#include <DHT.h>
#include <Adafruit_BMP085.h>
#include <SoftwareSerial.h>

#include "SevenSegmentTM1637.h"
#define DEBUG 0                                     // change value to 1 to enable debuging using serial monitor

String IP = "184.106.153.149"; // ThingSpeak IP Address: 184.106.153.149
String GET = "GET /update?key=XXXXXXXXXXXXX";    // replace with your channel key
int count = 1;

long previousMillis = 0;        // will store last time Thingspeak was updated
// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 300000;           // interval at which to update (milliseconds) (5 minuten)


float temp;
float p;
float hum;
int h;

#define CLK 2
#define DIO 3

#define CLL 4
#define DIP 5

#define DHTTYPE DHT22   // DHT 11
#define DHTPIN 8     // what digital pin is DHT connected to
#define BLED 12      // Blauwe LED   pinMode(BLED, OUTPUT); //initialiseer pin 12 (BLAUWE LED).
#define WLED 11

Adafruit_BMP085 bmp;
DHT dht(DHTPIN, DHTTYPE);
SevenSegmentTM1637 tm1637(CLK, DIO);
SevenSegmentTM1637 tm1638(CLL, DIP);
//{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
//0~9,A,b,C,d,E,F
SoftwareSerial mySerial(7, 6); // RX, TX

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  mySerial.begin(9600);
  delay(1000);

  tm1637.setBacklight(30);  // set the brightness to 100 %
  tm1638.setBacklight(30);  // set the brightness to 100 %


  delay(300); // Pauze om sensor stabiel te laten worden
  if (DEBUG) {
    Serial.println("Klaar om te beginnen...\r\n");
  }
  delay(700); // Nog een pauze (in totaal is 1sec nodig)

  if (!bmp.begin()) {
    if (DEBUG) {
      Serial.println("Could not find a valid BMP085 sensor, check wiring!");
    }
    while (1) {}
  }
  resetWiFi();
  tm1637.begin();            // initializes the display
  tm1637.print("INIT");      // display INIT on the display
  tm1638.begin();            // initializes the display
  tm1638.print("INIT");      // display INIT on the display

  delay(1000);
}

void loop() {
  byte  rawData;
  int disptemp;
  int har;
  int vocht;

  // put your main code here, to run repeatedly:

  //************************************************************************
  //*********** Temperatuur op display1 ************************************

  unsigned long currentMillis = millis();

  temp = dht.readTemperature();
  hum = dht.readHumidity();
  vocht = hum * 100;
  har = temp * 100;
  if (DEBUG) {
    Serial.print("Temp gelezen: ");
    Serial.println(temp);
    Serial.print("Vocht bewerkt:");
    Serial.println(vocht);
  }

  tm1637.clear();

  tm1637.setColonOn(1);
  rawData = B11100011;

  if (temp < 10)
  {
    tm1637.setCursor(0, 1);
  }
  else
  {
    tm1637.setCursor(0, 0);
  }
  if (temp < 0)
  {
    tm1637.setCursor(0, 0);
  }
  tm1637.print(har);
  tm1637.printRaw(rawData, 3);

  //*************************************************************************
  //*********  LUCHTDRUK op Display 2 ***************************************

  h = bmp.readPressure() / 100;
  for (int i = 0; i <= 4; i++) {
    tm1638.clear();
    tm1638.setColonOn(0);
    tm1638.print(h);

    delay(2000);
    // ******* En VOCHT ********************************************************
    tm1638.clear();
    tm1638.setColonOn(1);
    tm1638.print(vocht);
    //**************************************************************************
    //digitalWrite(BLED, HIGH);
    if (DEBUG) {
      Serial.println(i);
    }
    delay(2000);

  } //Einde FOR LOOP
  //**************************************************************************
  //**************************************************************************

  if (currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;
    digitalWrite(BLED, HIGH);         //Even laten zien dat we data uploaden...
    
    String cmd = "AT+CIPSTART=\"TCP\",\"";// Setup TCP connection
    cmd += IP;
    cmd += "\",80";
    mySerial.println(cmd);
    delay(5000);
    if (mySerial.find("Error")) {
      if (DEBUG) {
        Serial.println("ERROR while SENDING");
      }
      digitalWrite(WLED, LOW);
      resetWiFi();
      return;
    }
    cmd = GET + "&field1=" + String(temp) + "&field2=" + String(hum) + +"&field3=" + String(h) + "\r\n";
    mySerial.print("AT+CIPSEND=");
    mySerial.println(cmd.length());
    delay(15000);
    if (mySerial.find(">"))
    {
      mySerial.print(cmd);
      if (DEBUG) {
        Serial.println("Data sent");
      }
    } else
    {
      mySerial.println("AT+CIPCLOSE");
      if (DEBUG) {
        Serial.println("Connection closed");
      }
    }
    digitalWrite(BLED, LOW);
  }

  //**************************************************************************


}

void clearRXbuffer() {
  while (mySerial.available()) {
    char c = mySerial.read();
    //Serial.write(c);
    delay(3);
  }
}

void sendWiFi(String s, bool clearBuffer) {
  mySerial.println(s);
  mySerial.println("");
  delay(100);

  if (clearBuffer) clearRXbuffer();
}

void resetWiFi() {
  unsigned long timout = 0 ;
  digitalWrite(WLED, LOW);
  Serial.println ("WiFi Reset ");
  // Wait for an IP address (WIFI GOT IP)
  char c, buffer[25];
  int i = 0, w = 0;

  do {
    if (millis() > timout) {
      clearRXbuffer();
      timout = millis() + 20000;
      delay(1000);
      sendWiFi("AT+RST", true);
    }

    if (mySerial.available()) {
      c = mySerial.read();
      if (c == 'W' || i >= 20) i = 0;
      if (c > 9 && c < 91) {
        buffer[i++] = c;
      }
    } //else displayLoop(w++);

    if (w > 11) w = 0;

  } while (strstr(buffer, "WIFI GOT IP") == NULL);

  digitalWrite(WLED, HIGH);
}