Reading the value from Arduino

I am able to write data from BOLT to arduino successfully but unfortunately I am not able to read the data from Arduino.
I always get Receive buffer empty as an output. What should I do to read the data from arduino?

What code are you using for the same? Can you share it here.

I have used this code;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600); //Begin Serial at 9600
Serial.setTimeout(50); //Timeout serial if readString is unavailable
}

String inString="";

void loop() {
// put your main code here, to run repeatedly:
if (Serial.available() > 0) {
// get incoming String
inString = Serial.readString(); //Read input string from bolt
Serial.print(inString); //Send same string back to bolt
}
}

Hi,

Instead of readString use readStringUntil("\n") and make sure in your write data you append \n

This one showing error. I used the previous code only and I am able to read values from arduino to the internet bu entering two times serialRead function in adress bar

Hi,

I need to push all the readings which i am getting in serial monitor to the internet. I used the following code for the same but not getting the output on the internet. Please help me to find the interfacing error in the code!

#include <TimerOne.h>
#define pwmRegister OCR1A // the logical pin, can be set to OCR1B
const int outPin = 9; // the physical pin
long period = 100; // the period in microseconds
long pulseWidth = 18;
int Duty;
float Power1;
float Power2=0;
int iter=1;
int maxiter=6;
// width of a pulse in microseconds
int prescale[] = {0,1,8,64,256,1024}; // the range of prescale values
void setup()
{
pinMode(outPin, OUTPUT);
Timer1.initialize(period); // initialize timer1, 1000 microseconds
setPulseWidth(pulseWidth);
delay(500);
Serial.print("The Value of first duty cycle ");
Serial.println(pulseWidth);
float AV1 = analogRead(A0);
Serial.print("The sensed voltage 1 is ");
Serial.println(AV1);

float AI1 = analogRead(A1);
Power1 = AV1AI1100;

Serial.print("The sensed current 1 is ");
Serial.println(AV1);

Serial.print(“The Value of first power is “);
Serial.println(Power1);
Serial.begin(9600);
Serial.setTimeout(50); //Timeout serial if readString is unavailable
}
String inString=””;
void loop()
{
if(iter<maxiter)
{
Serial.print("The Value of first duty cycle ");
Serial.println(pulseWidth);
float AV1 = analogRead(A0);
Serial.print("The sensed voltage 1 is ");
Serial.println(AV1);

float AI1 = analogRead(A1);
Power1 = AV1AI1100;

Serial.print("The sensed current 1 is ");
Serial.println(AV1);

Serial.print("The Value of first power is ");
Serial.println(Power1);
pulseWidth = pulseWidth+4;
setPulseWidth(pulseWidth);
delay(500);
iter=iter+1;
}
else
{
pulseWidth = pulseWidth+4;
setPulseWidth(pulseWidth);
delay(500);
pulseWidth = pulseWidth+4;
setPulseWidth(pulseWidth);
delay(500);
pulseWidth = pulseWidth-4;
setPulseWidth(pulseWidth);
delay(500);
pulseWidth = pulseWidth-4;
setPulseWidth(pulseWidth);
delay(500);
}
}
bool setPulseWidth(long microseconds)
{
bool ret = false;
int prescaleValue = prescale[Timer1.clockSelectBits];
// calculate time per counter tick in nanoseconds
long precision = (F_CPU / 128000) * prescaleValue ;
period = precision * ICR1 / 1000; // period in microseconds
if( microseconds < period)
{
int duty = map(microseconds, 0,period, 0,1024);
if( duty < 1)
duty = 1;
if(microseconds > 0 && duty < RESOLUTION)
{
Timer1.pwm(outPin, duty);
ret = true;
}
}
return ret;
if (Serial.available() > 0) {
// get incoming String
inString = Serial.readString(); //Read input string from bolt
Serial.print(inString); //Send same string back to bolt
}
}

This is because Serial Timeout does not affect readString() function,
Instead use readStringUntil(char)… Make sure in SerialWR function you append the some char which is not present in the main commands

Hi,

I can not understand. Please elaborate a little!

Should be

void setup() {
// put your setup code here, to run once:
Serial.begin(9600); //Begin Serial at 9600
Serial.setTimeout(200); //Timeout serial if readString is unavailable
}

String inString = “”;
bool data_ready = false;
void loop() {
// put your main code here, to run repeatedly:
if (Serial.available() > 0) {
// get incoming String
inString = Serial.readStringUntil(’\n’); //Read input string from bolt
data_ready = true;
}

if (data_ready == true) {
Serial.print(inString); //Send same string back to bolt
data_ready = false;
}

}

Hi,

Thanks . I am happy that I am learning and you people are very helpful. I am getting the output.(but partially).

So, the problem now is that only first two lines are displayed on the webpage and not all the lines. I tried to put the following code snippet in different loops (because when such problem occurs generally looping is the issue) but that also din’t work out.

The code is -
// put your main code here, to run repeatedly:
if (Serial.available() > 0) {
// get incoming String
inString = Serial.readStringUntil(’\n’); //Read input string from bolt
data_ready = true;
}

if (data_ready == true) {
Serial.print(inString); //Send same string back to bolt
data_ready = false;
}

Question: As BOLT can only receive 256 Bytes of data so I think that memory should be the issue (since only shows two lines . One of duty cycle and other of sensed voltage). Am I correct or is their something else that I am missing ?

Note: As mentioned in your above replies, I have used http://192.168.43.70/serialWR?data=’\n’ in the address bar for reading the data of serial monitor from BOLT.

Hi

Try the following before changing the arduino code and let me know if this worked for you.

http://192.168.43.70/serialWR?data=hellofrombolt

I am getting again the same two lines only as an output from this URL as well. I am looking forward to fetch all the data from serial monitor to the bolt but not some part of it.

Help needed.

Can you mail me the code you are trying to use to contactus@inventrom.com

NOTE: open angular bracket for TimerOne.h is knowingly omitted to avoid the hiding of TimerOne.h text in the forum. While uploading it on the arduino open angular bracket has been used.

#include TimerOne.h>
#define pwmRegister OCR1A // the logical pin, can be set to OCR1B
const int outPin = 9; // the physical pin
long period = 100; // the period in microseconds
long pulseWidth = 84;
int Duty;
float Power1;
float Power2=0;
int iter=1;
int maxiter=24;
// width of a pulse in microseconds
int prescale[] = {0,1,8,64,256,1024}; // the range of prescale values
void setup()
{

pinMode(outPin, OUTPUT);
Timer1.initialize(period); // initialize timer1, 1000 microseconds
setPulseWidth(pulseWidth);
delay(500);
Serial.print("The Value of first duty cycle ");
Serial.println(pulseWidth);
float AV1 = analogRead(A0);
Serial.print("The sensed voltage 1 is ");
Serial.println(AV1);

float AI1 = analogRead(A1);
Power1 = AV1AI1100;

Serial.print("The sensed current 1 is ");
Serial.println(AV1);

Serial.print("The Value of first power is ");
Serial.println(Power1);
Serial.setTimeout(50); //Timeout serial if readString is unavailable
Serial.begin(9600);
Serial.setTimeout(200);
}
String inString="";
bool data_ready=false;
void loop()
{
if(iter<maxiter)
{
Serial.print("The Value of first duty cycle ");
Serial.println(pulseWidth);
float AV1 = analogRead(A0);
Serial.print("The sensed voltage 1 is ");
Serial.println(AV1);

float AI1 = analogRead(A1);
Power1 = AV1AI1100;

Serial.print("The sensed current 1 is ");
Serial.println(AV1);

pulseWidth = pulseWidth-1;
setPulseWidth(pulseWidth);
delay(500);
iter=iter+1;
Serial.print("The Value of first power is ");
Serial.println(iter);
}
else
{
Serial.print(“I am in loopfirst”);

pulseWidth = pulseWidth+2;
setPulseWidth(pulseWidth);
Serial.print("The Value of first duty is ");
Serial.println(pulseWidth);
delay(500);

pulseWidth = pulseWidth+2;
setPulseWidth(pulseWidth);
Serial.print("The Value of second duty is ");
Serial.println(pulseWidth);
delay(500);

pulseWidth = pulseWidth -2;
setPulseWidth(pulseWidth);
Serial.print("The Value of third duty is ");
Serial.println(pulseWidth);
delay(500);

pulseWidth = pulseWidth-2;
setPulseWidth(pulseWidth);
Serial.print("The Value of fourth duty is ");
Serial.println(pulseWidth);
delay(500);
}
}
bool setPulseWidth(long microseconds)
{
bool ret = false;
int prescaleValue = prescale[Timer1.clockSelectBits];
// calculate time per counter tick in nanoseconds
long precision = (F_CPU / 128000) * prescaleValue ;
period = precision * ICR1 / 1000; // period in microseconds
if( microseconds < period)
{
int duty = map(microseconds, 0,period, 0,1024);
if( duty < 1)
duty = 1;
if(microseconds > 0 && duty < RESOLUTION)
{
Timer1.pwm(outPin, duty);
ret = true;
}
}
return ret;
if (Serial.available() > 0) {
// get incoming String
inString = Serial.readStringUntil(’\n’); //Read input string from bolt
data_ready = true;
}

if (data_ready == true) {
Serial.print(inString); //Send same string back to bolt
data_ready = false;
}
}

I’ll try the code and let you know by eod

return ret;
if (Serial.available() > 0) {
// get incoming String
inString = Serial.readStringUntil(’\n’); //Read input string from bolt
data_ready = true;
}

if (data_ready == true) {
Serial.print(inString); //Send same string back to bolt
data_ready = false;
}

Hi you cannot put any lines of code after a returns statement,

Also

if (Serial.available() > 0) {
// get incoming String
inString = Serial.readStringUntil(’\n’); //Read input string from bolt
data_ready = true;
}

if (data_ready == true) {
Serial.print(inString); //Send same string back to bolt
data_ready = false;
}

This will just reply the same string bolt sends to arduino like an echo.

Also all other serial.print statements must be removed from the code ,or they will flood bolts buffer.

You can try using a softwareSerial for bolt communication instead. https://www.arduino.cc/en/Reference/SoftwareSerial

1 Like

Hi,

Thanks for writing in!!

I would like to know , are you getting output by this.??

Umm I am trying very hard over this.:disappointed:
Even though I did the alterations which you had written but unfortunately, I am getting the same output. Only first two lines of serial monitor are visible to the BOLT. I have tried the code for different loops but not getting the desired output . Please find the code which I have altered and do let me know if I have missed any concept. As now, I am so much attached with this interfacing of bolt and arduino that I do not want to keep it unfinished!

NOTE: open angular bracket for TimerOne.h, etc is knowingly omitted to avoid the hiding of TimerOne.h, etc texts in the forum. While uploading it on the arduino, an open angular bracket has been used.

#include TimerOne.h>
#include SoftwareSerial.h>
#define pwmRegister OCR1A // the logical pin, can be set to OCR1B
SoftwareSerial mySerial(0,1);
const int outPin = 9; // the physical pin
long period = 100; // the period in microseconds
long pulseWidth = 84;
int Duty;
float Power1;
float Power2=0;
int iter=1;
int maxiter=24;
// width of a pulse in microseconds
int prescale[] = {0,1,8,64,256,1024}; // the range of prescale values
void setup()
{

pinMode(outPin, OUTPUT);
Timer1.initialize(period); // initialize timer1, 1000 microseconds
setPulseWidth(pulseWidth);
delay(500);
mySerial.print("The Value of first duty cycle ");
mySerial.println(pulseWidth);
float AV1 = analogRead(A0);
mySerial.print("The sensed voltage 1 is ");
mySerial.println(AV1);

float AI1 = analogRead(A1);
Power1 = AV1AI1100;

mySerial.print("The sensed current 1 is ");
mySerial.println(AV1);

mySerial.print(“The Value of first power is “);
mySerial.println(Power1);
mySerial.setTimeout(50); //Timeout serial if readString is unavailable
mySerial.begin(9600);
mySerial.setTimeout(200);
}
String inString=””;
bool data_ready=false;
void loop()
{
if(iter)
mySerial.print("The Value of first duty cycle ");
mySerial.println(pulseWidth);
float AV1 = analogRead(A0);
mySerial.print("The sensed voltage 1 is ");
mySerial.println(AV1);

float AI1 = analogRead(A1);
Power1 = AV1AI1100;

mySerial.print("The sensed current 1 is ");
mySerial.println(AV1);

pulseWidth = pulseWidth-1;
setPulseWidth(pulseWidth);
delay(500);
iter=iter+1;
mySerial.print("The Value of first power is ");
mySerial.println(iter);
}
else
{
{
mySerial.print(“I am in loopfirst”);

pulseWidth = pulseWidth+2;
setPulseWidth(pulseWidth);
mySerial.print("The Value of first duty is ");
mySerial.println(pulseWidth);
delay(500);

pulseWidth = pulseWidth+2;
setPulseWidth(pulseWidth);
mySerial.print("The Value of second duty is ");
mySerial.println(pulseWidth);
delay(500);

pulseWidth = pulseWidth -2;
setPulseWidth(pulseWidth);
mySerial.print("The Value of third duty is ");
mySerial.println(pulseWidth);
delay(500);

pulseWidth = pulseWidth-2;
setPulseWidth(pulseWidth);
mySerial.print("The Value of fourth duty is ");
mySerial.println(pulseWidth);
delay(500);
}
}
bool setPulseWidth(long microseconds)
{
bool ret = false;
int prescaleValue = prescale[Timer1.clockSelectBits];
// calculate time per counter tick in nanoseconds
long precision = (F_CPU / 128000) * prescaleValue ;
period = precision * ICR1 / 1000; // period in microseconds
if( microseconds < period)
{
int duty = map(microseconds, 0,period, 0,1024);
if( duty < 1)
duty = 1;
if(microseconds > 0 && duty < RESOLUTION)
{
Timer1.pwm(outPin, duty);
ret = true;
}
}
if (mySerial.available() > 0) {
// get incoming String
inString = mySerial.readStringUntil(’\n’); //Read input string from bolt
data_ready = true;
}

if (data_ready == true) {
mySerial.print(inString); //Send same string back to bolt
data_ready = false;
}
return ret;

}

I will check this code and get back to you shortly

Hi,

You have used mySerial for all debug messages.

Instead use normal Serial for debug messages and use serial only for communication with Bolt.

Also

Instead of typing the entire code and then integrating first try simple communication with bolt and then move on to complex codes

Ex:

#include SoftwareSerial.h>
SoftwareSerial mySerial(9,10); //Do not use 0,1 they are used for the hardware UART

void setup()
{
Serial.begin(9600);
mySerial.begin(9600);
mySerial.setTimeout(200);
Serial.println(“in setup”); // a debug message

}

int msg_count = 0;
String for_bolt = “A message for bolt , msg num =”;

void loop(){
if (mySerial.available() > 0){
Serial.print(“Data from bolt is”);
Serial.println( mySerial.readStringUntil(’\n’));
count ++;
mySerial.println(for_bolt + String(count));
}
}