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
#define debug  Serial

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.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.print("Gateway       : ");
  Serial.print("Subnet Mask       : ");



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");
    else {
      state = HIGH;
      Serial.println("Status changed to HIGH");
      lcd.begin(16, 2);
      lcd.print("stop sending...");

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

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

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

    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 = ");

    lcd.setCursor(0, 0);
    lcd.setCursor(0, 1);
    lcd.print(temperature );

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

    Serial.println("-> Connected");

    // Make a HTTP request:
    client.print("GET"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" );
    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

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.