Hello Guys,
This is John a newbie in Arduino programming. Seeking your advice and help with my project work. I am working to create a web server with Basic Authentication and GSM to send messages when the button is press in the webserver. I also implemented automation in this project, wherein if it reached the set temperature and battery level, it would send a message to the recipient. Upon compiling and uploading the codes, i did not encounter any errors when i uploaded it to the Atmega324. However, the webserver is not showing up, and even the other functions are not working, like basic authentication. The web server cannot be reached. Here's the code for reference.
//For ethernet
#include <SPI.h>
#include <Ethernet.h>
//For relay
#include <Wire.h>
#include <Relay.h>
//For Temp
#include <OneWire.h>
#include <DallasTemperature.h>
//For GSM
#include <SoftwareSerial.h>
//For GSM---------------------------------------------------------------------------------
SoftwareSerial mySerial(2, 3);
char Rx_data[150];
unsigned char Rx_index = 0;
int i = 0;
char msg[160];
int sig;
#define DEBUG true
//-----------------------------------------------------------------------------------------
int passFlag = 0;
int passFlag1 = 0;
int passFlag2 = 0;
int passFlag3 = 0;
//Millis timeout---------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------
//Batt pin---------------------------------------------------------------------------------
#define SIGNAL_PIN A0 //battery level monitoring using 4.7K & 2.2K ohm resistor
//-----------------------------------------------------------------------------------------
//Temp Pin---------------------------------------------------------------------------------
#define ONE_WIRE_BUS 4
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
float temp;
//-----------------------------------------------------------------------------------------
//Batt computation-------------------------------------------------------------------------
float adc_voltage = 0.0;
float in_voltage = 0.0;
float R1 = 30000.0;
float R2 = 7500.0;
float ref_voltage = 5.0;
int adc_value = 0;
//-----------------------------------------------------------------------------------------
//Ethernet---------------------------------------------------------------------------------
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,18, 12);
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
//-----------------------------------------------------------------------------------------
// Relay state and pin---------------------------------------------------------------------
String relay1State1 = "ON";
String relay2State2 = "ON";
const int TempCon= 6;
const int ChgRlyCon = 7;
//-----------------------------------------------------------------------------------------
// Client variables------------------------------------------------------------------------
char linebuf[80];
int charcount=0;
boolean authentificated=false;
//-----------------------------------------------------------------------------------------
void dashboardPage(EthernetClient &client) {
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connnection: close");
client.println();
client.println("<!DOCTYPE HTML><html>");
client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"></head><body>");
client.println("<center><a href=\"/\">Refresh</a>");
client.println("<h3>SERVER TEMPERATURE</a></h3>");
client.println("<br>");
client.println(temp);
client.println("</br>");
// Generates buttons to control the relay
client.println("<a href=\"/ACON\"><button>ON</button></a>  ");
client.println("<a href=\"/ACOFF\"><button>OFF</button></a>");
client.println("<h3>SERVER BATTERY LEVEL</h3>");
client.println("<br>");
client.println(in_voltage);
client.println("</br>");
// Generates buttons to control the relay
client.println("<a href=\"/DCON\"><button>ON</button></a>  ");
client.println("<a href=\"/DCOFF\"><button>OFF</button></a></center>");
client.println("</body></html>");
}
void SendAuthentificationpage(EthernetClient &auth)
{
auth.println("HTTP/1.1 401 Authorization Required");
auth.println("WWW-Authenticate: Basic realm=\"Secure Area\"");
auth.println("Content-Type: text/html");
auth.println("Connnection: close");
auth.println();
auth.println("<!DOCTYPE HTML>");
auth.println("<HTML> <HEAD> <TITLE>Error</TITLE>");
auth.println(" </HEAD> <BODY><H1>401 Unauthorized.</H1></BODY> </HTML>");
}
void setup()
{
// For GSM
Serial.begin(9600);
mySerial.begin(19200);
//---------------------------------------------------------------------------------------
//For ethernet
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
//---------------------------------------------------------------------------------------
//For relay pins
pinMode(ChgRlyCon, OUTPUT);
digitalWrite(ChgRlyCon, HIGH);
pinMode(TempCon, OUTPUT);
digitalWrite(TempCon, HIGH);
//---------------------------------------------------------------------------------------
}
//-------------------------------------------------------------------------------------------
void loop() {
//----GSM------------------------------------------------------------------------------------
if (mySerial.available())
Serial.write(mySerial.read());
if (Serial.available())
mySerial.write(Serial.read());
// listen for incoming clients
//------------------------------------------------------------------------------------------
//----TEMP SENSOR---------------------------------------------------------------------------
sensors.requestTemperatures(); // Send the command to get temperatures
temp = sensors.getTempCByIndex(0);
sensors.requestTemperatures(); // Send the command to get temperatures
temp = sensors.getTempCByIndex(0);
//------------------------------------------------------------------------------------------
//----BATT SENSOR---------------------------------------------------------------------------
// Why "byIndex"?
// You can have more than one IC on the same bus.
// 0 refers to the first IC on the wire
// Read the Analog Input
adc_value = analogRead(SIGNAL_PIN);
// Determine voltage at ADC input
adc_voltage = (adc_value * ref_voltage) / 1024.0;
// Calculate voltage at divider input
in_voltage = adc_voltage / (R2 / (R1 + R2)) ;
//------------------------------------------------------------------------------------------
//----BASIC AUTHENTICATION------------------------------------------------------------------
// listen for incoming clients
EthernetClient auth = server.available();
EthernetClient client = server.available();
if (auth) {
memset(linebuf,0,sizeof(linebuf));
charcount=0;
authentificated=false;
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (auth.connected()) {
if (auth.available()) {
char a = auth.read();
linebuf[charcount]=a;
if (charcount<sizeof(linebuf)-1) charcount++;
Serial.write(a);
// 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 (a == '\n' && currentLineIsBlank) {
if (authentificated)
dashboardPage(client);
else
SendAuthentificationpage(auth);
break;
}
if (a == '\n') {
// you're starting a new line
currentLineIsBlank = true;
if (strstr(linebuf,"Authorization: Basic")>0 && strstr(linebuf,"YWRtaW46ZGF5d2Fsa2VyMDAy")>0)
authentificated=true;
memset(linebuf,0,sizeof(linebuf));
charcount=0;
}
else if (a != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
}
//------------------------------------------------------------------------------------------
//----BUTTONS IN WEB------------------------------------------------------------------------
if (client) {
memset(linebuf,0,sizeof(linebuf));
charcount=0;
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
//read char by char HTTP request
linebuf[charcount]=c;
if (charcount<sizeof(linebuf)-1) charcount++;
// 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) {
dashboardPage(client);
break;
}
if (c == '\n') {
if (strstr(linebuf,"GET /ACON") > 0){
digitalWrite(TempCon, HIGH);
init_GSM();
send_msg("***********", "AC HAS BEEN TURNED ON BY ADMIN");
}
else if (strstr(linebuf,"GET /ACOFF") > 0){
digitalWrite(TempCon, LOW);
init_GSM();
send_msg("***********", "AC HAS BEEN TURNED OFF BY ADMIN");
}
if (strstr(linebuf,"GET /DCON") > 0){
digitalWrite(ChgRlyCon, HIGH);
init_GSM();
send_msg("***********", "CHARGING THE BATTERY HAS BEEN CONNECTED BY ADMIN");
}
if (strstr(linebuf,"GET /DCOFF") > 0){
digitalWrite(ChgRlyCon, LOW);
init_GSM();
send_msg("***********", "CHARGING THE BATTERY HAS BEEN DISCONNECTED BY ADMIN");
}
currentLineIsBlank = true;
memset(linebuf,0,sizeof(linebuf));
charcount=0;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
}
//------------------------------------------------------------------------------------------
//----TEMP CONDITION------------------------------------------------------------------------
if( temp >= 35){
digitalWrite(TempCon, HIGH);
if(passFlag == 0){
init_GSM();
send_msg("***********", "AIRCON HAS BEEN TURNED ON");
passFlag = 1;
passFlag1 = 0;
}
}
if (temp <= 34){
digitalWrite(TempCon, LOW);
if(passFlag1 == 0){
init_GSM();
send_msg("***********", "AIRCON HAS BEEN TURNED OFF");
passFlag1 = 1;
passFlag = 0;
}
//------------------------------------------------------------------------------------------
//----BATT CONDITION------------------------------------------------------------------------
//--------------------------- bms 0, batt lt 13.4v--------------
if(in_voltage >= 15){
digitalWrite(ChgRlyCon, LOW);
if(passFlag2 == 0){
init_GSM();
send_msg("***********", "CHARGING THE BATTERY HAS BEEN DISCONNECTED");
passFlag2 = 1;
passFlag3 = 0;
}
}
//--------------------------- bms 0, batt is charged at 13.4V -------
if(in_voltage <= 6){
digitalWrite(ChgRlyCon, HIGH);
if(passFlag3 == 0){
init_GSM();
send_msg("***********", "CHARGING THE BATTERY HAS BEEN CONNECTED");
passFlag3 = 1;
passFlag2 = 0;
}
}
}
//------------------------------------------------------------------------------------------
}
//----------------------------------------------------------------------------------------------
//---SEND GSM STRING----
void send_msg(char *number, char *msg)
{
char at_cmgs_cmd[30] = {
'\0' };
char msg1[160] = {
'\0' };
char ctl_z = 0x1A;
sprintf(msg1, "%s%c", msg, ctl_z);
sprintf(at_cmgs_cmd, "AT+CMGS=\"%s\"\r\n",number);
sendGSM(at_cmgs_cmd);
delay(100);
delay(100);
delay(100);
sendGSM(msg1);
delay(100);
}
void sendGSM(char *string){
mySerial.write(string);
delay(90);
}
void clearString(char *strArray) {
int j;
for (j = 100; j > 0; j--)
strArray[j] = 0x00;
}
void send_cmd(char *at_cmd, char clr){
char *stat = '\0';
while(!stat){
sendGSM(at_cmd);
delay(200);
readSerialString(Rx_data);
Serial.write(Rx_data);
stat = strstr(Rx_data, "OK");
Serial.println("Success");
delay(50000);
}
if (clr){
clearString(Rx_data);
delay(200);
stat = '\0';
}
}
void init_GSM(){
sendData("AT\r\n",1000,DEBUG); // AT
sendData("AT+CMGF=1\r\n",1000,DEBUG); //AT+CMGF=1
sendData("AT+CMGD=1\r\n",1000,DEBUG); //AT+CMGD=1
delay(1000);
delay(1000);
delay(1000);
}
void readSerialString (char *strArray) {
if(!mySerial.available()) {
return;
}
while(mySerial.available()) {
strArray[i] = mySerial.read();
i++;
}
}
String sendData(String command, const int timeout, boolean debug)
{
String response = "";
mySerial.print(command);
long int time = millis();
while( (time+timeout) > millis())
{
while(mySerial.available())
{
char c = mySerial.read();
response+=c;
}
}
if(debug)
{
Serial.print(response);
}
return response;
}
Seeking for your help and advice how to make this codes work.
Thank you.
