# LAB8: MCU send data to IoT platform

### อุปกรณ์ทดลอง

1. M5stack demo board

### การทดลองที่1: ส่งข้อมูลเซนเซอร์ DHT12 ขึ้น Dashboard ของ Senses IoT platform

#### สร้าง Widget บน Dashboard สำหรับ แสดงข้อมูลอุณภูมิ และ ความชื้น ที่ได้รับจาก sensor  DHT12

1. ไปที่หน้า Dashboard ที่สร้างไว้(วิธีการสร้างdashboard คลิ๊ก[ วิธีสร้าง-Dashboard](https://app.gitbook.com/@aic-ecc/s/docs/~/drafts/-MJkijFBsJdZ6FsRfIC7/embedded-systems/microcontroller-for-iot-1/microcontroller-for-iot) ) >> ไปที่ edit Dashboard >>เลือกสร้าง widget Gauge >> setting >>ตั้งค่าดังภาพด้านล่าง

![สร้าง Widget gauge](https://paper-attachments.dropbox.com/s_2990F93D63431511BCFD8DE42E74EF440A1123ABA720A8E3F3F6DE20DA122D33_1602821004039_image.png)

![widget setting of widget gauge](https://paper-attachments.dropbox.com/s_2990F93D63431511BCFD8DE42E74EF440A1123ABA720A8E3F3F6DE20DA122D33_1602820816984_image.png)

&#x20;    2\. เลือกสร้าง widget chat>> setting >>ตั้งค่าดังภาพด้านล่าง>>Save

![สร้าง widget chat ](https://paper-attachments.dropbox.com/s_2990F93D63431511BCFD8DE42E74EF440A1123ABA720A8E3F3F6DE20DA122D33_1602821489435_image.png)

![widget setting of widget chat](https://paper-attachments.dropbox.com/s_2990F93D63431511BCFD8DE42E74EF440A1123ABA720A8E3F3F6DE20DA122D33_1602821382568_image.png)

&#x20;    3\. เมื่อตั้งค่าทั้งสอง widget เรียบร้อยแล้ว ให้กด Save edited Dashboard&#x20;

![](https://paper-attachments.dropbox.com/s_2990F93D63431511BCFD8DE42E74EF440A1123ABA720A8E3F3F6DE20DA122D33_1602825108351_image.png)

&#x20;   4\. กด F5 Dashboard จะแสดงหน้าต่าง widget พร้อมใช้งาน

![widget on Dashboard](https://paper-attachments.dropbox.com/s_2990F93D63431511BCFD8DE42E74EF440A1123ABA720A8E3F3F6DE20DA122D33_1602825255708_image.png)

#### ต่อวงจรระหว่าง M5Stack กับ DHT12 Sensor

| M5Stack (use pin  I2C ) | sensor DHT12 |
| :---------------------: | :----------: |
|           21D           |      SDA     |
|           22C           |      SCL     |
|           GND           |      GND     |

#### Code M5Stack send data sensor DHT12 to dashboard&#x20;

```

#include "Senses_wifi_esp32.h"
#include <WiFi.h>
#include <DHT12.h>

const char *ssid  = "...";   //  your network SSID      
const char *pass = "...";    //your network password  
const char *userid = "..." ;  // channel ID number of your Dashboard
const char * key = "..."; // Paste your Write Key of Dashboard 

WiFiClient  client;
DHT12 dht12;
Senses_wifi_esp32 myiot;

void setup()
{
    Serial.begin(115200);
    delay(10);
    dht12.begin();

// We start by connecting to a WiFi network and sense platform
 Serial.println("Senses Platform status: ");
 String response = myiot.connect(ssid, pass, userid, key);
  Serial.println(response);
    
}

void loop (){
  float tmp = dht12.readTemperature();
  float humid = dht12.readHumidity();

if (isnan(tmp) || isnan(humid)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
// myiot.send(send data to slot_number 1 of dashboard, send tmp data)
 String response = myiot.send(1,tmp); 
 Serial.println(response);
 Serial.print("Temperature :");
 Serial.println(tmp);

// myiot.send(send data to slot_number 2 of dashboard ,send Humid data) 
  response = myiot.send(2, humid); 
  Serial.println(response);
  Serial.print("Humidity :");
  Serial.println(humid);
  delay(2000);

}  
```

#### บันทึกผล หน้า Serial monitor และ dashboard ที่แสดงค่าข้อมูลที่ได้รับ

....................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................

### การทดลองที่2: ส่งข้อมูลเซนเซอร์ BMP280 ขึ้น Dashboard ของ Senses IoT platform

#### สร้าง Widget บน Dashboard สำหรับ แสดงข้อมูลความดันอากาศ จาก BMP280 sensor

1. การสร้างdashboard ใหม่มา (วิธีสร้าง dashboard คลิ๊ก[ วิธีสร้าง-Dashboard](https://app.gitbook.com/@aic-ecc/s/docs/~/drafts/-MJkijFBsJdZ6FsRfIC7/embedded-systems/microcontroller-for-iot-1/microcontroller-for-iot) )
2. หลังจากสร้าง Dashbboard ใหม่เรียบร้อยแล้วให้คลิ๊กที่ Dashborad menager >> เลือก Dashboard อันใหม่

![](https://paper-attachments.dropbox.com/s_2990F93D63431511BCFD8DE42E74EF440A1123ABA720A8E3F3F6DE20DA122D33_1602833221010_image.png)

![](https://paper-attachments.dropbox.com/s_2990F93D63431511BCFD8DE42E74EF440A1123ABA720A8E3F3F6DE20DA122D33_1602833363140_image.png)

&#x20;    3\. ไปที่ edit Dashboard >>เลือกสร้าง widget Gauge และ widget chat แบบการทดลองที่1&#x20;

<div align="center"><img src="https://paper-attachments.dropbox.com/s_2990F93D63431511BCFD8DE42E74EF440A1123ABA720A8E3F3F6DE20DA122D33_1602834949474_image.png" alt="Widget setting of  Widget Gauge"></div>

![Widget setting of  Widget Chat](https://paper-attachments.dropbox.com/s_2990F93D63431511BCFD8DE42E74EF440A1123ABA720A8E3F3F6DE20DA122D33_1602835788830_image.png)

&#x20;    4\. เมื่อตั้งค่าทั้งสอง widget เรียบร้อยแล้ว ให้กด Save edited dashboard&#x20;

&#x20;    5\. กด F5 Dashboard จะแสดงหน้าต่าง widget พร้อมใช้งาน

#### ต่อวงจรระหว่าง M5Stack กับ BMP280 Sensor

| M5Stack (use pin  I2C ) | sensor BMP280 |
| :---------------------: | :-----------: |
|           21D           |      SDA      |
|           22C           |      SCL      |
|           GND           |      GND      |

#### Code M5Stack send data sensor BMP280 to dashboard

```

#include "Senses_wifi_esp32.h"
#include <Wire.h>
#include "Adafruit_Sensor.h"
#include <Adafruit_BMP280.h>

const char *ssid  = "....";   // your network SSID (Hot spotname)     
const char *pass = "....";    // your network password  
const char *userid = "...." ;  // channel ID number (Senses ID)
const char * key = "....";    // Device key 
//------------------------------------------------------------------
WiFiClient  client;
Senses_wifi_esp32 myiot;
Adafruit_BMP280 bme;

void setup()
{
    Serial.begin(115200);
    delay(10);
    Wire.begin();
    if (!bme.begin(0x76)){
    Serial.println("Could not find a valid BMP280 sensor, check wiring!");
        while (1);
    }
    // We start by connecting to a WiFi network and sense platform
    Serial.println("Connect to hot spot");
    String response = myiot.connect(ssid, pass, userid, key);
    Serial.println(response);
    
}

void loop (){
  float pressure = bme.readPressure();
  String response = myiot.send(1,pressure); // myiot.send(slot_mnumber on SensesPlatform,data)
  Serial.println(response);
  Serial.print("Air pressure :");
  Serial.println(pressure);

}  
```

#### บันทึกผล หน้า Serial monitor และ dashboard ที่แสดงค่าข้อมูลที่ได้รับ

....................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................

### การทดลองที่3: ส่งข้อมูลเซนเซอร์ Microphone และ light  ขึ้น Dashboard ของ Senses IoT platform

#### สร้าง Widget บน Dashboard สำหรับ แสดงข้อมูลระดับความเข้มแสง ที่ได้รับจาก sensor LDR และ ข้อมูล เสียง ที่ได้รับจาก Microphone&#x20;

1. เนื่องจาก Senses IoT platform ให้สิทธิสำหรับผู้ใช้ฟรี ในการสร้าง dashboard ได้เพียง 2 Dashboard ดังนั้นเลือก 1 dashboard มาทำการ edit เพื่อสำหรับรับค่าข้อมูลของ sensor LDR และ เสียง จากMicrophone&#x20;
2. ไปที่ edit Dashboard
3. เลือกสร้าง widget Gauge 2 ตัว สำหรับแสดงค่า แสง เสียง ตั้งค่าตามภาพด้านล่าง

![Widget setting of Light widget gauge](https://paper-attachments.dropbox.com/s_2990F93D63431511BCFD8DE42E74EF440A1123ABA720A8E3F3F6DE20DA122D33_1602838405817_image.png)

![Widget setting of Microphone widget Gauge](https://paper-attachments.dropbox.com/s_2990F93D63431511BCFD8DE42E74EF440A1123ABA720A8E3F3F6DE20DA122D33_1602838738456_image.png)

&#x20;  4\. เลือกสร้าง widget chat  สำหรับแสดงค่า แสง แบบ chat ตั้งค่าตามภาพด้านล่าง

![Widget setting of Widget MixedChat](https://paper-attachments.dropbox.com/s_2990F93D63431511BCFD8DE42E74EF440A1123ABA720A8E3F3F6DE20DA122D33_1602838641546_image.png)

&#x20;  5\. เมื่อตั้งค่าเรียบร้อยแล้ว ให้กด Save edited dashboard&#x20;

&#x20;  6\. กด F5 Dashboard จะแสดงหน้าต่าง widget พร้อมใช้งาน

#### ต่อวงจรระหว่าง M5Stack กับ Light Sensor และ Microphone

| M5Stack | sensor Light | Microphone |
| :-----: | :----------: | :--------: |
|    36   |    Analog    |      -     |
|    35   |       -      |   Analog   |
|    2    |    Digital   |      -     |
|    5    |       -      |   Digital  |
|   GND   |      GND     |     GND    |

#### Code M5Stack send data (Analog and Digital data ) of Light sensor and Microphone to Dashboard

```

#include "Senses_wifi_esp32.h"
#include <WiFi.h>


const char *ssid  = "....";   // your network SSID (Hot spotname)     
const char *pass = "....";    // your network password  
const char *userid = "...." ;  // channel ID number (Senses ID)
const char * key = "....";    // Device key 
//------------------------------------------------------------------

WiFiClient  client;
Senses_wifi_esp32 myiot;
const int Analog1 = 36;
const int Analog2 = 35;
const int Digital1 = 2;
const int Digital2 = 5;

void setup()
{
    Serial.begin(115200);
    delay(10);
    pinMode(Digital1, INPUT_PULLUP);
    pinMode(Digital2, INPUT_PULLUP);
    // We start by connecting to a WiFi network and sense platform
    Serial.println("Connect to hot spot");
    String response = myiot.connect(ssid, pass, userid, key);
    Serial.println(response);
    
}

void loop (){
  float LightA = analogRead(Analog1);
  float LightD = digitalRead(Digital1);
  float MicA = analogRead(Analog2);
  float MicD = digitalRead(Digital2);

  String response = myiot.send(1,LightA); // myiot.send(slot_mnumber on SensesPlatform,data)
  Serial.println(response);
  Serial.print("Light dencity :");
  Serial.println(LightA);

  response = myiot.send(2, LightD); // myiot.send(slot_mnumber on SensesPlatform,data)
  Serial.println(response);
  Serial.print("Light status :");
  Serial.println(LightD);

  response = myiot.send(3,MicA); // myiot.send(slot_mnumber on SensesPlatform,data)
  Serial.println(response);
  Serial.print("Sound volumn :");
  Serial.println(MicA);

  response = myiot.send(4, MicD); // myiot.send(slot_mnumber on SensesPlatform,data)
  Serial.println(response);
  Serial.print("Sound status :");
  Serial.println(MicD);
  delay(500);

}  
```

#### บันทึกผล หน้า Serial monitor และ dashboard ที่แสดงค่าข้อมูลที่ได้รับ

....................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................

### Homework: เลือกเซนเซอร์ 3 ประเภท และออกแบบหน้า Dashboard สำหรับแสดงค่า sensor และเขียนโปรแกรมส่งข้อมูลเซนเซอร์ ขึ้น Dashboard&#x20;

Update:  2022

Author: Soontree Jaikhong (AIC-Researcher)

Author: Thanaluk Pranekunakol (AIC-Researcher)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.aic-eec.com/general/microcontroller-for-iot-1/lab-mcu-iot-send-data-to-senses-iot-platform.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
