#include <LiquidCrystal_I2C.h>
#include <SoftwareSerial.h>
#include <EEPROM.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
SoftwareSerial sim800l(4, 5);
#define echoPin 2
#define trigPin 3
#define BUZZER 9
#define red_light_pin 12
#define green_light_pin 11
#define blue_light_pin 10
#define buttonPin 6
#include <SPI.h>
#include <Ethernet.h>
// replace the MAC address below by the MAC address printed on a sticker on the Arduino Shield 2
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
EthernetClient client;
int HTTP_PORT = 80;
String HTTP_METHOD = "GET";
char HOST_NAME[] = "192.168.1.2"; // change to your PC's IP address
String PATH_NAME = "/WLMS/arduino_insert.php";
String PATH_GETDATA = "/WLMS/arduino_getData.php";
String queryString = "?waterlevel=";
char data;
long duration;
int distance;
char inChar;
int waterHeight = 0;
bool buttonState = 0;
int addr = 0;
String _buffer;
int _timeout;
const unsigned long eventInterval = 5000;
unsigned long previousTime = 0;
String fetchedData = "";
void setup(){
lcd.init();
lcd.backlight();
lcd.begin(20, 4);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(BUZZER, OUTPUT);
digitalWrite(BUZZER, LOW);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(red_light_pin, OUTPUT);
pinMode(green_light_pin, OUTPUT);
pinMode(blue_light_pin, OUTPUT);
digitalWrite(red_light_pin,0);
digitalWrite(green_light_pin,0);
digitalWrite(blue_light_pin,0);
sim800l.begin(9600);
Serial.begin(9600);
_buffer.reserve(50);
// initialize the Ethernet shield using DHCP:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to obtaining an IP address using DHCP");
while(true);
}
sim800l.println("AT+CNMI = 2,2,0,0,0");
sim800l.print("AT+CMGF=1\r");
//end of the setup
}
void loop(){
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.0340 / 2;
float tankHeight = 0;
buttonState = digitalRead(buttonPin);
if (buttonState == LOW){
Serial.print("pressed");
for (int i = 0 ; i < EEPROM.length() ; i++) {
EEPROM.write(i, 0);
}
delay(200);
EEPROM.write(addr, distance);
}
tankHeight= EEPROM.read(addr);
float waterHeight = tankHeight - distance;
float eachHeight = (tankHeight - 13)/3;
float lv1 = eachHeight + 13;
float lv2 = lv1 + eachHeight;
float lv3 = lv2 +eachHeight;
Serial.print("Distance: ");
Serial.print(distance);
Serial.println("cm");
lcd.setCursor(0, 0);
lcd.print(" WATER LVL SYSTEM ");
lcd.setCursor(0, 2);
lcd.print("TANK HEIGHT ");
lcd.print(tankHeight);
lcd.print("cm");
lcd.setCursor(0, 3);
lcd.print("WATER HEIGHT ");
lcd.print(waterHeight);
lcd.print("cm");
// connect to web server on port 80:
String sendSensorData;
sendSensorData = queryString + waterHeight;
unsigned long currentTime = millis();
if(currentTime - previousTime >= eventInterval){
if(client.connect(HOST_NAME, HTTP_PORT)) {
// if connected:
Serial.println("Connected to server");
// make a HTTP request:
// send HTTP header
client.println(HTTP_METHOD + " " + PATH_NAME + sendSensorData + " HTTP/1.1");
client.println("Host: " + String(HOST_NAME));
client.println("Connection: close");
client.println(); // end HTTP header
while(client.connected()) {
if(client.available()){
// read an incoming byte from the server and print it to serial monitor:
char c = client.read();
Serial.print(c);
}
}
// the server's disconnected, stop the client:
client.stop();
Serial.println();
Serial.println("disconnected");
}else {// if not connected:
Serial.println("connection failed");
}
previousTime = currentTime;
}
**if(client.connect(HOST_NAME, HTTP_PORT)) {**
** fetchedData = "";**
** // if connected:**
** Serial.println("Connected to server");**
** // make a HTTP request:**
** // send HTTP header**
** client.println(HTTP_METHOD + " " + PATH_GETDATA + " HTTP/1.1");**
** client.println("Host: " + String(HOST_NAME));**
** client.println("Connection: close");**
** client.println(); // end HTTP header**
** while(client.connected()) {**
** if(client.available()){**
** // read an incoming byte from the server and print it to serial monitor:**
** data = client.read();**
** Serial.print(data);** //result of this is <<<{+639277349078,+639277442124}>>>
** fetchedData = fetchedData + data;** // but this isnt working
** }**
** }**
** // the server's disconnected, stop the client:**
** client.stop();**
** Serial.println();**
** Serial.println("disconnected");**
** }**
** **
** Serial.println("fetched data: " + fetchedData);** // this reults to "fetcched data:" what i need is fetched data: <<<{+639277349078,+639277442124,}>>>
if(waterHeight <= 13){
lcd.setCursor(0, 1);
lcd.print(" NORMAL WATER LEVEL");
digitalWrite(BUZZER, LOW);
digitalWrite(green_light_pin, 255);
digitalWrite(blue_light_pin, 0);
digitalWrite(red_light_pin, 0);
}
else{
if(waterHeight >8 && waterHeight <= lv1){
lcd.setCursor(0, 1);
lcd.print("WARNING ALERT LEVEL1");
digitalWrite(blue_light_pin, 255);
digitalWrite(red_light_pin, 0);
digitalWrite(green_light_pin, 0);
tone(BUZZER, 1000, 500);
}
if(waterHeight > lv1 && waterHeight <= lv2){
lcd.setCursor(0, 1);
lcd.print("WARNING ALERT LEVEL2");
analogWrite(red_light_pin, 255);
analogWrite(green_light_pin, 30);
analogWrite(blue_light_pin, 0);
tone(BUZZER, 1000, 100);
}
if(waterHeight > lv2 && waterHeight <= lv3){
lcd.setCursor(0, 1);
lcd.print("WARNING ALERT LEVEL3");
digitalWrite(red_light_pin, 255);
digitalWrite(blue_light_pin, 0);
digitalWrite(green_light_pin, 0);
digitalWrite(BUZZER, HIGH);
SendSMS();
}
}
if(sim800l.available()>0){
inChar = sim800l.read();
if(inChar == 'U'){
delay(50);
inChar = sim800l.read();
if(inChar == 'P'){
delay(50);
inChar = sim800l.read();
if(inChar == 'D'){
delay(50);
inChar = sim800l.read();
if(inChar == 'A'){
delay(50);
inChar = sim800l.read();
if(inChar == 'T'){
delay(50);
inChar = sim800l.read();
if(inChar == 'E'){
delay(100);
sim800l.println("AT+CMGD=1,4"); //del all sms
delay(100);
smsUpdate();
}
}
}
}
}
}
}
}
void SendSMS(){
sim800l.print("AT+CMGF=1");
Serial.println("Sending SMS...");
sim800l.print("AT+CMGS=\"+639277342142\"\r");
sim800l.print("WARNING ALERT LV3 waterHeight:" + waterHeight);
sim800l.print((char)26);
sim800l.println();
Serial.println("Text Sent.");
_buffer = _readSerial();
}
String _readSerial() {
_timeout = 0;
while (!sim800l.available() && _timeout < 12000 )
{
delay(13);
_timeout++;
}
if (sim800l.available()) {
return sim800l.readString();
}
}
void smsUpdate(){
Serial.println("Sending SMS...");
sim800l.print("AT+CMGS=\"+639270265552\"\r");
sim800l.print(waterHeight + "cm");
sim800l.print((char)26);
sim800l.println();
Serial.println("Text Sent.");
}
data = client.read(); // WHAT I WANT IS TO PASS THIS data TO A GLOBAL VARIABLE
What data type is the data variable ?
char
What do you get when you print fetchedData and where exactly is fetchedData declared ?
Please post a complete sketch that illustrates the problem
fetchedData is a global variable.
printing fetchedData will get you "fetched data: "
what I want is "fetched data: {+639277347079,+639277230000}
In your code you have
data = client.read();
Serial.print(data);
What do you see printed ?
Add markers before and after printing data so that you know that the print command is being executed
the print command is being executed it result to what I want
result of Serial.print(data); : <<<{+639277347079,+639277230000}>>>
but the result of fetchedData = fetchedData + data; = incomlete like this ",+639277230000}>>>"
sometimes like this "000}>>>"
I need the result of fetchedData = fetchedData + data; the same with the result of Serial.print(data);
which is <<<{+639277347079,+639277230000}>>>
<<<{+63927734700,+639277342079,}>>> //THIS IS THE RESULT OF Serial.print(data)
disconnected
fetched data: +63927734700,+639277342079,}>>> // THIS IS THE RESULT OF Serial.println("fetched data: " + fetchedData);
Distance: 39cm
Connected to server
HTTP/1.1 200 OK
Date: Fri, 03 Jun 2022 07:08:02 GMT
Server: Apache/2.4.53 (Win64) OpenSSL/1.1.1n PHP/7.4.29
X-Powered-By: PHP/7.4.29
Content-Length: 35
Connection: close
Content-Type: text/html; charset=UTF-8
<<<{+63927734700,+639277342079,}>>> //THIS IS THE RESULT OF Serial.print(data)
disconnected
fetched data: 079,}>>> // THIS IS THE RESULT OF Serial.println("fetched data: " + fetchedData);
Distance: 39cm
Connected to server
HTTP/1.1 200 OK
Date: Fri, 03 Jun 2022 07:08:03 GMT
Server: Apache/2.4.53 (Win64) OpenSSL/1.1.1n PHP/7.4.29
X-Powered-By: PHP/7.4.29
Content-Length: 35
Connection: close
Content-Type: text/html; charset=UTF-8
What do you see if you print fetchedData immediately after concatenating the new data each time it is received ?
+63927734700,+639277342079,}>>> the the first result I get, and then its changes every iteration
it should be like this <<<{+63927734700,+639277342079,}>>> but I coudn't get it
The data variable is a char so can only be a single character. So, fetchedData should get one character longer for each character received. Try printing fetchedData on a new line after each concatenation, like this
if (client.available())
{
// read an incoming byte from the server and print it to serial monitor:
data = client.read();
Serial.print(data);
//result of this is <<<{+639277349078,+639277442124}>>>
fetchedData = fetchedData + data;
Serial.println(fetchedData);
}
What do you see ?
TF-8
<TF-8
<<TF-8
<<<TF-8
<<<{TF-8
<<<{+TF-8
<<<{+6TF-8
<<<{+63TF-8
<<<{+639TF-8
<<<{+6392TF-8
<<<{+63927TF-8
<<<{+639277TF-8
<<<{+6392773TF-8
<<<{+63927734TF-8
<<<{+639277347TF-8
<<<{+6392773470TF-8
<<<{+63927734700TF-8
<<<{+63927734700,TF-8
<<<{+63927734700,+TF-8
<<<{+63927734700,+6TF-8
<<<{+63927734700,+63TF-8
<<<{+63927734700,+639TF-8
<<<{+63927734700,+6392TF-8
<<<{+63927734700,+63927TF-8
<<<{+63927734700,+639277TF-8
<<<{+63927734700,+6392773TF-8
<<<{+63927734700,+63927734007079079,079,}079,}>079,}>>079,}>>>
disconnected
fetched data: 079,}>>>
That does not make any sense
Try this
char buffer[100];
byte index = 0;
while (client.connected())
{
if (client.available())
{
data = client.read();
Serial.print(data);
Serial.print(" index : ");
Serial.print(index);
Serial.print("\t");
buffer[index++] = data;
buffer[index] = '\0';
Serial.println(buffer);
}
}
and post the output
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.