I2C EK-TM4C129EXL: Reads bytes double - i2c

I'm trying to communicate on an TI launchpad via i2c with a GPS Receiver. The Launchpad I use is the EK-TM4C129EXL. For the initialization and the communication aim using the TIVA ware Library. The TI Launchpad is the Master and the GPS receiver is the Slave.
This is the Init function I use:
SYS_RESLT sys_I2cInitFunc(uint32_t g_ui32SysClock){
SYS_RESLT eFault = SYS_RESLT__OK;
//
// Stop the Clock, Reset and Enable I2C Module
// in Master Function
//
SysCtlPeripheralDisable(SYSCTL_PERIPH_I2C0);
SysCtlPeripheralReset(SYSCTL_PERIPH_I2C0);
SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0);
//
// Wait for the I2C0 module to be ready.
//
while (!SysCtlPeripheralReady(SYSCTL_PERIPH_I2C0))
{
}
//
//Enable GPIO for Configuring the I2C Interface Pins
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
//
// Wait for the Peripheral to be ready for programming
//
while (!SysCtlPeripheralReady(SYSCTL_PERIPH_GPIOB))
{
};
//
// Configure Pins for I2C2 Master Interface
//
GPIOPinConfigure(GPIO_PB2_I2C0SCL);
GPIOPinConfigure(GPIO_PB3_I2C0SDA);
GPIOPinTypeI2C(GPIO_PORTB_AHB_BASE, GPIO_PIN_3);
GPIOPinTypeI2CSCL(GPIO_PORTB_AHB_BASE, GPIO_PIN_2);
//
// Initialize Master
//
I2CMasterInitExpClk(I2C0_BASE,g_ui32SysClock, false);
return eFault;}
the Parameter g_ui32SysClock is the System Clock frequency.
Then I have a Function which I use to read.
SYS_RESLT gps_readByteI2C(uint8_t *pbBytesRead, uint8_t bLen){
SYS_RESLT eFault = SYS_RESLT__OK;
uint8_t bAdd = GPS_I2C_ADD
;
uint8_t bI = 0;// read only one byte
if (bLen == 1){ // wait
while (I2CMasterBusy(I2C0_BASE))
{
}
// set i2c address and read
I2CMasterSlaveAddrSet(I2C0_BASE, bAdd, true);
// wait
while (I2CMasterBusy(I2C0_BASE))
{
}
// read on the bus
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE);
// wait
while (I2CMasterBusy(I2C0_BASE))
{
}
// check fault
if (I2CMasterErr(I2C0_BASE) != I2C_MASTER_ERR_NONE)
{
eFault = SYS_RESLT__I2C; // fault
}
while (I2CMasterBusy(I2C0_BASE))
{
}
// read data
*pbBytesRead = I2CMasterDataGet(I2C0_BASE);
}
else // more then one byte to read
{
// wait
while (I2CMasterBusy(I2C0_BASE))
{
}
// set i2c address and read
I2CMasterSlaveAddrSet(I2C0_BASE, bAdd, true);
// wait
while (I2CMasterBusy(I2C0_BASE))
{
}
// read on the bus first byte
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_RECEIVE_START);
// wait
while (I2CMasterBusy(I2C0_BASE))
{
}
// check fault
if (I2CMasterErr(I2C0_BASE) != I2C_MASTER_ERR_NONE)
{
//test = I2CMasterErr(I2C0_BASE);
eFault = SYS_RESLT__I2C; // fault
}
while (I2CMasterBusy(I2C0_BASE))
{
}
// get data
pbBytesRead[0] = I2CMasterDataGet(I2C0_BASE);
// for all othe bytes
for (bI = 0; bI < bLen - 2; bI++)
{
// wait
while (I2CMasterBusy(I2C0_BASE))
{
}
// set i2c address and read
I2CMasterSlaveAddrSet(I2C0_BASE, bAdd, true);
// wait
while (I2CMasterBusy(I2C0_BASE))
{
}
// read on the bus
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_RECEIVE_CONT);
// wait
while (I2CMasterBusy(I2C0_BASE))
{
}
// check fault
if (I2CMasterErr(I2C0_BASE) != I2C_MASTER_ERR_NONE)
{
eFault = SYS_RESLT__I2C; // fault
}
while (I2CMasterBusy(I2C0_BASE))
{
}
// get data
pbBytesRead[bI + 1] = I2CMasterDataGet(I2C0_BASE);
// break if new message
if (pbBytesRead[bI + 1] == 0x24)
{
break;
}
}
// wait
while (I2CMasterBusy(I2C0_BASE))
{
}
// set i2c address and read
I2CMasterSlaveAddrSet(I2C0_BASE, bAdd, true);
// wait
while (I2CMasterBusy(I2C0_BASE))
{
}
// read on the bus last byte
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_RECEIVE_FINISH);
// wait
while (I2CMasterBusy(I2C0_BASE))
{
}
// check fault
if (I2CMasterErr(I2C0_BASE) != I2C_MASTER_ERR_NONE)
{
eFault = SYS_RESLT__I2C; // fault
}
while (I2CMasterBusy(I2C0_BASE))
{
}
// get data
pbBytesRead[bI + 1] = I2CMasterDataGet(I2C0_BASE);
}
return eFault;}
I Use a logic Analyzer to watch the i2c bus an there everything seams fine the message is correct and look like it should be. so I think the Problem is not the slave. the Problem is that in pbBytesRead are most of the bytes tow ore more times. for example the logic analyzer says on the bus was : 0x24 0x42 0x45 0x76 0x64. but the launchepad reads in pbBytesRead : 0x24 0x24 0x24 0x42 0x42 0x45 0x45 0x45 0x76 0x76 0x76 0x64 0x64 0x64. maybe something with the init is wrong? has anybody some clue or hint for me? if needed I can provide some more information.
kind regards

Related

CMSIS_driver I2C problem, status stuck in busy

I tried to learn the CMSIS driver in I2C. I have STM32F407VGT6 and EEPROM at24c256 as test hardware. First, create the I2C environment with CubeMX to config the I2C.
https://imgur.com/CbroTeo
https://imgur.com/nCP5eFt
then I followed the arm example from the arm-cmsis driver website.
https://arm-software.github.io/CMSIS_5/Driver/html/group__i2c__interface__gr.html
#include "Driver_I2C.h"
#define EEPROM_I2C_ADDR 0xA0 /* EEPROM I2C address */
/* I2C driver instance */
extern ARM_DRIVER_I2C Driver_I2C1;
static ARM_DRIVER_I2C *I2Cdrv = &Driver_I2C1;
static volatile uint32_t I2C_Event;
/* I2C Signal Event function callback */
void I2C_SignalEvent (uint32_t event) {
/* Save received events */
I2C_Event |= event;
/* Optionally, user can define specific actions for an event */
if (event & ARM_I2C_EVENT_TRANSFER_INCOMPLETE) {
/* Less data was transferred than requested */
}
if (event & ARM_I2C_EVENT_TRANSFER_DONE) {
/* Transfer or receive is finished */
}
if (event & ARM_I2C_EVENT_ADDRESS_NACK) {
/* Slave address was not acknowledged */
}
if (event & ARM_I2C_EVENT_ARBITRATION_LOST) {
/* Master lost bus arbitration */
}
if (event & ARM_I2C_EVENT_BUS_ERROR) {
/* Invalid start/stop position detected */
}
if (event & ARM_I2C_EVENT_BUS_CLEAR) {
/* Bus clear operation completed */
}
if (event & ARM_I2C_EVENT_GENERAL_CALL) {
/* Slave was addressed with a general call address */
}
if (event & ARM_I2C_EVENT_SLAVE_RECEIVE) {
/* Slave addressed as receiver but SlaveReceive operation is not started */
}
if (event & ARM_I2C_EVENT_SLAVE_TRANSMIT) {
/* Slave addressed as transmitter but SlaveTransmit operation is not started */
}
}
/* Read I2C connected EEPROM (event driven example) */
int32_t EEPROM_Read_Event (uint16_t addr, uint8_t *buf, uint32_t len) {
uint8_t a[2];
a[0] = (uint8_t)(addr >> 8);
a[1] = (uint8_t)(addr & 0xFF);
/* Clear event flags before new transfer */
I2C_Event = 0U;
I2Cdrv->MasterTransmit (EEPROM_I2C_ADDR, a, 2, true);
/* Wait until transfer completed */
while ((I2C_Event & ARM_I2C_EVENT_TRANSFER_DONE) == 0U);
/* Check if all data transferred */
if ((I2C_Event & ARM_I2C_EVENT_TRANSFER_INCOMPLETE) != 0U) return -1;
/* Clear event flags before new transfer */
I2C_Event = 0U;
I2Cdrv->MasterReceive (EEPROM_I2C_ADDR, buf, len, false);
/* Wait until transfer completed */
while ((I2C_Event & ARM_I2C_EVENT_TRANSFER_DONE) == 0U);
/* Check if all data transferred */
if ((I2C_Event & ARM_I2C_EVENT_TRANSFER_INCOMPLETE) != 0U) return -1;
return 0;
}
/* Read I2C connected EEPROM (pooling example) */
int32_t EEPROM_Read_Pool (uint16_t addr, uint8_t *buf, uint32_t len) {
uint8_t a[2];
a[0] = (uint8_t)(addr >> 8);
a[1] = (uint8_t)(addr & 0xFF);
I2Cdrv->MasterTransmit (EEPROM_I2C_ADDR, a, 2, true);
/* Wait until transfer completed */
while (I2Cdrv->GetStatus().busy);
/* Check if all data transferred */
if (I2Cdrv->GetDataCount () != len) return -1;
I2Cdrv->MasterReceive (EEPROM_I2C_ADDR, buf, len, false);
/* Wait until transfer completed */
while (I2Cdrv->GetStatus().busy);
/* Check if all data transferred */
if (I2Cdrv->GetDataCount () != len) return -1;
return 0;
}
/* Initialize I2C connected EEPROM */
int32_t EEPROM_Initialize (bool pooling) {
int32_t status;
uint8_t val;
if (pooling == true) {
I2Cdrv->Initialize (NULL);
} else {
I2Cdrv->Initialize (I2C_SignalEvent);
}
I2Cdrv->PowerControl (ARM_POWER_FULL);
I2Cdrv->Control (ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST);
I2Cdrv->Control (ARM_I2C_BUS_CLEAR, 0);
/* Check if EEPROM can be accessed */
if (pooling == true) {
status = EEPROM_Read_Pool (0x00, &val, 1);
} else {
status = EEPROM_Read_Event (0x00, &val, 1);
}
return (status);
}
above was the code example from cmsis-driver website. I have modified the EEPROM_I2C_ADDR to 0xA0(for AT24C256 EEPROM).
then I try to run main() with function
EEPROM_Initialize(true)
then this function will get into EEPROM_Read_Pool()
but the test always stuck in
while (I2Cdrv->GetStatus().busy);
after I2Cdrv->MasterTransmit (EEPROM_I2C_ADDR, a, 2, true);。
I have tried
use HAL library to run MEM_Write, MEM_Read with the EEPROM, and make sure the EEPROM could work.
HAL_I2C_Mem_Write(&hi2c1, EEPROM_ADDRESS, 0, 0xff, &data_to_write, 1,10);
HAL_Delay(10);
uint8_t data_read = 60;
HAL_I2C_Mem_Read(&hi2c1,EEPROM_ADDRESS,0,0xff,&data_to_read,1,1);
I have checked the return value of all below
I2Cdrv->PowerControl (ARM_POWER_FULL);
I2Cdrv->Control (ARM_I2C_BUS_SPEED, ARM_I2C_BUS_SPEED_FAST);
I2Cdrv->Control (ARM_I2C_BUS_CLEAR, 0);
I2Cdrv->MasterTransmit (EEPROM_I2C_ADDR, a, 2, true);
all the return value are 0x00000000(seems running ok).But still have no idea why the status is always busy.
I have checked that it has no chance to get into the
I2C_EV_IRQHandler or I2C_ER_IRQHandlerin debug mode to change the status or increase the TX count.

STM32 UART DMA can receive first time correct then it receive nothing

I'm trying to use dma with uart in stm32f746 nucleo.
In receiving mode I can receive correct data in first use. When i try to receive again, receiving buffer doesn't change its initial values before receiving although I can get receive complete signal callback every time.
/* Main Loop */
while(1)
{
HAL_GPIO_TogglePin(LED_PORT,LED_GREEN); // Led Example
HAL_Delay(200);
pRxData[0]=5;
pRxData[1]=0;
// HAL_UART_Receive_IT(&UartHandle,pRxData,2);
if( HAL_UART_Receive_DMA(&UartHandle,pRxData,2)==HAL_OK); // UART-DMA-IT Example
{
while (UartReady != SET) // wait for msg len
{
HAL_GPIO_WritePin(LED_PORT,LED_GREEN,1);
HAL_Delay(20);
HAL_GPIO_WritePin(LED_PORT,LED_GREEN,0);
HAL_Delay(400);
}
UartReady = RESET; // reset transmition flag
LEN = pRxData[0]+pRxData[1]*256; // calc msg length
if ( LEN > 0 )
{
pTxData[0]= 25;//LEN; // ACK
//HAL_UART_Transmit_IT(&UartHandle,pTxData,1);
if(HAL_UART_Transmit_DMA(&UartHandle,pTxData,1)==HAL_OK);
{
while (UartReady != SET)
{
HAL_Delay(1);
}
UartReady = RESET;
if(HAL_UART_Receive_IT(&UartHandle,pRxData,LEN)==HAL_OK)
{
//HAL_UART_Receive_DMA(&UartHandle,pRxData,LEN);
while (UartReady != SET) // waiting loop for msg
{
HAL_GPIO_WritePin(LED_PORT,LED_GREEN,1);
HAL_Delay(300);
HAL_GPIO_WritePin(LED_PORT,LED_GREEN,0);
HAL_Delay(50);
}
UartReady = RESET;
if (MBU_StrCompareNC(pRxData,"LED_ON_BLUE",LEN)==0)
{
HAL_GPIO_WritePin(LED_PORT,LED_BLUE,1);
}
if (MBU_StrCompareNC(pRxData,"LED_OFF_BLUE",LEN)==0)
{
HAL_GPIO_WritePin(LED_PORT,LED_BLUE,0);
}
if (MBU_StrCompareNC(pRxData,"LED_ON_RED",LEN)==0)
{
HAL_GPIO_WritePin(LED_PORT,LED_RED,1);
}
if (MBU_StrCompareNC(pRxData,"LED_OFF_RED",LEN)==0)
{
HAL_GPIO_WritePin(LED_PORT,LED_RED,0);
}
}
HAL_UART_Abort(&UartHandle);
}
}
}
}
}

Why "HAL_I2C_Master_Transmit" writes 2 bytes of data in each loop

I'm using HAL library for stm32f4 (V1.7.1) and trying to understand how HAL_I2C_Master_Transmit works. This function transmits a "master to slave" packet on SDA line.
In the stm32f4xx_hal_i2c.c code after sending slave address there is a loop (while(hi2c->XferSize > 0U)) that sends bytes which we want to be transmitted to slave. This loop works until all bytes are transmitted. But there is a question that "why the function wants to transmit TWO bytes in each loop?" There is an IF in loop (if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET) && (hi2c->XferSize != 0U))) that checks if the previous byte is currently transferred then send the the next byte!
I don't know what is the reason of existing this IF when in the next loop other bytes can be transmitted?!
HAL_StatusTypeDef HAL_I2C_Master_Transmit(I2C_HandleTypeDef *hi2c, uint16_t
DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout)
{
uint32_t tickstart = 0x00U;
/* Init tickstart for timeout management*/
tickstart = HAL_GetTick();
if(hi2c->State == HAL_I2C_STATE_READY)
{
/* Wait until BUSY flag is reset */
if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET,
I2C_TIMEOUT_BUSY_FLAG, tickstart) != HAL_OK)
{
return HAL_BUSY;
}
/* Process Locked */
__HAL_LOCK(hi2c);
/* Check if the I2C is already enabled */
if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
{
/* Enable I2C peripheral */
__HAL_I2C_ENABLE(hi2c);
}
/* Disable Pos */
hi2c->Instance->CR1 &= ~I2C_CR1_POS;
hi2c->State = HAL_I2C_STATE_BUSY_TX;
hi2c->Mode = HAL_I2C_MODE_MASTER;
hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
/* Prepare transfer parameters */
hi2c->pBuffPtr = pData;
hi2c->XferCount = Size;
hi2c->XferOptions = I2C_NO_OPTION_FRAME;
hi2c->XferSize = hi2c->XferCount;
/* Send Slave Address */
if(I2C_MasterRequestWrite(hi2c, DevAddress, Timeout, tickstart) != HAL_OK)
{
if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
{
/* Process Unlocked */
__HAL_UNLOCK(hi2c);
return HAL_ERROR;
}
else
{
/* Process Unlocked */
__HAL_UNLOCK(hi2c);
return HAL_TIMEOUT;
}
}
/* Clear ADDR flag */
__HAL_I2C_CLEAR_ADDRFLAG(hi2c);
while(hi2c->XferSize > 0U)
{
/* Wait until TXE flag is set */
if(I2C_WaitOnTXEFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
{
if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
{
/* Generate Stop */
hi2c->Instance->CR1 |= I2C_CR1_STOP;
return HAL_ERROR;
}
else
{
return HAL_TIMEOUT;
}
}
/* Write data to DR */
hi2c->Instance->DR = (*hi2c->pBuffPtr++);
hi2c->XferCount--;
hi2c->XferSize--;
if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET) && (hi2c->XferSize != 0U))
{
/* Write data to DR */
hi2c->Instance->DR = (*hi2c->pBuffPtr++);
hi2c->XferCount--;
hi2c->XferSize--;
}
/* Wait until BTF flag is set */
if(I2C_WaitOnBTFFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
{
if(hi2c->ErrorCode == HAL_I2C_ERROR_AF)
{
/* Generate Stop */
hi2c->Instance->CR1 |= I2C_CR1_STOP;
return HAL_ERROR;
}
else
{
return HAL_TIMEOUT;
}
}
}
/* Generate Stop */
hi2c->Instance->CR1 |= I2C_CR1_STOP;
hi2c->State = HAL_I2C_STATE_READY;
hi2c->Mode = HAL_I2C_MODE_NONE;
/* Process Unlocked */
__HAL_UNLOCK(hi2c);
return HAL_OK;
}
else
{
return HAL_BUSY;
}
}

STM32 - RS485 request-response

I am trying to send request to measuring device and receive it's response using UART with interrupts. However communication is unstable, I am receiving incomplete or corrupted responses. I am not sure but I think it's because switching driver enable signal. Could you look at my code and give me some advice what am I doing wrong?
Here is my code:
int main(void){
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART3_UART_Init();
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_9,GPIO_PIN_SET); //RS 485 transmit mode
while (1)
{
if(HAL_UART_Transmit_IT(&huart3, (uint8_t*)aTxBuffer, 2) != HAL_OK)
{
while(1);
}
while (UartReady != SET);
UartReady = RESET;
if(HAL_UART_Receive_IT(&huart3, (uint8_t*)aRxBuffer, 4) != HAL_OK)
{
while(1);
}
while (UartReady != RESET);
//do somethink with received data
}
}
Here are my callback functions:
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *UartHandle)
{
/* Set transmission flag: transfer complete */
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_9,GPIO_PIN_RESET); //RS 485 receive mode
//DataRecieved = 0;
UartReady = SET;
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle)
{
/* Set transmission flag: transfer complete */
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_9,GPIO_PIN_SET); //RS 485 transmit mode
//DataRecieved = 1;
UartReady = SET;
}
Thank you very much
The data you transmit, does it also appear at your RX-buffer, while you expect it not to?
I am receiving incomplete or corrupted responses. I am not sure but I
think it's because switching driver enable signal. Could you look at
my code and give me some advice what am I doing wrong?
Not sure, but maybe it would help to toggle the DE pin not in the interrupt callback functions, and instead switch to "transmit mode" right before invoking HAL_UART_Transmit_IT() and then to switch back to receive mode after the transmission, or even after calling HAL_UART_Receive_IT(), to make sure it will catch the incoming bytes. Tiny delays do matter sometimes:
void set_transmit_mode(void)
{
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_9, GPIO_PIN_SET); // RS 485 transmit mode
}
void set_receive_mode(void)
{
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_9, GPIO_PIN_RESET); // RS 485 receive mode
}
{
//...
set_transmit_mode();
if (HAL_UART_Transmit_IT(&huart3, (uint8_t*)aTxBuffer, 2) != HAL_OK) {
// ...
}
while (UartReady != SET);
//set_receive_mode();
UartReady = RESET;
if (HAL_UART_Receive_IT(&huart3, (uint8_t*)aRxBuffer, 4) != HAL_OK) {
// ...
}
set_receive_mode();
while (UartReady != RESET);
//...
}

Get Xbee response from Serial and send to a browser

I am trying to do some experiments with Arduino, Ethernet Shield and Xbee Shield.
I demonstrate my set up board like this:
Group 1: Arduino Uno + Xbee shield : broadcast the signal
Group 2: Arduino Uno + Xbee shield + Ethernet shield: receive the signal from
group 1, get the signal strength from AT command and print it into the browser.
The problem here is I can't get the result after sending to the Serial my ATDB command, actually, I am not sure it did worked as I expected.
Here is the code that I used to retrieve the signal strength.
int data;
void setup()
{
Serial.begin(9600);
}
void receiver_checker(){
delay(1200);
Serial.print("+++");
delay(1200);
bool bOK = false;
while (Serial.available() > 0) {
Serial.write(Serial.read());
bOK = true;
}
if(bOK)
{
Serial.println();
Serial.println("ATDB");
delay(100);
while (Serial.available() > 0) {
Serial.write(Serial.read());
}
Serial.println();
}
Serial.println();
}
void loop()
{
while(Serial.available() > 0){
data = Serial.read();
if(data == '1'){
// Broadcaster 1
//Serial.println("1------------------");
receiver_checker();
}
}
}
This part worked as I expected, it printed out in hex number the signal strength of the last package that it received.
Here is the code I combined the previous one and the server part from Web Server tutorial:
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0xCA, 0xFE, 0x00, 0x00, 0x00, 0x02
};
IPAddress ip(1, 1, 1, 2);
int data;
int count = 0;
char result;
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
// Serial.print("server is at ");
// Serial.println(Ethernet.localIP());
}
void loop() {
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
// Serial.println("new client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
// Serial.write(c);
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close"); // the connection will be closed after completion of the response
client.println("Refresh: 5"); // refresh the page automatically every 5 sec
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
if (Serial.available() > 0) {
// read the oldest byte in the serial buffer:
data = Serial.read();
// if it's a capital H (ASCII 72), turn on the LED:
if (data == '1') {
count += 1;
client.print("The number of times:");
client.print(count);
result = receiver_checker();
client.print("========================");
client.print(result);
client.print("========================");
}
}
client.println("</html>");
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
Serial.println("client disconnected");
}
}
char receiver_checker(){
char signal;
delay(1200);
Serial.print("+++");
delay(1200);
bool bOK = false;
while (Serial.available() > 0) {
Serial.write(Serial.read());
bOK = true;
}
if(bOK)
{
Serial.println();
Serial.println("ATDB");
delay(100);
while (Serial.available() > 0) {
signal = Serial.read();
}
Serial.println();
}
Serial.println();
return signal;
}
If there is another way to interact with the Xbee shield not go through Serial like I ask and get response directly, please let me know!
Your receiver_checker() function is reading characters back from the XBee module, and just returning the last character received, which is likely a carriage return or line feed.
Update the function to return an int, and replace your while (Serial.available() > 0) with the following:
signal = (int) strtoul(Serial.readString().c_str(), 0, 16);
That's for when the XBee returns a hexadecimal value. If it's returning a decimal value, change the 16 parameter to a 10.