Node MCU ESP8266 for IOT project

#define SensorPin A0          // the pH meter Analog output is connected with the Arduino’s Analog
unsigned long int avgValue;  //Store the average value of the sensor feedback
float b;
int buf[10],temp;

//Blynk Pins
#define pH_Pin V2
#define temp_Pin V3
#define CO2_Pin V4



// Include the libraries we need
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 6
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

//  CO2
#include <MHZ19.h>  // MHZ-19 library
#include <SoftwareSerial.h>
#define RX_PIN 3  // Tx pin of the MHZ19
#define TX_PIN 1  // Rx pin of the MHZ19
#define BAUDRATE 9600 // Device to MH-Z19 Serial baudrate (should not be changed)
SoftwareSerial mySerial(RX_PIN, TX_PIN);
MHZ19 myMHZ19;

#include <SoftwareSerial.h>

// BLYNK
#define BLYNK_PRINT Serial

/* Fill in information from Blynk Device Info here */
#define BLYNK_TEMPLATE_ID "TMPL29QyjCZ3W"
#define BLYNK_TEMPLATE_NAME "Quickstart Template"
#define BLYNK_AUTH_TOKEN "a8lT_TobHldCKJBXA_cGFc2zAe9QZjjg"


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Stopit";
char pass[] = "Stopitboy";

void setup()
{
  Serial.begin(9600);
  Serial.println("Ready");  //Test the serial monitor
  // Start up the library
  sensors.begin();
  // Start Co2
  mySerial.begin(BAUDRATE); // (Uno example) device to MH-Z19 serial start
  myMHZ19.begin(mySerial);  // *Serial(Stream) refence must be passed to library begin().
  myMHZ19.autoCalibration();  // Turn auto calibration ON (OFF autoCalibration(false))
  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass, 8080);
}
void loop()
{
  // Temp Probe
  sensors.requestTemperatures(); // Send the command to get temperatures
  float tempC = sensors.getTempCByIndex(0);
  Serial.print("Temperature = ");Serial.println(tempC);
  
  // pH Sensor
  for(int i=0;i<10;i++)       //Get 10 sample value from the sensor for smooth the value
  { 
    buf[i]=analogRead(SensorPin);
    delay(10);
  }
  for(int i=0;i<9;i++)        //sort the analog from small to large
  {
    for(int j=i+1;j<10;j++)
    {
      if(buf[i]>buf[j])
      {
        temp=buf[i];
        buf[i]=buf[j];
        buf[j]=temp;
      }
    }
  }
  avgValue=0;
  for(int i=2;i<8;i++)  //take the average value of 6 center sample
    avgValue+=buf[i];
  float phValue=(float)avgValue*5.0/1024/6; //convert the analog into millivolt
  phValue=3.5*phValue;                      //convert the millivolt into pH value
  Serial.print("pH = ");  
  Serial.println(phValue,2);

  //Get CO2
  int CO2;
  CO2 = myMHZ19.getCO2();
  Serial.print("CO2 = ");Serial.print(CO2);Serial.println(" ppm");
  delay(2000);
//  Send to Blynk
  Blynk.virtualWrite(pH_Pin, phValue);
  Blynk.virtualWrite(temp_Pin, tempC);
  Blynk.virtualWrite(CO2_Pin, CO2);
  Blynk.run();
}

Output:

 Variables and constants in RAM (global, static), used 30936 / 80192 bytes (38%)
║   SEGMENT  BYTES    DESCRIPTION
╠══ DATA     1532     initialized variables
╠══ RODATA   2916     constants       
╚══ BSS      26488    zeroed variables
. Instruction RAM (IRAM_ATTR, ICACHE_RAM_ATTR), used 62811 / 65536 bytes (95%)
║   SEGMENT  BYTES    DESCRIPTION
╠══ ICACHE   32768    reserved space for flash instruction cache
╚══ IRAM     30043    code in IRAM    
. Code in flash (default, ICACHE_FLASH_ATTR), used 261540 / 1048576 bytes (24%)
║   SEGMENT  BYTES    DESCRIPTION
╚══ IROM     261540   code in flash   
esptool.py v3.0
Serial port COM6
Connecting....
Chip is ESP8266EX
Features: WiFi
Crystal is 26MHz
MAC: 48:3f:da:03:c7:b2
Uploading stub...
Running stub...
Stub running...
Configuring flash size...
Auto-detected Flash size: 4MB
Compressed 300176 bytes to 219265...
Writing at 0x00000000... (7 %)
Writing at 0x00004000... (14 %)
Writing at 0x00008000... (21 %)
Writing at 0x0000c000... (28 %)
Writing at 0x00010000... (35 %)
Writing at 0x00014000... (42 %)
Writing at 0x00018000... (50 %)
Writing at 0x0001c000... (57 %)
Writing at 0x00020000... (64 %)
Writing at 0x00024000... (71 %)
Writing at 0x00028000... (78 %)
Writing at 0x0002c000... (85 %)
Writing at 0x00030000... (92 %)
Writing at 0x00034000... (100 %)
Wrote 300176 bytes (219265 compressed) at 0x00000000 in 19.9 seconds (effective 120.8 kbit/s)...
Hash of data verified.

Leaving...
Hard resetting via RTS pin...

Serial Monitor:

�P�{��E>��0ag��š��!�QR� �
�0l[�[���0ag��š��!�QR�!�
�3,�x���DФ&0ag��š��!�QR�!�
�?0u�X"��N�0ag��š��!�QR�!�
����L����P�0ag��š��!�QR�!�
�4@�ՂH�3�0ag��š��!�QR�!�
�({8}*Y �z�0ag��š��!�QR�!�
������I�9�0ag��š��!�QR�!�
�e��%�����&0ag��š��!�QR�!�
����L��c���0ag��š��!�QR�!�
��/���b���0ag��š��!�QR�!�
�3�S1};}X:p�0ag��š��!�QR�!�
�*
=8}:Y �P�0ag��š��!�QR�!�
�%�0%����J�3�0ag��š��!�QR�!�
�%��C�ՊHO3�0ag��š��!�QR�!�
�%��C�ՂHO��ag��š��!�QR�!�
�1�S	��}�:j

And the device is not coming online. I have switched boards, confirmed my wifi is 2.4 Ghz, and tried mobile hotspot. Still no dice. I also confirmed my baudrate matched in serial monitor, and serial monitor just feedsback random signs.

@David08755 Please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code and your compiler output so that they display correctly.
Triple backticks look like this:
```

Copy and paste these if you can’t find the correct symbol on your keyboard.

Pete.

Done. Thank you Pete. I hope it displays well now

Not really, as I’m sure you can see if you take a look at your post.
The triple backticks need to be on separate line. Once you have them in the correct position you’ll see two scrollable boxes, and your descriptive text will be outside of these boxes.

The next think I’ll ask for is your serial monitor output, and that also needs triple backticks.

Pete.

Done. Thank you.

You have your serial monitor set to the wrong baud rate. It needs to match this value…

but if you’re using a NodeMCU than you are better using 74880 in your sketch and your serial monitor.

Pete.

I tried 9600, 19200, and 74880. I am using a NodeMCU. I could 74880 again, and post the result.

I don’t really understand your last post.

It doesn’t matter which baud rate you use, provided your sketch and serial monitor match.
However, most NodeMCU boards use 74880 as their default baud rate, and output their bot messages at that baud rate.
It therefore makes sense to use 74880 as the maud rate for your serial debug messages in your sketch.

Maybe you can endeavour to be clearer in future posts?

EDIT - Okay, I see what you’ve done now - you’ve created a Software Serial port on pins 1 & 3, which are the pins used by your hardware serial port.

You need to change this…

to different pins, otherwise both your hardware and software serial ports are talking across eachother.

Pete.

Thank you very much Pete. I’ll test this out

Thank you Pete. So I changed the RX and TX and tried uploading the code and on serial monitor its showing

rf[112] : 0Т�*�P*V��ET����T��K��u�U+UZ��UT��T ����ŪTQT�UQ��jP����u�����ZT�(E����Q�UEu����*���jPE*�u��E+��TT)UZV���(ZV����(Qժ�T/���rf cal sector: 1020

freq trace enable 0

rf[112] : 0Т�*�P*V��ET����T��K��u�U+UZ��UT��T ����ŪTQT�UQ��jP����u�����ZT�(E����Q�UEu����*���jPE*�u��E+��TT)UZV���(ZV����(Qժ�T/���rf cal sector: 1020

freq trace enable 0

rf[112] : 0Т�*�P*V��ET����T��K��u�U+UZ��UT��T ����ŪTQT�UQ��jP����u�����ZT�(E����Q�UEu����*���jPE*�u��E+��TT)UZV���(ZV����(Qժ�T/���rf cal sector: 1020

freq trace enable 0

rf[112] : 0Т�*�P*V��ET����T��K��u�U+UZ��UT��T ����ŪTQT�UQ��jP����u�����ZT�(E����Q�UEu����*���jPE*�u��E+��TT)UZV���(ZV����(Qժ�T/���rf cal sector: 1020

freq trace enable 0

rf[112] : 0Т�*�P*V��ET����T��K��u�U+UZ��UT��T ����ŪTQT�UQ��jP����u�����ZT�(E����Q�UEu����*���jPE*�u��E+��TT)UZV���(ZV����(Qժ�T/���rf cal sector: 1020

freq trace enable 0

rf[112] : 0Ш؁�Q9�y�va���nb�P� a�����l`�����M ڊ��0��P� a�����V���r��(a� ���0���5���5�ЀB�(�)��r��Lp�����l`�P�z��l9���B�����<�͇A�����+�)��a����U���5�� C���Ż�����MA��B�����u���i;aقH����P��(���B���Q9��59�����$�l��r��Lp�����Z+�!9�,1�����$�l��9��8���B�R��Q9��(i���B

There was a yellow line that read “Not connected. Select a board and port to connect automatically”. But the board and port are clearly chosen because it displays below

Ln 50, Col 19 NodeMCU 1.0 (ESP-12E Module) on COM6

This tells us nothing.
Post your code, and explain what connections you now have, and tell us what baud rate you have selected in your serial monitor.

Reboot the device (using the RST button) and post the serial output that is displayed.

Pete.

So I changed the code to the basic blynk quickstart code to rule out hardware issues. But my device is bouncing back and forth between online and offline

/*************************************************************

  This is a simple demo of sending and receiving some data.
  Be sure to check out other examples!
 *************************************************************/

/* Fill-in information from Blynk Device Info here */
#define BLYNK_TEMPLATE_ID           "TMPL29QyjCZ3W"
#define BLYNK_TEMPLATE_NAME         "Quickstart Template"
#define BLYNK_AUTH_TOKEN            "P5QRoWVpBTsG3EmL8pr0bVzo6IKV3uj2"

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Stopit";
char pass[] = "Stopitboy";

BlynkTimer timer;

// This function is called every time the Virtual Pin 0 state changes
BLYNK_WRITE(V0)
{
  // Set incoming value from pin V0 to a variable
  int value = param.asInt();

  // Update state
  Blynk.virtualWrite(V1, value);
}

// This function is called every time the device is connected to the Blynk.Cloud
BLYNK_CONNECTED()
{
  // Change Web Link Button message to "Congratulations!"
  Blynk.setProperty(V3, "offImageUrl", "https://static-image.nyc3.cdn.digitaloceanspaces.com/general/fte/congratulations.png");
  Blynk.setProperty(V3, "onImageUrl",  "https://static-image.nyc3.cdn.digitaloceanspaces.com/general/fte/congratulations_pressed.png");
  Blynk.setProperty(V3, "url", "https://docs.blynk.io/en/getting-started/what-do-i-need-to-blynk/how-quickstart-device-was-made");
}

// This function sends Arduino's uptime every second to Virtual Pin 2.
void myTimerEvent()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V2, millis() / 1000);
}

void setup()
{
  // Debug console
  Serial.begin(9600);

  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass, "blynk.cloud", 8080);
  // You can also specify server:
  //Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass, "blynk.cloud", 80);
  //Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass, IPAddress(192,168,1,100), 8080);

  // Setup a function to be called every second
  timer.setInterval(1000L, myTimerEvent);
}

void loop()
{
  Blynk.run();
  timer.run();
  // You can inject your own code or combine it with other sketches.
  // Check other examples on how to communicate with Blynk. Remember
  // to avoid delay() function!
}

Serial Monitor

[18470] Ready (ping: 205ms).
����5�Qz9���[78] Connecting to Stopit
[6408] Connected to WiFi
[6408] IP: 192.168.137.19
[6408] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v1.3.2 on ESP8266

 #StandWithUkraine    https://bit.ly/swua


[6535] Connecting to blynk.cloud:80
[6937] Ready (ping: 205ms).
��������>Ak��[77] Connecting to Stopit
[8408] Connected to WiFi
[8409] IP: 192.168.137.19
[8409] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v1.3.2 on ESP8266

 #StandWithUkraine    https://bit.ly/swua


[8536] Connecting to blynk.cloud:80
[8747] Ready (ping: 102ms).

Is there any reason why you won’t post the serial output in full?

Pete.

It kept on overwriting my copy. I have updated it now

Change the baud rate in your sketch to 74880 and re-upload it to your NodeMCU and set the serial monitor to the same baud rate.
When you do that, this line of serial output…

will make more sense, and will show the reason why your device is rebooting.

Pete.

Serial Monitor on 74880

 /____/_/\_, /_//_/_/\_\
        /___/ v1.3.2 on ESP8266

 #StandWithUkraine    https://bit.ly/swua


[11368] Connecting to blynk.cloud:80
[11551] Ready (ping: 102ms).
��*�P*V��ET����T��K��u�U+UZ��UT��T����ŪTQT�UQ��jP����u�����ZT�(E����Q�UEu����*���jPE*�u��E+��TT)UZV���(ZV����(QU�QQ�*U�rf cal sector: 1020
freq trace enable 0
rf[112] : 0�[78] Connecting to Stopit
[11909] Connected to WiFi
[11909] IP: 192.168.137.91
[11909] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v1.3.2 on ESP8266

 #StandWithUkraine    https://bit.ly/swua


[11926] Connecting to blynk.cloud:80
[12356] Ready (ping: 204ms).
��*�P*V��ET����T��K��u�U+UZ��UT��T����ŪTQT�UQ��jP����u�����ZT�(E����Q�UEu����*���jPE*�u��E+��TT)UZV���(ZV����(QU�QQ�*U�rf cal sector: 1020
freq trace enable 0
rf[112] : 0�[77] Connecting to Stopit

That’s strange!

Can you confirm that the only thing connected to your NodeMCU is the USB serial cable?

When you compiled your code did you set the Debug Level (in the IDE > Tools > Debug level) to something other than “none” ?
The reason I ask is that some of these messages that you can now see look like debug messages from the IDE’s ESP core…

Pete.

Debug is set to none. And the only thing connected to my NodeMCU is the USB cable

Do you have another NodeMCU that you could try?

Pete.

I have 2. I tried both and got the same result