I need help with my code

I am rookie in nodemcu and i dont know how to pass the code of IDE arduino at Blynk.
I leave you the arduino IDE code I have. The sensor that i use is the GDK101 for UART.
I hope you answer me.

#include <SoftwareSerial.h>
#include <Timer.h>    // https://github.com/JChristensen/Timer
#include <mthread.h>   // https://github.com/jlamothe/mthread

/* Thread class for receive UART */
class FooThread : public Thread
{
  public: FooThread(int id);
  protected: bool loop();
  private: int id;
};

FooThread::FooThread(int id){
  this->id = id;
}

/* Command */
enum { 
  cmd_GAMMA_RESULT_QUERY         = 0x44,  // D, Read measuring value(10min avg, 1min update)
  cmd_GAMMA_RESULT_QUERY_1MIN    = 0x4D,  // M, Read measuring value(1min avg, 1min update)
  cmd_GAMMA_PROC_TIME_QUERY       = 0x54,  // T, Read measuring time
  cmd_GAMMA_MEAS_QUERY                  = 0x53,  // S, Read status
  cmd_GAMMA_FW_VERSION_QUERY    = 0x46,  // F, Read firmware version
  cmd_GAMMA_VIB_STATUS_QUERY      = 0x56,  // V, Response vibration status
  cmd_GAMMA_RESET                               = 0x52,  // R, Reset
  cmd_GAMMA_AUTO_SEND                     = 0x55,  // U, Set Auto_send status
  cmd_GAMMA_ALL_QUERY                      = 0x41,  // A, Read all data
};

SoftwareSerial mySerial(2, 3);
Timer ts1;
char rec_data[50];                    // Array for received command
bool request_flag = true;             // enable or disable send command automatically

void setup() {
  Serial.begin(9600);                // PC - Arduino
  mySerial.begin(9600);               // Arduino - Gamma Sensor
  ts1.every(1000, RequestData);       // Request to Gamma Sensor
  
  Gamma_INIT();
}

bool FooThread::loop()
{
  if(id == 1) RecUartData();           // Thread 1 - for Receive Data
  else ts1.update();                   // Thread 2 - for Timers
  
  return true;
}

/* Gamma Sensor Initialize  */
void Gamma_INIT() {
  main_thread_list->add_thread(new FooThread(1)); // Add Thread 1 for UART Response
  delay(1000);
  Read_FW();            // read FW version
  Reset();              // reset
  if(request_flag) main_thread_list->add_thread(new FooThread(2)); // Add Thread 2 for Timers
  Serial.println("====================================================");
}

/* Meawurement Reset */
void Reset(){
  // STX | 'R' | ':' | '1' | CR | LF
  byte send_data[6] = {0x02, cmd_GAMMA_RESET, ':', '1', 0x0D, 0x0A};
  mySerial.write(send_data, 6);
  Serial.println("Reset.");
  delay(100);
}

/* Read Firmware */
void Read_FW(){
  // STX | 'F' | ':' | '?' | CR | LF
  byte send_data[6] = {0x02, cmd_GAMMA_FW_VERSION_QUERY, ':', '?', 0x0D, 0x0A}; 
  mySerial.write(send_data, 6);
  delay(100);
}

/* Read all data (automatically) */
void RequestData(){
  // STX | 'A' | ':' | '?' | CR | LF
  byte send_data[6] = {0x02, cmd_GAMMA_ALL_QUERY, 0x3A, 0x3F, 0x0D, 0x0A};
  mySerial.write(send_data, 6);
}

void RecUartData(){
  int rec_size = mySerial.available();
  
  if (rec_size > 0) {
    for (int i = 0; i < rec_size; i++)
    {
      rec_data[i] = mySerial.read();
      Serial.print(rec_data[i]);
    }
  }
}

Please edit your code with triple BACKTICKS ``` in a separate line before and after the code.

The code you’re using is written for an Arduino, not a NodeMCU. The Arduino SoftwareSerial library isn’t compatible with the ESP core, so you should use the ESPSoftwareSerial library that is includes as part of the ESP core.

However, this wouldn’t be needed if you used the I2C interface on the sensor rather than the UART.

Pete.

I also have a code for I2C but for Arduino, which happens that I do not know how to pass either of them to Blynk. I use a nodemcu ESP8266.
The code for interface I2C is this:

#include <Wire.h>
/*
* We will be using the I2C hardware interface on the Arduino in
* combination with the built-in Wire library to interface.
* Arduino analog input 5 - I2C SCL
* Arduino analog input 4 - I2C SDA
*
* Command List
* 0xA0 :: Reset
* 0xB0 :: Read Status
* 0xB1 :: Read Measuring Time
* 0xB2 :: Read Measuring Value (10min avg / 1min update)
* 0xB3 :: Read Measuring Value (1min avg / 1min update)
* 0xB4 :: Read Firmware Version
*
* Address Assignment
* Default Address :: 0x18
* A0 Open, A1 Short :: 0x19
* A0 Short, A1 Open :: 0x1A
* A0 Open, A1 Open :: 0x1B
*/
int addr = 0x18;
int day,hour,min,sec = 0;
byte buffer[2] = {0,0};
int status = 0;

void setup() {
  //Arduino Initialize
  Serial.begin(9600);
  Wire.begin();
  Serial.println("Gamma Sensor Sensing Start");

  //Read Firmware version
  Gamma_Mod_Read(0xB4);
  //Reset before operating the sensor
  Gamma_Mod_Read(0xA0);
  Serial.println("================================================");
}

void loop() {
  delay(1000);
  //Read Statue, Measuring Time, Measuring Value
  Gamma_Mod_Read_Value();
  Serial.println("================================================");
}

void Gamma_Mod_Read_Value(){
  Gamma_Mod_Read(0xB0); // Read Status
  Gamma_Mod_Read(0xB1); // Read Measuring Time
  Gamma_Mod_Read(0xB2); // Read Measuring Value (10min avg / 1min update)
  Gamma_Mod_Read(0xB3); // Read Measuring Value (1min avg / 1min update)
}

void Gamma_Mod_Read(int cmd){
  /* Begin Write Sequence */
  Wire.beginTransmission(addr);
  Wire.write(cmd);
  Wire.endTransmission();
  /* End Write Sequence */
  delay(10);
  /* Begin Read Sequence */
  Wire.requestFrom(addr, 2);
  byte i = 0;
  while(Wire.available())
  {
  buffer[i] = Wire.read();
  i++;
  }
  /* End Read Sequence */

  /* View Results */
  Print_Result(cmd);
}
/*
* Calculation Measuring Time
* Format :: 0d 00:00:00 ( (day)d (hour):(min):(sec) )
*/
void Cal_Measuring_Time(){
  if(sec == 60) { sec = 0; min++; }
  if(min == 60) { min = 0; hour++; }
  if(hour == 24) { hour = 0; day++; }
  
  Serial.print("Measuring Time\t\t\t");
  Serial.print(day); Serial.print("d ");
  if(hour < 10) Serial.print("0");
  Serial.print(hour); Serial.print(":");
  if(min < 10) Serial.print("0");
  Serial.print(min); Serial.print(":");
  if(sec < 10) Serial.print("0");
  Serial.println(sec);
}

void Print_Result(int cmd){
  float value = 0.0f;
  switch(cmd){
    case 0xA0:
    Serial.print("Reset Response\t\t\t");
    if(buffer[0]== 1) Serial.println("Reset Success.");
    else              Serial.println("Reset Fail(Status - Ready).");
    break;

  case 0xB0:
    Serial.print("Status\t\t\t\t");
    switch(buffer[0]){
      case 0: Serial.println("Ready"); break;
      case 1: Serial.println("10min Waiting"); break;
      case 2: Serial.println("Normal"); break;
    }
     status = buffer[0];
      Serial.print("VIB Status\t\t\t");
      switch(buffer[1]){
        case 0: Serial.println("OFF"); break;
        case 1: Serial.println("ON"); break;
    }
     break;
    
    case 0xB1:
      if(status > 0){
        sec++;
        Cal_Measuring_Time();
    }
    break;

  case 0xB2:
    Serial.print("Measuring Value(10min avg)\t");
    value = buffer[0] + (float)buffer[1]/100;
    Serial.print(value); Serial.println(" uSv/hr");
    break;

  case 0xB3:
    Serial.print("Measuring Value(1min avg)\t");
    value = buffer[0] + (float)buffer[1]/100;
    Serial.print(value); Serial.println(" uSv/hr");
    break;

  case 0xB4:
    Serial.print("FW Version\t\t\t");
    Serial.print("V"); Serial.print(buffer[0]);
    Serial.print("."); Serial.println(buffer[1]);
    break;
  }
}

Okay, the I2C code looks like a much better starting point.
Have you tested this and gotten it working with your NodeMCU?

If so then read this:

And read this:

The delay in your void loop needs to go, and Gamma_Mod_Read_Value needs to be called with a BlynkTimer as described in the ‘keep your void loop clean’ document.

Pete.

I have tested the code and it doesnt work in NodeMCU. The out is always 0.
What can be the failure?

First of all, have you wired the sensor up to your NodeMCU correctly?

The SDA connection from the sensor should go to the pin labelled D2 (which is GPIO4)
The SCL connection from the sensor should go to the pin labelled D1 (which is GPIO5)

If that is correct then you need to check the I2C address that the board is using:

/*
* Address Assignment
* Default Address :: 0x18
* A0 Open, A1 Short :: 0x19
* A0 Short, A1 Open :: 0x1A
* A0 Open, A1 Open :: 0x1B
*/
int addr = 0x18;

If all appears good (solder pads A0 and A1 open and using 0x18 in the code) then try uploading this sketch to scan your I2C bus for devices:

// I2C scanner for NodeMCU
// D1 (GPIO5) = SCL
// D2 (GPIO4) = SDA

#include <Wire.h>
 
void setup()
{
  Wire.begin(4,5); // SDA, SCL (4,5 for ESP8266 default I2C pins)
  Serial.begin(74880);
  delay(2000);
  Serial.println("\nI2C Scanner");
}
 
void loop()
{
  byte error, address;
  int nDevices;
 
  Serial.println("Scanning...");
 
  nDevices = 0;
  for(address = 1; address < 127; address++ )
  {
    // The i2c_scanner uses the return value of
    // the Write.endTransmisstion to see if
    // a device did acknowledge to the address.
    Wire.beginTransmission(address);
    error = Wire.endTransmission();
 
    if (error == 0)
    {
      Serial.print("I2C device found at address 0x");
      if (address<16)
        Serial1.print("0");
      Serial.print(address,HEX);
      Serial.println("  !");
 
      nDevices++;
    }
    else if (error==4)
    {
      Serial.print("Unknown error at address 0x");
      if (address<16)
        Serial.print("0");
      Serial1.println(address,HEX);
    }    
  }
  if (nDevices == 0)
    Serial.println("No I2C devices found\n");
  else
    Serial.println("done\n");
 
  delay(2000);           // wait 2 seconds for next scan
}

Pete.

Okey, the addreess is 18. Can it fail because i need a pull-up resistor?

Is that based on running the scanner sketch, or the solder pads?

Where do you plan to put this pullup?

Pete.

I have connected slc and sda from the nodemcu in the sensor, and I plan to connect the pull up between the sensor sld and nodemcu and the sda, this form:
image

I have tested the code and it works correctly. Now, how can i collect the value of 1 min and display it in blunk?

I’ve never had to use pull up resistors on an I2C bus, but adding them probably won’t cause any problems.

Here is you code modified for Blynk:

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Wire.h>

char auth[] = "YourAuthToken";

// Your WiFi credentials.
char ssid[] = "YourNetworkName";
char pass[] = "YourPassword";

BlynkTimer timer;


/*
* We will be using the I2C hardware interface on the Arduino in
* combination with the built-in Wire library to interface.
* Arduino analog input 5 - I2C SCL
* Arduino analog input 4 - I2C SDA
*
* Command List
* 0xA0 :: Reset
* 0xB0 :: Read Status
* 0xB1 :: Read Measuring Time
* 0xB2 :: Read Measuring Value (10minute avg / 1minute update)
* 0xB3 :: Read Measuring Value (1minute avg / 1minute update)
* 0xB4 :: Read Firmware Version
*
* Address Assignment
* Default Address :: 0x18
* A0 Open, A1 Short :: 0x19
* A0 Short, A1 Open :: 0x1A
* A0 Open, A1 Open :: 0x1B
*/
int addr = 0x18;
int day,hour,minute,sec = 0;
byte buffer[2] = {0,0};
int status = 0;

void setup() {
  //Arduino Initialize
  Serial.begin(74880); // changed from 9600
  Wire.begin();

  Blynk.begin(auth, ssid, pass);
  timer.setInterval(1000L, take_timed_reading); // Call the function that wasin the void loop once every second
  
  Serial.println("Gamma Sensor Sensing Start");

  //Read Firmware version
  Gamma_Mod_Read(0xB4);
  //Reset before operating the sensor
  Gamma_Mod_Read(0xA0);
  Serial.println("================================================");
}

void loop()
{
  Blynk.run();
  timer.run();
}

void take_timed_reading()
{
  //Read Statue, Measuring Time, Measuring Value
  Gamma_Mod_Read_Value();  
  Serial.println("================================================");
}



void Gamma_Mod_Read_Value(){
  Gamma_Mod_Read(0xB0); // Read Status
  Gamma_Mod_Read(0xB1); // Read Measuring Time
  Gamma_Mod_Read(0xB2); // Read Measuring Value (10minute avg / 1minute update)
  Gamma_Mod_Read(0xB3); // Read Measuring Value (1minute avg / 1minute update)
}

void Gamma_Mod_Read(int cmd){
  /* Begin Write Sequence */
  Wire.beginTransmission(addr);
  Wire.write(cmd);
  Wire.endTransmission();
  /* End Write Sequence */
  delay(10);
  /* Begin Read Sequence */
  Wire.requestFrom(addr, 2);
  byte i = 0;
  while(Wire.available())
  {
  buffer[i] = Wire.read();
  i++;
  }
  /* End Read Sequence */

  /* View Results */
  Print_Result(cmd);
}
/*
* Calculation Measuring Time
* Format :: 0d 00:00:00 ( (day)d (hour):(minute):(sec) )
*/
void Cal_Measuring_Time(){
  if(sec == 60) { sec = 0; minute++; }
  if(minute == 60) { minute = 0; hour++; }
  if(hour == 24) { hour = 0; day++; }
  
  Serial.print("Measuring Time\t\t\t");
  Serial.print(day); Serial.print("d ");
  if(hour < 10) Serial.print("0");
  Serial.print(hour); Serial.print(":");
  if(minute < 10) Serial.print("0");
  Serial.print(minute); Serial.print(":");
  if(sec < 10) Serial.print("0");
  Serial.println(sec);
}

void Print_Result(int cmd){
  float value = 0.0f;
  switch(cmd){
    case 0xA0:
    Serial.print("Reset Response\t\t\t");
    if(buffer[0]== 1) Serial.println("Reset Success.");
    else              Serial.println("Reset Fail(Status - Ready).");
    break;

  case 0xB0:
    Serial.print("Status\t\t\t\t");
    switch(buffer[0]){
      case 0: Serial.println("Ready"); break;
      case 1: Serial.println("10minute Waiting"); break;
      case 2: Serial.println("Normal"); break;
    }
     status = buffer[0];
      Serial.print("VIB Status\t\t\t");
      switch(buffer[1]){
        case 0: Serial.println("OFF"); break;
        case 1: Serial.println("ON"); break;
    }
     break;
    
    case 0xB1:
      if(status > 0){
        sec++;
        Cal_Measuring_Time();
    }
    break;

  case 0xB2:
    Serial.print("Measuring Value(10minute avg)\t");
    value = buffer[0] + (float)buffer[1]/100;
    Serial.print(value); Serial.println(" uSv/hr");
    break;

  case 0xB3:
    Serial.print("Measuring Value(1minute avg)\t");
    value = buffer[0] + (float)buffer[1]/100;
    Serial.print(value); Serial.println(" uSv/hr");
    Blynk.virtualWrite(V1, value); // Sends the 1 minute average to the widget attached to pin V1
    break;

  case 0xB4:
    Serial.print("FW Version\t\t\t");
    Serial.print("V"); Serial.print(buffer[0]);
    Serial.print("."); Serial.println(buffer[1]);
    break;
  }
}

I had to change the integer variable min to minute as min is a reserved word in some of the Blynk libraries.

This like@

Blynk.virtualWrite(V1, value); // Sends the 1 minute average to the widget attached to pin V1`

sends your 1 minute average value to a widget attached to virtual pin 1 every second (1000ms).
Change the virtual pin number to one that suits you and test the code. if it works, and you want to see other values in your Blynk app then modify the code to send those values to the virtual pins of your choice.

You’ll obviously need to have the Blynk libraries correctly installed on your PC for this to work, and you’ll need to insert your auth code, SSID and Wi-Fi password in the appropriate places.

I’ve changed the serial baud rate to 74880 in the hope that this is also the native baud rate for your NodeMCU, and that you’ll be able to see the NodeMCU boot messages as well as the serial print messages in the serial monitor at the same time.
If this isn’t the correct baud rate then try 115200 instead.

Pete.

I use a code similar to yours, but I deleted all the data except the measurement in 1 minute.
This measurement is what I want to see in Blynk.
Now, i try Now, I will try to add two more sensors that I have.
This is my final code:

#define BLYNK_PRINT Serial

#include <Wire.h>
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
/*
 *   We will be using the I2C hardware interface on the Arduino in
 *   combination with the built-in Wire library to interface.
 *   Arduino analog input GPI05 (D1) - I2C SCL
 *   Arduino analog input GPI04 (D2) - I2C SDA
 *
 *   Command List
 *   0xA0  ::  Reset
 *   0xB0  ::  Read Status
 *   0xB1  ::  Read Measuring Time
 *   0xB2  ::  Read Measuring Value (10min avg / 1min update)
 *   0xB3  ::  Read Measuring Value (1min avg / 1min update)
 *   0xB4  ::  Read Firmware Version
 *
 *   Address Assignment
 *   Default Address   :: 0x18
 *   A0 Open, A1 Short :: 0x19
 *   A0 Short, A1 Open :: 0x1A
 *   A0 Open, A1 Open  :: 0x1B
 */
char auth[] = "--";
char ssid[] = "--";
char pass[] = "--";

int addr = 0x18;
byte buffer[2] = {0,0};
SimpleTimer timer1;

int status = 0;

void Gamma_Mod_Read_Value(){
  Gamma_Mod_Read(0xB3); // Read Measuring Value (1min avg / 1min update)
}

void Gamma_Mod_Read(int cmd){
  /* Begin Write Sequence */
  Wire.beginTransmission(addr);
  Wire.write(cmd);
  Wire.endTransmission();
  /* End Write Sequence */
  delay(10);
  /* Begin Read Sequence */
  Wire.requestFrom(addr, 2);
  byte i = 0;
  while(Wire.available())
  {
    buffer[i] = Wire.read();
    i++;
  }
  /* End Read Sequence */
  
  /* View Results */
  Print_Result(cmd);
}

void Print_Result(int cmd){
  float value = 0.0f;
  float value2;
  float value3;
  if(cmd = 0xB3){
      value = buffer[0] + (float)buffer[1]/100;       // uSv/hr
      value2 = 114*value;                             // uR / hr
      value3 = value2*5.3-2.39;                       // Beq / m^3
  }
  Blynk.virtualWrite(V0,value);
  Blynk.virtualWrite(V1,value3);
}

void setup() {
  Serial.begin(9600);
  Wire.begin(4,5);
  Blynk.begin(auth,ssid,pass);
  timer1.setInterval(1000L, Gamma_Mod_Read_Value);
}

void loop() {
  Blynk.run();
  timer1.run();
}

I can’t prove it yet, but I think it can work well.
I have added a temperature and humidity sensor and a CO2 one.
The final code is this:

// Gamma Sensor's Example Interface
#define BLYNK_PRINT Serial

#include <Wire.h>
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
/*
 *   We will be using the I2C hardware interface on the Arduino in
 *   combination with the built-in Wire library to interface.
 *   Arduino analog input GPI05 (D1) - I2C SCL
 *   Arduino analog input GPI04 (D2) - I2C SDA
 *
 *   Command List
 *   0xA0  ::  Reset
 *   0xB0  ::  Read Status
 *   0xB1  ::  Read Measuring Time
 *   0xB2  ::  Read Measuring Value (10min avg / 1min update)
 *   0xB3  ::  Read Measuring Value (1min avg / 1min update)
 *   0xB4  ::  Read Firmware Version
 *
 *   Address Assignment
 *   Default Address   :: 0x18
 *   A0 Open, A1 Short :: 0x19
 *   A0 Short, A1 Open :: 0x1A
 *   A0 Open, A1 Open  :: 0x1B
 */
char auth[] = "--";
char ssid[] = "--";
char pass[] = "--";

#define DHTPIN 2
#define CO2 A0

#define DHTTYPE DHT22

DHT dht(DHTPIN, DHTTYPE);
SimpleTimer timer1;

int addr = 0x18;
byte buffer[2] = {0,0};

int status = 0;

void SendSensor(){
  float h = dht.readHumidity();
  float t = dht.readTemperature();

  if (isnan(h) || isnan(t)){
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  float v = analogRead(CO2)*3.3/1023.0;
  float ppm = int((v-0.4)*3125.0);

  Blynk.virtualWrite(V1, h);
  Blynk.virtualWrite(V2, t);
  Blynk.virtualWrite(V3, ppm);
  Gamma_Mod_Read(0xB3); // Read Measuring Value (1min avg / 1min update)
}

void Gamma_Mod_Read(int cmd){
  /* Begin Write Sequence */
  Wire.beginTransmission(addr);
  Wire.write(cmd);
  Wire.endTransmission();
  /* End Write Sequence */
  delay(10);
  /* Begin Read Sequence */
  Wire.requestFrom(addr, 2);
  byte i = 0;
  while(Wire.available())
  {
    buffer[i] = Wire.read();
    i++;
  }
  /* End Read Sequence */
  
  /* View Results */
  Print_Result(cmd);
}

void Print_Result(int cmd){
  float value = 0.0f;
  float value2;
  float value3;
  if(cmd = 0xB3){
      value = buffer[0] + (float)buffer[1]/100;       // uSv/hr
      value2 = 114*value;                             // uR / hr
      value3 = value2*5.3-2.39;                       // Beq / m^3
  }
  Blynk.virtualWrite(V4,value);
  Blynk.virtualWrite(V5,value3);
}

void setup() {
  Serial.begin(9600);
  Wire.begin(4,5);
  Blynk.begin(auth,ssid,pass);
  timer1.setInterval(1000L, SendSensor);
}

void loop() {
  Blynk.run();
  timer1.run();
}