2018/03/15

自建 IOT 伺服器 透過手機 APP 控制 ----- Blynk Client Arduino UNO 加 Ethernet Shield 設定

Blynk Client Arduino UNO 加 Ethernet Shield 設定

Arduino UNO + Ethernet Shield + DHT11 溫溼度偵測。


手機 Blynk APP 設定一新專案,Arduino UNO + Ethernet 及 2 個 Gauge,1個 LCD 顯示。



範例程式

#include <BlynkSimpleEthernet.h>
#include <SPI.h>
#include <Ethernet.h>
#include <DHT.h>

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {
  0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02
};
boolean SerialDispFlag = false;
float h, t, f, hif, hic;
char buffer[35];
char auth[] = "1ffb88d481384cf789f560e9d0548466";
WidgetLCD lcd(V2);
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;
EthernetServer server(80);

#define DHTPIN 3 // what digital pin we're connected to
#define DHTTYPE DHT11   // DHT 11  
DHT dht(DHTPIN, DHTTYPE);

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  // this check is only needed on the Leonardo:
  //while (!Serial) {
  //  ; // wait for serial port to connect. Needed for native USB port only
  //}
  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    for (;;)
      ;
  }
  Blynk.begin(auth, IPAddress(192,168,0,124), 8442);
  server.begin();
  // print your local IP address:
  printIPAddress();
  dht.begin();
}

void loop() {
  //client = server.available();
  delay(2000);
  printDHTData();
  //if (client) {
  //  sendWebContent();
  //}

}

void printIPAddress()
{
  Serial.print("My IP address: ");
  for (byte thisByte = 0; thisByte < 4; thisByte++) {
    // print the value of each byte of the IP address:
    Serial.print(Ethernet.localIP()[thisByte], DEC);
    Serial.print(".");
  }
  Serial.println();
}

void printDHTData()
{
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  h = dht.readHumidity();
  Blynk.virtualWrite(V0,h);
  // Read temperature as Celsius (the default)
  t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  Blynk.virtualWrite(V1,t);
  lcd.clear();
  lcd.print(0,0,"Temperature:");
  sprintf(buffer,"%i",(int)t);
  lcd.print(12,0, buffer);
  sprintf(buffer,"Humidity:%i %",(int)h);
  lcd.print(0,1, buffer);
  
  f = dht.readTemperature(true);

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println("Failed to read from DHT sensor!");
  } else {
    // Compute heat index in Fahrenheit (the default)
    hif = dht.computeHeatIndex(f, h);
    // Compute heat index in Celsius (isFahreheit = false)
    hic = dht.computeHeatIndex(t, h, false);
    if (SerialDispFlag) {
      Serial.print("Humidity: ");
      Serial.print(h);
      Serial.print(" %\t");
      Serial.print("Temperature: ");
      Serial.print(t);
      Serial.print(" *C ");
      Serial.print(f);
      Serial.print(" *F\t");
      Serial.print("Heat index: ");
      Serial.print(hic);
      Serial.print(" *C ");
      Serial.print(hif);
      Serial.println(" *F");
    }
  }
}



沒有留言:

張貼留言