Skip to content

14. Interface and application programming

Group assignment:

  • Compare as many tool options as possible

To see our group assignment click here

Individual assignment:

  • Write an application that interfaces a user with input and/or output device(s) on a board that you made.

FFor this week’s assignment, we chose to use node red, a nodejs-based solution, to develop an interface for communicating with two XIO ESP32C3.

Installation of node-red

Node-RED is a programming tool for wiring together hardware devices, APIs and online services in new and interesting ways. It provides a browser-based editor that makes it easy to wire together flows using the wide range of nodes in the palette that can be deployed to its runtime in a single-click. for more details click here

To install the red node, we’ll use a Raspberry pi.

To connect to the raspberry pi with ssh we used windows powershell.

  • connection to the Raspberry pi
1
2
3
4
ssh <username>@<host_ip_address> -P <port_number>

# For example
ssh pi@192.168.1.210 -P 1022
  • now that we are connected to the raspberry pi we will update the pi before any other command.
1
2
sudo apt-get update 
sudo apt-get upgrade

1
bash <(curl -sL https://raw.githubusercontent.com/node-red/linux-installers/master/deb/update-nodejs-and-nodered)

once the installation is finished, we entered the address of our Raspberry pi followed by “:1880” to access the node-red home page.

In order to program our interface with node-red we need to install an extension as shown below.

Once the installation is complete you can enter the IP address of the Raspberry followed by “:1880/ui” to access the dashboard.

Programming the interface of Node-Red

For the programming of the node red interface, we’ll use switches to control a lamp, a pump and a solenoid valve. To display temperature and humidity values, we’ll use an input field.

  • We chose the modules we were interested in from the palette on the left of the screen and made a click and drag as shown below.

  • In order to set the different nodes, double click on them and the settings window will appear as shown below.

  • once the nodes are set up we click on deploy and we should have the following result

  • Now we can visualize our interface by entering the IP address of the raspberry pi followed by “:1880/ui”. Here is our interface.

I know more than one person is wondering but how to reproduce the interface don’t worry you just need to download my source file and go to the node red menu and click on import as shown below.

Programming the MCU

XIAO 1

Xiao_esp32c3_1.ino
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <WiFi.h>
#include <PubSubClient.h>
#include "DHT.h"

const char* ssid = "...............";
const char* password = ".............";

// Add your MQTT Broker IP address, example:
const char* mqtt_server = "................";

WiFiClient espClient;
PubSubClient client(espClient);

#define DHTPIN D10

#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

// Variables to hold sensor readings
float temp;
float hum;

#define LEDPIN D1

#define RELAY_1 D2
#define RELAY_2 D3

unsigned long previousMillis = 0;   // Stores last time temperature was published
const long interval = 3000;        // Interval at which to publish sensor readings

void setup_wifi() {

  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  randomSeed(micros());

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();


  if ((char)payload[0] == '1') {
    digitalWrite(LEDPIN, HIGH);   // Turn the LED on

  } 
     else if ((char)payload[0] == '2') {
    digitalWrite(LEDPIN, LOW);   // Turn the LED on
  } 
   else if ((char)payload[0] == '3') {
    digitalWrite(RELAY_2 , HIGH);   // Turn the LED on
  } 
     else if ((char)payload[0] == '4') {
    digitalWrite(RELAY_2 , LOW);   // Turn the LED on
  } 
     else if ((char)payload[0] == '5') {
    digitalWrite(RELAY_1 , HIGH);   // Turn the LED on
  } 
     else if ((char)payload[0] == '6') {
    digitalWrite(RELAY_1 , LOW);   // Turn the LED on
  } 
}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Create a random client ID
    String clientId = "XIAO_55";
    clientId += String(random(0xffff), HEX);
    // Attempt to connect
    if (client.connect("XIAO_45")) {
      Serial.println("connected");
      client.subscribe("XIAO_45");
     } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void setup() {
  Serial.begin(115200);
  pinMode(LEDPIN,OUTPUT);
  pinMode(RELAY_2,OUTPUT);
  pinMode(RELAY_1,OUTPUT);

  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
  dht.begin();

}

void loop() {
    if (!client.connected()) {
    reconnect();
  }
  client.loop();

  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    // Save the last time a new reading was published
    previousMillis = currentMillis;
    // New DHT sensor readings
    hum = dht.readHumidity();
    // Read temperature as Celsius (the default)
    temp = dht.readTemperature();

    // Check if any reads failed and exit early (to try again).
    if (isnan(temp) || isnan(hum)) {
      Serial.println(F("Failed to read from DHT sensor!"));
      return;
    }

    client.publish("yello_lab/salle_2/humidite_dht11",String(hum).c_str());
    client.publish("yello_lab/salle_2/temperature_dht11",String(temp).c_str());
  }

}

XIAO 2

Xiao_esp32c3_2.ino
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <WiFi.h>
#include <PubSubClient.h>
#include <Wire.h>
#include <Adafruit_BMP085.h>

const char* ssid = "...............";
const char* password = "..................";

// Add your MQTT Broker IP address, example:
const char* mqtt_server = "...............";

WiFiClient espClient;
PubSubClient client(espClient);

#define RELAY D10


Adafruit_BMP085 bmp;

unsigned long previousMillis = 0;   // Stores last time temperature was published
const long interval = 3000;        // Interval at which to publish sensor readings

void setup_wifi() {

  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  randomSeed(micros());

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();


  if ((char)payload[0] == '1') {
    digitalWrite(RELAY, HIGH);   // Turn the LED on 
  } 
  else
  {
    digitalWrite(RELAY, LOW);  // Turn the LED off 
  }
}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Create a random client ID
    String clientId = "XIAO_72";
    clientId += String(random(0xffff), HEX);
    // Attempt to connect
    if (client.connect("XIAO_752")) {
      Serial.println("connected");
      client.subscribe("XIAO_ESP_7");
     } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void setup() {
  Serial.begin(115200);
  pinMode(RELAY,OUTPUT);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
  if (!bmp.begin()) {
    Serial.println("Could not find a valid BMP085/BMP180 sensor, check wiring!");
    while (1) {}
  }
}

void loop() {

  if (!client.connected()) {
    reconnect();
  }
  client.loop();

  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
  // Save the last time a new reading was published
    previousMillis = currentMillis;

  float temp = bmp.readTemperature();

  float pressure = bmp.readPressure();

  float altitude = bmp.readAltitude(102000);

  client.publish("temperature",String(temp).c_str());
  client.publish("pressure",String(pressure).c_str());
  client.publish("altitude",String(altitude).c_str());

  }

}

Conclusion

Here’s a video showing how our system works

HERO Video 😊😊😊

Files


Last update: July 4, 2023