วันเสาร์ที่ 11 พฤศจิกายน พ.ศ. 2560

Error: Your local changes to the following files [git pull ESP32-Arduino]

Git pull ในการอัพเดท Library Esp32




Error: Your local changes to the following files would be overwritten by merge:

Error ดังกล่าว หมายถึง ท่านได้เข้าไป Edit file ซึ่งทำให้ไม่ Git pull ให้อัพเดท Library ต่อ

*ก่อนหน้านี้ 11/2017 ผมเองก็ได้ Edit ไฟล์พวกนี้ไว้นานแล้ว แต่ไม่เคยเกิด Error สงสังว่า Github คงจะมีการตรวจสอบข้อมูล หากมีการ Edit file แตกต่างจากต้นฉบับจะไม่ดำเนินการต่อ

วิธีแก้ไข: กลับไปลบข้อมูลที่ทำการ Edit file กลับตามเดิมก่อน แล้วสั่ง git pull ใหม่อีกรอบ


อัพเดทได้เรียบร้อย




วันพุธที่ 8 พฤศจิกายน พ.ศ. 2560

Raspberrypi: How to setting static IP

การตั้งค่า IP [Ethernet and WiFi]

+----------------------------------------------------+
$ sudo nano /etc/network/interfaces                |
+----------------------------------------------------+
# interfaces(5) file used by ifup(8) and ifdown(8)

# Please note that this file is written to be used with dhcpcd
# For static IP, consult /etc/dhcpcd.conf and 'man dhcpcd.conf'

# Include files from /etc/network/interfaces.d:
source-directory /etc/network/interfaces.d

auto lo
iface lo inet loopback

#iface eth0 inet manual
auto eth0
iface eth0 inet static
    address 192.168.100.10
    #gateway 192.168.100.1
    netmask 255.255.255.0
    network 192.168.100.0
    broadcast 192.168.100.255

allow-hotplug wlan0
#iface wlan0 inet manual
iface wlan0 inet static
    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
    address 192.168.1.160
    gateway 192.168.1.1
    netmask 255.255.255.0
    network 192.168.1.0
    broadcast 192.168.1.255
    dns-nameservers 8.8.8.8 8.8.4.4


allow-hotplug wlan1
iface wlan1 inet manual
    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf




+----------------------------------------------------+
+----------------------------------------------------+
save and exit
Ctrl + X >> Y

+----------------------------------------------------+
Restart network or Reboot
# /etc/init.d/networking reload

# reboot

+----------------------------------------------------+
Show IP
# ifconfig


Raspberrypi: How to setting WiFi [SSID Password]

การเชื่อมต่อ WiFi

+--------------------------------------------------------------------------+
$ sudo nano /etc/wpa_supplicant/wpa_supplicant.conf               |
+--------------------------------------------------------------------------+

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=TH

network={
        ssid="SSID"
        psk="Your_Password"
        key_mgmt=WPA-PSK
}



+----------------------------------------------------+
+----------------------------------------------------+
save and exit
Ctrl + X >> Y

+----------------------------------------------------+
Restart network
# /etc/init.d/networking reload

+----------------------------------------------------+
Show IP
# ifconfig

Raspberrypi: How to connect Rpi Ethernet notebook [No Display]

วิธีการเชื่อมต่อ Raspberry PI กับ Notebook ผ่าน LAN 
*การตั้งค่าต่อไปนี้ไม่ได้เป็นการ Share internet แต่จะสามารถประยุกต์ได้ต่อไป


การใช้งาน raspberry pi สิ่งแรกที่ต้องการ คือ เราจะ connect ssh ไปยัง Command line [cmd] ได้อย่างไร กรณีที่ต่อจอ HDMI ได้ก็สามารถตั้งค่า IP ได้ผ่านหน้า Desktop ได้เลย แต่สำหรับผมแล้ว raspberry pi คือ Server ดีๆตัวนึง ซึ่งไม่จำเป็นจะต้องต่อจอเพื่อแสดงผล ดังนั้น การควบคุมทำงานของ raspberry pi จะต้องผ่านการ ssh ไปเท่านั้น จึงหาวิธีการและสรุปได้ ดังต่อไปนี้

- ทำการติดตั้ง OS ของ raspberry pi ใหม่
- เชื่อมต่อ raspberry pi กับคอมพิวเตอร์ด้วย port LAN
*ในตอนนี้เราจะไม่รู้ข้อมูล IP ของ raspberry pi

- Notebook ไปยังการตั้งค่า IP port LAN ตั้งค่าเป็น DHCP(Dynamic Host Configuration Protocol)

Control Panel >> Network and Sharing Center >> Click Local area network
*หากไม่ได้เชื่อต่อ raspberry pi ด้วย port LAN สถานะ Local area network จะไม่มีการแสดงผล


- ตรวจสอบ Network Detail จะไม่มีการแจก IP
>> Click Details.....



- ไปยังการตั้งค่า  WiFi เพื่อแชร์ Internet ให้กับ port LAN เพื่อต้องการแจก IP ให้กับ port LAN (ซึ่งจะได้ IP ชุด 192.168.137.xxx)

Control Panel >> Network and Sharing Center >> Click

- Interface card WiFi set sharing
  >Properties
  >Tab bar sharing
  >Select is 2 position [Allow other network]
  >Drop down Select [Local network]
  >OK



- จากนั้นกด setting เพื่ออนุญาติให้ใช้ Service  Remote desktop, Https, Http

select 1.Remote desktop 2.Https 3.Http  OK.



- ไปยัง port LAN เพื่อตรวจสอบการ share internet จาก WiFi ซึ่งจะได้ IP 192.168.137.xx

- Interface card lan will has ip 192.168.137.xx


- เปิดโปรแกรม Advanced IP Scanner
กำหนดให้แสกน IP 192.168.137.1 - 192.168.137.254   
จากตั้วอย่าง raspberry pi IP 192.168.137.24
*กรณีไม่เจอ IP ของ raspberry pi จะต้องตรวจสอบการตั้งค่าตามขั้นต้นอีกครั้ง




- ทดสอบด้วยการ ping ip 192.168.137.24


- ssh IP 192.168.137.24 เพื่อไปตั้งค่า IP

รหัสผ่าน
user    pi
pass    raspberry

- เข้าไปแก้ไขข้อมูลในไฟล์
# nano /etc/network/interfaces

ตั้งค่า Static IP ดังนี้
*ตัวอย่าง จะใช้ IP 192.168.100.xxx ในการติดต่อกับ raspberry pi

#iface eth0 inet manual
auto eth0
iface eth0 inet static
    address 192.168.100.10
    #gateway 192.168.100.1
    netmask 255.255.255.0
    network 192.168.100.0
    broadcast 192.168.100.255


*ค้นหาเพิ่มเติม Static กับ DHCP ต่างกันอย่างไร?


- เมื่อตั้งค่าเรียบร้อยแล้ว บันทึกและออก
Ctrl + X  >>  Y

- restart service
# /etc/init.d/networking reload

หรือรีสตาร์ท
# reboot

*เมื่อสั่ง restart service การ ssh ก็จะหลุดไปเพราะ IP raspberry pi กับ computer คนละชุดกัน
ดังนั้นจะต้องกลับไปตั้ง IP port LAN ที่คอมพิวเตอร์ให้เป็นวงเดียว 192.168.100.xx
แต่จะต้องกลับไปปิดการ Share WiFi จากตอนต้น

Interface card WiFi set sharing
  > Unselect [Allow other network]


- ถึงขั้นตอนนี้ Computer ไม่สามารถติดต่อกับ raspberry pi ได้ด้วย IP 192.168.100.xx ดังนั้นต้องกลับไม่ตั้งค่า IP ที่ port LAN ให้เป็นแบบ Static ด้วยเช่นกัน

*ทำไมต้องตั้งค่า Static IP เพราะหากเลือกใช้ DHCP จาก WiFi Sharing ก็สามารถใช้ได้ แต่จะต้องมา Scan IP ทุกครั้ง เพราะเมื่อไหร่ที่สั่ง Reboot หรือ Restart network ก็จะได้ IP ใหม่ 192.168.137.xx 

- port LAN ตั้งค่า IP  192.168.100.5 สำคัญที่ Subnet Mask จะต้องเป็น 255.255.255.0 เมื่อตั้งค่า IP Address เรียบร้อยแล้ว กด Tab ระบบจะใส่ให้อัตโนมัติ


- ทดสอบโดยการ Ping IP raspberry pi

ping 192.168.100.10


- ssh ไปยัง IP 192.168.100.10



- สามารถ Connect raspberry pi ด้วย port LAN ได้ตลอดและ IP ก็จะไม่เปลี่ยน


เสร็จเรียบร้อย

เพิ่มเติม: การตั้งค่า Static IP และการเข้าไป Setting WiFi SSID
Raspberrypi: How to setting WiFi [SSID Password]

Raspberrypi: How to setting static IP


Blog เรื่องนี้ที่เขียนเนื้อหาอาจจะไม่ได้ลงรายละเอียด 100% มีบางขั้นตอนอาจจะมีข้ามไปบ้าง หากท่านติดปัญหาในส่วนใด สามารถสอบถามได้ทาง Inbox facebook

Contacht: https://www.facebook.com/dan.thummaratkmitl

วันศุกร์ที่ 3 มีนาคม พ.ศ. 2560

Software serial arduino vs NodeMCU

Software serial Arduino vs NodeMCU




Hardware Connection

// Arduino UNO          9               8
//                                  |                 |
//                                  |                 |
// NodeMCU            14(D6)       12(D5)


Software serial  เป็นไลบลารีตัวนึงที่มีประโยชน์ และนำไปใช้บ่อยมาก เกี่ยวกับงานที่ต้องใช้ Uart TTL มากกว่า 1 ช่อง

โดยทั่วหากนำไลบลารี SoftwareSerial.h มาใช้เลยมันก็ง่าย แต่มันจะมีปัญหาในส่วนที่ ใช้กับ MCU คนละตัวกัน และ NodeMCU ไม่สามารถใช้ไลบลารีร่วมกับ UNO ได้นั้นเอง

แก้ปัญหา ดังนี้
//   MCU                     Library                        inculed
// Arduino UNO       AltSoftSerial                   AltSoftSerial.h
// NodeMCU            espsoftwareserial            SoftwareSerial.h

สาเหตุที่ Arduino UNO ต้องใช้ไลบลารี AltSoftSerial  เนื่องด้วยหากติดตั้งไบลารี SoftwareSerial.h ของ UNO จะชนกับไลบลารีของ NodeMCU นั้นเอง


จากการทดสอบ board rate NodeMCU รับได้เพียงแค่ 9600 เท่านั้น



<<===Arduino UNO========================>>
#include <AltSoftSerial.h>

AltSoftSerial altSerial;
// Board            Serial1 TX   AltSoftSerial RX
// -----            ----------   ----------------
// Arduino UNO          9               8

void setup() {
  Serial.begin(9600);
  Serial.println("AltSoftSerial Test Begin");
  altSerial.begin(9600);
  altSerial.println("Hello World");
}

void loop() {
  String content;

  if (altSerial.available()) {
    content = altSerial.readStringUntil('\r');
    Serial.print(content);
  }

  altSerial.println("Hello World");
  delay(500);
}
<<======================================>>


<<===NodeMCU ===========================>>
#include <SoftwareSerial.h>

//SoftwareSerial swSer(14, 12, false, 256);   //Rx  Tx
SoftwareSerial swSer(D5, D6, false, 256);   //Rx  Tx
// Board            Serial TX       Serial RX
// -----            ----------      -----------
// NodeMCU            14(D6)          12(D5)

void setup() {
  Serial.begin(9600);
  swSer.begin(9600);

  Serial.println("\nSoftware serial test started");
  for (char ch = ' '; ch <= 'z'; ch++) {
    swSer.write(ch);
  }
  swSer.println("");
}
void loop() {
  String content;
  while (swSer.available() > 0) {
    content = swSer.readStringUntil('\r');
    Serial.print(content);
  }
//  swSer.println("Hello World");
//  delay(1000);
}
<<======================================>>

วันเสาร์ที่ 25 กุมภาพันธ์ พ.ศ. 2560

Esp32 arduino error [File "get.py", line 25]


เมื่อติดตั้ง Esp32 ใหม่ เกิด Error หรือไม่สามารถรันคำสั่ง python get.py เพื่อติดตั้ง toolchain  [xtensa-esp32-elf-win32]

$ python get.py
Traceback (most recent call last):
  File "get.py", line 25, in <module>
    import requests
ImportError: No module named requests



จากการใช้โปรแกรม MSYS2  ในการติดตั้งหรือรัน Command พบว่าครั้งนี้เจอปัญหาไม่สามารถรันคำสั่ง python get.py ได้

แก้ไขโดยการใช้ Command line on windows สามารถรันคำสั่ง python get.py ได้ปกติ



วันจันทร์ที่ 9 มกราคม พ.ศ. 2560

Project Monitor Temp

ทำเล่นให้เป็นจริง
Project Monitor Temp 


     ขอบอกก่อนว่าโพสต์นี้ผมทำเป็นรูปแบบอัพข้อมูลเรื่อยๆ ทีละขั้นตอน อาจใช้เวลานานกว่าจะสมบูรณ์หากไม่ติดปัญหาใดๆ ก็จะอัพข้อมูลให้เรื่อยๆ

     Project Monitor Temp จะทำในรูปแบบให้ Esp หนึ่งตัวเป็น Server และอีก 2 ตัวหรือมากว่านั้นเป็น Client สามารถติดต่อกันได้ภายในโดยไม่ต้องใช้ Internet หรือ Router เป็นตัวกลาง
    สามารถนำไปประยุกต์ใช้ในระบบโรงเรือน โรงงาน เพื่อต้องการให้อุปกรณ์สื่อสารกันได้ระยะไกลแบบไร้สายนั้นเอง
 
รูป ไดอะแกรมเบื้องต้น



ขั้นตอนการทำ
ศึกษาและทดสอบ
- Esp8266 server + OLED
  |-Mode AP
- Esp8266 Client
  |-Mode Static
-รับ ส่ง ข้อมูลรูปแบบ json
*ข้อมูลชนิด json ถือว่าเป็นเรื่องที่จำเป็นต้องศึกษา เพราะในโลกของ web database ส่วนใหญ่ก็จะใช้รูปแบบการสื่อสาร json เข้ามาช่วย แทนการส่งข้อมูลเป็นก้อนๆ ออกไป

ฮาดแวร์ Esp server + Esp client

กำหนดให้จอ OLED แสดงผลตัวแปรที่อ้างอิงก่อนเพื่อใช้ในการทดสอบ และวางกราฟฟิก
-โดยให้แสดงผล IP ของ Esp8266 ไม่ว่าจะเป็นโหมด Static หรือ AP
-ให้แสดงผลของ Client ที่สามารถ Connect ได้ แต่ในการทดสอบก็อ้างอิงตัวแปร float  เพื่อใช้ในการแสดงผลก่อน







#include <ESP8266WiFi.h>
#include <WiFiClient.h> 
#include <ESP8266WebServer.h>
#include "global.h"
#include "Initial.h"

#include <ESP_SSD1306.h>    // Modification of Adafruit_SSD1306 for ESP8266 compatibility
#include <Adafruit_GFX.h>   // Needs a little change in original Adafruit library (See README.txt file)
#include <SPI.h>            // For SPI comm (needed for not getting compile error)
#include <Wire.h>           // For I2C comm, but needed for not getting compile error
#include <TimeLib.h>

ESP_SSD1306 display(OLED_RESET); // FOR I2C


//***Other************************************//

String line;

//***OLED************************************//
// Pin definitions
#define OLED_RESET  16  // Pin 15 -RESET digital signal


// Display demo definitions
#define NUMFLAKES 10
#define XPOS 0
#define YPOS 1
#define DELTAY 2
#define LOGO16_GLCD_HEIGHT 16
#define LOGO16_GLCD_WIDTH  16



//***WiFi************************************//
const char* ssid = "ROMSAI-A1";
const char* password = "rsap1111";
/* Set these to your desired credentials. */
const char *ssidAP = "ESPap";
const char *passwordAP = "esp@thai";

//***************************************//
const char* wifi_ip[4] = {"192", "168", "1", "250"}; // Change your ip local network
const char* wifi_subnet[4] = {"255", "255", "255", "0"}; // Change your subnet local network
const char* wifi_gateway[4] = {"192", "168", "1", "1"}; // Change your gateway local network
void setup()
{
  Serial.begin(115200);
  WiFi.disconnect(true);  // delete old config
  SetMode("WIFI_AP_STA");    //WIFI_STA and WIFI_AP_STA

  // SSD1306 Init
  display.begin(SSD1306_SWITCHCAPVCC);  // Switch OLED
  display.clearDisplay();

  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(0,0);
  display.println("Hello");
  display.display();
  delay(2000);
  display.clearDisplay();
}

void loop()
{
  dsp_myIP();
}

void dsp_myIP()
{
  IPAddress myIPx = WiFi.softAPIP();
  display.setTextSize(1);
  display.setCursor(0,10);
  display.println( myIPx );
  display.display();
  delay(2000);
  display.clearDisplay();
}

void wifiScan(void)
{
    Serial.println("scan start");

    // WiFi.scanNetworks will return the number of networks found
    int n = WiFi.scanNetworks();
    Serial.println("scan done");
    if (n == 0) {
        Serial.println("no networks found");
    } else {
        Serial.print(n);
        Serial.println(" networks found");
        for (int i = 0; i < n; ++i) {
            // Print SSID and RSSI for each network found
            Serial.print(i + 1);
            Serial.print(": ");
            Serial.print(WiFi.SSID(i));
            Serial.print(" (");
            Serial.print(WiFi.RSSI(i));
            Serial.print(")");
            Serial.println((WiFi.encryptionType(i) == ENC_TYPE_NONE)?" ":"*");
            delay(10);
        }
    }
//    Serial.println("Setup done");
    Serial.println("==============================");
}
//****************************************//
//***************************************//
void configWiFiModeSTA(void)
{
  WiFi.begin(ssid, password);
  //WiFi.begin(ssid, password);
  Serial.println("");

  /*=== If you not use static ip comment here ===*/
  WiFi.config(
    IPAddress(atoi(wifi_ip[0]), atoi(wifi_ip[1]), atoi(wifi_ip[2]), atoi(wifi_ip[3])),
    IPAddress(atoi(wifi_gateway[0]), atoi(wifi_gateway[1]), atoi(wifi_gateway[2]), atoi(wifi_gateway[3])),
    IPAddress(atoi(wifi_subnet[0]), atoi(wifi_subnet[1]), atoi(wifi_subnet[2]), atoi(wifi_subnet[3]))
  );
  /*=== End comment here ===*/
  
  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
  Serial.println("");
  delay(1000);
  
}
void configModeAP(void)
{
    Serial.println("");
    Serial.println("Configuring access point...");
    /* You can remove the password parameter if you want the AP to be open. */
    WiFi.softAP(ssidAP, passwordAP);
    
    IPAddress myIP = WiFi.softAPIP();
    Serial.print("AP IP address: ");
    Serial.println(myIP);
    delay(1000);

}
//****************************************//
//***ConfigMode***************************//
void ConfigMode(String strMode)
{
  Serial.print("Setup Mode : ");
  Serial.println(strMode);
  
  if( strMode == "WIFI_AP_STA" )
  {
    configModeAP();
  }
  if( strMode == "WIFI_STA" )
  {
    wifiScan();
    configWiFiModeSTA();
  }
  
  Serial.println("Esp32 Ready...");
  Serial.println("==============================");
}
//***************************************//
//***************************************//
//**SET-MODE*****************************//
void SetMode(String strWIFI_mode)
{
  //Set WiFi to station mode and disconnect from an AP if it was previously connected
  //WiFi.mode =>  WIFI_AP_STA OR  WIFI_STA
  
  if( strWIFI_mode == "WIFI_STA" )
    WiFi.mode(WIFI_STA);
  else if( strWIFI_mode == "WIFI_AP_STA" )
    WiFi.mode(WIFI_AP_STA);
  WiFi.disconnect();
  delay(1000);
  
  ConfigMode(strWIFI_mode);
//
//  confStage = strWIFI_mode;       //confStage to void handleConfig
}

//======================================================//

ขอพูดถึงข้อมูลชนิด json จะประกอบด้วย Root และ Data ข้อมูลจะอยู่ภายใต้เครื่องหมาย {} ซึ่งจะคล้าายกับตัวแปล Array เพียงแต่การอ้างอิงตำแหน่งใช้ ข้อมูลอ้างอิงดึงค่าออกมาได้เลย


"{"client_01":{"temperature":"123.45","humidity":"567.89"}}"



//======================================================//

Esp8266 server mode AP + Esp8266 client mode Static

กำหนดให้ Esp8266 server เป็นโหมด AP เพื่อปล่อยสัญญาณ WiFi ให้อุปกรณ์ตัวอื่นมาเกาะ และให้ Esp8266 client เป็นโหมด Static ไปจับสัญญาณ WiFi
ในที่นี้ได้โปรแกรมโหมด AP สามารถกำหนด IP Address ได้เช่นกัน

*SSID และ Pass ต้องตรงกันทั้ง 2 ฝั่ง หาก Client ไปเกาะ WiFi ตัวอื่นหรือ Router ทำให้อุปกรณ์หากันไม่เจอ และ IP address ต้องไม่ชนกันหรือไม่ใช้เบอร์เดียวกัน

 Esp8266 client เมื่อมีการ Refresh web page ให้ไปอ่านค่า Temp เพื่อมาแสดงผล ซึ่งข้อมูลเป็นชนิด json และโปรแกรมมีการป้องการไม่ให้ Esp8266 เกิด WDT Error ด้วยการให้อ่านค่า Temp เมื่อเวลาต่างกับ 2 mS เป็นต้นไป หากมีการ Refresh หน้าเว็บซ้ำๆ ถี่ๆ ก็จะได้ข้อมูลเดิมๆ

ทดสอบเขียนโปรแกรมโดยให้ Esp8266 client ไปจับ WiFi Esp8266 server เมื่อ Connect กันได้แล้ว ให้ Server Query web page ด้วยข้อมูลชนิด json และแยกข้อมูลออกเป็นส่วนๆ Name, temperature, humidity  นำไปแสดงผล



การแสดงตำแหน่งตัวเลขผลกำหนดให้ Temp แสดงผล  000.00   Hum แสดงผล 00.00  เนื่องด้วยพื้นที่จำกัด

//======================================================//
เมื่อ Connect Esp client ได้หนึ่งตัวแล้ว ตัวสองก็ตามมา



ทดสอบเมื่อ Client ไม่สามารถ Connect ได้ให้แสดงผล Null ทดสอบทั้ง 2 ตัวสลับไปมา

*ลองใช้ Esp8266 V.01  V.12 เป็น Client ผลคือ V.01 มีปัญหา หาก V.12 ไม่มีการ Connect ตัวมันเองก็จะตัดการเชื่อมต่อเองไปด้วย ซึ่งเคยบอกหลายๆคนเสมอว่า V.01 ปัญญหาเยอะจัด แต่เอาไว้เทสได้








//======================================================//

Down load : https://drive.google.com/file/

Contact :https://www.facebook.com/dan.thummaratkmitl