2017/3/28

WeMos D1 簡單的 Projects

WeMos D1 簡單的 Projects

(一)WeMos D1 連網及控制LED閃爍:

使用ESP8266 Arduino IDE core, 非 AT command
需WiFi連網的SSID及密碼
D3/SCL/D15→GPIO5 程式裡定義pin5,接線在模組的D3接腳
D3接腳接LED再接個限流電阻再接地。
程式碼:
#include <ESP8266WiFi.h>
const int ledPin = 5;
const char* ssid = “********";
const char* password = “*******";
void setup()
{
    Serial.begin(9600);
    pinMode(ledPin, OUTPUT);
    Serial.print("Trying to connect to ");
    Serial.println(ssid);
    // attempt to connect to Wifi network:
   WiFi.begin(ssid, password);
   while (WiFi.status() != WL_CONNECTED) {
       delay(500);
       Serial.print(".");
   }

   Serial.println("");
   Serial.println("WiFi connected");
   Serial.println(WiFi.macAddress());
   Serial.print("IP:");
   Serial.println(WiFi.localIP());
   Serial.print("Subnet:");
   Serial.println(WiFi.subnetMask());
   Serial.print("Gateway:");
   Serial.println(WiFi.gatewayIP());
   Serial.print("Channel:");
   Serial.println(WiFi.channel());
   Serial.print("Status:");
   Serial.println(WiFi.status());
} // setup
void loop() {
    digitalWrite(ledPin, HIGH);
    delay(500);
    digitalWrite(ledPin, LOW);
    delay(500);
}
WiFi.status() 編號和列舉名稱對照

結果



(二)WeMos D1 當TCP Server 由 Client 控制LED:


程式碼:
#include <ESP8266WiFi.h>
const int ledPin = 5;
const char* ssid = “*********";
const char* password = “********";
WiFiServer server(12345);
WiFiClient client;

void setup() {
   Serial.begin(9600);
   pinMode(ledPin, OUTPUT);
   WiFi.begin(ssid,password);
   Serial.println("");
   while(WiFi.status() != WL_CONNECTED) {
     delay(500);
     Serial.print(".");
   }
   Serial.print("Connected to ");
   Serial.println(ssid);
   Serial.print("IP Address: ");
   Serial.println(WiFi.localIP());
   server.begin(); // Start the TCP server
}

void loop() {
   WiFiClient client = server.available();
   if (!client) {
       return; }
   Serial.println("new client");
   while(!client.available()){
      delay(1);
   }
   String receivedData = client.readStringUntil('\r');
   Serial.println(receivedData);
   client.flush();
   if (receivedData.indexOf("1") == 0)
       digitalWrite(ledPin, HIGH);
   else if (receivedData.indexOf("0") == 0)
       digitalWrite(ledPin, LOW);
   else {
      Serial.println("invalid request");
      client.stop();
      return;
    } 
}



(三)WeMos D1 TCP client 連網並上傳溫濕度到Thingspeak:


需WiFi連網的SSID及密碼
需Thingspeak的帳號及定義一個channel兩個fields及寫入的API key
D9/TX1→GPIO2 程式裡定義pin 2,線接在模組的D9接腳
程式碼
#include <ESP8266WiFi.h>
#include <DHT.h>
#define DHTPIN 2
#define DHTTYPE DHT11
const char* ssid = “your ssid";
const char* password = “******";
const char* host = "184.106.153.149";
const char* writeAPIKey = "LAGP*******D2L";
DHT dht(DHTPIN, DHTTYPE); // Initialize DHT sensor
WiFiClient client;

void setup() {
   Serial.begin(9600);
   dht.begin();
   WiFi.begin(ssid,password);
   Serial.println("");
   while(WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
   }
   Serial.print("Connected to ");
   Serial.println(ssid);
   Serial.print("IP Address: ");
   Serial.println(WiFi.localIP());
}

void loop() {
   delay(2000);
   float h = dht.readHumidity();
   float t = dht.readTemperature();
   if (isnan(h) || isnan(t) ) {
      Serial.println("Failed to read from DHT sensor!");
      return;
   }
   String HH = String(h);
   String TT = String(t);
   Serial.println(HH);
   Serial.println(TT);
   updateTemp(TT,HH);
   delay(5000);
}

void updateTemp(String TT, String HH)   {
   const int httpPort = 80;
   if (!client.connect(host, httpPort)) {
       return;
   }
   String url = "/update?key=";
   url+=writeAPIKey;
   url+="&field1=";
   url+=TT;
   url+="&field2=";
   url+=HH;
   url+="\r\n";
   client.print(String("GET ")+url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" +
   "Connection: close\r\n\r\n" );
}

沒有留言:

張貼留言