How to send data from arduino to NodeMCU? I ady using the software serial (D2,D3) for other device. Can I straight away use TX and RX pin?
I am using TX and RX pin of arduino connect to RX and TX pin of NodeMCU. The data does not received, the data upload to thingspeak is zero.
3 Comments
Time Descendingif true #include <SoftwareSerial.h> #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> //#include <AltSoftSerial.h>
// For the i2c supported Oled display module which is 128x64 #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 64 // OLED display height, in pixels #define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin) Adafruit_SSD1306 display(128, SCREEN_HEIGHT, &Wire, OLED_RESET);
//declare the digital to control the operation of RS-485 modbus #define RE 8
// The following are the Inquiry frames which are send to the NPK sensor //for reading the Nitrogen, Phosphorus, and Potassium values // We defined three arrays with names nitro_inquiry_frame, phos_inquiry_frame, and pota_inquiry_frame // Each inquiry frame have 8 values const byte nitro_inquiry_frame[] = {0x01,0x03, 0x00, 0x1e, 0x00, 0x01, 0xe4, 0x0c}; const byte phos_inquiry_frame[] = {0x01,0x03, 0x00, 0x1f, 0x00, 0x01, 0xb5, 0xcc}; const byte pota_inquiry_frame[] = {0x01,0x03, 0x00, 0x20, 0x00, 0x01, 0x85, 0xc0};
char buffer[40]; byte values[11]; SoftwareSerial modbus(2,3); //AltSoftSerial espSerial;
// we will need three variables of the type byte to store the values of // Nitrogen, phosphorus, and Potassium. byte nitrogen_val,phosphorus_val,potassium_val;
void setup() { Serial.begin(115200); modbus.begin(4800); //espSerial.begin(1200); pinMode(RE, OUTPUT);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //initialize with the I2C addr 0x3C (128x64)
delay(500);
display.clearDisplay();
display.setCursor(25, 15);
display.setTextSize(1);
display.setTextColor(WHITE);
display.println(" NPK Sensor");
display.setCursor(25, 35);
display.setTextSize(1);
display.print("Initializing");
display.display();
}
void loop() { nitrogen_val = nitrogen(); delay(100); phosphorus_val = phosphorous(); delay(100); potassium_val = potassium(); delay(100);
//------Sending Data to ESP8266--------// sprintf(buffer,"*%03d%03d%03d#",nitrogen_val,phosphorus_val,potassium_val);//Sending *xxxxxxxxx# to esp8266 Serial.println(buffer); delay(2000); //------------------------------------//
// The following code is used to display the data on the Oled display display.clearDisplay(); display.setTextSize(2); display.setCursor(0, 5); display.print("N: "); display.print(nitrogen_val); display.setTextSize(1); display.print(" mg/kg");
display.setTextSize(2);
display.setCursor(0, 25);
display.print("P: ");
display.print(phosphorus_val);
display.setTextSize(1);
display.print(" mg/kg");
display.setTextSize(2);
display.setCursor(0, 45);
display.print("K: ");
display.print(potassium_val);
display.setTextSize(1);
display.print(" mg/kg");
display.display();
}
byte nitrogen(){ digitalWrite(RE,HIGH); delay(10); if(modbus.write(nitro_inquiry_frame,sizeof(nitro_inquiry_frame))==8){ digitalWrite(RE,LOW); // When we send the inquiry frame to the NPK sensor, then it replies with the response frame // now we will read the response frame, and store the values in the values[] arrary, we will be using a for loop. for(byte i=0;i<7;i++){ //Serial.print(modbus.read(),HEX); values[i] = modbus.read(); // Serial.print(values[i],HEX); } Serial.println(); } return values[5]; // returns the Nigtrogen value only, which is stored at location 4 in the array }
byte phosphorous(){ digitalWrite(RE,HIGH); delay(10); if(modbus.write(phos_inquiry_frame,sizeof(phos_inquiry_frame))==8){ digitalWrite(RE,LOW); for(byte i=0;i<7;i++){ //Serial.print(modbus.read(),HEX); values[i] = modbus.read(); // Serial.print(values[i],HEX); } Serial.println(); } return values[5]; }
byte potassium(){ digitalWrite(RE,HIGH); delay(10); if(modbus.write(pota_inquiry_frame,sizeof(pota_inquiry_frame))==8){ digitalWrite(RE,LOW); for(byte i=0;i<7;i++){ //Serial.print(modbus.read(),HEX); values[i] = modbus.read(); //Serial.print(values[i],HEX); } Serial.println(); } return values[5]; } end
This is my arduino coding: { #include SoftwareSerial.h #include Wire.h #include Adafruit_GFX.h #include Adafruit_SSD1306.h //#include AltSoftSerial.h
// For the i2c supported Oled display module which is 128x64 #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 64 // OLED display height, in pixels #define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin) Adafruit_SSD1306 display(128, SCREEN_HEIGHT, &Wire, OLED_RESET);
//declare the digital to control the operation of RS-485 modbus #define RE 8
// The following are the Inquiry frames which are send to the NPK sensor //for reading the Nitrogen, Phosphorus, and Potassium values // We defined three arrays with names nitro_inquiry_frame, phos_inquiry_frame, and pota_inquiry_frame // Each inquiry frame have 8 values const byte nitro_inquiry_frame[] = {0x01,0x03, 0x00, 0x1e, 0x00, 0x01, 0xe4, 0x0c}; const byte phos_inquiry_frame[] = {0x01,0x03, 0x00, 0x1f, 0x00, 0x01, 0xb5, 0xcc}; const byte pota_inquiry_frame[] = {0x01,0x03, 0x00, 0x20, 0x00, 0x01, 0x85, 0xc0};
char buffer[40]; byte values[11]; SoftwareSerial modbus(2,3); //AltSoftSerial espSerial;
// we will need three variables of the type byte to store the values of // Nitrogen, phosphorus, and Potassium. byte nitrogen_val,phosphorus_val,potassium_val;
void setup() { Serial.begin(115200); modbus.begin(4800); //espSerial.begin(1200); pinMode(RE, OUTPUT);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //initialize with the I2C addr 0x3C (128x64)
delay(500);
display.clearDisplay();
display.setCursor(25, 15);
display.setTextSize(1);
display.setTextColor(WHITE);
display.println(" NPK Sensor");
display.setCursor(25, 35);
display.setTextSize(1);
display.print("Initializing");
display.display();
}
void loop() { nitrogen_val = nitrogen(); delay(100); phosphorus_val = phosphorous(); delay(100); potassium_val = potassium(); delay(100);
//------Sending Data to ESP8266--------// sprintf(buffer,"*%03d%03d%03d#",nitrogen_val,phosphorus_val,potassium_val);//Sending *xxxxxxxxx# to esp8266 Serial.println(buffer); delay(2000); //------------------------------------//
// The following code is used to display the data on the Oled display display.clearDisplay(); display.setTextSize(2); display.setCursor(0, 5); display.print("N: "); display.print(nitrogen_val); display.setTextSize(1); display.print(" mg/kg");
display.setTextSize(2);
display.setCursor(0, 25);
display.print("P: ");
display.print(phosphorus_val);
display.setTextSize(1);
display.print(" mg/kg");
display.setTextSize(2);
display.setCursor(0, 45);
display.print("K: ");
display.print(potassium_val);
display.setTextSize(1);
display.print(" mg/kg");
display.display();
}
byte nitrogen(){ digitalWrite(RE,HIGH); delay(10); if(modbus.write(nitro_inquiry_frame,sizeof(nitro_inquiry_frame))==8){ digitalWrite(RE,LOW); // When we send the inquiry frame to the NPK sensor, then it replies with the response frame // now we will read the response frame, and store the values in the values[] arrary, we will be using a for loop. for(byte i=0;i<7;i++){ //Serial.print(modbus.read(),HEX); values[i] = modbus.read(); // Serial.print(values[i],HEX); } Serial.println(); } return values[5]; // returns the Nigtrogen value only, which is stored at location 4 in the array }
byte phosphorous(){ digitalWrite(RE,HIGH); delay(10); if(modbus.write(phos_inquiry_frame,sizeof(phos_inquiry_frame))==8){ digitalWrite(RE,LOW); for(byte i=0;i<7;i++){ //Serial.print(modbus.read(),HEX); values[i] = modbus.read(); // Serial.print(values[i],HEX); } Serial.println(); } return values[5]; }
byte potassium(){ digitalWrite(RE,HIGH); delay(10); if(modbus.write(pota_inquiry_frame,sizeof(pota_inquiry_frame))==8){ digitalWrite(RE,LOW); for(byte i=0;i<7;i++){ //Serial.print(modbus.read(),HEX); values[i] = modbus.read(); //Serial.print(values[i],HEX); } Serial.println(); } return values[5]; } }