Transfer data from analytical scale to the Cloud (NodeMCU + ThingSpeak)

Dear all,

In this new project I would like to make the data from an analytical scale (weight samples) to the internet (ThingSpeak).
I am using a NodeMCU module.

I am also using a RS232 Serial Port To TTL Converter Module to connect the scale serial port to the NodeMCU. I have no idea how to do it but following common sense I would say:

Vcc → Vin (NodeMCU)
Gnd → GND (NodeMCU)
RX → Rx (NodeMCU)
TX → Tx (NodeMCU)

Is this correct?

Also, I adapted the code from this thread in order to trasnmit the data:

// Code for receiving the data from my analytical scale through a serial RS232 with parsing and send them wirelessly to the Cloud

//************************************************************************
//*  This code will gather data from an analytical scale as a whole        *
//*  buffer, parse it and gather the weight values. The data will be then  *
//*  send to the Cloud https://thingspeak.com/                             *
//*  In this particular scale we have 3 different buffers(lines) output.   *
//************************************************************************

// Libraries
#include <ESP8266WiFi.h> // Lib for the Wi-Fi
#include <WiFiClient.h> // Lib for the Wi-Fi
#include <ThingSpeak.h> // Lib for the cloud https://thingspeak.com/

// Define the scale variables:
const byte bufferSize = 20; // Instead of using INT, it saves memory. Max buffer size (1st line 18 + 1 for the NULL, rounded to 20)
byte bufferPosition = 0; // Initialises the buffer index
char inputBuffer[bufferSize]; // Array of the data from the scale
boolean newData = false; // Flag that identifies the presence of data

double weight; // Defines the variable for the weight reading

// Define the Wi-Fi and Cloud variables:
const char* ssid = "MyNetwork"; // The SSID (name) of the Wi-Fi network you want to connect to
const char* password = "MyPassword"; // The password of the Wi-Fi network
WiFiClient client; // Create a client that can connect to a specified internet IP address and port
unsigned long myChannelNumber = MyChannel; // ThingSpeak channel number
const char * myWriteAPIKey = "MyKey"; // ThingSpeak API write key

//----------------------------------------------------------------------

void setup()
{
  Serial.begin(9600, SERIAL_7N1); // Initialises the serial communication with the scale, using the specific scale parameters (baud 9600, sets data 7, parity NONE, stop bits 1)

  // Connect to Wi-Fi network
  Serial.println('\n');
  WiFi.begin(ssid, password); // Connect to the network
  Serial.print("Connecting to ");
  Serial.print(ssid);
  Serial.println(" ...");
  int i = 0;
  while (WiFi.status() != WL_CONNECTED) // Wait for the Wi-Fi to connect
  {
    delay(1000);
    Serial.print(++i); Serial.print(' ');
  }
  Serial.println('\n');
  Serial.println("Connection established!");
  Serial.print("IP address:\t");
  Serial.println(WiFi.localIP()); // Send the IP address of the ESP8266 to the computer
  Serial.println('\n');

  ThingSpeak.begin(client); // Connect to the Cloud
}

//----------------------------------------------------------------------

void loop()
{
  recvWithEndMarker(); // Receives the buffers with the end marker
  if (newData)
  {
    scanBufferType(inputBuffer);
    newData = false; // Resets the flag so the process can start again
  }
}

//----------------------------------------------------------------------

void recvWithEndMarker() // Build one of the 3 buffers
{
  const  char endMarker = '\n';
  char receivedData;

  while (Serial.available() > 0 && newData == false)
  {
    receivedData = Serial.read(); // Reads the data from the scale
    if (receivedData != endMarker) // Now we copy the data into the buffer until we find \n
    {
      inputBuffer[bufferPosition] = receivedData;
      bufferPosition++; // Increases the buffer index
      if (bufferPosition >= bufferSize) // Let's avoid overflow
      {
        bufferPosition = bufferSize - 1;
      }
    }
    else
    {
      inputBuffer[bufferPosition] = '\0'; // Terminates the string
      bufferPosition = 0; // Resets the buffer index
      newData = true; // We have new data read now
    }
  }
}

//----------------------------------------------------------------------

void scanBufferType(char * buffer) // Selects the buffer/line type and parse the one we need (Line 2, Weight)
{
  if (strlen(buffer) < 5)
  {
    return; // Relates to the 3rd (meaningless) buffer/line
  }
  else if (strstr(buffer, "G OK")) // Refers to the 2nd buffer line.
  {
    parseVal(buffer);
    sendData(); // At this point we are ready to go and send to the Cloud
  }
}

//----------------------------------------------------------------------

void parseVal(char * buffer) // Parses the buffer related to the weight reading (Line 2)
{
  weight = strtod(buffer, NULL); // Converts the reading as a string into a double precision floating number
}

//----------------------------------------------------------------------

void sendData() // Send the data to the Serial Monitor and ThingSpeak
{
  Serial.println(weight, 7); // 7 are the siginificative digits (the particular scale has max 7)
  Serial.println('\n');

  ThingSpeak.writeField(myChannelNumber, 4, weight, myWriteAPIKey); // Send the sample's weight to the Cloud
  delay(30000); // IMPORTANT - ThingSpeak will only accept updates every 15 seconds (FREE LICENCE)
}

Any first thoughts about this code, please?

Really thanks.

M.

Hi LordKelvin

Vcc → Vin (NodeMCU)
Gnd → GND (NodeMCU)
RX → Rx (NodeMCU)
TX → Tx (NodeMCU)

Check that the nodeMCU works at 3.3V so you have to adapt the 5V from the TTL converter to the 3.3V. Also you will need to adapt the Tx pin from the TTL converter to the nodeMCU´s Rx pin, a resistive divider will works.

Danicap:
Hi LordKelvin

Check that the nodeMCU works at 3.3V so you have to adapt the 5V from the TTL converter to the 3.3V. Also you will need to adapt the Tx pin from the TTL converter to the nodeMCU´s Rx pin, a resistive divider will works.

Hi,

Thanks for the first answer of the thread :slight_smile:

I thought that the NodeMCU can handle 5V through the Vin PIN...? All the other PINs have the label "3.3V" so I was not using them...

When I connected to the Arduino I was using:

TX --> RX Arduino (D0)
RX --> TX Arduino (D1)

Why cannot connect them straight as for the Arduino UNO board? May you explain this subject to me or point me what to study please?

Thanks a lot.

Why cannot connect them straight as for the Arduino UNO board? May you explain this subject to me or point me what to study please?

What does TX on the scale mean to you? To me, it suggests that that is the pin that is uses to transmit data. You use your mouth for the same purpose.

What does RX on the not-really-an-Arduino mean to you? To me, that suggests that it is the pin used to receive data. You use your ear for the same purpose.

If you wished to communicate with another person would you speak into their mouth, and listen to their ear?

PaulS:
What does TX on the scale mean to you? To me, it suggests that that is the pin that is uses to transmit data. You use your mouth for the same purpose.

What does RX on the not-really-an-Arduino mean to you? To me, that suggests that it is the pin used to receive data. You use your ear for the same purpose.

If you wished to communicate with another person would you speak into their mouth, and listen to their ear?

Paul, I know my question might look abit silly, but it was actually a doubt connected to this post:

Danicap:
Also you will need to adapt the Tx pin from the TTL converter to the nodeMCU´s Rx pin, a resistive divider will works.

So I was wondering why I need this resistive divider.

Also I did not get if I could actually connect the Vcc of the TLL to the Vin of the NodeMCU...

Thanks and hope whoever had a long weekend enjoyed it :slight_smile:

So I was wondering why I need this resistive divider.

What voltage does the TTL converter output?
What voltage does the MCU run at?

Are they the same? If so, you don't need a resistor divider. If not, you do.

This is what I found:

* The ESP8266 chip requires 3.3V power supply voltage. It should not be powered with 5 volts like other arduino boards.
* NodeMCU ESP-12E dev board can be connected to 5Vusing micro USB connector or Vin pin available on board.
* The I/O pins of ESP8266 communicate or input/output max 3.3V only. i.e. the pins are NOT 5V tolerant inputs.

(from this instructables)

So The TLL is on 5V and I could connect with the Vin pin on NOdeMCU, IF I understood well.

So The TLL is on 5V and I could connect with the Vin pin on NOdeMCU, IF I understood well.

You can't read serial data from the Vin pin. You read serial data from the digital pins, which expect 3.3V.

The power level of the TTL converter MAY depend on the power you supply it, but that power won't come from the Vin (IN!) pin.