Hi GoForSmoke,
here is the code:
//================================================LIBRARIES
#include "SIM900.h"
#include <SoftwareSerial.h>
#include "inetGSM.h"
#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal.h>
//================================================DEFINE AND VARIABLES
// Data wire is plugged into port 3 on the Arduino
#define ONE_WIRE_BUS 3
#define TEMPERATURE_PRECISION 9
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
// arrays to hold device addresses
DeviceAddress Thermometer1, Thermometer2, Thermometer3;
// BS E D4 D5 D6 D7
LiquidCrystal lcd(7, 6, 28, 26, 24, 22);
int temp1;
int temp2;
int temp3;
InetGSM inet;
char msg[50];
char buff[100];
char senddata[64];
int numdata;
char inSerial[50];
int i=0;
boolean started=false;
const int GSMOnPIN = 9;
const int GSMresetPIN = 10;
const int ledPin = 8;
const unsigned long readsensorsinterval = 30000UL; //30 seconds
const unsigned long gsmsendinterval = 60000UL; //60 seconds
// Variable holding the timer value so far. One for each "Timer"
unsigned long readsensorstimer;
unsigned long gsmsendtimer;
//================================================SETUP
void setup()
{
pinMode(GSMOnPIN, OUTPUT);
pinMode(GSMresetPIN, OUTPUT);
pinMode(ledPin, OUTPUT);
delay(100);
Serial.begin(19200);
// Start up the Sensors library
sensors.begin();
// locate devices on the bus
Serial.print(F("Locating devices..."));
Serial.print(F("Found "));
Serial.print(sensors.getDeviceCount(), DEC);
Serial.println(F(" devices."));
// report parasite power requirements
Serial.print(F("Parasite power is: "));
if (sensors.isParasitePowerMode()) Serial.println(F("ON"));
else Serial.println(F("OFF"));
oneWire.reset_search();
if (!oneWire.search(Thermometer1)) Serial.println(F("Unable to find address for Thermometer1"));
if (!oneWire.search(Thermometer2)) Serial.println(F("Unable to find address for Thermometer2"));
if (!oneWire.search(Thermometer3)) Serial.println(F("Unable to find address for Thermometer3"));
// show the addresses we found on the bus
Serial.print(F("Device 1 Address: "));
printAddress(Thermometer1);
Serial.println();
Serial.print(F("Device 2 Address: "));
printAddress(Thermometer2);
Serial.println();
Serial.print(F("Device 3 Address: "));
printAddress(Thermometer3);
Serial.println();
// set the resolution to 9 bit
sensors.setResolution(Thermometer1, TEMPERATURE_PRECISION);
sensors.setResolution(Thermometer2, TEMPERATURE_PRECISION);
sensors.setResolution(Thermometer3, TEMPERATURE_PRECISION);
Serial.print(F("Device 1 Resolution: "));
Serial.print(sensors.getResolution(Thermometer1), DEC);
Serial.println();
Serial.print(F("Device 2 Resolution: "));
Serial.print(sensors.getResolution(Thermometer2), DEC);
Serial.println();
Serial.print(F("Device 3 Resolution: "));
Serial.print(sensors.getResolution(Thermometer3), DEC);
Serial.println();
readsensorstimer = millis ();
gsmsendtimer = millis ();
}
//================================================FUNCTIONS
void serialhwread(){
i=0;
if (Serial.available() > 0){
while (Serial.available() > 0) {
inSerial[i]=(Serial.read());
delay(10);
i++;
}
inSerial[i]='\0';
if(!strcmp(inSerial,"/END")){
Serial.println("_");
inSerial[0]=0x1a;
inSerial[1]='\0';
gsm.SimpleWriteln(inSerial);
}
//Send a saved AT command using serial port.
if(!strcmp(inSerial,"TEST")){
Serial.println(F("SIGNAL QUALITY"));
gsm.SimpleWriteln("AT+CSQ");
}
//Read last message saved.
if(!strcmp(inSerial,"MSG")){
Serial.println(msg);
}
else{
Serial.println(inSerial);
gsm.SimpleWriteln(inSerial);
}
inSerial[0]='\0';
}
}
void serialswread(){
gsm.SimpleRead();
}
// function to print a device address
void printAddress(DeviceAddress deviceAddress)
{
for (uint8_t i = 0; i < 8; i++)
{
// zero pad the address if necessary
if (deviceAddress[i] < 16) Serial.print(F("0"));
Serial.print(deviceAddress[i], HEX);
}
}
// function to print the temperature for a device
void printTemperature(DeviceAddress deviceAddress)
{
float tempC = sensors.getTempC(deviceAddress);
Serial.print(F("Temp C: "));
Serial.print(tempC);
}
// function to print a device's resolution
void printResolution(DeviceAddress deviceAddress)
{
Serial.print(F("Resolution: "));
Serial.print(sensors.getResolution(deviceAddress));
Serial.println();
}
// main function to print information about a device
void printData(DeviceAddress deviceAddress)
{
Serial.print(F("Device Address: "));
printAddress(deviceAddress);
Serial.print(F(" "));
printTemperature(deviceAddress);
Serial.println();
}
// read sensors
void readsensorstolcd(){
//READ TEMPERATURE
Serial.print(F("Requesting temperatures..."));
sensors.requestTemperatures();
Serial.println(F("DONE"));
Serial.print(F("Temperatures are "));
Serial.print(sensors.getTempC(Thermometer1));
Serial.print(F(" Celsius, "));
Serial.print(sensors.getTempC(Thermometer2));
Serial.print(F(" Celsius, "));
Serial.print(sensors.getTempC(Thermometer3));
Serial.print(F(" Celsius, "));
Serial.print(F("\n\r"));
delay(100);
temp1=(sensors.getTempC(Thermometer1));
temp2=(sensors.getTempC(Thermometer2));
temp3=(sensors.getTempC(Thermometer3));
delay(100);
readsensorstimer = millis ();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("T1: C");
lcd.setCursor(0, 1);
lcd.print("T2: C");
lcd.setCursor(0, 2);
lcd.print("T3: C");
lcd.setCursor(3, 0);
lcd.print(temp1);
lcd.setCursor(3, 1);
lcd.print(temp2);
lcd.setCursor(3, 2);
lcd.print(temp3);
lcd.setCursor(3, 3);
delay(200);
}
//GSM start,send ,stop
void gsmandsend(){
digitalWrite(GSMOnPIN, LOW);
delay(3000);
Serial.println(F("GSM Shield starting..."));
digitalWrite(GSMOnPIN, HIGH); // sets the GSM on
delay(800);
Serial.println(F("Its now Started"));
digitalWrite(GSMOnPIN, LOW);
delay(5000);
if (gsm.begin(4800)){
Serial.println(F("\nstatus=READY"));
started=true;
}
else Serial.println(F("\nstatus=IDLE"));
if(started){
if (inet.attachGPRS("***********************", "", ""))
Serial.println(F("status=ATTACHED"));
else Serial.println(F("status=ERROR attaching APN"));
delay(1000);
if (inet.connectedClient())
digitalWrite(ledPin, HIGH);
else
digitalWrite(ledPin, LOW);
//Read IP address.
gsm.SimpleWriteln("AT+CIFSR");
delay(5000);
//Read until serial buffer is empty.
gsm.WhileSimpleRead();
// Sending data
sprintf(senddata,"/update?key=***************************",temp1,temp2,temp3);
delay(500);
numdata=inet.httpPOST("*************************", 80, senddata, buff, msg, 50);
//Print the results.
Serial.println(F("\nNumber data received:"));
Serial.println(numdata);
Serial.println(F("\nData received:"));
Serial.println(msg);
}
serialhwread();
//Read for new byte on NewSoftSerial.
serialswread();
delay(800);
digitalWrite(GSMOnPIN, LOW);
delay(3000);
Serial.println(F("GSM Shield stopping..."));
digitalWrite(GSMOnPIN, HIGH); // sets the GSM off
delay(800);
Serial.println(F("Its now Stopped"));
digitalWrite(GSMOnPIN, LOW);
gsmsendtimer = millis ();
delay(5000);
}
//================================================LOOP
void loop()
{
if ( ( millis() - readsensorstimer ) >= readsensorsinterval )
{
readsensorstolcd();
readsensorstimer += readsensorsinterval; }
if (( millis() - gsmsendtimer ) >= gsmsendinterval )
{
gsmandsend();
gsmsendtimer += gsmsendinterval;
}
}
This is for testing purposes, it will be extended with more sensors. So it is now reading sensors in every 30 seconds, and sending data in every 60 seconds (what can be found in temp variables at that time will be send, thinking if it is the right way, or should I read sensors again before sending the data in gsmandsend() ).
So main goal is to read sensor values and send to lcd, and in other hand send the sensor values with gsm.
-
If I would like to have 'independent' routines for showing data on LCD and for sending data, I should put sensor readings too to gsmandsend() routing. Is it correct?
-
I would like to have a pushbutton to set modes for sensor readings and drive relays for example (push1 - mode1: switch relay with X temp1 sensor value ; push2-mode2: switch relay with Y temp1 value ; push3-mode1;push4-mode2 and so on) . Beside the values have to send with gsmandsend()
-
After further tests (and learning ) I would like to have lcd and button to set the temp1 values for example (but this is not important at this point.
Thanks for any help!