Imprimer
Catégorie : Embedded Systems
Affichages : 226


Preamble


Classical arduino development board (Uno, genuino, ...) do not host a wifi chipset. A wifi "shield" (i.e. an additional board that plugs on the pins of the arduino board) must be used for that. Nevertheless, there are other micro-controllers that are already capable of communicating over wifi, bluetooth, ... for example esp32 and their "little brother", the esp8266.

Thanks to the arduino development kit for these micro-controllers, it is very easy to setup a wifi connection and to communicate with a web server, or a classical one directly over TCP. The goal of the following exercises is to discover how.

BEWARE ! these boards may be fragile, so do not touch the surface component, at risk to burn the card

  

Software setup

https://arduino.esp8266.com/stable/package_esp8266com_index.json,https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json

The breadboard setup
 

The following exercises are based on a breadboard with an esp32 dev. board and few "modules" :

 

For the following exercises, we will only use the LCD display and the temperature/... sensor.

 

Exercice #1: BME280 + TM1637 + deep-sleep
 

Working principles:

  • the BME280 sensor uses the i2c protocol to communicate with the µC on pins 18 (SDA) and 23 (SCL). A dedicated arduino library is used to initialize the communication link and then to retrieve temperature, humidity and pressure every 5 seconds. The three values are printed on Serial
  • The pressure is also displayed on the LCD based on a TM1637 chipset. It uses a kind of i2c protocol to communicated with the µC on pins 33 (DIO) and 32 (CLK). Once again, a dedicated library is used to display the pressure.
  • After that, the µC waits for 2 seconds, then goes into deep-sleep for 3 seconds.

 

Installing dedicated libraries :

  • In Tools -> Manage libraries, type tm1637 in the research field.
  • There are a lot of possibilities. Choose : Groove 4-digit display, then click on install.
  • Now type bme280 in the research field.
  • There are also several choices. Choose : BME280 by Tyler Glenn, then click on install.

Remarks :

  • Libraries are installed in a directory named libraries, within the directory where your sketch rely. For example, on a linux system, it is generally in ~/sketchbook/libraries
  • Generally, there is an examples directory within the installation directory of each library. There you can find sketches to illustrate how to use a given library. For example, to solve this exercise, you can start from sketches NumberFlow.ino within the examples for the tm1637, and BME_280_I2C_Test.ino for the BME280.

 

What must be done :

  • Start arduino and create a new sketch.
  • Copy the following pattern of code within
#include <BME280I2C.h>
#include <Wire.h>
#include <WiFi.h>
#include "TM1637.h"

#define BME280_SDA 18
#define BME280_SCL 23
#define TM1637_CLK 32
#define TM1637_DIO 33

TM1637 tm1637(TM1637_CLK,TM1637_DIO);
BME280I2C bme;

void setup() {
  Serial.begin(115200);
  while(!Serial);
  // initialize i2c with bme280
  Wire.begin(BME280_SDA, BME280_SCL);
  // initialize BME280
  while(!bme.begin()) {
    Serial.println("Could not find BME280 sensor!");
    delay(1000);
  }
  // to fulfill
}

void loop() {
  /* to fulfill:
    - read temperature/humidity/pressure
    - print them on Serial
    - display pressure on tm1637
    - wait for 2 seconds
    - go into deep-sleep for 3 seconds
  */
}
 
  • Then, add what is needed to fulfill the requirements given above.
 

Remarks :

  • Since the BME280 is not using the defaults pins for i2c protocol on esp32, it is mandatory to "force" the esp32 to use the pins we want. This is the reason of the instruction Wire.begin(...) (NB : the library Wire is part of the esp32 dev. kit and does not need to be installed)
  • Normally, the pressure can be above or under 1000. Thus, we must use 3 or 4 digits on the tm1637 to display it. In case of this is 3, it must be the right-most digit, leaving digit 0 empty.

Exercise #2: connect to Wifi AP + send TCP request
 

Working principles:

  • During setup, the esp32 connects to a wifi access point, then to a server written in Java.
  • During loop, the esp32 does the same things as in exercise 1, except that before going into deep-sleep, it also sends a request to the server to store data and waits for its answer.

 

What must be done :

  • create a new sketch and copy the sktech of exercise 1.
  • add function to read a line from the server,
  • add functions to establish a connection to the Wifi access point and the TCP server, as seen in the course. For that, use
    • AP credentials : ssid = testingiot, password = testingiot
    • TCP serveur : ip = 192.168.0.2, port = 12345. IMPORTANT : as soon as the connection is established, the server sends a line containing the id of the client.
  • add a function to send a request to the server and to wait for the answer :
    • the format of the request is : 1 id_client temperature humidity pressure, where id_client must be replaced by the value received from the server after connection, and temperature/humidity/pressure by the values read from bme280.
    • the format of the answer is a single line, containing OK if the request is valid, or ERR message if invalid.