Arduino get function pass var from sensor reading

#include "Ultrasonic.h"
#include <math.h>
#include <Wire.h>
#include "rgb_lcd.h"
#include <Ethernet.h>
#include <PubSubClient.h>

 
#if defined(ARDUINO_ARCH_AVR)
#define debug  Serial
#elif defined(ARDUINO_ARCH_SAMD) ||  defined(ARDUINO_ARCH_SAM)
#define debug  SerialUSB
#else
#define debug  Serial
#endif

//server
byte mac[]    = {  0xA8, 0x61, 0x0A, 0xAE, 0x2E, 0x03 };   //pls change to your arduino MAC, refer to the sticker on the side
IPAddress server(192, 168, 10, 122);   //change to the php server ip      
EthernetClient client;


//preset if statement 
int reading = LOW;
int state = HIGH;
int previous = LOW;
int buttonPin = 6;
int stateTime = 0;
int debounce = 100;

//ultrasonic ranger 
 
Ultrasonic ultrasonic(7);

//temperature sensor
const int B = 4275;               // B value of the thermistor
const long R0 = 100000;            // R0 = 100k
const int pinTempSensor = A0;     // Grove - Temperature Sensor connect to A0
int percentage;

//lcd colour 
rgb_lcd lcd;
const int colorR = 255;
const int colorG = 255;
const int colorB = 255;



void setup() {
  pinMode(buttonPin, INPUT);
  Serial.begin(9600);
  Serial.println("Pushing data");
  Ethernet.begin(mac); // ethernet card to start connection
  delay(1500); //wait for connection
      
  //print connection detail 
  Serial.print("Arduino IP Address        : ");   //ip
  Serial.println(Ethernet.localIP());
  Serial.print("Gateway       : ");
  Serial.println(Ethernet.gatewayIP());
  Serial.print("Subnet Mask       : ");
  Serial.println(Ethernet.subnetMask());

  
  

  
}

void loop() {
  // read the pushbutton input pin
  reading = digitalRead(buttonPin);


  // determine if the button is pressed with debounce
  if (reading == HIGH && previous == LOW && (millis() - stateTime) > debounce)
  {
    Serial.println("Button PUSHED");
    if (state == HIGH)
    {
      state = LOW;
      Serial.println("Status changed to LOW");
      lcd.begin(16, 2);
      lcd.print("start sending");
      delay(2000);
    }
    else {
      state = HIGH;
      Serial.println("Status changed to HIGH");
      lcd.begin(16, 2);
      lcd.print("stop sending...");
      delay(2000);

      //off the lcd
      const int colorR = 0;
      const int colorG = 0;
      const int colorB = 0;
      lcd.noDisplay();
      delay(2000);
      lcd.setRGB(colorR, colorG, colorB);
      
    }
    stateTime = millis();
  }

  
  if (state == LOW)
  {
    lcd.begin(16, 2);
    lcd.setRGB(colorR, colorG, colorB);

   //ultrasonic
    long RangeInCentimeters;
    RangeInCentimeters = ultrasonic.MeasureInCentimeters();
    percentage = map(RangeInCentimeters, 5, 20, 100, 0);
    Serial.println("water level percentage is: ");
    Serial.println(percentage);
    delay(5000);

    //temperature 
    int a = analogRead(pinTempSensor);
    float R = 1023.0/a-1.0;
    R = R0*R;
    float temperature = 1.0/(log(R/R0)/B+1/298.15)-273.15; // convert to temperature via datasheet
    Serial.print("temperature = ");
    Serial.println(temperature);
    delay(100);

    //lcd
    lcd.setCursor(0, 0);
    lcd.print("waterlevel:");
    lcd.print(percentage);
    lcd.print("%");
    lcd.setCursor(0, 1);
    lcd.print("temp:");
    lcd.print(temperature );
    delay(5000);

    //sending data
    
     if (client.connect(server, 80)) 
  { 
   

    Serial.println("-> Connected");

    // Make a HTTP request:
    client.print("GET http://192.168.10.122/send.php?coolant="percentage"&temperature=" temperature"&co=1&tp=1");   //change based on the server ip
   

    client.println( " HTTP/1.1");
    client.print( "Host: " );
    client.println( "Connection: close" );
    client.println();
    client.println();
    client.stop();
    delay(5000);     //every 5 second send one reading
   }
}


  

  // update previous button state reading
  previous = reading;
}

how do i pass in variable in get request ?

Please follow the advice given in the link below when posting code , use code tags and post the code here to make it easier to read and copy for examination

Usually you can split the printing into a few parts; I'm however not familiar with networking on Arduino but would suggest that you give the below idea a try.

Print some text, print variable, print more text, print variable and so on.

If that does not work, compose the GET request first and next print it.

1 Like