ComputerAngel
Junior Member level 2
Hello, I am working on automatic irrigation system with gsm notification. I used a potentiometer to step down the voltge level to 2.9v for the gsm module. When I run the codes below however, the microcontroller(PIC16F887) outputs the "AT" commands on my LCD instead of pushing them to the UART PORT and as such the gsm module is unable to send sms. I need help
Code:
#include<stdio.h>
// LCD module connections
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// End LCD module connections
unsigned int temp;
unsigned int humi;
float h;
float a;
float hs;
float r_per;
float attemp ;
char humidity [4];
char msg1[] = "System failure.";
char msg[] = "Humidity level normalized";
char number[] = "AT+CMGS=\"+2348187848800\"";
char car[] = "AT+CSCS=\"GSM\"";
char start[] = "AT+CMGF=1";
void write_lcd()
{
Lcd_Cmd(_LCD_CLEAR);
Delay_ms(300);
Lcd_Out(1,1, "Initializing....");
Delay_ms(5000);
Lcd_Cmd(_LCD_CLEAR);
Delay_ms(500);
Lcd_Out(1, 1, "Initialization");
Lcd_Out(2, 3, "Complete");
Delay_ms(5000);
Lcd_Cmd(_LCD_CLEAR);
Delay_ms(200);
}
void main(){
ADC_Init();
UART1_Init(9600);
ANSEL = 0;
ANSELH = 0;
Lcd_Init();
Lcd_Cmd(_LCD_CURSOR_OFF);
TRISA0_bit = 1;
TRISD = 0;
TRISC = 0;
RD2_bit =0;
RC3_bit = 0;
RC2_bit = 0;
RC1_bit = 0;
RC0_bit = 0;
Delay_ms(200);
RC3_bit = 1; //This sets the blue LED on system boot.
Delay_ms(100);
write_lcd();
RC3_bit = 0; //after system boot it is then turned off.
Delay_ms(500);
while(1) {
humi = ADC_Get_Sample(1);
h = (humi*5000)/1023;
hs = h/1000;
r_per = hs*20; //
a = (100-r_per);
FloatToStr(a, humidity);
humidity[4] = 0;
Lcd_Cmd(_LCD_CLEAR);
Delay_ms(500);
Lcd_Out(1, 1, "Irrigation Init");
Lcd_Out(2,1, "Humidity:");
Lcd_Out(2,12, humidity);
Delay_ms(3000);
if(hs >= 2)
{
//RC3,RC2,RC1,RC0 ==red led, green led, blue led and water sensor
RC3_bit = 1; //Turn ON Blue LED
RD2_bit = 1; //Turn ON the water pump
Delay_ms(9000);
if(RC4_bit == 1) //after turning ON the water sensor, check if it's despencing
{
UART1_Write_Text("AT");
UART1_Write(0x0D);
Delay_ms(5000);
UART1_Write_Text("ATE0");
UART1_Write(0x0D);
Delay_ms(100);
UART1_Write_Text(start);
UART1_Write(0x0D);
Delay_ms(100);
UART1_Write_Text(car);
UART1_Write(0x0D);
Delay_ms(100);
UART1_Write_Text(number);
UART1_Write(0x0D);
Delay_ms(100);
UART1_Write_Text(msg);
UART1_Write(0x0D);
Delay_ms(100);
UART1_Write(0x1A);
Delay_ms(100);
RC3_bit = 1;
Delay_ms(1500);
RC3_bit = 0;
Delay_ms(4000);
}
else
{
UART1_Write_Text("AT");
UART1_Write(0x0D);
Delay_ms(5000);
UART1_Write_Text("ATE0");
UART1_Write(0x0D);
Delay_ms(100);
UART1_Write_Text(start);
UART1_Write(0x0D);
Delay_ms(100);
UART1_Write_Text(car);
UART1_Write(0x0D);
Delay_ms(100);
UART1_Write_Text(number);
UART1_Write(0x0D);
Delay_ms(100);
UART1_Write_Text(msg1);
UART1_Write(0x0D);
Delay_ms(100);
UART1_Write(0x1A);
Delay_ms(3000);
RC3_bit = 1;
Delay_ms(2000);
RC3_bit = 0;
RD2_bit = 0; //Shutdown the water pump
Delay_ms(2000);
}
}
if(hs < 2)
{
RD1_bit = 0;
RC3_bit = 0;
RD2_bit = 0; //turn OFF the water pump.
RC2_bit = 1; //Turn ON the green LED, system is normal
Lcd_Cmd(_LCD_CLEAR);
Delay_ms(500);
Lcd_Out(1, 1, "Irrigation Sys");
Lcd_Out(2,1, "Humidity: ");
Lcd_Out(2, 11, Humidity);
Delay_ms(5000);
Lcd_Cmd(_LCD_CLEAR);
Delay_ms(500);
Lcd_Out(1, 1, "Onas Irrigation ");
Lcd_Out(2,1, "Sys Stat: Good ");
Delay_ms(5000);
}
}
}