Sending values from function to another?

Hey guys. I'd like some help about passing variables to SIM800.

I've got a function to read sensors, and a function to send data over softserial/SIM800L

But i cannot get my head around on how to do it.

/*
   MultiLogger for sailboat V0.5


   DHT      pin 11
   FET      pin 10
   Batt_V   pin A0
   ACS      pin A1
   SIMxxx   SoftSerial


   Todo:


  - SIM 808
  - Printing and sending data
  - Reply to SMS with data

  - Wake up from sleep every x minutes - switchable with a switch/internet/sms?
  - Sleep when last sensor is read or sent

  Remember that this requires capacitor

*/
#include <SoftwareSerial.h>
#include "LowPower.h"
#include "DHT.h"
#define DHTPIN 11     // DHT pin
#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

DHT dht(DHTPIN, DHTTYPE);

//SIM800 TX is connected to Arduino D8/D7
#define SIM800_TX_PIN 8
#define SIM800_RX_PIN 7
SoftwareSerial serialSIM800(SIM800_TX_PIN, SIM800_RX_PIN);




// Variables
int fetPin = 10;
int battPin = A0;
int currPin = A1;
int ledPin = 13;

volatile float h;
volatile float t;

// Variables for Voltage measurement
float vout = 0.0;
float vin = 0.0;
float R1 = 99300.0;
float R2 = 32915.0;
int voltVal = 0;

// Variables for Current measurement
float acsVoltage = 0;
float acsCurrent = 0;


// READ VOLTAGE
void readVoltage() {
  delay(50);
  voltVal = analogRead(battPin);
  vout = (voltVal * 5.00) / 1024.0; // Set multiplier according to VCC
  vin = vout / (R2 / (R1 + R2));  // Voltage at battPin
  if (vin < 1.00) {
    vin = 0.0;
  }

  // PRINT
  Serial.print("Voltage: ");
  Serial.println(vin);

}

// READ CURRENT
void readCurrent() {
  // Read 10 times to average
  for (int i = 0; i < 10; i++) {
    acsVoltage = (acsVoltage + (.0049 * analogRead(currPin)));   // (5 V / 1024 = 0.0049) which converter Measured analog input voltage to 5 V Range
    delay(1);
  }
  acsVoltage = acsVoltage / 1000;
  acsCurrent = (acsVoltage - 2.5) / 0.100; // Voltage to current

  // PRINT
  Serial.print("Current: ");
  Serial.println(acsCurrent);
}

// READ TEMP / HUMI
void readDHT() {
  // Wait a few seconds between measurements.
  delay(1000); //-- ENABLE IF NEEDED

  // Reading temperature or humidity takes about 250 milliseconds!
  //  float h = dht.readHumidity();
  //  float t = dht.readTemperature();


  h = dht.readHumidity();
  t = dht.readTemperature();



  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  // PRINT TO SERIAL FOR DEBUG

  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.print(" *C ");
  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.println(" %\t");
  delay(500);
}


// SLEEP FOR 8 SECONDS
void enterSleep() {
  Serial.println("Entering sleep...");
  Serial.flush(); // Garbage cleaning from serial communication

  // Enter power down state for 8 s with ADC and BOD module disabled
  LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
}

// Blink onboard led
void flash() {
  pinMode(ledPin, OUTPUT);
  for (byte f = 0; f < 5; f++) {
    digitalWrite(ledPin, HIGH);
    delay(20);
    digitalWrite(ledPin, LOW);
    delay(50);
  }
  pinMode(ledPin, INPUT); // Switch pin to INPUT to save power
}

void deviceOn() {
  pinMode(fetPin, OUTPUT);
  digitalWrite(fetPin, HIGH);
  Serial.println("Fet is ON");
}

void deviceOff() {
  digitalWrite(fetPin, LOW);
  Serial.println("Fet is OFF");
  pinMode(fetPin, INPUT);
}

void sendData() {
  Serial.println("Starting data transfer");
  int u;
  for (u = 0; u < 20; u++) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");

  delay(3000);
  Serial.println("Sending data");
  delay(100);
  serialSIM800.println("AT+SAPBR=3,1,\"APN\",\"internet\"");
  delay(1000);
  serialSIM800.println("AT+SAPBR=1,1");
  delay(3000);
  serialSIM800.println("AT+HTTPINIT");
  delay(500);
  serialSIM800.println("AT+HTTPPARA=\"CID\",1");
  delay(500);

  // add values to URL
  //serialSIM800.println("AT+HTTPPARA=\"URL\",\"http://domain.org/data.php?arvo=20\"");

  char urltext[30];
  char buff[80];
  //int  value = 20;

  sprintf(urltext, "http://huono.org/data?value=%d", vin);
  strcpy(buff, "AT+HTTPPARA=\"URL\",");
  strcat(buff, urltext);
  serialSIM800.println(buff);
  
  delay(500);
  serialSIM800.println("AT+HTTPACTION=0");
  delay(3000);
  Serial.println("Data sent!");
}




// -----------------------------------------SETUP-----------------------------------------------
void setup() {
  Serial.println("Paatti V0.5");
  Serial.begin(9600);
  serialSIM800.begin(9600);

  Serial.println("starting up DHT communication...");
  dht.begin();

  pinMode(battPin, INPUT);
  pinMode(currPin, INPUT);

  deviceOff();                        // Start with FET off
  Serial.println("Devices offline");

  Serial.println("Setup ready");


  // SIM800TEST

  //deviceOn();
  //sendData();

  // END OF SIMTEST

}

// ------------------LOOP---------------
void loop() {

  flash();          // blink led to inform i'm awake

  //Read SIM800 output (if available) and print it in Arduino IDE Serial Monitor
  if (serialSIM800.available()) {
    Serial.write(serialSIM800.read());
  }
  //Read Arduino IDE Serial Monitor inputs (if available) and send them to SIM800
  if (Serial.available()) {
    serialSIM800.write(Serial.read());
  }

  deviceOn();       // Turn FET on
  delay(500);       // Wait for devices to start
  readDHT();        // Read DHT value


  readVoltage();  // Read voltage from divider
  //readCurrent();  // Read current from ACS

Serial.println("DEBUG ");
Serial.println(vin);
Serial.println(t);
Serial.println(h);

      
  sendData();      // how to add variables for this function from other functions?
  deviceOff();      // Power off devices to save power
  flash();
  enterSleep();     // Go to sleep (defined in the variable)
  //delay(500);
}

It does now send to www-server, but i cant add variables to the output from other functions. Otherwise it seems to work.

You should be able to do something like this

serialSIM800.println(myVariable);

...R

This line:

sprintf(urltext, "http://huono.org/data?value=%d", vin);

I want to add values from multiple functions. I want to use http GET to save data to a mysql server. Server side is okay, but for example that variable vin does not work.

It does not properly print it to the server.

So the question is, how do i get variables from other functions to this URL?

Thanks for the reply again!

It does seem that my problem is floats.

Do you know any really good for dummies tutorial on how to use the dtostrf and sprintf?

Or perhaps an example on how to get three floats on the same URL?

Do you know any really good for dummies tutorial on how to use the dtostrf and sprintf?

Mr. Google does.

Or perhaps an example on how to get three floats on the same URL?

float pi = 3.14159;
float two_pi = pi * 2;
float half_pi = pi / 2;

char stgA[10], stgB[10], stgC[10];

dtostrf(pi, 8, 4, stgA);
dtostrf(two_pi, 8, 4, stgB);
dtostrf(half_pi, 8, 4, stgC);

char sameURL[80];
strcpy(sameURL, "pi=");
strcat(sameURL, stgA);
strcat(sameURL, "&two_pi=");
strcat(sameURL, stgB);
strcat(sameURL, "&half_pi=");
strcat(sameURL, stgC);

If you can concert one float to one string, it really isn't that difficult to extend it to converting three floats to three strings. Concatenating strings is simple.

I actually did get some progress.

Here is the serial output now:

starting up DHT communication...
Fet is OFF
Devices offline
Setup ready
Fet is ON
Temperature: 28.70 *C Humidity: 36.30 %	
Voltage: 12.20
DEBUG 
12.20
28.70
36.30
Starting data transfer
....................
Sending data
Payload: AT+HTTPPARA="URL","http://huono.org/data.php?voltage=12.200&temperature=28.700 EOL
Data sent!
Fet is OFF
Entering sleep...

But for some reason nothing comes up in the www server logs, so i guess the modem is not really sending it?
"Data send" is just a "guess" from earlier tests and is only based on time. Not sure yet to add if it received 200 Ok from server.

Updated code here:

/*
   MultiLogger for sailboat V0.5


   DHT      pin 11
   FET      pin 10
   Batt_V   pin A0
   ACS      pin A1
   SIMxxx   SoftSerial


   Todo:


  - SIM 808
  - Printing and sending data
  - Reply to SMS with data

  - Wake up from sleep every x minutes - switchable with a switch/internet/sms?
  - Sleep when last sensor is read or sent

  Remember that this requires capacitor

*/
#include <SoftwareSerial.h>
#include "LowPower.h"
#include "DHT.h"
#define DHTPIN 11     // DHT pin
#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

DHT dht(DHTPIN, DHTTYPE);

//SIM800 TX is connected to Arduino D8/D7
#define SIM800_TX_PIN 8
#define SIM800_RX_PIN 7
SoftwareSerial serialSIM800(SIM800_TX_PIN, SIM800_RX_PIN);




// Variables
int fetPin = 10;
int battPin = A0;
int currPin = A1;
int ledPin = 13;

volatile float h;
volatile float t;

// Variables for Voltage measurement
float vout = 0.0;
float vin = 0.0;
String vinStr;
float R1 = 99300.0;
float R2 = 32915.0;
int voltVal = 0;

// Variables for Current measurement
float acsVoltage = 0;
float acsCurrent = 0;


// READ VOLTAGE
void readVoltage() {
  delay(50);
  voltVal = analogRead(battPin);
  vout = (voltVal * 5.00) / 1024.0; // Set multiplier according to VCC
  vin = vout / (R2 / (R1 + R2));  // Voltage at battPin
  if (vin < 1.00) {
    vin = 0.0;
  }

  String vinStr = String(vin);



  // PRINT
  Serial.print("Voltage: ");
  Serial.println(vinStr);

}

// READ CURRENT
void readCurrent() {
  // Read 10 times to average
  for (int i = 0; i < 10; i++) {
    acsVoltage = (acsVoltage + (.0049 * analogRead(currPin)));   // (5 V / 1024 = 0.0049) which converter Measured analog input voltage to 5 V Range
    delay(1);
  }
  acsVoltage = acsVoltage / 1000;
  acsCurrent = (acsVoltage - 2.5) / 0.100; // Voltage to current

  // PRINT
  Serial.print("Current: ");
  Serial.println(acsCurrent);
}

// READ TEMP / HUMI
void readDHT() {
  // Wait a few seconds between measurements.
  delay(1000); //-- ENABLE IF NEEDED

  // Reading temperature or humidity takes about 250 milliseconds!
  //  float h = dht.readHumidity();
  //  float t = dht.readTemperature();


  h = dht.readHumidity();
  t = dht.readTemperature();



  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  // PRINT TO SERIAL FOR DEBUG

  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.print(" *C ");
  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.println(" %\t");
  delay(500);
}


// SLEEP FOR 8 SECONDS
void enterSleep() {
  Serial.println("Entering sleep...");
  Serial.flush(); // Garbage cleaning from serial communication

  // Enter power down state for 8 s with ADC and BOD module disabled
  LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
}

// Blink onboard led
void flash() {
  pinMode(ledPin, OUTPUT);
  for (byte f = 0; f < 5; f++) {
    digitalWrite(ledPin, HIGH);
    delay(20);
    digitalWrite(ledPin, LOW);
    delay(50);
  }
  pinMode(ledPin, INPUT); // Switch pin to INPUT to save power
}

void deviceOn() {
  pinMode(fetPin, OUTPUT);
  digitalWrite(fetPin, HIGH);
  Serial.println("Fet is ON");
}

void deviceOff() {
  digitalWrite(fetPin, LOW);
  Serial.println("Fet is OFF");
  pinMode(fetPin, INPUT);
}

void sendData() {
  Serial.println("Starting data transfer");
  int u;
  for (u = 0; u < 20; u++) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");

  delay(3000);
  Serial.println("Sending data");
  delay(100);
  serialSIM800.println("AT+SAPBR=3,1,\"APN\",\"internet\"");
  delay(1000);
  serialSIM800.println("AT+SAPBR=1,1");
  delay(3000);
  serialSIM800.println("AT+HTTPINIT");
  delay(500);
  serialSIM800.println("AT+HTTPPARA=\"CID\",1");
  delay(500);

  //working
  //  serialSIM800.println("AT+HTTPPARA=\"URL\",\"http://huono.org/data.php?arvo=20\"");

  char valA[10], valB[10], valC[10];

  dtostrf(vin, 3, 3, valA);
  dtostrf(t, 3, 3, valB);
  dtostrf(h, 3, 3, valC);

  char urlData[80];

  strcpy(urlData, "AT+HTTPPARA=\"URL\",\"http://huono.org/data.php?");
  strcat(urlData, "voltage=");
  strcat(urlData, valA);
  strcat(urlData, "&temperature=");
  strcat(urlData, valB);
//  strcat(urlData, "&humidity=");
//  strcat(urlData, valC);

  Serial.print("Payload: ");      // 
  Serial.print(urlData);          // Print to serial console to see what is inside the payload 
  Serial.println(" EOL");         //
  
  serialSIM800.println(urlData);  // Print to modem


  delay(3000);
  serialSIM800.println("AT+HTTPACTION=0");
  delay(5000);
  Serial.println("Data sent!");
}




// -----------------------------------------SETUP-----------------------------------------------
void setup() {
  Serial.println("Paatti V0.5");
  Serial.begin(9600);
  serialSIM800.begin(9600);

  Serial.println("starting up DHT communication...");
  dht.begin();

  pinMode(battPin, INPUT);
  pinMode(currPin, INPUT);

  deviceOff();                        // Start with FET off
  Serial.println("Devices offline");

  Serial.println("Setup ready");


  // SIM800TEST

  //deviceOn();
  //sendData();

  // END OF SIMTEST

}

// ------------------LOOP---------------
void loop() {

  flash();          // blink led to inform i'm awake

  //Read SIM800 output (if available) and print it in Arduino IDE Serial Monitor
  if (serialSIM800.available()) {
    Serial.write(serialSIM800.read());
  }
  //Read Arduino IDE Serial Monitor inputs (if available) and send them to SIM800
  if (Serial.available()) {
    serialSIM800.write(Serial.read());
  }

  deviceOn();       // Turn FET on
  delay(500);       // Wait for devices to start
  readDHT();        // Read DHT value


  readVoltage();  // Read voltage from divider
  //readCurrent();  // Read current from ACS

  Serial.println("DEBUG ");
  Serial.println(vin);
  Serial.println(t);
  Serial.println(h);


  sendData();
  deviceOff();      // Power off devices to save power
  flash();
  enterSleep();     // Go to sleep (defined in the variable)
  //delay(500);
}

Not sure if it has something to do with missing "" on the payload?

Did you change data.php to change the number of arguments it expects? Did you change it to change the names of the name=value pairs?

PaulS:
Did you change data.php to change the number of arguments it expects? Did you change it to change the names of the name=value pairs?

Actually the values i will change when all the rest is done. I'm just looking for apache logs to see if it connects or not.

But access.log gets nothing with the new test. But if uncomment the following code, i get a reply on the access.log.

  //working
  //  serialSIM800.println("AT+HTTPPARA=\"URL\",\"http://huono.org/data.php?arvo=20\"");;"

Is this correct?

serialSIM800.println(urlData);  // Print to modem

Is this correct?

The only way to know is to print the data in urlData.

Every one of your println() calls with an AT command in it generates a response. delay() does NOT read and/or print the response. YOU need to write code to do that, and STOP USING delay().