DHT12 Sensor Interfacing with Linkit Smart 7688 Duo using Python/Arduino

I got the Linkit 7688 Duo couple of years back but was not able to work on it apart from getting started and programming the onboard Atmega(w/ Arduino Bootloader) using Arduino IDE video. But recently I thought of checking out the python programming aspect of it, as the MPU runs an OpenWRT based lightweight OS over which we can run any programming language as long as the limited resources available on the board are sufficient. The board comes with one MPU (MT7688) and one MCU(Atmega32U4) I am working on a video around it to use Firmata with Python to access the Atmega controller from the MPU and access connected sensors and publish the data from the sensors using MQTT(w/ Python), but this post is not about it. Over here I am writing about a little program that I worked on to test/interface the DHT12 sensor with the Atmega controller’s GPIO and access the data using Python from the MPU through the serial interface using PySerial. So let’s check out how to do so.

The GPIO layout of the Linkit 7688 DUO module is as follows,

The sensor I am going to use is DHT12 and it comes with an I2C interface. I will be connecting the sensor to the I2C interface of the Atmega32U4 microcontroller of the Linkit module. Then I will be uploading a sketch to the which will read the sensors data from the connected sensor and publish the same to through the serial/UART interface as JSON string.

Connect the DHT12 sensor to the Linkit7688 DUO module as per the following connection diagram,

Now let’s add the support for Linkit Module to the Arduino IDE, to do so you can follow this article which is pretty easy to follow.

Now let’s upload the sketch to the Atmega controller. Connect the Linkkit module to your PC using a USB cable. You might need to install the required drivers, to do so follow this article. Make sure you are connecting the PWR/MCU USB port to the PC.

The code to be uploaded to the controller is,

#include <Wire.h>
#include <ArduinoJson.h>

char data[5];
void setup() {
  // put your setup code here, to run once:
  Wire.begin();
  Serial1.begin(9600);
}
float temperature = 0.0;
float humidity = 0;
StaticJsonDocument < 200 > jsonData;
char inData = '0';
void loop() {
  if (Serial1.available() > 0) {
    inData = (char) Serial1.read();
    //    Serial1.println(inData);
  }
  if (inData == '1') {
    inData = '0';
    Wire.beginTransmission(0x5c);
    Wire.write(0);
    if (Wire.endTransmission() != 0) {
      //Error
    }
    Wire.requestFrom(0x5c, (uint8_t) 5);
    for (uint8_t i = 0; i < 5; ++i) {
      data[i] = Wire.read();
    }

    if (data[4] == (data[0] + data[1] + data[2] + data[3])) {
      humidity = (data[0] + (float) data[1] / 10);

      byte scaleValue = data[3] & B01111111; // 
      byte signValue = data[3] & B10000000; // Fetch sign of the tempperature value 1: Negative; 0: Positive

      temperature = (data[2] + (float) scaleValue / 10); // ((data[2] & 0x7F)*256 + data[3]);
      temperature = signValue ? -temperature : temperature;

      //    Serial1.print("Temperature: ");
      //    Serial1.println(temperature);
      //    Serial1.print("Humidity: ");
      //    sprintf(jsonData, "{\"TEMP\":%.2f,\"HUM\":%.2f}",temperature,humidity);
      jsonData["TEMP"] = temperature;
      jsonData["HUM"] = humidity;
      //      sprintf(jsonData, "%.2f",temperature);
      serializeJson(jsonData, Serial1);
      //Serial1.println(jsonData);
    } else {
      Serial1.println("Checksum/ Communication Error");
    }
  }
}

The code uses the ArduinoJson library to create a JSON string to send to the MPU. Notice over here Serial1 is used instead of the usual Serial object, that is because the Serial1 interface is used to connect to the MCU to the MPU. To read the data from the sensor  I have not used any sort of library, it’s a raw one. The code writes the data to the serial interface whenever it receives 1.

Now as the MCU is uploading data through the serial interface to the MPU, we need to write our code to send ‘1’ to the MCU and after that read the data from the serial port. To interact with the serial port in python I have used pyserial. If it is not available in your device use pip install pyserial. But of course, to do so you need to have access to the module using SSH. To set up the Linkit7688 module you can follow this video.

The python scripts loop in and send 1 to the serial port and read the data from it and print it to the console every 10 seconds. You can change the script to do whatever you want to. The script is,

import serial,io,time,json

#Initalize the serial port
ser = serial.Serial('/dev/ttyS0',9600,timeout=1)
sio = io.TextIOWrapper(io.BufferedRWPair(ser, ser))

# Loop forever to send command to Arduino and read values from it.
degree = u"\N{DEGREE SIGN}"
while 1:
    ser.write(b'1')
    time.sleep(2)
    if ser.inWaiting() > 0:
        serial_data = sio.readline()
    
    json_data = json.loads(serial_data)
    print("Temperature: "+str(json_data['TEMP'])+degree.encode('utf-8')+"C")
    print("Humidity: "+str(json_data['HUM'])+" %RH")
    time.sleep(5)

Now all that is left is to run the script. If you want to run your script when the Linkit module boots up you can run the python code a service.

So, that’s pretty much all about it, thanks for checking it out.

Bikash Panda
Catch Me On

Related posts