2017年10月18日 星期三

ch05 繪圖與電路佈局.pdf

          ch05 繪圖與電路佈局.pdf  

                           下載
http://www.mediafire.com/file/uc6a5ev5te5oe2p/ch05+%E7%B9%AA%E5%9C%96%E8%88%87%E9%9B%BB%E8%B7%AF%E4%BD%88%E5%B1%80.pdf

第四章 簡易記事本

Public Class Form1

    Private Sub 開啟OToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles 開啟OToolStripMenuItem.Click

        If OpenFileDialog1.ShowDialog <> Windows.Forms.DialogResult.Cancel Then

            TextBox1.Text = My.Computer.FileSystem.ReadAllText(OpenFileDialog1.FileName, _
     System.Text.Encoding.Default)
        End If

End Sub


    Private Sub 另存新檔AToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles 另存新檔AToolStripMenuItem.Click
        If SaveFileDialog1.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
            My.Computer.FileSystem.WriteAllText(SaveFileDialog1.FileName, TextBox1.Text, False)
        End If
    End Sub

    Private Sub 新增NToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles 新增NToolStripMenuItem.Click
        OpenFileDialog1.FileName = Nothing
        TextBox1.Clear()
    End Sub

    Private Sub 儲存SToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles 儲存SToolStripMenuItem.Click
        If OpenFileDialog1.FileName = Nothing Then
            If SaveFileDialog1.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
                My.Computer.FileSystem.WriteAllText(SaveFileDialog1.FileName, TextBox1.Text, False)
            End If
        Else
            My.Computer.FileSystem.WriteAllText(OpenFileDialog1.FileName, TextBox1.Text, False)
        End If
    End Sub

    Private Sub 結束XToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles 結束XToolStripMenuItem.Click
        Application.Exit()
    End Sub

    Private Sub 剪下TToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles 剪下TToolStripMenuItem.Click
        TextBox1.Cut()
    End Sub

    Private Sub 複製CToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles 複製CToolStripMenuItem.Click
        TextBox1.Copy()
    End Sub

    Private Sub 貼上PToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles 貼上PToolStripMenuItem.Click
        TextBox1.Paste()
    End Sub

    Private Sub 全選AToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles 全選AToolStripMenuItem.Click
        TextBox1.SelectAll()
    End Sub

    Private Sub 搜尋取代ToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles 搜尋取代ToolStripMenuItem1.Click
        GroupBox1.Visible = True
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles Button3.Click
        GroupBox1.Visible = False
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles Button1.Click
        Dim P As Integer
        If TextBox1.SelectionStart > 0 Then
            P = TextBox1.Text.IndexOf(TextBox2.Text, TextBox1.SelectionStart + 1)
        Else
            P = TextBox1.Text.IndexOf(TextBox2.Text, TextBox1.SelectionStart)
        End If
        If P < 0 Then
            MsgBox("未發現搜尋字串!")
        Else
            TextBox1.SelectionStart = P
            TextBox1.SelectionLength = TextBox2.TextLength
            TextBox1.Select()
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles Button2.Click
        TextBox1.SelectedText = TextBox3.Text
    End Sub

    Dim mdx As Integer, mdy As Integer

    Private Sub GroupBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) _
    Handles GroupBox1.MouseDown
        mdx = e.X
        mdy = e.Y
    End Sub

    Private Sub GroupBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) _
    Handles GroupBox1.MouseMove
        If e.Button = Windows.Forms.MouseButtons.Left Then
            GroupBox1.Left += e.X - mdx
            GroupBox1.Top = e.Y - mdy
        End If
    End Sub

    Private Sub 字型CToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles 字型CToolStripMenuItem.Click
        FontDialog1.ShowDialog()
        TextBox1.Font = FontDialog1.Font
    End Sub

    Private Sub 顏色OToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles 顏色OToolStripMenuItem.Click
        ColorDialog1.ShowDialog()
        TextBox1.ForeColor = ColorDialog1.Color
    End Sub
End Class

Use NodeMcu send message to MQTT BROKER







/* 
 * Use NodeMCU to drive DHT11 and send temperature/humidity value to MQTT server
 * Tutorial URL http://osoyoo.com/2016/11/24/use-nodemcu-to-send-temperaturehumidity-data-to-mqtt-iot-broker/
 * CopyRight John Yu
 */
 /* NodeMCU Pin assigment
  GPIO Pin  I/O Index Number
  GPIO0  3   GPIO1  10 GPIO2  4    GPIO3  9     GPIO4  2   GPIO5   1   GPIO6  N/A  GPIO7  N/A
  GPIO8  N/A GPIO9  11 GPIO10  12  GPIO11   N/A GPIO12  6  GPIO13  7   GPIO14   5  GPIO15   8   GPIO16   0 */
 
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>

// Define NodeMCU D3 pin to as temperature data pin of  DHT11
#define DHT11_PIN D3   //GPIO0  3
DHT dht(DHT11_PIN, DHT11);

// Update these with values suitable for your network.
//const char* ssid = "your_hotspot_ssid";
//const char* password = "your_hotspot_password";
const char* ssid = "74170287";
const char* password = "24063173";

const char* mqtt_server = "broker.mqtt-dashboard.com";

//const char* mqtt_server = "iot.eclipse.org";

WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;

void setup_wifi() {
   delay(100);
  // We start by connecting to a WiFi network
    Serial.print("Connecting to ");
    Serial.println(ssid);
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED)
    {
      delay(500);
      Serial.print(".");
    }
  randomSeed(micros());
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void callback(char* topic, byte* payload, unsigned int length)
{
  Serial.print("Command is : [");
  Serial.print(topic);
  int p =(char)payload[0]-'0';
  //int chk = dht.read11(DHT11_PIN);
  // if MQTT comes a 0 message, show humidity
  if(p==0)
  {
    Serial.println("to show humidity!]");
    Serial.print(" Humidity is: " );
    Serial.print(dht.readHumidity(), 1);
    Serial.println('%');
  }
  // if MQTT comes a 1 message, show temperature
  if(p==1)
  {
  // digitalWrite(BUILTIN_LED, HIGH);
    Serial.println(" is to show temperature!] ");
  // int chk = DHT.read11(DHT11_PIN);
   Serial.print(" Temp is: " );
   Serial.print(dht.readTemperature(), 1);
   Serial.println(' C');
  }
  Serial.println();
} //end callback

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected())
  {
    Serial.print("Attempting MQTT connection...");
    // Create a random client ID
    String clientId = "ESP8266Client-";
    clientId += String(random(0xffff), HEX);
    // Attempt to connect
    //if you MQTT broker has clientID,username and password
    //please change following line to    if (client.connect(clientId,userName,passWord))
    if (client.connect(clientId.c_str()))
    {
      Serial.println("connected");
     //once connected to MQTT broker, subscribe command if any
      client.subscribe("OsoyooCommand");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 6 seconds before retrying
      delay(6000);
    }
  }
} //end reconnect()

void setup() {
  Serial.begin(115200);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
 // int chk = DHT.read11(DHT11_PIN);
  Serial.print(" Starting Humidity: " );
  Serial.print(dht.readHumidity(), 1);
  Serial.println('%');
  Serial.print(" Starting Temparature ");
  Serial.print( dht.readTemperature(), 1);
  Serial.println('C');
}

void loop() {
  if (!client.connected()) {
    reconnect();
  }
  client.loop();
  long now = millis();
  // read DHT11 sensor every 6 seconds
  if (now - lastMsg > 6000) {
     lastMsg = now;
     //int chk = DHT.read11(DHT11_PIN);
     String msg="real time temperature: ";
     msg= msg+ dht.readTemperature();
     msg = msg+" C ;real time Humidity: " ;
     msg=msg+dht.readHumidity() ;
     msg=msg+"%";
     char message[58];
     msg.toCharArray(message,58);
     Serial.println(message);
     //publish sensor data to MQTT broker
    client.publish("Alex9ufo DHT11", message);
  }
}

2017年10月17日 星期二

Use NodeMCU to send temperature/humidity data to MQTT IOT broker





http://broker.mqtt-dashboard.com/index.html


/* 
 * Use NodeMCU to drive DHT11 and send temperature/humidity value to MQTT server
 * Tutorial URL http://osoyoo.com/2016/11/24/use-nodemcu-to-send-temperaturehumidity-data-to-mqtt-iot-broker/
 * CopyRight John Yu
 */
 /* NodeMCU Pin assigment
  GPIO Pin  I/O Index Number
  GPIO0  3   GPIO1  10 GPIO2  4    GPIO3  9     GPIO4  2   GPIO5   1   GPIO6  N/A  GPIO7  N/A
  GPIO8  N/A GPIO9  11 GPIO10  12  GPIO11   N/A GPIO12  6  GPIO13  7   GPIO14   5  GPIO15   8   GPIO16   0 */
 
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>

// Define NodeMCU D3 pin to as temperature data pin of  DHT11
#define DHT11_PIN D3   //GPIO0  3
DHT dht(DHT11_PIN, DHT11);

// Update these with values suitable for your network.
//const char* ssid = "your_hotspot_ssid";
//const char* password = "your_hotspot_password";
const char* ssid = "74170287";
const char* password = "24063173";

const char* mqtt_server = "broker.mqtt-dashboard.com";

//const char* mqtt_server = "iot.eclipse.org";

WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;

void setup_wifi() {
   delay(100);
  // We start by connecting to a WiFi network
    Serial.print("Connecting to ");
    Serial.println(ssid);
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED)
    {
      delay(500);
      Serial.print(".");
    }
  randomSeed(micros());
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void callback(char* topic, byte* payload, unsigned int length)
{
  Serial.print("Command is : [");
  Serial.print(topic);
  int p =(char)payload[0]-'0';
  //int chk = dht.read11(DHT11_PIN);
  // if MQTT comes a 0 message, show humidity
  if(p==0)
  {
    Serial.println("to show humidity!]");
    Serial.print(" Humidity is: " );
    Serial.print(dht.readHumidity(), 1);
    Serial.println('%');
  }
  // if MQTT comes a 1 message, show temperature
  if(p==1)
  {
  // digitalWrite(BUILTIN_LED, HIGH);
    Serial.println(" is to show temperature!] ");
  // int chk = DHT.read11(DHT11_PIN);
   Serial.print(" Temp is: " );
   Serial.print(dht.readTemperature(), 1);
   Serial.println(' C');
  }
  Serial.println();
} //end callback

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected())
  {
    Serial.print("Attempting MQTT connection...");
    // Create a random client ID
    String clientId = "ESP8266Client-";
    clientId += String(random(0xffff), HEX);
    // Attempt to connect
    //if you MQTT broker has clientID,username and password
    //please change following line to    if (client.connect(clientId,userName,passWord))
    if (client.connect(clientId.c_str()))
    {
      Serial.println("connected");
     //once connected to MQTT broker, subscribe command if any
      client.subscribe("OsoyooCommand");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 6 seconds before retrying
      delay(6000);
    }
  }
} //end reconnect()

void setup() {
  Serial.begin(115200);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
 // int chk = DHT.read11(DHT11_PIN);
  Serial.print(" Starting Humidity: " );
  Serial.print(dht.readHumidity(), 1);
  Serial.println('%');
  Serial.print(" Starting Temparature ");
  Serial.print( dht.readTemperature(), 1);
  Serial.println('C');
}

void loop() {
  if (!client.connected()) {
    reconnect();
  }
  client.loop();
  long now = millis();
  // read DHT11 sensor every 6 seconds
  if (now - lastMsg > 6000) {
     lastMsg = now;
     //int chk = DHT.read11(DHT11_PIN);
     String msg="real time temperature: ";
     msg= msg+ dht.readTemperature();
     msg = msg+" C ;real time Humidity: " ;
     msg=msg+dht.readHumidity() ;
     msg=msg+"%";
     char message[58];
     msg.toCharArray(message,58);
     Serial.println(message);
     //publish sensor data to MQTT broker
    client.publish("Alex9ufo DHT11", message);
  }
}

Use NodeMcu send message to MQTT BROKER









/*
 Basic ESP8266 MQTT example

 This sketch demonstrates the capabilities of the pubsub library in combination
 with the ESP8266 board/library.

 It connects to an MQTT server then:
  - publishes "hello world" to the topic "Alex9ufo outTopic" every two seconds
  - subscribes to the topic "inTopic", printing out any messages
    it receives. NB - it assumes the received payloads are strings not binary
  - If the first character of the topic "inTopic" is an 1, switch ON the ESP Led,
    else switch it off

 It will reconnect to the server if the connection is lost using a blocking
 reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to
 achieve the same result without blocking the main loop.

 To install the ESP8266 board, (using Arduino 1.6.4+):
  - Add the following 3rd party board manager under "File -> Preferences -> Additional Boards Manager URLs":
       http://arduino.esp8266.com/stable/package_esp8266com_index.json
  - Open the "Tools -> Board -> Board Manager" and click install for the ESP8266"
  - Select your ESP8266 in "Tools -> Board"

*/

#include <ESP8266WiFi.h>
#include <PubSubClient.h>

// Update these with values suitable for your network.

const char* ssid = "74170287";
const char* password = "24063173";
const char* mqtt_server = "broker.mqtt-dashboard.com";

WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;

void setup_wifi() {

  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  randomSeed(micros());

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();

  // Switch on the LED if an 1 was received as first character
  if ((char)payload[0] == '1') {
    digitalWrite(BUILTIN_LED, LOW);   // Turn the LED on (Note that LOW is the voltage level
    // but actually the LED is on; this is because
    // it is acive low on the ESP-01)
  } else {
    digitalWrite(BUILTIN_LED, HIGH);  // Turn the LED off by making the voltage HIGH
  }

}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Create a random client ID
    String clientId = "ESP8266Client-";
    clientId += String(random(0xffff), HEX);
    // Attempt to connect
    if (client.connect(clientId.c_str())) {
      Serial.println("connected");
      // Once connected, publish an announcement...
      client.publish("Alex9ufo outTopic", "hello world");
      // ... and resubscribe
      client.subscribe("inTopic");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void setup() {
  pinMode(BUILTIN_LED, OUTPUT);     // Initialize the BUILTIN_LED pin as an output
  Serial.begin(115200);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
}

void loop() {

  if (!client.connected()) {
    reconnect();
  }
  client.loop();

  long now = millis();
  if (now - lastMsg > 2000) {
    lastMsg = now;
    ++value;
    snprintf (msg, 75, "hello world #%ld", value);
    Serial.print("Publish message: ");
    Serial.println(msg);
    client.publish("Alex9ufo outTopic", msg);
  }
}

2017年10月15日 星期日

NodeMcu Blynk Wireless Temperature, Humidity DHT 11 Sensor





/* NodeMCU Pin assigment 
  GPIO Pin  I/O Index Number
  GPIO0  3   GPIO1  10 GPIO2  4    GPIO3  9     GPIO4  2   GPIO5   1   GPIO6  N/A  GPIO7  N/A
  GPIO8  N/A GPIO9  11 GPIO10  12  GPIO11   N/A GPIO12  6  GPIO13  7   GPIO14   5  GPIO15   8   GPIO16   0 */

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>           // including the library of DHT11 temperature and humidity sensor
#include <Adafruit_Sensor.h>
#include <DHT_U.h>
#include <SimpleTimer.h>   //including the library of SimpleTimer
#define DHTTYPE DHT11      // DHT 11

#define dht_dpin 14
DHT dht(dht_dpin, DHTTYPE); 
SimpleTimer timer;
//char auth[] = "Your Auth. Key";            // You should get Auth Token in the Blynk App.
                                           // Go to the Project Settings (nut icon).
const char auth[] = "bd2c91fd614b4d478c780881d06b75b6";
const char ssid[] = "74170287";    // Your WiFi credentials.
const char pass[] = "24063173";  // Set password to "" for open networks.

float t;                                   // Declare the variables 
float h;

void setup()
{
    Serial.begin(9600);// Debug console
    Blynk.begin(auth, ssid, pass);
    dht.begin();
    timer.setInterval(2000, sendUptime);
}

void sendUptime()
{
  
  float h = dht.readHumidity();
  float t = dht.readTemperature(); 
  Serial.println("Humidity and temperature\n\n");
  Serial.print("Current humidity = ");
  Serial.print(h);
  Serial.print("%  ");
  Serial.print("temperature = ");
  Serial.print(t); 
  Blynk.virtualWrite(V0, t);
  Blynk.virtualWrite(V1, h);
  
}

void loop()
{
  Blynk.run();
  timer.run();
}

NodeMCU Temperature, Humidity data upload on Thingspeak on Arduino IDE

NodeMCU Temperature, Humidity data upload on Thingspeak on Arduino IDE








// Simple code upload the tempeature and humidity data using thingspeak.com
// Hardware: NodeMCU,DHT11

#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#include <ESP8266WiFi.h>
 
//String apiKey = "Your API of thingsspeak";     //  Enter your Write API key from ThingSpeak
String apiKey = "3V8PO4MIDZ5DGKMT";     //  Enter your Write API key from ThingSpeak

//const char *ssid =  "Your wifi Network name";     // replace with your wifi ssid and wpa2 key
//const char *pass =  "Network password";
const char* ssid = "74170287";
const char* password = "24063173";
const char* server = "api.thingspeak.com";

#define DHTPIN 0          //pin where the dht11 is connected
 
DHT dht(DHTPIN, DHT11);

WiFiClient client;
 
void setup() 
{
       Serial.begin(115200);
       delay(10);
       dht.begin();
 
       Serial.println("Connecting to ");
       Serial.println(ssid);
       WiFi.begin(ssid, password);
 
      while (WiFi.status() != WL_CONNECTED) 
     {
            delay(500);
            Serial.print(".");
     }
      Serial.println("");
      Serial.println("WiFi connected");
 
}
 
void loop() 
{
  
      float h = dht.readHumidity();
      float t = dht.readTemperature();
      
              if (isnan(h) || isnan(t)) 
                 {
                     Serial.println("Failed to read from DHT sensor!");
                      return;
                 }

                         if (client.connect(server,80))   //   "184.106.153.149" or api.thingspeak.com
                      {  
                            
                             String postStr = apiKey;
                             postStr +="&field1=";
                             postStr += String(t);
                             postStr +="&field2=";
                             postStr += String(h);
                             postStr += "\r\n\r\n";
 
                             client.print("POST /update HTTP/1.1\n");
                             client.print("Host: api.thingspeak.com\n");
                             client.print("Connection: close\n");
                             client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
                             client.print("Content-Type: application/x-www-form-urlencoded\n");
                             client.print("Content-Length: ");
                             client.print(postStr.length());
                             client.print("\n\n");
                             client.print(postStr);
 
                             Serial.print("Temperature: ");
                             Serial.print(t);
                             Serial.print(" degrees Celcius, Humidity: ");
                             Serial.print(h);
                             Serial.println("%. Send to Thingspeak.");
                        }
          client.stop();
 
          Serial.println("Waiting...");
  
  // thingspeak needs minimum 15 sec delay between updates, i've set it to 30 seconds
  delay(10000);
}