Ok, i try use the software serial pin like the tutorial above, but still dint get the data by looking in my series motor. Because i plan to received the data from arduino by using esp8266,then send to the data from esp8266 to cayenne dashboard.
arduino coding :
`#include <SoftwareSerial.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.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};
byte values[11];
SoftwareSerial modbus(2,3);
SoftwareSerial esp(5,6);
String str;
void setup() {
Serial.begin(4800);
modbus.begin(4800);
esp.begin(4800);
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() {
// 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;
String myString;
String buf;
unsigned int len;
nitrogen_val = nitrogen();
delay(100);
phosphorus_val = phosphorous();
delay(100);
potassium_val = potassium();
delay(100);
Serial.print("N: “);
Serial.print(nitrogen_val);
Serial.print(” P: “);
Serial.print(phosphorus_val);
Serial.print(” K: ");
Serial.print(potassium_val);
str =String("coming from arduino: ")+String("N= ")+String(nitrogen_val)+String("P= ")+String(phosphorus_val)+String("K= ")+String(potassium_val);
esp.println(str);
delay(1000);
// 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];
}`
ESP8266 coding:
#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>
// WiFi network info.
char ssid = “huimei”;
char wifiPassword = “huimei0098”;
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username = “dfaf54e0-b6ce-11eb-883c-638d8ce4c23d”;
char password = “de97db5e14c192eb268bb5d48ab40307553ec68a”;
char clientID = “4a62c6c0-6217-11ec-ad90-75ec5e25c7a4”;
String nitrogen_val,phosphorus_val,potassium_val;
void setup() {
Serial.begin(4800);
Cayenne.begin(username, password, clientID, ssid, wifiPassword);
}
void loop() {
if (Serial.available())
{
Serial.write(Serial.read());
}
String N = nitrogen_val;
Serial.println(N);
delay(1000);
//Cayenne.loop();
}
//CAYENNE_OUT_DEFAULT()
//{
// Cayenne.virtualWrite(0,nitrogen_val);
//Cayenne.virtualWrite(1,phosphorus_val);
//Cayenne.virtualWrite(2,potassium_val);
//}