ก๊วนซอฟท์แวร์ </softganz> SoftGang (Gang Software)

Web &amp; Software Developer Gang.

Topic List

โดย Little Bear on 17 พ.ย. 62 11:41

What is Google Data Studio?

Google Data Studio is a FREE data visualization tool that allows you to import data from multiple sources and visualize them on a report that is easy to read and share. You can import data from any source using Google Sheets or Data Connectors to create the report you want.

What is Google Colaboratory?

Colaboratory is a free Jupyter notebook environment that requires no setup and runs entirely in the cloud. With Colaboratory you can write and execute code, save and share your analyses, and access powerful computing resources, all for free from your browser.


Resource

โดย Little Bear on 9 พ.ย. 62 14:46

<script src="https://cdn.netpie.io/microgear.js"></script>

<script> const APPID = "softganz-test"; // AppID const KEY = "TIoxpISdG19dnmH"; // Key const SECRET = "flK17giWJ1pCMuepAD0HTOK6p"; // secret

const ALIAS = &quot;browser&quot;;          // ตั้งชื่ออุปกรณ์ เป็นอะไรก็ได้

var microgear = Microgear.create({
    key: KEY,
    secret: SECRET,
    alias : ALIAS
});

<br /> // สร้างฟังก์ชั่นที่จะตอบสนองต่อ message ที่ เข้ามา microgear.on('message', function(topic,msg) {
//ในที่นี้ เราจะเอาข้อความไปแทนข้อความของ HTML element ชื่อ data document.getElementById("data").innerHTML = msg; });

  // สร้างฟังก์ชั่นที่จะถูกเรียกเมื่อ connect NETPIE สำเร็จ
microgear.on('connected', function() {
    microgear.setAlias(ALIAS);      // สามารถเปลี่ยน alias ได้

    // แสดงข่อความให้ทราบว่า connect สำเร็จ
    document.getElementById(&quot;data&quot;).innerHTML = &quot;Now I am connected with netpie...&quot;;    

<br /> // ตั้ง timer ให้ทำงานทุก 1 วินาที (1000 หมายถึง 1000 ms) setInterval(function() {

        // ส่งข้อความไปยังอุปกรณ์ชื่อตามตัวแปร ALIAS ซึ่งก็คือตัวเอง
        microgear.chat(ALIAS,&quot;Hello from myself at &quot;+Date.now());
    },1000);
});

microgear.connect(APPID);  // สร้างการเชื่อมต่อไปยัง NETPIE

</script>

<div id="data">_____</div>




<script src="https://cdn.netpie.io/microgear.js"></script>

<script> const APPID = "softganz-test"; // AppID const KEY = "LdB6MvSys2PWB9j"; // Key const SECRET = "CuUiEku60fhEw7TmFqezhmHN8"; // Secret

const MYNAME = &quot;alice&quot;;          // ชื่อของตัวเอง
const YOURNAME = &quot;bob&quot;;         // ชื่อของเพื่อนที่จะคุยด้วย

var microgear = Microgear.create({
    key: KEY,
    secret: SECRET,
});

var times = 0

microgear.on('message',function(topic,msg) {
    document.getElementById(&quot;data&quot;).innerHTML = topic+&quot; : &quot;+msg;
});

microgear.on('connected', function() {
    microgear.setAlias(MYNAME);         // ตั้งชื่อตัวเอง
    document.getElementById(&quot;data&quot;).innerHTML = &quot;Now I am connected with netpie...&quot;;&lt;br /&gt;

    setInterval(function() {

        // chat หาเพื่อน
        times++
        microgear.chat(YOURNAME,&quot;Hello from &quot;+MYNAME+ &quot; #&quot;+times+&quot; at &quot;+Date.now());
    },1000);
});
microgear.on('disconnected', function() {
    document.getElementById(&quot;data&quot;).innerHTML = &quot;Now I am disconnected with netpie...&quot;;
});

microgear.connect(APPID);

</script>

<div id="data">_____</div>

<script src="https://cdn.netpie.io/microgear.js"></script>

<script>
    const APPID = "softganz-test";    // AppID
    const KEY = "cCm6NzTxtkscroD";        // Key
    const SECRET = "h4kTjFAQBUhPEP8o0U8O3fZ09";   // Secret

    const MYNAME = "bob";        // ชื่อของตัวเอง
    const YOURNAME = "alice";         // ชื่อของเพื่อนที่จะคุยด้วย

    var microgear = Microgear.create({
        key: KEY,
        secret: SECRET,
    });
    
    var times = 0

    microgear.on('message',function(topic,msg) {
        document.getElementById("data").innerHTML = topic+" : "+msg;
    });

    microgear.on('connected', function() {
        microgear.setAlias(MYNAME);         // ตั้งชื่อตัวเอง
        document.getElementById("data").innerHTML = "Now I am connected with netpie...";<br />

        setInterval(function() {

            // chat หาเพื่อน
            times++
            microgear.chat(YOURNAME,"Hello from "+MYNAME+ " #"+times+" at "+Date.now());
        },1000);
    });
    
    microgear.on('disconnected', function() {
        document.getElementById("data").innerHTML = "Now I am disconnected with netpie...";
    });

    microgear.connect(APPID);
</script>

<div id="data">_____</div>
<script src="https://cdn.netpie.io/microgear.js"></script>

<script>
   const APPID = "softganz-test";             
   const KEY = "1xQjGzMtaNdX6R1";                 
   const SECRET = "6YyQujajj2nOcagkMyxckfvcL";    

   const ALIAS = "switch";

  var microgear = Microgear.create({
    key: KEY,
    secret: SECRET,
    alias : ALIAS
  });

 function toggle() {
    if(document.getElementById("button").innerText=="OFF"){
    document.getElementById("button").innerText = "Waiting for ON"
        microgear.chat('pieled','1');   
    }
    else{
    document.getElementById("button").innerText = "Waiting for OFF"
        microgear.chat('pieled','0');
    }
 }
    
  microgear.on('message',function(topic,msg) {
      document.getElementById("data").innerHTML = "LED is "+(msg == 0 ? "OFF" : "ON");<br />
      if(msg=="1"){
          document.getElementById("button").innerText="ON";
      }else if(msg=="0"){
          document.getElementById("button").innerText="OFF";
      }
  });
  microgear.on('connected', function() {
    microgear.setAlias(ALIAS);
    document.getElementById("data").innerHTML = "Now I am connected with netpie...";
  });

microgear.connect(APPID);
</script>

<div id="data">_____</div>
<center>
<button onclick="toggle()" id="button">OFF</button>
</center>
// <a class="hashtag" href="/tags/include">#include</a>  2 บรรทัดนี้ ต้องมีเสมอ
<a class="hashtag" href="/tags/include">#include</a> <ESP8266WiFi.h>
<a class="hashtag" href="/tags/include">#include</a> <MicroGear.h>       <br />
<br />
<br />
const char* ssid     = "Handy6Plus";        // ชื่อ ssid
const char* password = "nopassword";    // รหัสผ่าน wifi

<a class="hashtag" href="/tags/define">#define</a> APPID   "softganz-test"          // ให้แทนที่ด้วย AppID
<a class="hashtag" href="/tags/define">#define</a> KEY     "G0Z1T2E0GSOtfqJ"            // ให้แทนที่ด้วย Key
<a class="hashtag" href="/tags/define">#define</a> SECRET  "WGbwwUPpwcT10xL5fEEHoSWG1"         // ให้แทนที่ด้วย Secret

<a class="hashtag" href="/tags/define">#define</a> ALIAS   "pieblink"  // ตั้งชื่อเล่นให้ device นี้

WiFiClient client;

int timer = 0;
char state = 0;

MicroGear microgear(client);     // ประกาศตัวแปร microgear

// สร้างฟังก์ชั่นที่จะถูกเรียกเมื่อมี message เข้ามา
void onMsghandler(char *topic, uint8_t* msg, unsigned int msglen) {
    Serial.print("Incoming message --> ");
    msg[msglen] = '\0';
    Serial.println((char *)msg);

    // ถ้า message ที่เข้ามาเป็น 1 ให้เปิด LED ถ้าเป็น 0 ให้ปิด LED
    if(*(char *)msg == '1'){
      digitalWrite(LED_BUILTIN, LOW);  // LED on
      microgear.chat("switch","1");
    }else{
      digitalWrite(LED_BUILTIN, HIGH);  // LED off
      microgear.chat("switch","0");
    }
}

// สร้างฟังก์ชั่นที่จะถูกเรียกเมื่อ microgear connect กับ NETPIE สำเร็จ
void onConnected(char *attribute, uint8_t* msg, unsigned int msglen) {
    Serial.println("Connected to NETPIE...");
    // เราอาจจะใช้โอกาสนี้ ตั้งชื่อ หรือเปลี่ยนชื่อ หรืออาจจะทำอะไรบางอย่าง เช่น subscribe<br />
    microgear.setAlias(ALIAS);
}

void setup() {
    // ประกาศให้ เวลามี message เข้ามา ให้กระโดดไปทำฟังก์ชั่น onMsghandler()
    microgear.on(MESSAGE,onMsghandler);

    // ประกาศให้เมื่อ connect สำเร็จ ให้กระโดดไปทำฟังก์ชั่น onConnected()
    microgear.on(CONNECTED,onConnected);

    Serial.begin(115200);
    Serial.println("Starting...");

    pinMode(LED_BUILTIN, OUTPUT);

   // initiate Wifi
    if (WiFi.begin(ssid, password)) {
        while (WiFi.status() != WL_CONNECTED) {
            delay(500);
            Serial.print(".");
        }
    }

    Serial.println("WiFi connected");<br />
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());

   // initial ตัวแปร microgear<br />
    microgear.init(KEY,SECRET,ALIAS);

    // เชื่อมต่อไป NETPIE ไปยัง AppID ที่กำหนด
    microgear.connect(APPID);  // ฟังก์ชั่นเชื่อมต่อ NETPIE
}

void loop() {
    // เช็คว่า  microgear ยัง connect หรือเปล่า
    if (microgear.connected()) { // ตรวจสอบการเชื่อมต่อ NETPIE
        Serial.println("..."); // พิมพ์แจ้งการเชื่อมต่อ NETPIE สำเร็จ
        // เราต้องเรียก microgear.loop() เป็นระยะ ไม่เช่นนั้น connection จะหลุด
        microgear.loop();

        if (timer >= 10000) {
            Serial.println("Publish..."); // พิมพ์แจ้งการส่งข้อมูล NETPIE

           // chat sาตัวเอง ด้วย state ที่ตรงข้ามกัน
            if(state==0){
               microgear.chat(ALIAS,state);
               state=1;
            }else{
               microgear.chat(ALIAS,state);
               state=0;
            }
            timer = 0;
        }<br />
        else timer += 100;
    }
    else {
        Serial.println("connection lost, reconnect...");<br />
        if (timer >= 5000) {
            microgear.connect(APPID);<br />
            timer = 0;
        }
        else timer += 100;
    }
    delay(100);
}