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.