-->
Page 1 of 1

Problem with serial comm from arduino nano to esp8266

PostPosted: Fri Aug 11, 2017 12:24 pm
by D Les Well
Hi all.
I need a bit of help.
My project is up and running its a 3 phase wind turbine charging 4x 12V battery's.
I want to add serial communication between the Arduino Nano that is controlling PWM to slow the turbine and an esp8266 to upload data to BLYNK IOT

I am using the Arduino nano as the main controller as it is MISSION CRITICAL i.e. if that code blocks bye bye turbine and battery bank.
A logic level shiftier 5v to 3.3v is fitted between tx rx lines.

I am only able to pass my variable data (the battery voltage) once to the esp8266 then it will not update any more.

If someone could have a look at my code and give me some advice, I would be grateful.

Arduino code
Code: Select all//wind turbine ARDUINO code

#include <EasyTransfer.h>

//create two objects
EasyTransfer ETin, ETout;


struct RECEIVE_DATA_STRUCTURE{
  //put your variable definitions here for the data you want to receive
  //THIS MUST BE EXACTLY THE SAME ON THE OTHER ARDUINO
 // int16_t Voltage;
 int16_t data1;
  int16_t data2;
};

struct SEND_DATA_STRUCTURE{
  //put your variable definitions here for the data you want to receive
  //THIS MUST BE EXACTLY THE SAME ON THE OTHER ARDUINO
 
  int16_t Voltage;
};

//give a name to the group of data
RECEIVE_DATA_STRUCTURE rxdata;
SEND_DATA_STRUCTURE txdata;

#include <Timer.h>
//http://www.doctormonk.com/2012/01/arduino-timer-library.html
Timer t;
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address

#include <Adafruit_ADS1015.h>
 Adafruit_ADS1115 ads;  /* Use this for the 16-bit version */
//Adafruit_ADS1115 ads1115(0x48);  // construct an ads1115 at address 0x48

float volt0, volt1, volt2, volt3, voltage;

//int voltagePin = A0; //connect to voltage divider on main battery voltage
  int pwmPin = 6;  //connect to driver for cooling fan
//  int currentPin = A1;  //connect to current sensor
  int fanPin = 9; // connect to fan
  double voltageRegOn;  //
  int pwmAmount; // store pwm value
 
 double batteryVoltage; //store battery voltage
 double currentVoltage;  //store current sensor raw voltage
 double amps; //store calculated amps
 
void setup() {
  Serial.begin(2400);
  //start the library, pass in the data details and the name of the serial port. Can be Serial, Serial1, Serial2, etc.
  ETin.begin(details(rxdata), &Serial);
  ETout.begin(details(txdata), &Serial);
  ads.begin();
 // The ADC input range (or gain) can be changed via the following
  // functions, but be careful never to exceed VDD +0.3V max, or to
  // exceed the upper and lower limits if you adjust the input range!
  // Setting these values incorrectly may destroy your ADC!
  //                                                                ADS1015  ADS1115
  //                                                                -------  -------
  // ads.setGain(GAIN_TWOTHIRDS);  // 2/3x gain +/- 6.144V  1 bit = 3mV      0.1875mV (default)
  // ads.setGain(GAIN_ONE);        // 1x gain   +/- 4.096V  1 bit = 2mV      0.125mV
  // ads.setGain(GAIN_TWO);        // 2x gain   +/- 2.048V  1 bit = 1mV      0.0625mV
  // ads.setGain(GAIN_FOUR);       // 4x gain   +/- 1.024V  1 bit = 0.5mV    0.03125mV
  // ads.setGain(GAIN_EIGHT);      // 8x gain   +/- 0.512V  1 bit = 0.25mV   0.015625mV
 // ads.setGain(GAIN_SIXTEEN);    // 16x gain  +/- 0.256V  1 bit = 0.125mV  0.0078125mV


  // put your setup code here, to run once:
 voltageRegOn = 27.6; //set regulator start voltage here

 // Wire.begin(0, 2); //define pin for  scl sda D3 D4 (gpio)
  lcd.begin(20,4);
lcd.backlight();
lcd.setCursor(0,0); //Start at character 0 on line 0
lcd.print("Booting Up....Arduino NANO code");
  delay (1000);
  lcd.clear();
 
 
}





void takeReadings () {
int16_t adc0, adc1, adc2, adc3;
 
  adc0 = ads.readADC_SingleEnded(0);
  adc1 = ads.readADC_SingleEnded(1);
  adc2 = ads.readADC_SingleEnded(2);
  adc3 = ads.readADC_SingleEnded(3);
// Serial.print("AIN0: "); Serial.println(adc0);
// Serial.print("Volt3: "); Serial.println(voltage);
 // Serial.print("PWM: "); Serial.println(pwmAmount);
// Serial.print("AIN1: "); Serial.println(adc1);
// Serial.print("AIN2: "); Serial.println(adc2);
// Serial.print("AIN3: "); Serial.println(adc3);
// Serial.println(" ");
 
//volt3 = adc0*0.0001875; // Volts
//volt3 = (volt3 *6);
  voltage = adc0*0.00113088; //30/26528 measured at pentomiter 30v
  volt2 = adc1;


 
 
   
}



void oneSecond () {
  //FAN CODE to turn on DUMP LOAD cooling fan.
  if (pwmAmount >= 50){
    analogWrite(fanPin,100);
  }
  else {
    analogWrite(fanPin,0);
  }

 lcd.setCursor(0,0); //Start at character 0 on line 0
lcd.print("Volts:");
  lcd.setCursor(6,0); //Start at character 0 on line 0
lcd.print(voltage);

lcd.setCursor(0,1); //Start at character 0 on line 0
lcd.print("AmpsV: ");
lcd.setCursor(7,1); //Start at character 0 on line 0
//lcd.print(amps);
lcd.print(currentVoltage);

lcd.setCursor(0,2); //Start at character 0 on line 0
lcd.print("PWM:      ");
lcd.setCursor(7,2); //Start at character 0 on line 0
//lcd.print(amps);
lcd.print(pwmAmount);
}

void TxRx (){
 //  txdata.buttonstate = HIGH;
 txdata.Voltage = voltage;

  //then we will go ahead and send that data out
  ETout.sendData();

//there's a loop here so that we run the recieve function more often then the
 //transmit function. This is important due to the slight differences in
 //the clock speed of different Arduinos. If we didn't do this, messages
 //would build up in the buffer and appear to cause a delay.
  for(int i=0; i<5; i++){
    //remember, you could use an if() here to check for new data, this time it's not needed.
    ETin.receiveData();
    //Voltage = rxdata.Voltage
    //set our LED on or off based on what we received from the other Arduino   
  //  digitalWrite(13, rxdata.buttonstate);
 
}
}

void loop() {
  // put your main code here, to run repeatedly:
 t.update();
    int afterOneSecond = t.every(50, oneSecond);
   // int afterOneMilliSecond = t.every(1,oneMilliSecond);
    int afterTakeReadings = t.every(50,takeReadings);
    int afterTxRx = t.every(50,TxRx);

// Apply a load to slow wind turbine if battery bank is fully charged (set by voltageRegOn)
//while
if ( voltage > voltageRegOn && pwmAmount <255){
  pwmAmount = pwmAmount +15;
  analogWrite(pwmPin,pwmAmount); //send output to mosfet
  }
  if ( voltage < voltageRegOn && pwmAmount >0){
  pwmAmount = pwmAmount -15;
  analogWrite(pwmPin,pwmAmount); //send output to mosfet
  }



   

}



ESP8266 code
Code: Select all//ESP8266 NODEMCU CODE
#include <EasyTransfer.h>
//http://www.billporter.info/2011/05/30/easytransfer-arduino-library/comment-page-12/#comments
//create two objects
EasyTransfer ETin, ETout;


struct RECEIVE_DATA_STRUCTURE{
  //put your variable definitions here for the data you want to receive
  //THIS MUST BE EXACTLY THE SAME ON THE OTHER ARDUINO
  int16_t Voltage;
};

struct SEND_DATA_STRUCTURE{
  //put your variable definitions here for the data you want to receive
  //THIS MUST BE EXACTLY THE SAME ON THE OTHER ARDUINO
  int16_t data1;
  int16_t data2;
};

//give a name to the group of data
RECEIVE_DATA_STRUCTURE rxdata;
SEND_DATA_STRUCTURE txdata;

// http://www.billporter.info/2011/05/30/easytransfer-arduino-library/comment-page-12/#comments
int Voltage;
int temp;
#include <Timer.h>
//http://www.doctormonk.com/2012/01/arduino-timer-library.html
Timer t;
// #define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
// Attach virtual serial terminal to Virtual Pin V10
//WidgetTerminal terminal(V10);

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "22ce317618cc4f7cb4c99d9b11b8a1fc";

#include <LiquidCrystal_I2C.h>
#include <Wire.h>


LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address


void setup() {
   temp=0;
 //start the library, pass in the data details and the name of the serial port. Can be Serial, Serial1, Serial2, etc.
ETin.begin(details(rxdata), &Serial);
ETout.begin(details(txdata), &Serial);
 
  Wire.begin(0, 2); //define pin for  scl sda D3 D4 (gpio)
  lcd.begin(20,4);
lcd.backlight();
lcd.setCursor(0,0); //Start at character 0 on line 0
lcd.print("Booting Up....");
  delay (1000);
  lcd.clear();
            Serial.begin(2400);       
  Blynk.begin(auth, "FRITZ!Box 7490", "40253267300484638395");

   
}

void blinkV (){
 //terminal.print("PWM: ");
 //  terminal.print(pwmAmount);
 //  terminal.println(); //new line
   // Ensure everything is sent
 // terminal.flush();

Blynk.virtualWrite(2,rxdata.Voltage); // Volts
 
}

void oneSecond()  //runs every one second
{
 lcd.setCursor(0,0); //Start at character 0 on line 0
lcd.print("Volts: ");
  lcd.setCursor(8,0); //Start at character 0 on line 0
lcd.print(rxdata.Voltage);
 Blynk.virtualWrite(1,temp); // Volts
 temp++;
}

void TxRx (){
 //  txdata.buttonstate = HIGH;

  //then we will go ahead and send that data out

ETout.sendData();

//there's a loop here so that we run the recieve function more often then the
 //transmit function. This is important due to the slight differences in
 //the clock speed of different Arduinos. If we didn't do this, messages
 //would build up in the buffer and appear to cause a delay.
  for(int i=0; i<5; i++){
    //remember, you could use an if() here to check for new data, this time it's not needed.
   ETin.receiveData();
    Voltage = rxdata.Voltage;
    //set our LED on or off based on what we received from the other Arduino   
  //  digitalWrite(13, rxdata.buttonstate);
 


  }

}

void loop() {
   Blynk.run();
    t.update();
    int afterOneSecond = t.every(1000, oneSecond);
//int afterOneMilliSecond = t.every(1, oneMilliSecond);
int afterBlinkV = t.every(6000, blinkV);
int afterTxRx = t.every(2500,TxRx);


}