Skip to content

Instantly share code, notes, and snippets.

@mussacharles60
Last active August 3, 2022 15:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mussacharles60/e7fba8ff963d3afe956e2930dbee47cb to your computer and use it in GitHub Desktop.
Save mussacharles60/e7fba8ff963d3afe956e2930dbee47cb to your computer and use it in GitHub Desktop.
iot-cooler-system-code
<?php
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json');
require_once './database.php';
// require_once './models.php';
// require_once './formatter.php';
class AddApi {
private Database $database;
/** @var bool|\mysqli */
private $conn;
private string $id = '';
private int $power_state = 0;
private int $power_off_count = 0;
private string $power_off_times = '';
private int $door_state = 0;
private int $door_open_count = 0;
private string $door_open_times = '';
private float $light = 0.0;
private float $location_lat = 0.0;
private float $location_lng = 0.0;
private float $temperature_in = 0.0;
private float $temperature_out = 0.0;
// private float $humidity = 0.0;
// private int $time = 0;
public function __construct() {
// $_POST = json_decode(file_get_contents('php://input'), true);
$this->id = isset($_REQUEST['d_id']) ? $_REQUEST['d_id'] : '';
$this->power_state = isset($_REQUEST['d_pr']) ? $_REQUEST['d_pr'] : -1;
$this->power_off_count = isset($_REQUEST['d_pr0']) ? $_REQUEST['d_pr0'] : -1;
$this->power_off_times = isset($_REQUEST['d_pr0t']) ? $_REQUEST['d_pr0t'] : '';
$this->door_state = isset($_REQUEST['d_dr']) ? $_REQUEST['d_dr'] : -1;
$this->door_open_count = isset($_REQUEST['d_dr1']) ? $_REQUEST['d_dr1'] : -1;
$this->door_open_times = isset($_REQUEST['d_dr1t']) ? $_REQUEST['d_dr1t'] : '';
$this->light = isset($_REQUEST['d_li']) ? $_REQUEST['d_li'] : 0;
$this->location_lat = isset($_REQUEST['d_lt']) ? $_REQUEST['d_lt'] : 0;
$this->location_lng = isset($_REQUEST['d_lg']) ? $_REQUEST['d_lg'] : 0;
$this->temperature_in = isset($_REQUEST['d_ti']) ? $_REQUEST['d_ti'] : 0;
$this->temperature_out = isset($_REQUEST['d_to']) ? $_REQUEST['d_to'] : 0;
// $log = 'date: ' . date("F j, Y, g:i a") . PHP_EOL.
// 'id: ' . $this->id . PHP_EOL.
// 'power_state: ' . $this->power_state . PHP_EOL.
// 'power_off_count: ' . $this->power_off_count . PHP_EOL .
// 'power_off_times: ' . $this->power_off_times . PHP_EOL.
// 'door_state: ' . $this->door_state . PHP_EOL.
// 'door_open_count: ' . $this->door_open_count . PHP_EOL.
// 'door_open_times: ' . $this->door_open_times . PHP_EOL.
// 'light: ' . $this->light . PHP_EOL.
// 'latitude: ' . $this->location_lat . PHP_EOL.
// 'longitude: ' . $this->location_lng . PHP_EOL.
// 'temp_in: ' . $this->temperature_in . PHP_EOL.
// 'temp_out: ' . $this->temperature_out . PHP_EOL.
// "-------------------------------------------" . PHP_EOL;
// file_put_contents('./add.txt', $log, FILE_APPEND);
}
public function init() {
$this->database = new Database();
$this->conn = $this->database->connect();
if (!$this->conn) {
die('IOT:SEND:ERROR');
} else {
$this->insertData();
$this->closeConnection();
}
}
private function insertData() {
if ($this->id != "" && $this->power_state > -1 && $this->door_state > -1 && $this->power_off_count > -1 && $this->door_open_count > -1) {
$humidity = 100 * pow((((112 - (0.1 * $this->temperature_out) + $this->temperature_in) / (112 + (0.9 * ($this->temperature_out))))), 8);
$humidity = round($humidity, 2);
if ($humidity < 0) $humidity = 0;
if ($humidity > 100) $humidity = 100;
$time = round(microtime(true) * 1000);
$result = $this->conn->query(
"INSERT INTO `$this->id` (`id`, `power_state`, `power_off_count`, `power_off_times`, `door_state`, `door_open_count`, `door_open_times`, `light`, `location_lat`, `location_lng`, `temperature_in`, `temperature_out`, `humidity`, `time`)
VALUES ('$this->id', $this->power_state, $this->power_off_count, '$this->power_off_times', $this->door_state, $this->door_open_count, '$this->door_open_times', $this->light, $this->location_lat, $this->location_lng, $this->temperature_in, $this->temperature_out, $humidity, $time)"
);
// var_dump($result);
if ($result) {
// update device's last_update_time and increment total_entries
mysqli_query($this->conn, "UPDATE devices SET last_updated = $time, total_entries = total_entries + 1 WHERE id = '$this->id'");
echo 'IOT:SEND:OK';
} else {
echo 'IOT:SEND:ERROR';
}
} else {
echo 'IOT:SEND:ERROR';
}
}
private function closeConnection() {
if ($this->conn) {
mysqli_close($this->conn);
}
}
}
$addApi = new AddApi();
$addApi->init();
// // https://iot-cooler.imperialinnovations.co.tz/api/v1/add.php?id=574392&pr=0&prc=12&dr=1&drc=15&li=50&lt=0.00&lg=0.00&ti=0.00&to=0.00
// // echo 'RECEIVED:OK ';
// // header('Access-Control-Allow-Origin: *');
// // header('Content-Type: text/plain');
// /*
// {
// "d_id": "574391",
// "d_pr": 0,
// "d_pr0": 12,
// "d_pr0t": "2,4,15,5,80,120,48,96,500",
// "d_dr": 0,
// "d_dr1": 8,
// "d_dr1t": "2,4,15,5,80,120,48,96",
// "d_li": 76.00,
// "d_lt": 0.000000,
// "d_lg": 0.000000,
// "d_ti": 10.40,
// "d_to": 32.42
// }
// */
// // $_POST = json_decode(file_get_contents('php://input'), true);
// $id = "";
// if (isset($_REQUEST['d_id'])) {
// $id = $_REQUEST['d_id'];
// }
// // else if (isset($_POST['d_id'])) {
// // $id = $_POST['d_id'];
// // }
// $pr = -1;
// if (isset($_REQUEST['d_pr'])) {
// $pr = $_REQUEST['d_pr'];
// }
// // else if (isset($_POST['d_pr'])) {
// // $pr = $_POST['d_pr'];
// // }
// $prc = -1;
// if (isset($_REQUEST['d_pr0'])) {
// $prc = $_REQUEST['d_pr0'];
// }
// // else if (isset($_POST['d_pr0'])) {
// // $prc = $_POST['d_pr0'];
// // }
// $prct = "";
// if (isset($_REQUEST['d_pr0t'])) {
// $prct = $_REQUEST['d_pr0t'];
// }
// // else if (isset($_POST['d_pr0t'])) {
// // $prct = $_POST['d_pr0t'];
// // }
// $dr = -1;
// if (isset($_REQUEST['d_dr'])) {
// $dr = $_REQUEST['d_dr'];
// }
// // else if (isset($_POST['d_dr'])) {
// // $dr = $_POST['d_dr'];
// // }
// $drc = -1;
// if (isset($_REQUEST['d_dr1'])) {
// $drc = $_REQUEST['d_dr1'];
// }
// // else if (isset($_POST['d_dr1'])) {
// // $drc = $_POST['d_dr1'];
// // }
// $drct = "";
// if (isset($_REQUEST['d_dr1t'])) {
// $drct = $_REQUEST['d_dr1t'];
// }
// // else if (isset($_POST['d_dr1t'])) {
// // $drct = $_POST['d_dr1t'];
// // }
// $li = 0;
// if (isset($_REQUEST['d_li'])) {
// $li = $_REQUEST['d_li'];
// }
// // else if (isset($_POST['d_li'])) {
// // $li = $_POST['d_li'];
// // }
// $lat = 0;
// if (isset($_REQUEST['d_lt'])) {
// $lat = $_REQUEST['d_lt'];
// }
// // else if (isset($_POST['d_lt'])) {
// // $lat = $_POST['d_lt'];
// // }
// $lng = 0;
// if (isset($_REQUEST['d_lg'])) {
// $lng = $_REQUEST['d_lg'];
// }
// // else if (isset($_POST['d_lg'])) {
// // $lng = $_POST['d_lg'];
// // }
// $ti = 0;
// if (isset($_REQUEST['d_ti'])) {
// $ti = $_REQUEST['d_ti'];
// }
// // else if (isset($_POST['d_ti'])) {
// // $ti = $_POST['d_ti'];
// // }
// $to = 0;
// if (isset($_REQUEST['d_to'])) {
// $to = $_REQUEST['d_to'];
// }
// // else if (isset($_POST['d_to'])) {
// // $to = $_POST['d_to'];
// // }
// // $log = 'date: ' . date("F j, Y, g:i a") . PHP_EOL.
// // 'id: ' . $id . PHP_EOL.
// // 'pr: ' . $pr . PHP_EOL.
// // 'prc: ' . $prc . PHP_EOL .
// // 'prct: ' . $prct . PHP_EOL.
// // 'dr: ' . $dr . PHP_EOL.
// // 'drc: ' . $drc . PHP_EOL.
// // 'drct: ' . $drct . PHP_EOL.
// // 'li: ' . $li . PHP_EOL.
// // 'lat: ' . $lat . PHP_EOL.
// // 'lng: ' . $lng . PHP_EOL.
// // 'ti: ' . $ti . PHP_EOL.
// // 'to: ' . $to . PHP_EOL.
// // "------------------------------" . PHP_EOL;
// // file_put_contents('./add_php.log', $log, FILE_APPEND);
// if ($id != "" && $pr > -1 && $dr > -1 && $prc > -1 && $drc > -1) {
// $servername = "localhost";
// $username = "imccotz_iot-cooler-admin";
// $dbname = "imccotz_iot-cooler-system";
// $password = "imperialinnovations";
// $CRLF = "\n\r";
// // Create connection.
// $conn = mysqli_connect($servername, $username, $password, $dbname);
// if (!$conn) {
// echo 'SEND:ERROR';
// die;
// }
// $con_result = mysqli_select_db($conn, $dbname);
// if (!$con_result) {
// echo 'SEND:ERROR';
// die;
// }
// $sql = "";
// $humidity = 100 * pow((((112 - (0.1 * $to) + $ti) / (112 + (0.9 * ($to))))), 8);
// $humidity = round($humidity, 2);
// if ($humidity < 0) $humidity = 0;
// if ($humidity > 100) $humidity = 100;
// $time = round(microtime(true) * 1000);
// // $log = 'date: ' . date("F j, Y, g:i a") . PHP_EOL.
// // 'power: ' . $power . PHP_EOL.
// // 'door: ' . $door . PHP_EOL.
// // 'light: ' . $light . PHP_EOL .
// // 'location: ' . $location . PHP_EOL.
// // 'temp: ' . $temp . PHP_EOL.
// // 'humidity: ' . $power . PHP_EOL.
// // "--------------------------------------------------" . PHP_EOL;
// // // file_put_contents('./log_'.date("j.n.Y").'.log', $log, FILE_APPEND);//Save string to log, use FILE_APPEND to append.
// // file_put_contents('./add_php.log', $log, FILE_APPEND);//Save string to log, use FILE_APPEND to append.
// $query = "INSERT INTO
// devices_data (`id`, `power_state`, `power_off_count`, `power_off_times`, `door_state`, `door_open_count`, `door_open_times`, `light`, `location_lat`, `location_lng`, `temperature_in`, `temperature_out`, `humidity`, `time`)
// VALUES ('$id', $pr, $prc, '$prct', $dr, $drc, '$drct', $li, $lat, $lng, $ti, $to, $humidity, $time)";
// $result = mysqli_query($conn, $query);
// if (!$result) {
// echo 'IOT:SEND:ERROR';
// mysqli_close($conn);
// } else {
// echo 'IOT:SEND:OK';
// $query_1 = "UPDATE main_devices SET last_updated = $time WHERE main_devices.id = '$id'";
// mysqli_query($conn, $query_1);
// mysqli_close($conn);
// }
// } else echo 'IOT:SEND:ERROR';
?>
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#include <DS18B20.h>
#include <elapsedMillis.h>
SoftwareSerial GPS(2, 3); // RX = pin 2, TX = pin 3
SoftwareSerial GSM(7, 6); // RX = pin 6, TX = pin 7
TinyGPSPlus gps;
elapsedMillis gps_timer;
elapsedMillis gsm_timer;
elapsedMillis power_off_timer;
elapsedMillis door_open_timer;
String DEVICE_ID = "D574393";
String apn = "internet"; // APN
String apn_u = ""; // APN-Username
String apn_p = ""; // APN-Password
String url = "http://iot-cooler.imc.co.tz/api/v1/add.php"; // URL of Server
unsigned int gsm_stage = 1;
boolean gsm_data_sent = false;
char latitude[15] = "0.0000000";
char longitude[15] = "0.0000000";
DS18B20 dsFridge(8);
DS18B20 dsSurr(9);
// #define buzzer 10
// #define Reed 11
// #define RedLed 13
// #define Ldr A0
// #define power A1
float temp_in_data = 0.0;
float temp_out_data = 0.0;
float light_data = 0.0;
boolean power_data = 0;
boolean door_data = 0;
unsigned int power_off_count = 0;
unsigned int door_open_count = 0;
boolean last_power_state = 0;
boolean last_door_state = 0;
String power_off_times = "";
String door_open_times = "";
int power_off_countdown = 0;
int door_open_countdown = 0;
boolean is_reading_gps = true;
unsigned int gps_counts = 0;
double mapf(double val, double in_min, double in_max, double out_min, double out_max)
{
return (val - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
String s_data = "";
String gsm_data = "";
void setup()
{
Serial.begin(9600);
while (!Serial)
{
; // wait for serial port to connect. Needed for native USB port only
}
GPS.begin(9600);
while (!GPS)
{
; // wait for serial port to connect. Needed for native USB port only
}
GSM.begin(9600);
while (!GSM)
{
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println(F("GPS/GSM setup complete"));
pinMode(10, OUTPUT); // buzzer
pinMode(11, INPUT_PULLUP); // reed
pinMode(13, OUTPUT); // red led
pinMode(A0, INPUT); // ldr
pinMode(A1, INPUT); // power
digitalWrite(13, 1);
delay(3000);
digitalWrite(10, 1);
delay(250);
digitalWrite(13, 0);
digitalWrite(10, 0);
delay(250);
digitalWrite(13, 1);
digitalWrite(10, 1);
delay(250);
digitalWrite(13, 0);
digitalWrite(10, 0);
gps_timer = 0;
gsm_timer = 0;
power_off_timer = 0;
door_open_timer = 0;
send("AT+SAPBR=3,1,Contype,GPRS", 3000);
send("AT+SAPBR=3,1,APN," + apn + "", 3000);
if (apn_u != "")
{
send("AT+SAPBR=3,1,USER," + apn_u + "", 3000);
}
if (apn_p != "")
{
send("AT+SAPBR=3,1,PWD," + apn_p + "", 3000);
}
}
void loop()
{
GPS.listen();
while (GPS.available() > 0 && is_reading_gps)
{
gps.encode(GPS.read());
// Serial.print(F(": status: gps- loop: "));
// Serial.print(gps_timer);
if (gps_timer > 5000)
{
gps_timer = 0;
is_reading_gps = !is_reading_gps;
if (!is_reading_gps)
{
break;
}
}
deltaFunc();
}
if (gps_timer > 5000)
{
gps_timer = 0;
is_reading_gps = !is_reading_gps;
}
deltaFunc();
// send("AT", 1000);
// send("AT+SAPBR=1,1", 1000);
// send("AT+SAPBR=2,1", 1000);
// send("AT+HTTPINIT", 1000);
// send("AT+HTTPPARA=CID,1", 1000);
// send("AT+HTTPPARA=URL," + url + "", 3000);
// send("AT+HTTPPARA=CONTENT,application/x-www-form-urlencoded", 3000);
// send("AT+HTTPDATA=200,5000", 3000);
// String d_data = "";
// d_data += "d_id=";
// d_data += DEVICE_ID;
// d_data += "&d_pr=";
// d_data += power_data ? 1 : 0;
// d_data += "&d_pr0=";
// d_data += power_off_count;
// d_data += "&d_pr0t=";
// d_data += power_off_times;
// d_data += "&d_dr=";
// d_data += door_data ? 1 : 0;
// d_data += "&d_dr1=";
// d_data += door_open_count;
// d_data += "&d_dr1t=";
// d_data += door_open_times;
// d_data += "&d_li=";
// d_data += light_data;
// d_data += "&d_lt=";
// d_data += latitude;
// d_data += "&d_lg=";
// d_data += longitude;
// d_data += "&d_ti=";
// d_data += temp_in_data;
// d_data += "&d_to=";
// d_data += temp_out_data;
// d_data += "&d_z=xyz";
// send(d_data, 3000);
// send("AT+HTTPACTION=1", 3000);
// send("AT+HTTPREAD", 3000);
// send("AT+HTTPTERM", 3000);
// send("AT+SAPBR=0,1", 3000);
// send("AT", 1000);
}
void readData()
{
if (gps.location.isUpdated())
{
String(gps.location.lat(), 12).toCharArray(latitude, 12);
String(gps.location.lng(), 12).toCharArray(longitude, 12);
// Serial.print(F(": status: gps- data: updated: "));
// Serial.print(gps_timer);
// Serial.print(F(" | Lat: "));
// Serial.print(latitude);
// Serial.print(F(" lng: "));
// Serial.println(longitude);
}
power_data = digitalRead(A1) == 1;
door_data = digitalRead(11) == 1;
light_data = mapf(analogRead(A0), 0.0, 1023.0, 100.0, 0.0);
if (power_data != last_power_state)
{
last_power_state = power_data;
if (!power_data)
{ // check if power goes off
power_off_count++;
}
else
{ // else check if power goes on again
if (power_off_countdown > 0)
{
if (power_off_times.length() > 0)
{
power_off_times += "," + String(power_off_countdown);
}
else
{
power_off_times += String(power_off_countdown);
}
power_off_countdown = 0;
}
}
}
if (!power_data)
{ // check if power goes off
if (power_off_timer > 1000)
{
power_off_timer = 0;
power_off_countdown++; // increment power off timer
}
}
else
power_off_timer = 0; // else reset power off timer
if (door_data != last_door_state)
{
last_door_state = door_data;
if (!door_data)
{ // check if door is opened
door_open_count++;
}
else
{ // else check if door is closed again
if (door_open_countdown > 0)
{
if (door_open_times.length() > 0)
{
door_open_times += "," + String(door_open_countdown);
}
else
{
door_open_times += String(door_open_countdown);
}
door_open_countdown = 0;
}
}
}
if (!door_data)
{ // check if door is opened
if (door_open_timer > 1000)
{
door_open_timer = 0;
door_open_countdown++; // increment door open timer
}
}
else
door_open_timer = 0; // else reset door open timer
}
void deltaFunc()
{
readData();
if (gsm_stage == 1)
{
if (!gsm_data_sent)
{
GSM.println(F("AT"));
gsm_data_sent = true;
gsm_timer = 0;
}
checkIfGSMHasData();
if (gsm_timer > 1000 && gsm_data_sent)
{
gsm_timer = 0;
gsm_stage = 2;
gsm_data_sent = false;
}
}
if (gsm_stage == 2)
{
if (!gsm_data_sent)
{
GSM.println(F("AT+SAPBR=1,1"));
gsm_data_sent = true;
gsm_timer = 0;
}
checkIfGSMHasData();
if (gsm_timer > 2000 && gsm_data_sent)
{
gsm_timer = 0;
gsm_stage = 3;
gsm_data_sent = false;
}
}
if (gsm_stage == 3)
{
if (!gsm_data_sent)
{
GSM.println(F("AT+SAPBR=2,1"));
gsm_data_sent = true;
gsm_timer = 0;
}
checkIfGSMHasData();
if (gsm_timer > 2000 && gsm_data_sent)
{
gsm_timer = 0;
gsm_stage = 4;
gsm_data_sent = false;
}
}
if (gsm_stage == 4)
{
if (!gsm_data_sent)
{
GSM.println(F("AT+HTTPINIT"));
gsm_data_sent = true;
gsm_timer = 0;
}
checkIfGSMHasData();
if (gsm_timer > 2000 && gsm_data_sent)
{
gsm_timer = 0;
gsm_stage = 5;
gsm_data_sent = false;
}
}
if (gsm_stage == 5)
{
if (!gsm_data_sent)
{
GSM.println(F("AT+HTTPPARA=CID,1"));
gsm_data_sent = true;
gsm_timer = 0;
}
checkIfGSMHasData();
if (gsm_timer > 3000 && gsm_data_sent)
{
gsm_timer = 0;
gsm_stage = 6;
gsm_data_sent = false;
}
}
if (gsm_stage == 6)
{
if (!gsm_data_sent)
{
GSM.println("AT+HTTPPARA=URL," + url + "");
gsm_data_sent = true;
gsm_timer = 0;
}
checkIfGSMHasData();
if (gsm_timer > 3000 && gsm_data_sent)
{
gsm_timer = 0;
gsm_stage = 7;
gsm_data_sent = false;
}
}
if (gsm_stage == 7)
{
if (!gsm_data_sent)
{
GSM.println(F("AT+HTTPPARA=CONTENT,application/x-www-form-urlencoded"));
gsm_data_sent = true;
gsm_timer = 0;
}
checkIfGSMHasData();
if (gsm_timer > 3000 && gsm_data_sent)
{
gsm_timer = 0;
gsm_stage = 8;
gsm_data_sent = false;
}
}
if (gsm_stage == 8)
{
if (!gsm_data_sent)
{
GSM.println(F("AT+HTTPDATA=200,5000"));
gsm_data_sent = true;
gsm_timer = 0;
}
checkIfGSMHasData();
if (gsm_timer > 3000 && gsm_data_sent)
{
gsm_timer = 0;
gsm_stage = 9;
gsm_data_sent = false;
}
}
if (gsm_stage == 9)
{
if (!gsm_data_sent)
{
readData();
//
String d_data = "";
d_data += "d_id=";
d_data += DEVICE_ID;
d_data += "&d_pr=";
d_data += power_data ? 1 : 0;
d_data += "&d_pr0=";
d_data += power_off_count;
d_data += "&d_pr0t=";
d_data += power_off_times;
d_data += "&d_dr=";
d_data += door_data ? 1 : 0;
d_data += "&d_dr1=";
d_data += door_open_count;
d_data += "&d_dr1t=";
d_data += door_open_times;
d_data += "&d_li=";
d_data += light_data;
d_data += "&d_lt=";
d_data += latitude;
d_data += "&d_lg=";
d_data += longitude;
d_data += "&d_ti=";
d_data += temp_in_data;
d_data += "&d_to=";
d_data += temp_out_data;
d_data += "&d_z=xyz";
GSM.println(d_data);
gsm_data_sent = true;
gsm_timer = 0;
}
checkIfGSMHasData();
if (gsm_timer > 3000 && gsm_data_sent)
{
gsm_timer = 0;
gsm_stage = 10;
gsm_data_sent = false;
// reset states
power_off_count = 0;
door_open_count = 0;
power_off_times = "";
door_open_times = "";
}
}
if (gsm_stage == 10)
{
if (!gsm_data_sent)
{
GSM.println(F("AT+HTTPACTION=1"));
gsm_data_sent = true;
gsm_timer = 0;
}
checkIfGSMHasData();
if (gsm_timer > 3000 && gsm_data_sent)
{
gsm_timer = 0;
gsm_stage = 11;
gsm_data_sent = false;
}
}
if (gsm_stage == 11)
{
if (!gsm_data_sent)
{
GSM.println(F("AT+HTTPREAD"));
gsm_data_sent = true;
gsm_timer = 0;
}
checkIfGSMHasData();
if (gsm_timer > 3000 && gsm_data_sent)
{
gsm_timer = 0;
gsm_stage = 12;
gsm_data_sent = false;
}
}
if (gsm_stage == 12)
{
if (!gsm_data_sent)
{
GSM.println(F("AT+HTTPTERM"));
gsm_data_sent = true;
gsm_timer = 0;
}
checkIfGSMHasData();
if (gsm_timer > 3000 && gsm_data_sent)
{
gsm_timer = 0;
gsm_stage = 13;
gsm_data_sent = false;
}
}
if (gsm_stage == 13)
{
if (!gsm_data_sent)
{
GSM.println(F("AT+SAPBR=0,1"));
gsm_data_sent = true;
gsm_timer = 0;
}
checkIfGSMHasData();
if (gsm_timer > 3000 && gsm_data_sent)
{
gsm_timer = 0;
gsm_stage = 14;
gsm_data_sent = false;
}
}
if (gsm_stage == 14)
{
if (!gsm_data_sent)
{
GSM.println(F("AT"));
gsm_data_sent = true;
gsm_timer = 0;
}
checkIfGSMHasData();
if (gsm_timer > 1000 && gsm_data_sent)
{
gsm_timer = 0;
gsm_stage = 1;
gsm_data_sent = false;
}
}
}
void checkIfGSMHasData()
{
while (GSM.available())
{
Serial.write(GSM.read());
}
}
void send(String command, unsigned long timeout)
{
// Serial.println("Send ->: " + command);
GSM.println(command);
if (command.indexOf("d_id") != -1)
{
// reset states
power_off_count = 0;
door_open_count = 0;
power_off_times = "";
door_open_times = "";
}
long wtimer = millis();
while (wtimer + timeout > millis())
{
readData();
while (GSM.available())
{
Serial.write(GSM.read());
}
}
Serial.println();
}
/*
void deltaFunc() {
// Serial.print(F(": deltaFunc: called: stage: "));
// Serial.println(gsm_stage);
if (gps.location.isUpdated()) {
String(gps.location.lat(), 12).toCharArray(latitude, 12);
String(gps.location.lng(), 12).toCharArray(longitude, 12);
// Serial.print(F(": status: gps- data: updated: "));
// Serial.print(gps_timer);
// Serial.print(F(" | Lat: "));
// Serial.print(latitude);
// Serial.print(F(" lng: "));
// Serial.println(longitude);
}
power_data = digitalRead(A1) == 1;
door_data = digitalRead(11) == 1;
light_data = mapf(analogRead(A0), 0.0, 1023.0, 100.0, 0.0);
if (power_data != last_power_state) {
last_power_state = power_data;
if (!power_data) { // check if power goes off
power_off_count++;
}
else {// else check if power goes on again
if (power_off_countdown > 0) {
if (power_off_times.length() > 0) {
power_off_times += "," + String(power_off_countdown);
}
else {
power_off_times += String(power_off_countdown);
}
power_off_countdown = 0;
}
}
}
if (!power_data) { // check if power goes off
if (power_off_timer > 1000) {
power_off_timer = 0;
power_off_countdown++; // increment power off timer
}
}
else power_off_timer = 0; // else reset power off timer
if (door_data != last_door_state) {
last_door_state = door_data;
if (!door_data) { // check if door is opened
door_open_count++;
}
else {// else check if door is closed again
if (door_open_countdown > 0) {
if (door_open_times.length() > 0) {
door_open_times += "," + String(door_open_countdown);
}
else {
door_open_times += String(door_open_countdown);
}
door_open_countdown = 0;
}
}
}
if (!door_data) { // check if door is opened
if (door_open_timer > 1000) {
door_open_timer = 0;
door_open_countdown++; // increment door open timer
}
}
else door_open_timer = 0; // else reset door open timer
if (gsm_stage == 1) {
if (!gsm_data_sent) {
GSM.print(F("AT\r\n"));
gsm_data_sent = true;
gsm_timer = 0;
}
if (gsm_timer > 600 && gsm_data_sent) {
gsm_timer = 0;
gsm_stage = 2;
gsm_data_sent = false;
}
}
if (gsm_stage == 2) {
if (!gsm_data_sent) {
GSM.print(F("AT+CGATT=1\r\n"));
gsm_data_sent = true;
gsm_timer = 0;
}
if (gsm_timer > 600 && gsm_data_sent) {
gsm_timer = 0;
gsm_stage = 3;
gsm_data_sent = false;
}
}
if (gsm_stage == 3) {
if (!gsm_data_sent) {
GSM.print(F("AT+SAPBR=3,1,Contype,GPRS\r\n"));
gsm_data_sent = true;
gsm_timer = 0;
}
if (gsm_timer > 1000 && gsm_data_sent) {
gsm_timer = 0;
gsm_stage = 4;
gsm_data_sent = false;
}
}
if (gsm_stage == 4) {
if (!gsm_data_sent) {
GSM.print(F("AT+SAPBR=1,1\r\n"));
gsm_data_sent = true;
gsm_timer = 0;
}
if (gsm_timer > 1000 && gsm_data_sent) {
gsm_timer = 0;
gsm_stage = 5;
gsm_data_sent = false;
}
}
if (gsm_stage == 5) {
if (!gsm_data_sent) {
GSM.print(F("AT+SAPBR=2,1\r\n"));
gsm_data_sent = true;
gsm_timer = 0;
}
if (gsm_timer > 1000 && gsm_data_sent) {
gsm_timer = 0;
gsm_stage = 6;
gsm_data_sent = false;
}
}
if (gsm_stage == 6) {
if (!gsm_data_sent) {
GSM.print(F("AT+HTTPINIT\r\n"));
gsm_data_sent = true;
gsm_timer = 0;
}
if (gsm_timer > 1000 && gsm_data_sent) {
gsm_timer = 0;
gsm_stage = 7;
gsm_data_sent = false;
}
}
if (gsm_stage == 7) {
if (!gsm_data_sent) {
GSM.print(F("AT+HTTPPARA=CID,1\r\n"));
gsm_data_sent = true;
gsm_timer = 0;
}
if (gsm_timer > 1000 && gsm_data_sent) {
gsm_timer = 0;
gsm_stage = 8;
gsm_data_sent = false;
}
}
if (gsm_stage == 8) {
if (!gsm_data_sent) {
GSM.print(F("AT+HTTPPARA=URL,http://iot-cooler.imperial"));
gsm_data_sent = true;
gsm_timer = 0;
}
if (gsm_timer > 100 && gsm_data_sent) {
gsm_timer = 0;
gsm_stage = 9;
gsm_data_sent = false;
}
}
if (gsm_stage == 9) {
if (!gsm_data_sent) {
GSM.print(F("innovations.co.tz/api/v1/add.php?"));
gsm_data_sent = true;
gsm_timer = 0;
}
if (gsm_timer > 100 && gsm_data_sent) {
gsm_timer = 0;
gsm_stage = 10;
gsm_data_sent = false;
}
}
if (gsm_stage == 10) {
if (!gsm_data_sent) {
GSM.print("d_id=" + DEVICE_ID + "&d_pr=" + String(power_data ? 1 : 0) + "&d_pr0=" + String(power_off_count));
gsm_data_sent = true;
gsm_timer = 0;
}
if (gsm_timer > 200 && gsm_data_sent) {
gsm_timer = 0;
gsm_stage = 11;
gsm_data_sent = false;
}
}
if (gsm_stage == 11) {
if (!gsm_data_sent) {
GSM.print("&d_pr0t=" + power_off_times);
gsm_data_sent = true;
gsm_timer = 0;
}
if (gsm_timer > 200 && gsm_data_sent) {
gsm_timer = 0;
gsm_stage = 12;
gsm_data_sent = false;
}
}
if (gsm_stage == 12) {
if (!gsm_data_sent) {
GSM.print("&d_dr=" + String(door_data ? 1 : 0) + "&d_dr1=" + String(door_open_count));
gsm_data_sent = true;
gsm_timer = 0;
}
if (gsm_timer > 200 && gsm_data_sent) {
gsm_timer = 0;
gsm_stage = 13;
gsm_data_sent = false;
}
}
if (gsm_stage == 13) {
if (!gsm_data_sent) {
GSM.print("&d_dr1t=" + door_open_times);
gsm_data_sent = true;
gsm_timer = 0;
}
if (gsm_timer > 200 && gsm_data_sent) {
gsm_timer = 0;
gsm_stage = 14;
gsm_data_sent = false;
// reset states
power_off_count = 0;
door_open_count = 0;
power_off_times = "";
door_open_times = "";
}
}
if (gsm_stage == 14) {
if (!gsm_data_sent) {
GSM.print("&d_li=" + String(light_data));
gsm_data_sent = true;
gsm_timer = 0;
}
if (gsm_timer > 200 && gsm_data_sent) {
gsm_timer = 0;
gsm_stage = 15;
gsm_data_sent = false;
}
}
if (gsm_stage == 15) {
if (!gsm_data_sent) {
GSM.print("&d_lt=" + String(latitude) + "&d_lg=" + String(longitude));
gsm_data_sent = true;
gsm_timer = 0;
}
if (gsm_timer > 200 && gsm_data_sent) {
gsm_timer = 0;
gsm_stage = 16;
gsm_data_sent = false;
}
}
if (gsm_stage == 16) {
if (!gsm_data_sent) {
temp_in_data = dsFridge.getTempC();
temp_out_data = dsSurr.getTempC();
GSM.print("&d_ti=" + String(temp_in_data) + "&d_to=" + String(temp_out_data) + "\r\n");
gsm_data_sent = true;
gsm_timer = 0;
}
if (gsm_timer > 1000 && gsm_data_sent) {
gsm_timer = 0;
gsm_stage = 17;
gsm_data_sent = false;
}
}
if (gsm_stage == 16) {
if (!gsm_data_sent) {
GSM.print("&d_ti=" + String(temp_in_data) + "&d_to=" + String(temp_out_data) + "\r\n");
gsm_data_sent = true;
gsm_timer = 0;
}
if (gsm_timer > 1000 && gsm_data_sent) {
gsm_timer = 0;
gsm_stage = 17;
gsm_data_sent = false;
}
}
if (gsm_stage == 17) {
if (!gsm_data_sent) {
GSM.print(F("AT+HTTPPARA=CONTENT,application/x-www-form-urlencoded\r\n"));
gsm_data_sent = true;
gsm_timer = 0;
}
if (gsm_timer > 500 && gsm_data_sent) {
gsm_timer = 0;
gsm_stage = 18;
gsm_data_sent = false;
}
}
if (gsm_stage == 18) {
if (!gsm_data_sent) {
String url = "d_id=" + DEVICE_ID + "&d_pr=";
url += power_data ? 1 : 0;
url += "&d_pr0=";
url += power_off_count;
url += "&d_pr0t=";
url += power_off_times;
url += "&d_dr=";
url += door_data ? 1 : 0;
url += "&d_dr1=";
url += door_open_count;
url += "&d_dr1t=";
url += door_open_times;
url += "&d_li=";
url += light_data;
url += "&d_lt=";
url += latitude;
url += "&d_lg=";
url += longitude;
url += "&d_ti=";
url += temp_in_data;
url += "&d_to=";
url += temp_out_data;
int url_length = url.length();
GSM.print("AT+HTTPDATA=" + String(url_length + 4) + ",5000\r\n");
gsm_data_sent = true;
gsm_timer = 0;
}
if (gsm_timer > 5000 && gsm_data_sent) {
gsm_timer = 0;
gsm_stage = 19;
gsm_data_sent = false;
}
}
if (gsm_stage == 19) {
if (!gsm_data_sent) {
GSM.print(F("AT+HTTPACTION=0\r\n"));
gsm_data_sent = true;
gsm_timer = 0;
}
if (gsm_timer > 3000 && gsm_data_sent) {
gsm_timer = 0;
gsm_stage = 20;
gsm_data_sent = false;
}
}
if (gsm_stage == 20) {
if (!gsm_data_sent) {
GSM.print(F("AT+HTTPREAD\r\n"));
gsm_data_sent = true;
gsm_timer = 0;
}
if (gsm_timer > 5000 && gsm_data_sent) {
gsm_timer = 0;
gsm_stage = 21;
gsm_data_sent = false;
}
}
if (gsm_stage == 21) {
if (!gsm_data_sent) {
GSM.print(F("AT+HTTPTERM\r\n"));
gsm_data_sent = true;
gsm_timer = 0;
}
if (gsm_timer > 1000 && gsm_data_sent) {
gsm_timer = 0;
gsm_stage = 22;
gsm_data_sent = false;
}
}
if (gsm_stage == 22) {
if (!gsm_data_sent) {
GSM.print("AT\r\n");
gsm_data_sent = true;
gsm_timer = 0;
}
if (gsm_timer > 3000 && gsm_data_sent) {
gsm_timer = 0;
gsm_stage = 1;
gsm_data_sent = false;
}
}
// while (GSM.available() > 0 && !is_reading_gps) {
// Serial.print(GSM.readStringUntil('\r'));
// // Serial.print(F("status: gsm- loop: "));
// // Serial.println(gps_timer);
// if (gps_timer > 5000) {
// gps_timer = 0;
// is_reading_gps = !is_reading_gps;
// if (is_reading_gps) {
// break;
// }
// }
// }
}
*/
// void send(String cmd, /*String expected_reply,*/ unsigned long timeout)
// {
// // if (!gsm_data_sent) {
// // GSM.print(cmd);
// // gsm_timer = 0;
// // gsm_data_sent = true;
// // }
// // if (gsm_timer > timeout && gsm_data_sent) {
// // gsm_timer = 0;
// // gsm_stage++;
// // }
// if (cmd == "0x1A")
// {
// GSM.write(0x1A);
// }
// else
// {
// GSM.print(cmd);
// }
// String res = "";
// long c_time = millis();
// while (1)
// {
// // sensors();
// if ((millis() - c_time) > timeout)
// {
// break;
// }
// while (GSM.available() > 0)
// {
// char c = GSM.read();
// res += c;
// delay(10);
// // Serial.print(GSM.readStringUntil('\r'));
// }
// if (res != "")
// {
// Serial.print(res);
// if (
// cmd.indexOf("AT\r\n") >= 0 ||
// cmd.indexOf("AT+CFUN") >= 0 ||
// cmd.indexOf("AT+CIPSHUT") >= 0 ||
// cmd.indexOf("AT+CIPMUX") >= 0 ||
// cmd.indexOf("AT+CGATT") >= 0 ||
// cmd.indexOf("AT+CSTT") >= 0 ||
// cmd.indexOf("AT+CIICR") >= 0 ||
// cmd.indexOf("AT+CIPSTATUS") >= 0 ||
// cmd.indexOf("AT+CIPHEAD") >= 0)
// {
// if (res.indexOf("OK") >= 0 || res.indexOf("ERR") >= 0)
// {
// delay(100);
// break;
// }
// }
// else if (cmd.indexOf("AT+CIFSR") >= 0 && (res.indexOf(".") >= 0 || res.indexOf("ERR") >= 0))
// {
// delay(100);
// break;
// }
// else if (cmd.indexOf("AT+CIPSTART") >= 0 && res.indexOf("CONNECT") >= 0)
// {
// delay(100);
// break;
// }
// else if (cmd.indexOf("AT+CIPSEND") >= 0 && (res.indexOf(">") >= 0 || res.indexOf("ERR") >= 0))
// {
// delay(100);
// break;
// }
// else if (cmd.indexOf("d_") >= 0 || cmd.indexOf("Host:") >= 0 || res.indexOf("CLOSE") >= 0)
// {
// delay(100);
// break;
// }
// else if (cmd.indexOf("Connection:") >= 0)
// {
// delay(100);
// break;
// }
// else if (res.indexOf("+IPD") >= 0)
// {
// // GSM.println();
// delay(100);
// break;
// }
// if (res.indexOf("OK") >= 0 || res.indexOf("DOWNLOAD") >= 0 || res.indexOf("ERR") >= 0)
// {
// delay(1000);
// break;
// }
// else if (cmd != "0x1A" && (res.indexOf("ERR") >= 0 /* || res.indexOf("SEND OK") >= 0*/))
// {
// delay(100);
// break;
// }
// res = "";
// }
// }
// }
// void sendData()
// {
// send("AT+CPIN?\r\n", 600);
// send("ATE1\r\n", 600);
// send("AT+CSQ\r\n", 600);
// send("AT+CCID\r\n", 600);
// send("AT+CREG?\r\n", 600);
// // send("AT+SAPBR=3,1,\"APN\",\"mobile.o2.co.uk 1\"\r\n", 1000);
// // send("AT+SAPBR=3,1,\"USER\",\"o2web\"\r\n", 1000);
// // send("AT+SAPBR=3,1,\"PWD\",\"password\"\r\n", 1000);
// send("AT+HTTPPARA=URL,http://iot-cooler.imc.co.tz/api/v1/add.php?", 1000);
// send("d_id=574393", 100);
// send("&d_pr=" + String(power_data ? 1 : 0) + "&d_pr0=" + String(power_off_count), 200);
// send("&d_pr0t=" + power_off_times, 500);
// send("&d_dr=" + String(door_data ? 1 : 0) + "&d_dr1=" + String(door_open_count), 200);
// send("&d_dr1t=" + door_open_times, 500);
// send("&d_li=" + String(light_data), 100);
// send("&d_lt=" + String(latitude) + "&d_lg=" + String(longitude), 200);
// send("&d_ti=" + String(temp_in_data) + "&d_to=" + String(temp_out_data) + "\r\n", 1000);
// /////////////////////////////////////////
// // String HOST = "iot-cooler.imc.co.tz";
// send("AT\r\n", 600);
// send("AT+CFUN=1\r\n", 600);
// send("AT+CIPSHUT\r\n", 600);
// send("AT+CIPMUX=0\r\n", 600);
// send("AT+CGATT=1\r\n", 600);
// send("AT+CSTT=\"internet\",\"\",\"\"\r\n", 1000);
// send("AT+CIICR\r\n", 600);
// send("AT+CIFSR\r\n", 1000);
// send("AT+CIPSTATUS\r\n", 1000);
// send("AT+CIPHEAD=1\r\n", 1000);
// send("AT+CIPSTART=\"TCP\",\"iot-cooler.imc.co.tz\",\"80\"\r\n", 2000);
// send("AT+CIPSEND=" + String(250) + "\r\n", 1000);
// send("GET /api/v1/add.php?d_id=574393", 100);
// send("&d_pr=" + String(power_data ? 1 : 0) + "&d_pr0=" + String(power_off_count), 100);
// send("&d_pr0t=" + power_off_times, 100);
// send("&d_dr=" + String(door_data ? 1 : 0) + "&d_dr1=" + String(door_open_count), 100);
// send("&d_dr1t=" + door_open_times, 100);
// send("&d_li=" + String(light_data), 100);
// send("&d_lt=" + String(latitude) + "&d_lg=" + String(longitude), 100);
// send("&d_ti=" + String(temp_in_data) + "&d_to=" + String(temp_out_data) + " HTTP/1.1\r\n", 100);
// send("Host: iot-cooler.imc.co.tz\r\n", 100);
// send("Connection: close\r\n\r\n", 10);
// // reset states
// power_off_count = 0;
// door_open_count = 0;
// power_off_times = "";
// door_open_times = "";
// send("0x1A", 6000); // Ctrl Z // SEND OK +IPD,901:HTTP/1.1 Blah! Blah!
// send("AT+CIPCLOSE\r\n", 600);
// send("AT+CIPSHUT\r\n", 600);
// }
////////////////////////////////////////
/* mussacharles60@gmail.com +255716581823 */
/**
AT
AT+CGATT=1
AT+SAPBR=3,1,Contype,GPRS
AT+SAPBR=1,1
AT+SAPBR=2,1
AT+HTTPINIT
AT+HTTPPARA=CID,1
AT+HTTPPARA=URL,http://iot-cooler.imc.co.tz/api/v1/add.php?d_id=D57439&d_pr=0&d_pr0=0&d_pr0t=&d_dr=1&d_dr1=0&d_dr1t=&d_li=3.52&d_lt=0.0000000&d_lg=0.0000000&d_ti=26.75&d_to=26.63
AT+HTTPPARA=CONTENT,application/x-www-form-urlencoded
AT+HTTPDATA=123,5000
AT+HTTPACTION=0
AT+HTTPREAD
AT+HTTPTERM
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment