Machinechat with MKRWiFi1010 and Sensirion SHT31 sensor

Description

This project sets up an Arduino MKRWIFI1010 board to read temperature and humidity from a Seeed SHT31 sensor board and uses WiFi to HTTP post the sensor data to machinechat’s JEDI One IoT data collector. JEDI One is running on a Raspberry Pi 4.

image

image

Hardware

Software

  • JEDI One
    JEDI One is a ready-to-use IoT data management software solution. Capabilities include: collect data from sensors, devices and machines; build intuitive real-time and historical data and system view dashboards; create rules to monitor and respond to data conditions automatically; receive alert notifications by email and SMS.
  • Arduino
    Arduino is an open-source electronics platform based on easy-to-use hardware and software.

Implementation

For this project, the MKRWIFI1010 is wired to the SHT31 sensor and I2C is used to communicate with the sensor. Electrical connections are shown below:

MKRWIFI1010 pin SHT31 sensor pin
VCC (3.3V) VCC
GND GND
12 SCL SCL
11 SDA SDA

Set up the MKRWIFI1010 and SHT31 sensor application

1 - Set up Arduino on the MKRWIFI1010. See link Getting started with the MKR WiFi 1010

2 - Install libraries needed for application. Add these libraries thru Arduino’s Library Manager:

3 - Code walkthrough (filename: MKR1010wifi_sht31rev1.ino)

Initial setup and connect to Wifi network

#include <Arduino.h>
#include <WiFiNINA.h>
#include <ArduinoHttpClient.h>
#include "arduino_secrets.h" 
#include <Wire.h>
#include "SHT31.h"

// Create a unique ID for the data from MKRWiFi1010 running this code
const char* jediID = "MKR1010WiFiSensor_SHT31";

//include Json library
#include <ArduinoJson.h>

///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID;        // your network SSID (name)
char pass[] = SECRET_PASS;    // your network password (use for WPA, or use as key for WEP)
int status = WL_IDLE_STATUS;     // the Wifi radio's status

// IP address of server or Raspberry Pi running Machinechat JEDI software
// If you changed the JEDI port number, replace 8100 with the new port
char serverAddress[] = "192.168.1.7";  // server address
int port = 8100;

WiFiClient client;
HttpClient http = HttpClient(client, serverAddress, port);
 
SHT31 sht31 = SHT31();
 
void setup() {  
  Serial.begin(9600);
  while(!Serial);
  Serial.println("begin...");  
  sht31.begin();  

  // attempt to connect to Wifi network:
  WiFi.begin(ssid, pass);  
  Serial.print("Attempting to connect to network: ");
  Serial.println(ssid);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
 
  // you're connected now, so print out the data:
  Serial.println("You're connected to the network");
  Serial.println("----------------------------------------");
  printData();
  Serial.println("----------------------------------------");
}
// wifi network info
void printData() {
  Serial.println("Board Information:");
  // print your board's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
  Serial.println();
  Serial.println("Network Information:");
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());
  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.println(rssi);
  byte encryption = WiFi.encryptionType();
  Serial.print("Encryption Type:");
  Serial.println(encryption, HEX);
  Serial.println();
}

Set up sensor data in JSON format and send to JEDI One using HTTP post

void loop() {
  // prep for sending HTTP to JEDI One
  String postData1; //Json string 

  // SHT31 sensor info
  float tempC = sht31.getTemperature();
  float humid = sht31.getHumidity();
  Serial.print("Temp = "); 
  Serial.print(tempC);
  Serial.println(" C"); 
  Serial.print("Hum = "); 
  Serial.print(humid);
  Serial.println("%"); 
  Serial.println();
  
  //Following code creates the serialized JSON string to send to JEDI One
  //using ArduinoJson library
  StaticJsonDocument <200> doc;
  JsonObject context = doc.createNestedObject("context");
  context["target_id"] = String(jediID);
  JsonObject data = doc.createNestedObject("data");
  data["tempC"] = tempC;
  data["humi"] = humid;
  serializeJson(doc, postData1);
  Serial.println(postData1);    //debug, can be commented out

  //HTTP post code start
  if (WiFi.status() == WL_CONNECTED) {
    //format http post to be compatible with JEDI one
    String contentType = "application/json";
    http.post("/v1/data/mc", contentType, postData1);
    // read the status code and body of the response
    int statusCode = http.responseStatusCode();
    String response = http.responseBody();
    Serial.print("Status code: ");
    Serial.println(statusCode);
    Serial.print("Response: ");
    Serial.println(response);
  } else {
    Serial.println("Error in WiFi connection");
  }
  
  delay(10000); //delay between sensor measurements
}

Latest source code for the MKRWIFI1010 SHT31 sensor application is on github at below link:

Set up the JEDI One

1 - If machinechat JEDI One is not already installed on the Raspberry Pi see below:

2 - Set up the JEDI One dasboard

In the JEDI One, select “Dashboards” tab, then select “+” to add a new chart and configure.

Name the chart, select “Chart Type”, select “Source” (MKR1010WiFiSensor_SHT31), select "Property (humi or tempC), enter “Units” and enter “Refresh Interval”. When complete the dashboard should liook similar to below.

Conclusion

The combination of machinechat’s JEDI One data management software and a Raspberry Pi results in a standalone, low cost, easy to use IoT data management platform. Sensors based on Arduino hardware platforms like the MKRWIFI1010 are low cost and easy to implement and add to the JEDI One.

References