SoftwareSerial on

Support forum for MegunoLink
Post Reply
zaft3
Posts: 2
Joined: Wed Jan 30, 2019 6:22 am

Wed Jan 30, 2019 3:13 pm

Hi,

I had countered an issue regarding on using software serial:
1) using on CommandHandler
2) Message MyCSVMessage for logging

1) when i using button to cmd_set, there is no response i get on monitor. (need to click the button many times then it will appear the response)
2) on logging part, the data can be log, but the name cannot be log either im using rs232_Serial.print("Time"); or rs232_Serial.print(F("Time"));

#include <Time.h>
#include <TimeLib.h>
#include "DS1307.h"


#include "MegunoLink.h"
#include "CommandHandler.h"

#include <Wire.h>
#include <Adafruit_ADS1015.h>
#include "DHT.h"

#define set_hour clock.hour
#define set_minute clock.minute
#define set_second clock.second
#define set_day clock.dayOfMonth
#define set_month clock.month
#define set_year clock.year+2000

int set_clock_enable = 0;
const String months [] ={"", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
const String hours [] ={"12", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11"};
char jam[8], minit[8], saat[8];
char masa[20], tarikh[20];

float h, m, s;

float Stab, LowStab[10], lowestStab;
float Field;
float Remote;
float Proximity;
float Humidity;
float Temp;
float Temp_F;
float _FG;
float DistCell = 30; // distance between cell = 30cm

int i,k;

#define DHTPIN 9
#define DHTTYPE DHT11
#include <SoftwareSerial.h>

Adafruit_ADS1115 ads_top(0x48); /* Use this for the 16-bit version */
Adafruit_ADS1115 ads_bottom(0x4B);
DHT dht(DHTPIN, DHTTYPE);
SoftwareSerial rs232_Serial(2, 3); // RX, TX

DS1307 clock;//define a object of DS1307 class


long LastSent;
char field[8], fg[8]; // Buffer big enough for 7-character float
char stab[8], remote[8], proximity[8], _loweststab[8];
int FlagStab;

unsigned long startMillis; //some global variables available anywhere in the program
unsigned long currentMillis;
const unsigned long period = 1000; //the value is a number of milliseconds

Table MyTable;
CommandHandler<> SerialCommandHandler(rs232_Serial);
InterfacePanel MyPanel("", rs232_Serial);
Message MyCSVMessage("Data",rs232_Serial); //"Data" = the taget message channel (remember to select this in megunolink)
TimePlot FGdata("FGplot",rs232_Serial), CPdata("CPplot",rs232_Serial);

void Cmd_SetStabLock(CommandParameter &Parameters)
{
FlagStab=Parameters.NextParameterAsInteger(FlagStab); //!SetStabLock [1]\r
rs232_Serial.println();
rs232_Serial.print("flag stab: ");
rs232_Serial.println(FlagStab);
rs232_Serial.println();
}

void Cmd_GetStabLock(CommandParameter &Parameters)
{
Parameters.GetSource().print(F("Lock Stab Value = "));
Parameters.GetSource().println(Stab);
dtostrf(Stab, 6, 2, stab); // Leave room for too large numbers!

MyPanel.SetText(F("LockStab"), stab);
}

void setup(void)
{
Serial.begin(9600);
rs232_Serial.begin(9600);
//Serial.println("Hello!");

CPdata.SetTitle("Catode Protection CP Data");
CPdata.SetXlabel("Time");
CPdata.SetYlabel("Voltage (mV)");

FGdata.SetTitle("FG data");
FGdata.SetXlabel("Time");
FGdata.SetYlabel("uV/cm");

FGdata.SetSeriesProperties("FG", Plot::Blue, Plot::Solid, 2, Plot::Square);

CPdata.SetSeriesProperties("Field", Plot::Blue, Plot::Solid, 2, Plot::Square);
CPdata.SetSeriesProperties("Remote", Plot::Red, Plot::Solid, 2, Plot::Square);
CPdata.SetSeriesProperties("Proximity", Plot::Magenta, Plot::Solid, 2, Plot::Square);

------- -------
ads_top.setGain(GAIN_TWOTHIRDS); // 2/3x gain +/- 6.144V 1 bit = 3mV 0.1875mV (default)
ads_bottom.setGain(GAIN_TWOTHIRDS);

SerialCommandHandler.AddCommand(F("GetStabLock"), Cmd_GetStabLock);
SerialCommandHandler.AddCommand(F("SetStabLock"), Cmd_SetStabLock);
SerialCommandHandler.AddCommand(F("SetValues"), Cmd_SetValues);
clock.begin();

MyCSVMessage.Begin();
rs232_Serial.print("Time");
rs232_Serial.print(",");
rs232_Serial.print("Field");
rs232_Serial.print(",");
rs232_Serial.print("Stab");
rs232_Serial.print(",");
rs232_Serial.print("Remote");
rs232_Serial.print(",");
rs232_Serial.print("Prox");
MyCSVMessage.End();
//
ads_top.begin();
ads_bottom.begin();
dht.begin();
//clock.begin();
set_clock_enable = 0 ;
startMillis = millis(); //initial start time
i = 0;
}

void printTime()
{
String dataString;
String DataString;
clock.getTime();

//date - time
DataString += String(2000+clock.year); DataString +="/"; DataString += String(clock.month); DataString +="/";DataString += String(clock.dayOfMonth);
dataString += String(hours [clock.hour]); dataString +=":";
if(clock.minute <10){dataString +="0";dataString += String(clock.minute); }else{dataString += String(clock.minute);}dataString +=":";
if(clock.second <10){dataString +="0";dataString += String(clock.second); }else{dataString += String(clock.second);}
if(clock.hour>11)dataString +="PM";else dataString +="AM";

DataString.toCharArray(tarikh,20);
dataString.toCharArray(masa,20);

MyPanel.SetText(F("Tarikh"), tarikh);
MyPanel.SetText(F("Masa"), masa);
}

void Cmd_SetValues(CommandParameter &Parameters){
//Serial.println("update clock");
rs232_Serial.println("update clock");
h = Parameters.NextParameterAsDouble();
m = Parameters.NextParameterAsDouble();
s = Parameters.NextParameterAsDouble();

//clock.fillByYMD(2013,1,19);//Jan 19,2013
clock.fillByHMS(h,m,s);//15:28 30"
//clock.fillDayOfWeek(SAT);//Saturday
clock.setTime();//write time to the RTC chip

}

void CalculateData(){
/* Be sure to update this value based on the IC and the gain settings! */
//float multiplier = 3.0F; /* ADS1015 @ +/- 6.144V gain (12-bit results) */
float multiplier = 0.1875F; /* ADS1115 @ +/- 6.144V gain (16-bit results) */
//float seconds;

//seconds = (float)millis()/1000;

Stab = float(ads_top.readADC_Differential_0_1())*multiplier;
Field = float(ads_top.readADC_Differential_2_3()*multiplier);
Remote = float(ads_bottom.readADC_Differential_0_1())*multiplier;
Proximity = Stab - Remote;
Temp = dht.readTemperature();
Humidity = dht.readHumidity();

_FG = Field*1000/DistCell;

float hic = dht.computeHeatIndex(Temp, Humidity, false);
}

void LockStab_(){

if(FlagStab == 1){
rs232_Serial.print(F("*******************************************"));
for(int j=0; j<10; j++){
float multiplier = 0.1875F; /* ADS1115 @ +/- 6.144V gain (16-bit results) */
Stab = float(ads_top.readADC_Differential_0_1())*multiplier;
LowStab[j] = Stab;
}
k = 0;
for(int j=0; j<10; j++){
int _LowStab = LowStab[k];
if (_LowStab < LowStab[k]){
lowestStab = _LowStab;
}
else{
lowestStab = LowStab[k];
}
k++;
}
dtostrf(lowestStab, 6, 2, _loweststab); // Leave room for too large numbers!
MyPanel.SetText(F("LockStab"), _loweststab);
FlagStab = 0;
}
}

void LogData(){

currentMillis = millis(); //get the current "time" (actually the number of milliseconds since the program started)

if (currentMillis - startMillis >= period) //test whether the period has elapsed
{

//Send data to Interface
CPdata.SendData(F("Field"),Field); // Field = series name, Field = data to plot
//MyPlot.SendData(F("Stab"),Stab); // Stab = series name, Stab = data to plot
CPdata.SendData(F("Remote"),Remote); // Remote = series name, Remote = data to plot
CPdata.SendData(F("Proximity"),Proximity); // Proximity = series name, Proximity = data to plot

FGdata.SendData(F("FG"),_FG); // FG data

dtostrf(Field, 6, 2, field); // Leave room for too large numbers!
dtostrf(Stab, 6, 2, stab); // Leave room for too large numbers!
dtostrf(Remote, 6, 2, remote); // Leave room for too large numbers!
dtostrf(Proximity, 6, 2, proximity); // Leave room for too large numbers!
dtostrf(_FG, 6, 2, fg);


MyPanel.SetText(F("Field"), field);
MyPanel.SetText(F("Stab"), stab);
MyPanel.SetText(F("Remote"), remote);
MyPanel.SetText(F("Prox"), proximity);

MyPanel.SetText(F("FG"), fg);

String dataString;
clock.getTime();

// date - time
//dataString += String(2000+clock.year); dataString +="/"; dataString += String(clock.month); dataString +="/";dataString += String(clock.dayOfMonth);dataString +=",";
dataString += String(hours [clock.hour]); dataString +=":";
if(clock.minute <10){dataString +="0";dataString += String(clock.minute); }else{dataString += String(clock.minute);}dataString +=":";
if(clock.second <10){dataString +="0";dataString += String(clock.second); }else{dataString += String(clock.second);}
if(clock.hour>11)dataString +="PM";else dataString +="AM";

MyCSVMessage.Begin();
rs232_Serial.print(dataString);
rs232_Serial.print(",");
rs232_Serial.print(Field);
rs232_Serial.print(",");
rs232_Serial.print(Stab);
rs232_Serial.print(",");
rs232_Serial.print(Remote);
rs232_Serial.print(",");
rs232_Serial.print(Proximity);
MyCSVMessage.End();

startMillis = currentMillis; //IMPORTANT to save the start time of the current LED state.
}
}

void loop(void)
{


printTime();
CalculateData();
// LockStab_();
LogData();
SerialCommandHandler.Process();

delay(5);
}
zaft3
Posts: 2
Joined: Wed Jan 30, 2019 6:22 am

Fri Feb 01, 2019 5:58 pm

does anyone got the answer? :(
philr
Posts: 229
Joined: Mon May 26, 2014 10:58 am

Sat Feb 02, 2019 1:31 am

Hi Zaft, to start with you need to get rid of the delay in your loop. The command handler only grabs one character at a time which means it will be slow to respond to the command.

Im not sure what you mean by the name can not be logged... what is the name? Do you get serial data in the monitor?

Cheers
Phil
Post Reply