# Machinechat with STM32F746 and X-NUCLEO-IKS01A3

**URL:** <https://forum.digikey.com/t/machinechat-with-stm32f746-and-x-nucleo-iks01a3/9330>\
**Category:** machinechat\
**Created:** [October 8, 2020, 8:08pm UTC](https://forum.digikey.com/t/machinechat-with-stm32f746-and-x-nucleo-iks01a3/9330 "2020-10-08T20:08:28Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![RobertCNelson](https://sea2.discourse-cdn.com/digikey/user_avatar/forum.digikey.com/robertcnelson/32/12_2.png) [@RobertCNelson](https://forum.digikey.com/u/RobertCNelson)\
**Post date:** [October 8, 2020, 8:08pm UTC](https://forum.digikey.com/t/machinechat-with-stm32f746-and-x-nucleo-iks01a3/9330/1 "2020-10-08T20:08:28Z")

</div>

# Software

- [JEDI One](https://www.digikey.com/en/supplier-centers/machinechat)
- [Arduino](https://www.arduino.cc/)
- [STM32DUINO](https://github.com/stm32duino/Arduino_Core_STM32)

# Project Parts

- [NUCLEO-F746ZG](https://www.digikey.com/en/products/detail/stmicroelectronics/NUCLEO-F746ZG/5806779)
- [X-NUCLEO-IKS01A3](https://www.digikey.com/en/products/detail/stmicroelectronics/X-NUCLEO-IKS01A3/9950636)

In Arduino, use this link in the " _Additional Boards Managers URLs_ " field: to add STM32 support.

```auto
https://github.com/stm32duino/BoardManagerFiles/raw/master/STM32/package_stm_index.json

```

Add these libraries thru Arduino’s Library Manager:

- [STM32Ethernet](https://github.com/stm32duino/STM32Ethernet)
- [ArduinoHttpClient](https://github.com/arduino-libraries/ArduinoHttpClient)
- [ArduinoJson](https://arduinojson.org/)
- [X-NUCLEO-IKS01A3](https://github.com/stm32duino/X-NUCLEO-IKS01A3)

Approve the install of these library dependencies:

- [STM32duino LwIP](https://github.com/stm32duino/LwIP)
- [STM32duino LSM6DSO](https://github.com/stm32duino/LSM6DSO)
- [STM32duino LIS2DW12](https://github.com/stm32duino/LIS2DW12)
- [STM32duino LIS2MDL](https://github.com/stm32duino/LIS2MDL)
- [STM32duino HTS221](https://github.com/stm32duino/HTS221)
- [STM32duino LPS22HH](https://github.com/stm32duino/LPS22HH)
- [STM32duino STTS751](https://github.com/stm32duino/STTS751)
- [STM32duino LSM6DSOX](https://github.com/stm32duino/LSM6DSOX)

# Lab1

With the STM32H746 plugged into your PC thru the USB connector (CN1), let’s make sure we can program the board by using a simple Blink LED Demo, this code will blink LD1.

```auto
void setup() {
  // initialize digital pin LED_BUILTIN as an output
  pinMode(LED_BUILTIN, OUTPUT);
};

void loop() {
  digitalWrite(LED_BUILTIN, HIGH);
  delay(500);
  digitalWrite(LED_BUILTIN, LOW);
  delay(500);
}

```

_Full Source:_

> <https://github.com/eewiki/machinechat/blob/master/STM32F746_DEVCON_2020/Lab1/Lab1.ino>

Verify and Upload:  
LD1 should be blinking.

# Lab2

The STM32H746 has an onboard ethernet, which can be enabled thru the [STM32Ethernet](https://github.com/stm32duino/STM32Ethernet) library. Initialize this library with [Ethernet.begin()](https://www.arduino.cc/en/Reference/EthernetBegin)., then use [Ethernet.localIP()](https://www.arduino.cc/en/Reference/EthernetLocalIP) to print our new IP address to the debug terminal. In this example we should get an IP address from our local DHCP server.

Add LwIP.h and STM32Ethernet.h header files:

```auto
#include <LwIP.h>
#include <STM32Ethernet.h>

```

Enable Serial Port for debugging:

```auto
  // Open serial communications and wait for port to open:
  Serial.begin(115200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

```

Initialize Ethernet Library

```auto
  // give the ethernet module time to boot up:
  delay(1000);

  // start the Ethernet connection:
  Ethernet.begin();

  // print the Ethernet board/shield's IP address:
  Serial.print("My IP address: ");
  Serial.println(Ethernet.localIP());

```

_Full Source_

> <https://github.com/eewiki/machinechat/blob/master/STM32F746_DEVCON_2020/Lab2/Lab2.ino>

Verify and Upload, Arduino’s Serial Monitor should report an IP address:

```auto
My IP address: 192.168.3.229

```

# Lab3

Next let’s verify we have an active link, with [Ethernet.linkStatus()](https://www.arduino.cc/en/Reference/EthernetLinkStatus), the LED will only be on with an active connection. Test by removing the Ethernet Cable.

```auto
void loop() {
  if (Ethernet.linkStatus() == LinkON) {
    Serial.println("Link status: On");

    digitalWrite(LED_BUILTIN, HIGH);
  }
  else if (Ethernet.linkStatus() == LinkOFF) {
    Serial.println("Link status: Off");

    digitalWrite(LED_BUILTIN, LOW);
  } 
  delay(500); 
}

```

_Full Source_

> <https://github.com/eewiki/machinechat/blob/master/STM32F746_DEVCON_2020/Lab3/Lab3.ino>

Verify and Upload, Arduino’s Serial Monitor should also report Link status, matching the LD1, unplug Ethernet Cable to verify.

```auto
My IP address: 192.168.3.229
Link status: On
Link status: On
Link status: Off
Link status: Off
Link status: On

```

# Lab4

Add STTS751 Sensor

```auto
#include <STTS751Sensor.h>

STTS751Sensor *STTS751_Temp;

```

```auto
  // Initialize I2C bus.
  Wire.begin();

  // Initialize STTS751 Sensor 
  STTS751_Temp = new STTS751Sensor (&Wire);
  STTS751_Temp->Enable();

```

```auto
void loop() {
  //Read STTS751 Temperature
  float STTS751_tempC = 0;
  STTS751_Temp->GetTemperature(&STTS751_tempC);
  float STTS751_tempF = (STTS751_tempC * 1.8) + 32.0F;

  Serial.print(" | Temp[F]: ");
  Serial.print(STTS751_tempF, 2);
  Serial.println(" |");

```

_Full Source_

> <https://github.com/eewiki/machinechat/blob/master/STM32F746_DEVCON_2020/Lab4/Lab4.ino>

Verify and Upload, Arduino’s Serial Monitor should now include Temperature data from the STTS751

```auto
My IP address: 192.168.3.229
 | Temp[F]: 74.30 |
Link status: On
 | Temp[F]: 74.30 |
Link status: On

```

# Lab5

Add HTS221 Sensor

```auto
#include <HTS221Sensor.h>

HTS221Sensor *HTS221_HumTemp;

```

```auto
  // Initialize HTS221 Sensor 
  HTS221_HumTemp = new HTS221Sensor (&Wire);
  HTS221_HumTemp->Enable();

```

```auto
  // Read HTS221 Humidity and Temperature
  float HTS221_humidity = 0, HTS221_tempC = 0;
  HTS221_HumTemp->GetHumidity(&HTS221_humidity);
  HTS221_HumTemp->GetTemperature(&HTS221_tempC);
  float HTS221_tempF = (HTS221_tempC * 1.8) + 32.0F;

  Serial.print(" | Temp[F]: ");
  Serial.print(STTS751_tempF, 2);
  Serial.print(" | Temp[F]: ");
  Serial.print(HTS221_tempF, 2);
  Serial.print("| Hum[%]: ");
  Serial.print(HTS221_humidity, 2); 
  Serial.println(" |");

```

_Full Source_

> <https://github.com/eewiki/machinechat/blob/master/STM32F746_DEVCON_2020/Lab5/Lab5.ino>

Verify and Upload, Arduino’s Serial Monitor should now include Temperature and Humidity data from the HTS221.

```auto
My IP address: 192.168.3.229
 | Temp[F]: 74.07 | Temp[F]: 72.32| Hum[%]: 41.20 |
Link status: On
 | Temp[F]: 74.19 | Temp[F]: 72.32| Hum[%]: 41.20 |
Link status: On

```

# Lab6

Add LPS22HH Sensor

```auto
#include <LPS22HHSensor.h>

LPS22HHSensor *LPS22HH_PressTemp;

```

```auto
  // Initialize LPS22HH Sensor 
  LPS22HH_PressTemp= new LPS22HHSensor(&Wire);
  LPS22HH_PressTemp->Enable();

```

```auto
  // Read LPS22HH Pressure and Temperature.
  float LPS22HH_pressure = 0, LPS22HH_tempC = 0;
  LPS22HH_PressTemp->GetPressure(&LPS22HH_pressure);
  LPS22HH_PressTemp->GetTemperature(&LPS22HH_tempC);
  float LPS22HH_tempF = (LPS22HH_tempC * 1.8) + 32.0F;

  Serial.print(" | Temp[F]: ");
  Serial.print(STTS751_tempF, 2);
  Serial.print(" | Temp[F]: ");
  Serial.print(HTS221_tempF, 2);
  Serial.print(" | Temp[F]: ");
  Serial.print(LPS22HH_tempF , 2);
  Serial.print("| Hum[%]: ");
  Serial.print(HTS221_humidity, 2); 
  Serial.print(" | Pres[hPa]: ");
  Serial.print(LPS22HH_pressure, 2); 
  Serial.println(" |");

```

_Full Source_

> <https://github.com/eewiki/machinechat/blob/master/STM32F746_DEVCON_2020/Lab6/Lab6.ino>

Verify and Upload, Arduino’s Serial Monitor should now include Temperature and Pressure data from the LPS22HH.

```auto
My IP address: 192.168.3.229
 | Temp[F]: 73.96 | Temp[F]: 72.14 | Temp[F]: 74.35| Hum[%]: 41.30 | Pres[hPa]: 979.12 |
Link status: On
 | Temp[F]: 73.96 | Temp[F]: 72.14 | Temp[F]: 74.35| Hum[%]: 41.30 | Pres[hPa]: 978.99 |
Link status: On

```

# Lab7

Generate Json data object

```auto
#include <ArduinoJson.h>

```

```auto
// Create a unique ID for the data from each STM32 running this code
const char* jediID = "STM32F7_IKSO1A3";

```

```auto
void loop() {
  String postData;

  StaticJsonDocument <200> doc;
  
  JsonObject context = doc.createNestedObject("context");
  context["target_id"] = String(jediID);

  JsonObject data = doc.createNestedObject("data");
  data["HTS221_humidity"] = HTS221_humidity;
  data["HTS221_tempF"] = HTS221_tempF;
  data["LPS22HH_pressure"] = LPS22HH_pressure;
  data["LPS22HH_tempF"] = LPS22HH_tempF;
  data["STTS751_tempF"] = STTS751_tempF;

  serializeJson(doc, postData);

  //This prints the JSON to the serial monitor screen
  Serial.println(postData);

```

_Full Source_

> <https://github.com/eewiki/machinechat/blob/master/STM32F746_DEVCON_2020/Lab7/Lab7.ino>

Verify and Upload, Arduino’s Serial Monitor should now include the Json data object

```auto
{"context":{"target_id":"STM32F7_IKSO1A3"},"data":{"HTS221_humidity":41,"HTS221_tempF":72.14,"LPS22HH_pressure":978.8882,"LPS22HH_tempF":74.39,"STTS751_tempF":73.9625}}

```

# Lab8

Transmit Json data object

```auto
#include <ArduinoHttpClient.h>

```

```auto
char serverAddress[] = "192.168.3.104"; // server address
int port = 8100;

// initialize the library instance:
EthernetClient eth;
HttpClient client = HttpClient(eth, serverAddress, port);

```

```auto
  if (Ethernet.linkStatus() == LinkON) {
    Serial.println("Link status: On");

    digitalWrite(LED_BUILTIN, HIGH);

    String contentType = "application/json";

    client.post("/v1/data/mc", contentType, postData);

    // read the status code and body of the response
    int statusCode = client.responseStatusCode();
    String response = client.responseBody();
  
    Serial.print("Status code: ");
    Serial.println(statusCode);
    Serial.print("Response: ");
    Serial.println(response);
  }
  else if (Ethernet.linkStatus() == LinkOFF) {
    Serial.println("Link status: Off");

    digitalWrite(LED_BUILTIN, LOW);
  }  
  delay(500);
}

```

_Full Source_

> <https://github.com/eewiki/machinechat/blob/master/STM32F746_DEVCON_2020/Lab8/Lab8.ino>

Verify and Upload, Arduino’s Serial Monitor should now report back a server response code:

```auto
Status code: 200
Response: "Data sent successfully"

```
