Hello Im trying to connect a ESP8266 to a STM32 via UART.
I want to use the STM32 to send AT commands to the ESP, so that the ESP can do HTTP GET requests.
The code down here works when I use my PC to send data via FTDI to the STM, then the interrupt is called everytime a byte is recieved.
But when i hook up the ESP8266 I don't get a response even thought im sending AT\r\n;
Also i just connected everything like so:
SMT32 Tx -- ESP Rx
ESP Tx -- FTDI Rx
And I sended the AT\r\n command from the SMT to the ESP and on my pc in putty I got the OK command back, so the ESP is responding. But the interrupt is not called.
#include "main.h"
#include "usb_device.h"
#include "usbd_cdc_if.h"
UART_HandleTypeDef huart1;
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART1_UART_Init(void);
char Rx_Byte[1]; // Creating a single byte buffer
uint8_t Rx_Buffer[256]; // Full buffer
uint8_t Rx_Buffer_Index = 0;
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart){
HAL_UART_Receive_IT(&huart1, (uint8_t *)Rx_Byte, 1);
memcpy(Rx_Buffer + Rx_Buffer_Index, Rx_Byte, 1);
Rx_Buffer_Index++;
if (*Rx_Byte == '\r' || *Rx_Byte == '\n') {
// I could send data via putty and FTDI, and when enter was pressed it printed the data in the buffer back to you via uart
// HAL_UART_Transmit(&huart1, (uint8_t *) Rx_Buffer, Rx_Buffer_Index, 1000);
memset(Rx_Buffer, 0, sizeof(Rx_Buffer)); // Clear buffer
Rx_Buffer_Index = 0;
}
}
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART1_UART_Init();
MX_USB_DEVICE_Init();
char echo_off[] = "ATE0\r\n";
char data[] = "AT\r\n";
HAL_Delay(1000);
HAL_UART_Transmit(&huart1, (uint8_t*)echo_off, sizeof(echo_off), 10);
while (1)
{
HAL_GPIO_TogglePin(GPIOA, LED_1_Pin);
HAL_UART_Transmit(&huart1, (uint8_t*)data, sizeof(data), 1000);
HAL_Delay(500);
}
}
Related
I have 2 LoRa modules. I want to use one of them as receiver and the other one as transmitter. I can do this with 2 Arduino Unos, but I want to use one STM32 board as transmitter and Arduino Uno board as receiver. I am using an Ebyte LoRa module, E32433T20D.
I set the receiver like this :
{0x00, 0x01, 0x1A, 0x17, 0xC0} ;
Here is my Arduino transmitter code.
#define PIN_SERIAL1_TX (0u)
#define PIN_SERIAL1_RX (1u)
#define M0 14
#define M1 15
void setup() {
pinMode(M0, OUTPUT);
pinMode(M1, OUTPUT);
digitalWrite(M0, LOW);
digitalWrite(M1, LOW);
Serial1.begin(9600); // UART initialize I am using RP2040 with Arduino code
}
void loop() {
Serial1.write((byte)0x00); // Receiver address
Serial1.write(0x01); // Receiver address
Serial1.write(0x17); // Receiver channel = 0x17 = 23 (410M+23=433 MHz)
Serial1.println("123");
delay(500);
}
This code can transmit "123" without any problem. When I try this in C:
#include "main.h"
UART_HandleTypeDef huart1;
UART_HandleTypeDef huart2;
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART2_UART_Init(void); //debug
static void MX_USART1_UART_Init(void); //Lora
/* USER CODE BEGIN 0 */
uint16_t readValue;
uint8_t charToTransmit[1];
/* USER CODE END 0 */
int main(void) {
HAL_Init();
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART2_UART_Init(); //DEBUG
MX_USART1_UART_Init();
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_5,0); //M0
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_7,0); //M1
HAL_Delay(100);
uint8_t config[]= {0x00,0x01,0x1A,0X17,0XC0};
/* USER CODE BEGIN WHILE */
while (1) {
charToTransmit[0] = 1 ;
HAL_UART_Transmit(&huart1,config,sizeof(config),0);
HAL_Delay(500);
HAL_UART_Transmit(&huart1,charToTransmit,1,100);
HAL_Delay(500);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
// There is HAL library initialization over here
This code is not transmitting or my receiver is not receiving, I don't know, but I can't see "1" on my receiver. Since I am a beginner in C, is my UART protocol wrong or something?
Here is my receiver program, I wrote it in the Arduino IDE too.
#include <Arduino.h>
#define PIN_SERIAL1_TX (0u)
#define PIN_SERIAL1_RX (1u)
#define M0 14
#define M1 15
char rc;
void setup() {
Serial.begin(115200);
Serial1.begin(9600);
pinMode(M0, OUTPUT);
pinMode(M1, OUTPUT);
digitalWrite(M0, LOW);
digitalWrite(M1, LOW);
}
void loop() {
while (Serial1.available()) {
rc=Serial1.read();
Serial.print(rc);
}
}
I am doing an autonomous car project, I need manual control as well as an autonomous function, so the manual control is done through wifi using "gesture control" and for the autonomous control I want to send the location data via an HTTP request, but then I have to switch to an STA mode, so I want to use a Toggle switch connected to ground and a pin as input and put it in an "if statement", but all of the initial setups come in the void setup(), so I don't know how to proceed I have provided the AP part of the main code I am using
#include <esp_now.h>
#include <WiFi.h>
#include <esp_wifi.h>
#include <TinyGPS++.h> // Tiny GPS Plus Library
#define CHANNEL 4
uint8_t mac[] = {0x36, 0x33, 0x33, 0x33, 0x33, 0x33};
struct __attribute__((packed)) DataStruct {
//char text[32];
int x;
int y;
unsigned long time;
};
DataStruct myData;
//**********************************************
// GPS Locations
unsigned long Distance_To_Home; // variable for storing the distance to destination
int ac =0; // GPS array counter
int wpCount = 0; // GPS waypoint counter
double Home_LATarray[50]; // variable for storing the destination Latitude - Only Programmed for 5 waypoint
double Home_LONarray[50]; // variable for storing the destination Longitude - up to 50 waypoints
int increment = 0;
#define autopilot 13
void gesturecontroll();
void getGPS();
void getCompass();
void setWaypoint();
void move();
int blueToothVal;
void setup()
{ Serial.begin(9600); // Serial 0 is for communication with the computer
S2.begin(9600); // Serial 2 is for GPS communication at 9600 baud - DO NOT MODIFY - Ublox Neo 6m
Serial.println("ESPNow/Basic/Slave Example");
//Set device in AP mode to begin with
WiFi.mode(WIFI_AP);
// configure device AP mode
// This is the mac address of the Slave in AP Mode
esp_wifi_set_mac(ESP_IF_WIFI_STA, &mac[0]);
Serial.print("AP MAC: "); Serial.println(WiFi.softAPmacAddress());
// Init ESPNow with a fallback logic
if (esp_now_init()!=0) {
Serial.println("*** ESP_Now init failed");
while(true) {};
}
// Once ESPNow is successfully Init, we will register for recv CB to
// get recv packer info.
esp_now_register_recv_cb(OnDataRecv);
Serial.print("Aheloiioi");
// Extras////////////////////////////////////////////////////////////////////////////////////
pinMode(autopilot, INPUT);
}
void OnDataRecv(const uint8_t *mac_addr, const uint8_t *data, int data_len) {
memcpy(&myData, data, sizeof(myData));
char macStr[18];
snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x",
mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
Serial.print("Last Packet Recv from: "); Serial.println(macStr);
Serial.print("Last Packet Recv Data: ");
Serial.println(myData.x);
Serial.println(myData.y);
Serial.println("");
Serial.println();
//move();
Serial.println();
}
//********************************************************************************************************
// Main Loop
void loop()
{ if (autopilot == HIGH)
{
// going for manual control, BUT WHAT SHOULD I PUT HERE?
}
else
{
getGPS(); // Update the GPS location
getCompass(); // Update the CompaSerial Heading
Ping(); // Use at your own discretion, this is not fully tested
}
}
I'm beginer in STM32, i have a project and need to receive data from another device like arduino, and now I try transmit data from UART 3 and I receive data with UART 1. but I can't get any data. I connect TX uart 3 to RX uart 1 and TX uart 1 to RX uart 3.
/* USER CODE BEGIN PV */
int i = 0;
char bufferReceive[6], bufferTransmit[10];
/* USER CODE END PV */
/* USER CODE BEGIN 0 */
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if (huart->Instance == USART1) //current UART
{
HAL_UART_Receive_IT(&huart1, (uint8_t*)bufferReceive, 1); //activate UART receive interrupt every time
}
}
/* USER CODE END 0 */
int main(void)
{
HAL_UART_Receive_IT(&huart1, (uint8_t*)bufferReceive, 1);
while (1)
{
/* USER CODE END WHILE */`enter code here`
sprintf(bufferTransmit,"%d\n",i);
HAL_UART_Transmit(&huart3, (uint8_t*)bufferTransmit, sizeof(bufferTransmit), 1000);
HAL_Delay(500);
i++;
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
First thing your code is messy and hard to read.
Second, you are missing your entire initialization region before while(1) so your system shouldn't be doing anything.
/* USER CODE BEGIN PV */
int i = 0;
char bufferReceive[6], bufferTransmit[10];
/* USER CODE END PV */
/* USER CODE BEGIN 0 */
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if (huart->Instance == USART1)
{
HAL_UART_Receive_IT(&huart1, (uint8_t*)bufferReceive, 1);
}
}
/* USER CODE END 0 */
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART3_UART_Init();
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */
HAL_UART_Receive_IT(&huart1, (uint8_t*)bufferReceive, 1);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
sprintf(bufferTransmit,"%d\n",i);
HAL_UART_Transmit(&huart3, (uint8_t*)bufferTransmit, sizeof(bufferTransmit), 1000);
HAL_Delay(500);
i++;
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
You are receiving one byte at HAL_UART_Receive_IT(&huart1, (uint8_t*)bufferReceive, 1);.
Which does not wait it simply moves onward.
Next, you transmit bufferTransmit which then causes UART1 to receive one byte and continues to receive all bytes one by one in the same memory location.
It seems you are not using i properly as you probably want something like HAL_UART_Receive_IT(&huart1, (uint8_t*)&bufferReceive[i], 1);
You do not initialize anything. Peripherals, pins, clocks etc have to be set up before they can work.
I am working with GSM (sim900a) Module interfaced with PIC18F4520 Microcontroller.
Here I am trying to send SMS through PIC controller serially to GSM Module but unable to receive any response from GSM and not receiving any Messages.
When I am trying to connect only GSM Module to Hyperterminal then I am able to send SMS. In the similar fashion when I am trying to send AT commands from PIC to HYPERTERMINAL then I am receiving Commands Serially.
Code Below
#include<p18f4520.h> /* Header File */
#include<string.h>
void delay_ms(int ms);
void Data(char Value);
void Cmd(char Value);
char temp;
void main()
{
char tocheck[] ="AT";/*Basic command to test GSM is working or not */
char sendingsmsmode[]="AT+CMGF=1";/* To change the GSM Module in SMS
sending Mode */
char newsms[]="AT+CMGS=918500923915";/*Set Text mode for SMS */
char msg[]="WelcometoGSM";/* The text which you want to send */
char terminator=0x1A;
int i=0;
TRISC = 0x80; /* RC6=0 (O/P) RC7=1(I/P) */
SPBRG = 0x33; /* Serial Port Baud rate Generator (9600) */
TXSTA = 0X24; /* Transmission Enabling(TXEN=1,SYNC=0,BRGH=1)
*/
RCSTA = 0X90; /* Rception Enabling (SPEN=1,CREN=1) */
TRISC=0X00; /* (RC1,RC0 ->O/P Setting by Zero)
*/
TRISD=0X00; /* PORTD (0 - 7)pins Config as Output
*/
while(tocheck[i]!='\0'){
while(TXSTAbits.TRMT==0);
TXREG=tocheck[i];
delay_ms(30);
i++;
}
i=0;
while(sendingsmsmode[i]!='\0'){
while(TXSTAbits.TRMT==0);
TXREG=sendingsmsmode[i];
delay_ms(30);
i++;
}
i=0;
while(newsms[i]!='\0'){
while(TXSTAbits.TRMT==0);
TXREG=newsms[i];
delay_ms(30);
i++;
}
i=0;
while(msg[i]!='\0'){
while(TXSTAbits.TRMT==0);
TXREG=msg[i];
delay_ms(30);
i++;
}
TXREG=terminator;
delay_ms(3000);
while(1);
}
void Cmd(char Value)
{
PORTD=Value;
PORTCbits.RC1=0; /* RC1=0(RS=0) [Command Registr
Selection]) */
PORTCbits.RC0=0; /* RC0=0(R/W=0) [Write Process])
*/
PORTCbits.RC2=1; /* RC2=1(Enable=1) [Enable Line ON]
*/
delay_ms(4); /* Minimun Delay For Hold On Data
*/
PORTCbits.RC2=0; /* RC2=0(Enable=0) [Enable Line OFF]
*/
}
void Data(char Value)
{
PORTD=Value;
PORTCbits.RC1=1; /* RC1=1(RS=1) [Data Registr
Selection]) */
PORTCbits.RC0=0; /* RC0=0(R/W=0) [Write Process])
*/
PORTCbits.RC2=1; /* RC2=1(Enable=1) [Enable Line ON]
*/
delay_ms(4); /* Minimun Delay For Hold On Data
*/
PORTCbits.RC2=0; /* RC2=0(Enable=0) [Enable Line OFF]
*/
}
void delay_ms(int ms)
{
int i,count;
for(i=1;i<=ms;i++)
{
count=498;
while(count!=1)
{
count--;
}
}
}
You need to send CR or CR+LF (characters number 13 and 10 respectively) after every string you send to the modem.
When using a terminal emulator like HyperTerminal, you press the Enter key, right? If you don't press that key, the modem does not know that the command is terminated and must be executed. The same must be done in your firmware.
There is more: you must allow some delay, and you should read back modem responses, but this could be a second step; first of all, to check the setup is working at the base level, you must send an "AT", followed by a CR, then see if the modem replies with OK (+ CR and LF...).
Modify
char tocheck[] ="AT";
in
char tocheck[] ="AT\r"; // the \r is the CR (carriage return)
and you will see that the modem will reply, if all is connected well.
I m using dspic33f series micro controller to receive SMS from modem SIM800A. I m using interrupt method to receive response from the modem using serial communication . If I send more than one AT command to modem via UART sequentially I'm getting response into the recieve buffer only for 1st command and no response in the receive buffer for the remaining commands though the modem was responding.
After debugging I found the reason for this peculiar behavior. The reason was I was clearing the receive buffer after receiving the response into the buffer and printing on the console i.e. before sending next command to modem via UART using memset function in c.
But on commenting this memset function i was able receive the response into the receive buffer for the all the AT commands that was sent sequentially, if the memset function is not commented then no response is filled in the receive buffer though the modem was responding so please help out in receiving the response into the buffer.
Code which i have written
#include "p33FJ64GS606.h"
#include <stdio.h>
#define FCY 40000000UL
#include <libpic30.h>
#include <string.h>
_FOSC(FCKSM_CSECMD & OSCIOFNC_OFF)
_FWDT(FWDTEN_OFF)
_FPOR(FPWRT_PWR128 )
_FICD(ICS_PGD1 & JTAGEN_OFF)
char StringLoop[161];
void Init_Uart1(int uart_no)
{
U1MODEbits.STSEL = 0; // 1-Stop bit
U1MODEbits.PDSEL = 0; // No Parity, 8-Data bits
U1MODEbits.ABAUD = 0; // Auto-Baud disabled
U1MODEbits.BRGH = 0; // Standard-Speed mode
U1BRG = UBRG1_VALUE; // Baud Rate setting for 115200
U1STAbits.UTXISEL0 = 0; // Interrupt after one TX character is transmitted
U1STAbits.UTXISEL1 = 0;
U1STAbits.URXISEL0 = 0;
U1STAbits.URXISEL1 = 0;//jkv
IEC0bits.U1RXIE = 1;
IEC0bits.U1TXIE = 0; // Enable UART TX interrupt
U1MODEbits.UARTEN = 1; // Enable UART
U1STAbits.UTXEN = 1; // Enable UART TX
U1MODEbits.USIDL=0;
}
void UART1_puts(unsigned char data)
{
while (U1STAbits.TRMT==0);
U1TXREG = data;
}
void UART1_send(unsigned char *s)
{
memset(StringLoop,'\0',sizeof(StringLoop)); /* if I comment this line then i can receive the response into the buffer StringLoop continuously else i receive the response only for the 1st AT command and I don't get the response for other AT commands into the StringLoop though the modem is responding*/
while(*s)
{
UART1_puts(*s);
s++;
}
}
void __attribute__((interrupt, no_auto_psv)) _U1RXInterrupt(void)
{
if(IFS0bits.U1RXIF)
{
StringLoop[rcindex++] = U1RXREG;
if (rcindex >= (sizeof(StringLoop) - 1))
rcindex = 0;
}
IFS0bits.U1RXIF=0;
}
}
void main()
{
int i=0,j=0;
Init_Clocks();
Init_Uart2(1);
Init_Uart1(1);
TRISFbits.TRISF1=0;
LATFbits.LATF1=1;
UART1_send("AT+CMGR=1\r\n");
__delay_ms(2000);
printf("stringloop is %s\n",StringLoop);
UART1_send("AT+CPMS=?\r\n");
__delay_ms(2000);
printf("stringloop is %s\n",StringLoop);
UART1_send("AT+CPMS?\r\n");
__delay_ms(2000);
printf("stringloop is %s\n",StringLoop);
}