วันอาทิตย์ที่ 27 ธันวาคม พ.ศ. 2563

Arduino Nano ต่อกับ OLED

 OLED แบบ ใช้ Library ของ SSD1306 ง่ายๆราคาไม่แพงการต่อให้ต่อ Arduino Nano ขา A4->OLED,SDA  และ A5->OLED,SCK ที่เหลือต่อ 5+ (ในนี้ใช้ Pin2) และ GND



code ต้อง Init ใน Loop เพื่อแสดง Text



#include "ssd1306.h"

 int vccP=2;

void setup()

{

 

   pinMode(vccP,OUTPUT); digitalWrite(vccP,HIGH);

    /* Replace the line below with ssd1306_128x32_i2c_init() if you need to use 128x32 display */

    ssd1306_128x64_i2c_init();

    ssd1306_fillScreen(0x00);

    ssd1306_setFixedFont(ssd1306xled_font6x8);

    ssd1306_printFixed (0,  8, "Line 1. Normal text", STYLE_NORMAL);

    ssd1306_printFixed (0, 16, "Line 2. Bold text", STYLE_BOLD);

    ssd1306_printFixed (0, 24, "Line 3. Italic text", STYLE_ITALIC);

    ssd1306_printFixedN (0, 32, "Line 4. Double size", STYLE_BOLD, FONT_SIZE_2X);

delay(1000);

}

int count=1;

void loop()

{

 

     ssd1306_128x64_i2c_init();

    ssd1306_fillScreen(0x00);

    ssd1306_setFixedFont(ssd1306xled_font6x8);

   char stt[30]="";

   sprintf(stt," cc %d",count);

      ssd1306_printFixed (0,  8, stt, STYLE_NORMAL);

count++;

   delay(1000);

   

}

วันเสาร์ที่ 12 ธันวาคม พ.ศ. 2563

ย้าย Line จาก Android ไป IPhone 12

 Line บน Android จะ ไม่มี Tool โดยตรงย้ายไป IPhone ทั้ง 2 ระบบมี backup Android จะ backup บน Google Cloud, IPone จะbackup ที่ ICloud

จะมี โปรแกรม ชื่อ backuptrans Android to Iphone

https://www.backuptrans.com/

การซื้อเป็น personal จะใช้ 1ชุด 3 เครื่อง คือ Android, Windows, Iphone 

ขั้นตอน Windows 10 และ Android (3-10 ชม)

1.ซื้อ และติดตั้ง บน Window 10 64 bits ตัวที่ อยู่บน Download กับ ตัวที่ซื่อคนละตัว เวลา ลง Register ต้องใช้ตัวที่ส่งมาทาง Email

2. ต่อ Android และ เปิด เป็น Developer mode Enable USB debug ค้นจาก google

3. backup Line เข้าที่ setting->chart เข้า Google Drive (ถ้ามีปัญหาเอากลับเครื่องเดิมได้)

3.ลง Itune และ connect Iphone ให้ได้ก่อน

4. กลับมาที่ android จะทำ หลายขั้นตอน

  ลง Appclone และ เพิ่ม Line กับ Chrome

  เปิด Line ใน Appclone และ activate , restore  จาก Google Drive เสร็จแล้ว setting ใน Appclone ปิด App Line ด้วย

5.ต่อ USB android เปิด โปรแกรม backuptrans จะ connect โปรแกรม ต้องเป็น File Transfer(ในหน้าของ Android)และ ลงโปรแกรม Transfter ใน Android ปล่อยให้โปรแกรม ดูด Line เข้าเครื่อง. (หลาย ชั่วโมง)

6. เสร็จ ห้าม ดึงสายUSB ออก  กด save local ก่อน

7. save และ ตรวจfile มีแล้ว เอาสายออกได้

ต่อไป เป็นการส่งเข้า IPhone 12 ประมาณ 1 ชม

1. ใส่ Sim แล้ว Reboot IPhone 

2. ปิด Fine my Iphone และ Share location ใน setting

3. ลง Line และ activate ไปก่อน

4. ต่อ usb Iphone และ  Itune setup New Iphone ให้มี ชื่อก่อน connect ให้เห็น file transfer

     Itune จะให้ใส่ Passcode บน จอ Iphone

5. เปิด backuptrans จะมี Warning ก็ปล่อยผ่านไป ดึง data จาก line บน IPhone

6. กดเลือก local ที่ Data file ที่ tree และ กดเมนู Local Data to Iphone 

7.เมื่อเสร็จ IPhone จะ Restore Mode ทำตามขั้นตอน

8. IPhone กด เลือก ไม่ต้อง Restore App


การต่อวงจร LC ลด Noise ของ วงจรวัดอุณหถูมิ Max6675

 Noise มาตามวัด อุณหถุมิ ยิ่งยาวจะมีมาก วิธีลด ให้ใช้ วงจร LC Low Pass Filter

ค่า L 10 mH(เขียว), C 106 หรือ 10 micro F

การต่อให้ L สอง สาย ก่อน C 

C ให้ต่อ 2 Input  และ เอา  C อีก 2 ตัว ต่อ แต่ละสาย ลง Ground




วันพุธที่ 2 ธันวาคม พ.ศ. 2563

Arduino ต่อ Buzzer

 จะมี 3 Pin

หันหน้าเข้า อุปกรณ์




I S เป็น+

กลาง เป็น signal pin

- เป็น Ground



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

Inno setup ให้มี Input Page Custom มี Serial number,Email เก็บใน registry

 Inno Setupใช้ ภาษา Pascal การทำให้มี Custom page ทำโดย สร้าง Code ให้ Return ค่าจาก Page

โดยที่ ตัวแปรของ Page จะตามลำดับ Input 0..n

ในส่วนของ Registry ก็ให้ getค่ามาจาก page นี้ ValueData: "{code:GetInstallName}"



[Code]

var

 UserInputsPage: TInputQueryWizardPage;

function GetUserName(Param: string): string;

begin

  Result := UserInputsPage.Values[0];

end;

function GetUserCompany(Param: string): string;

begin

  Result := UserInputsPage.Values[1];

end;

function GetUserEmail(Param: string): string;

begin

  Result := UserInputsPage.Values[2];

end;

function GetUserSerialNo(Param: string): string;

begin

  Result := UserInputsPage.Values[3];

end;


procedure InitializeWizard;

begin

  { Create the page }

  UserInputsPage :=

    CreateInputQueryPage(wpWelcome,

      'User information', 'User Information',

      'Please specify the following information, then click Next.');


 UserInputsPage.Add('Name:', False); 

 UserInputsPage.Add('Company,Organization:', False);

 UserInputsPage.Add('Email Name:', False);

 UserInputsPage.Add('Serial Number:', False);

end;


[Registry]

Root: HKCU; Subkey: "Software\Power Partners\abc"; \

    ValueType: string; ValueName: "Name"; ValueData: "{code:GetInstallName}"

Root: HKCU; Subkey: "Software\Power Partners\abc"; \

    ValueType: string; ValueName: "UserCompany"; ValueData: "{code:GetUserCompany}"

 Root: HKCU; Subkey: "Software\Power Partners\abc"; \

    ValueType: string; ValueName: "UserEmail"; ValueData: "{code:GetUserEmail}"

Root: HKCU; Subkey: "Software\Power Partners\abc"; \

    ValueType: string; ValueName: "sn"; ValueData: "{code:GetUserSerialNo}"


วันพุธที่ 21 ตุลาคม พ.ศ. 2563

NodeJs express เรียก static html page

 แก้ปัญหา HTML page แบบโบราณ บน Node server


app.use(['/wildcard*', '/wildcard'], (req, res) => {
let dynamicPath = req.baseUrl.replace('/wildcard', '');
fs.stat('<absolute path>' + dynamicPath, (err, stats)=>{
if(err!==null){
res.status(200).send('No file or directory with path : ' + dynamicPath);
}else if(stats.isFile()){
res.sendFile('<absolute path>' + dynamicPath);
}else if(stats.isDirectory()){
dynamicPath = dynamicPath === '' ? '/' : dynamicPath;
res.status(200).send('Found directory with path : ' + dynamicPath);
}
})

วันอังคารที่ 20 ตุลาคม พ.ศ. 2563

NodeJS ส่ง Print Text จริงๆ

 แสดง. Text มี 2 แบบ เป็น HTML และ Text ต้องบอก res.type('txt').  ถ้าไม่นั้นต้อง .replace('\n', <br/>)

app.get('/textfile', (req, res) => {
// var text = 'Hello world!\nabcde'
//res.attachment('filename.txt')
var fs = require("fs");
var text = fs.readFileSync('./test.txt').toString();
res.type('txt')
res.send(text)
})

การให้ NodeJs เรียก PHP

 PHP เป็นระบบ Application Run เป็น Interpreter และ NodeJs ก็คล้ายกัน แต่ NodeJs เป็นทั้ง Server และ Client  App ทั้ง 2 แบบมี จำนวนมาก เพื่อให้ run ใน Port 80 เหมือนกัน ต้องให้เป็น Host 1 ตัว

เลือก NodeJs เป็น Host

1.ติดตั้ง nodejs และ php ให้เรียบร้อย

2.ใน Node directory 

     npm install express

     npm install exec-php


3. code ของ PHP

    เช่น

    <?php

echo "One";
function my_function($arg1, $arg2){
echo "Two";
return $arg1 + $arg2;
}

และ Code ของ Node
server.js
const express = require('express')
const app = express()
const PORT = process.env.PORT || 8080
const { doPHP, phpMiddleware } = require('./app.js')

app.get('/mid', phpMiddleware, (req, res) => res.send(req.result + ""))

app.listen(PORT, () => {
console.log(`Server is running on port : ${PORT}`)
})
module.exports = app


สำหรับ app.js

function phpMiddleware(req, res, next){
execPhp('file.php', function (error, php, outprint) {

// outprint is now `One'.

php.my_function(1, 2, function (err, result, output, printed) {
// result is now `3'
console.log(result)
req.result = [result, output, printed].join(" ");
// res.send(result+"")
// res.sendStatus(400)
next();
// output is now `One'.
// printed is now `Two'.
});
});
}

module.exports = {doPHP, phpMiddleware}


การใข้ Middleware จะทำให้สะดวกขึ้น ไม่ต้องส่ง ต้ว app.get(.. res) res ไปให้ Function ที่ execPHP เพราะ execPHP จะ run เป็น Asyn ไม่ได้รอกัน ต้องส่ง res ไปให้ update callback(cb)ค่ากลับมา ตัวอย่าง
const execPhp = require('exec-php');
function doPHP(cb) {
var out1 = 'x';
execPhp('file.php', function (error, php, outprint) {

// outprint is now `One'.

php.my_function(1, 2, function (err, result, output, printed) {
// result is now `3'
console.log(result)
out1 = result;
cb.status(200).send(result.toString())
// output is now `One'.
// printed is now `Two'.
});
});
}
ต้นทาง
app.get('/php', (req, res) => doPHP(res))

วันศุกร์ที่ 16 ตุลาคม พ.ศ. 2563

การ Run node background ใน FreeBSD

 จะ run Node ต้องเช็ค Port ก่อน ใช้ socks tat

socks tat -4 -l

ดูว่ามี Process ใหน Lock port แล้ว 

kill -9 <pid>

ถ้าไม่ได้ ต้อง sudo kill -9 <pid>

สำหรับ run ให้ ใช้

nohub npm run start &

ตรวจว่า มี error ที่ nohub.out

ถ้า ไม่ได้ ต้องใช้ sudo nohub npm run start &

 

วันอังคารที่ 29 กันยายน พ.ศ. 2563

load และ save Autodesk forge Viewer measurement

 คำสั่งใน Javascript สำหรับ Forge Viewer Measurement Extension V7.x


const extension = viewer.getExtension('Autodesk.Measure');

เมื่อ เขียน จะได้  array ของ measurement

var xx1=extension.getMeasurementList() 

var xx2=xx1[0]  สมมติเอาตัวที่ 0

ไปลบ measurement ออกให้หมด. extension.deleteMeasurements()

extension.setActive(true);
extension.setMeasurementList(xx2)
extension.setActive(false);
จะแสดงกลับมา
ต้องการ Calibrate ให้
extension.forceCAlibrate=1
แล้วเลือกวาง Measurement

c# เชื่อม mongodb

 Mongodb ที่จะเชื่อมให้ทดสอบ จาก Command line ก่อนว่า Ip และ port ใช้ได้ใหม

จากนั้น ติดต่อโดย

สร้าง Class ให้ตรงกับ ใน Database

เช่นใน Database ชื่อ admin และ มี Collections students

  1. _id
    :
    5f732bb0bbf8cd98789271b8
  2. name
    :
    "abcd"
  3. data
    :
    "xxx"


สร้าง Class

 public class studentsinfo

    {

        public ObjectId _id { get; set; }

        public string name { get; set; }

        public string data { get; set; }

    }



ในการติดต่อให้ ติดต่อโดนลง nuget ใน Visualstudio

nuget install mongoldb.driver


สำหรับ Code ใน C#


 {

        List<string> names = new List<string>();

                                                                                    // user : password      @ip:port.    /                       database assign to

        MongoClient client = new MongoClient("mongodb://admin:passw@192.168.4.1:2277/admin");



        var myDB = client.GetDatabase("admin");

      

        var Students = myDB.GetCollection<studentsinfo>("students");



        foreach (studentsinfo Astudent in Students.Find(name=> true).ToList()) 

        {

         

            names.Add(Astudent.name);

        }

        ListBox1.DataSource = names;

        ListBox1.DataBind();

    

    }


สำหรับ Insert 

 {

        var dbClient = new MongoClient(constr);


        IMongoDatabase db = dbClient.GetDatabase("admin");


        var cdb = db.GetCollection<BsonDocument>("students");


        var doc = new BsonDocument

            {

                {"name", "BMW"},

                {"data", "34621"}

            };


        cdb.InsertOne(doc);

    }

ถ้า Update ต้องมี 3 คำสั่ง filter,updatedata,Update

   var filter = Builders<BsonDocument>.Filter.Eq("name", "ชื่อ");
            var update = Builders<BsonDocument>.Update.Set("data", 52000);

            cdb.UpdateOne(filter, update);


วันจันทร์ที่ 28 กันยายน พ.ศ. 2563

javascript ใน ASPX call และ ส่ง Data Method ใน c#

 ASP เป็น ระบบโปรแกรมที่ใช้ทำ Web ของ Microsoft IIS สามารถทำงาน เชื่อมกับ Javascript ได้

โดยใช้ 

[WebMethod] ก้บ asp:ScriptManager

ตัวอย่างจะมี Button ทั้ง 2 ชนิด เป็น Asp:Button และ html button

code ใน Client Front

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>


<!DOCTYPE html>


<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">  

    <title></title>  

    <script type="text/javascript">  

        function SendToback() {  

           var name = document.getElementById('<%=txtname.ClientID %>').value;  

            var testStr = "client";

  

            PageMethods.doBackEnd(name, testStr, onSucess, onError);

           function onSucess(result) {  

                alert(result);  

            }  

  

            function onError(result) {  

                alert('Something wrong.');  

            }  

        }  

   </script>  

</head>  

<body>



    <form id="form1" runat="server">

   <div>  

    <p>Javascript send to Postbacks.</p>  

  

    <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"></asp:ScriptManager>  

  

    <asp:TextBox ID="txtname" runat="server"></asp:TextBox>  

    <br />  

   

    <asp:Button ID="btnCreateAccount" runat="server" Text="Asp" OnClientClick="SendToback(); return false;" />  

     

    </form> 

     <input id="Button1" type="button" value="html" onclick="SendToback();" /></div>  

</body>

</html>



สำหรับ back ใน c#


using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Services;

using System.Web.UI;

using System.Web.UI.WebControls;


public partial class _Default : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {


    }

    [WebMethod]

    public static string doBackEnd(string name, string text)

    {

        string result = "Receive " + name + ". text = '" + text + "'.";

        return result;

    }

}

วันอาทิตย์ที่ 27 กันยายน พ.ศ. 2563

การเชื่อมโยง Web html. javascript client กับ NodeJs server

 การส่งค่า ระหว่าง Web Html กับ NodeJs ทั้ง 2 ทาง ทำได้ 2 วิธี คือ เป็น Web Post กับ Javascript

ใน ส่วนของ server จำเป็นต้อง Install Tool เพิ่ม 2 ตัว ได้แก่ express กับ body-parser

สร้าง folder เช่น nodeExpress

คำสั่ง

cd nodeExpress

npm install express

nom install body-parser

File server.js


const express = require('express');

const bodyParser = require('body-parser');

const app = express();

// ให้ แยก body ทำ Urlencode

app.use(bodyParser.urlencoded({ extended: true }));

//ส่ง Index.html ให้แสดงเป็น Root

app.get('/', function(request, response){

  response.sendFile(__dirname+'/index.html');

});


// สำหรับการ Form Post

app.post('/example', (req, res) => {

  console.log(req.body.fname) // ระวังการใช้ Quote จะใช้ กับ Express ต้องเป็นแบบตย

  res.send(`Full name is:${req.body.fname} ${req.body.lname}.`);

  console.log(`${req.body.fname}`);

});


// สำหรับ javascript


app.post('/foo', foo);

function foo(req, res){

  console.log(req.body); // ที่รับมา

 res.send('hello world'); // ส่งกลับ

};


//  port

const port = 8090;


app.listen(port, () => {

  console.log(`Server running on port${port}`);

});


html ต้องส่งมาจาก server จะเรียกจาก File ไมได้ติด Securityของ Browser
สำหรับ หน้า จอ index.html. , 
check me javascript เป็นการเรึยกโดย javascript
Send to Backend เป็นการ Post ของ Form
การ Encode ต้องใช้ เป็น application/x-www-form-urlencoded
data ที่ส่งเป็น name=Big จะเป็น {name:"Big} ใน Server 
และ ต้องการต่อ ใช้ &
ดูค่า ให้ใช้ Chrome และ กด inspect จะเห็น Console

<!DOCTYPE html>
<html>
<body>
<script>
function myFunction(){
var data = "name=Big";// data ที่จะส่งด้วย javascript

var xhr = new XMLHttpRequest();
xhr.withCredentials = true;

xhr.addEventListener("readystatechange", function() {
if(this.readyState === 4) {
console.log(this.responseText);
}
});
xhr.open("POST", "http://localhost:8090/foo");
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send(data); // do send data here
}
</script>


<button onclick="myFunction()">Click me Javascript </button>
<form action="http://localhost:8090/example" method="POST">
First name: <input type="text" name="fname"><br>
Last name: <input type="text" name="lname"><br>
<button type="submit">Send to backend</button>
</form>
</body>
</html>


วันจันทร์ที่ 24 สิงหาคม พ.ศ. 2563

Asp.net และ upload folder

 ทำ Code ใน Asp.net ต้องการ upload เช่น App_Data มักจะเกิดปัญหา ว่า Upload ไม่ได้

ปัญหาคือ folder นั้น ไม่ได้ เปิด ให้ IIS user read/write

ไป แก้ ใน File Explorer และ iisreset ก็จะใช้ได้

อาจจะมีปีญหาในการ Deploy ครั้งต่อไปได้


วันจันทร์ที่ 17 สิงหาคม พ.ศ. 2563

การ สร้าง Project ใหม่ใน github

 ให้สร้าง repository ใหม่ ใน github ก่อน จะให้เป็น Public หรือ Private กำหนดให้เสร็จ


ใน PC หรือ window  ให้ เข้า directory ของ Project ถ้าทำไม่ได้ ให้ ลบ folder .git ไปก่อน

  1. git init
  2. git add .
  3. git commit -m "initial commit"
  4.  git remote add origin <remote repository URL>
  5. ตรวจ
    git remote -v
  6.  git push -f origin master
ถ้าต้องการ Share ให้ เลือก ที่ ใน Web github เข้า Repo และ Setting->Manage และ Invite ชื่อของคนที่ Register