· 4 years ago · Jun 23, 2021, 06:42 PM
1/*
2 Rui Santos
3 Complete project details at https://RandomNerdTutorials.com/esp32-sim800l-publish-data-to-cloud/
4
5 Permission is hereby granted, free of charge, to any person obtaining a copy
6 of this software and associated documentation files.
7
8 The above copyright notice and this permission notice shall be included in all
9 copies or substantial portions of the Software.
10*/
11
12// Your GPRS credentials (leave empty, if not needed)
13const char apn[] = ""; // APN (example: internet.vodafone.pt) use https://wiki.apnchanger.org
14const char gprsUser[] = ""; // GPRS User
15const char gprsPass[] = ""; // GPRS Password
16
17// SIM card PIN (leave empty, if not defined)
18const char simPIN[] = "";
19
20// Server details
21// The server variable can be just a domain name or it can have a subdomain. It depends on the service you are using
22const char server[] = "example.com"; // domain name: example.com, maker.ifttt.com, etc
23const char resource[] = "/post-data.php"; // resource path, for example: /post-data.php
24const int port = 80; // server port number
25
26// Keep this API Key value to be compatible with the PHP code provided in the project page.
27// If you change the apiKeyValue value, the PHP file /post-data.php also needs to have the same key
28String apiKeyValue = "tPmAT5Ab3j7F9";
29
30// TTGO T-Call pins
31#define MODEM_RST 5
32#define MODEM_PWKEY 4
33#define MODEM_POWER_ON 23
34#define MODEM_TX 27
35#define MODEM_RX 26
36#define I2C_SDA 21
37#define I2C_SCL 22
38// BME280 pins
39#define I2C_SDA_2 18
40#define I2C_SCL_2 19
41
42// Set serial for debug console (to Serial Monitor, default speed 115200)
43#define SerialMon Serial
44// Set serial for AT commands (to SIM800 module)
45#define SerialAT Serial1
46
47// Configure TinyGSM library
48#define TINY_GSM_MODEM_SIM800 // Modem is SIM800
49#define TINY_GSM_RX_BUFFER 1024 // Set RX buffer to 1Kb
50
51// Define the serial console for debug prints, if needed
52//#define DUMP_AT_COMMANDS
53
54#include <Wire.h>
55#include <TinyGsmClient.h>
56
57#ifdef DUMP_AT_COMMANDS
58 #include <StreamDebugger.h>
59 StreamDebugger debugger(SerialAT, SerialMon);
60 TinyGsm modem(debugger);
61#else
62 TinyGsm modem(SerialAT);
63#endif
64
65#include <Adafruit_Sensor.h>
66#include <Adafruit_BME280.h>
67
68// I2C for SIM800 (to keep it running when powered from battery)
69TwoWire I2CPower = TwoWire(0);
70
71// I2C for BME280 sensor
72TwoWire I2CBME = TwoWire(1);
73Adafruit_BME280 bme;
74
75// TinyGSM Client for Internet connection
76TinyGsmClient client(modem);
77
78#define uS_TO_S_FACTOR 1000000UL /* Conversion factor for micro seconds to seconds */
79#define TIME_TO_SLEEP 3600 /* Time ESP32 will go to sleep (in seconds) 3600 seconds = 1 hour */
80
81#define IP5306_ADDR 0x75
82#define IP5306_REG_SYS_CTL0 0x00
83
84bool setPowerBoostKeepOn(int en){
85 I2CPower.beginTransmission(IP5306_ADDR);
86 I2CPower.write(IP5306_REG_SYS_CTL0);
87 if (en) {
88 I2CPower.write(0x37); // Set bit1: 1 enable 0 disable boost keep on
89 } else {
90 I2CPower.write(0x35); // 0x37 is default reg value
91 }
92 return I2CPower.endTransmission() == 0;
93}
94
95void setup() {
96 // Set serial monitor debugging window baud rate to 115200
97 SerialMon.begin(115200);
98
99 // Start I2C communication
100 I2CPower.begin(I2C_SDA, I2C_SCL, 400000);
101 I2CBME.begin(I2C_SDA_2, I2C_SCL_2, 400000);
102
103 // Keep power when running from battery
104 bool isOk = setPowerBoostKeepOn(1);
105 SerialMon.println(String("IP5306 KeepOn ") + (isOk ? "OK" : "FAIL"));
106
107 // Set modem reset, enable, power pins
108 pinMode(MODEM_PWKEY, OUTPUT);
109 pinMode(MODEM_RST, OUTPUT);
110 pinMode(MODEM_POWER_ON, OUTPUT);
111 digitalWrite(MODEM_PWKEY, LOW);
112 digitalWrite(MODEM_RST, HIGH);
113 digitalWrite(MODEM_POWER_ON, HIGH);
114
115 // Set GSM module baud rate and UART pins
116 SerialAT.begin(115200, SERIAL_8N1, MODEM_RX, MODEM_TX);
117 delay(3000);
118
119 // Restart SIM800 module, it takes quite some time
120 // To skip it, call init() instead of restart()
121 SerialMon.println("Initializing modem...");
122 modem.restart();
123 // use modem.init() if you don't need the complete restart
124
125 // Unlock your SIM card with a PIN if needed
126 if (strlen(simPIN) && modem.getSimStatus() != 3 ) {
127 modem.simUnlock(simPIN);
128 }
129
130 // You might need to change the BME280 I2C address, in our case it's 0x76
131 if (!bme.begin(0x76, &I2CBME)) {
132 Serial.println("Could not find a valid BME280 sensor, check wiring!");
133 while (1);
134 }
135
136 // Configure the wake up source as timer wake up
137 esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
138}
139
140void loop() {
141 SerialMon.print("Connecting to APN: ");
142 SerialMon.print(apn);
143 if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
144 SerialMon.println(" fail");
145 }
146 else {
147 SerialMon.println(" OK");
148
149 SerialMon.print("Connecting to ");
150 SerialMon.print(server);
151 if (!client.connect(server, port)) {
152 SerialMon.println(" fail");
153 }
154 else {
155 SerialMon.println(" OK");
156
157 // Making an HTTP POST request
158 SerialMon.println("Performing HTTP POST request...");
159 // Prepare your HTTP POST request data (Temperature in Celsius degrees)
160 String httpRequestData = "api_key=" + apiKeyValue + "&value1=" + String(bme.readTemperature())
161 + "&value2=" + String(bme.readHumidity()) + "&value3=" + String(bme.readPressure()/100.0F) + "";
162 // Prepare your HTTP POST request data (Temperature in Fahrenheit degrees)
163 //String httpRequestData = "api_key=" + apiKeyValue + "&value1=" + String(1.8 * bme.readTemperature() + 32)
164 // + "&value2=" + String(bme.readHumidity()) + "&value3=" + String(bme.readPressure()/100.0F) + "";
165
166 // You can comment the httpRequestData variable above
167 // then, use the httpRequestData variable below (for testing purposes without the BME280 sensor)
168 //String httpRequestData = "api_key=tPmAT5Ab3j7F9&value1=24.75&value2=49.54&value3=1005.14";
169
170 client.print(String("POST ") + resource + " HTTP/1.1\r\n");
171 client.print(String("Host: ") + server + "\r\n");
172 client.println("Connection: close");
173 client.println("Content-Type: application/x-www-form-urlencoded");
174 client.print("Content-Length: ");
175 client.println(httpRequestData.length());
176 client.println();
177 client.println(httpRequestData);
178
179 unsigned long timeout = millis();
180 while (client.connected() && millis() - timeout < 10000L) {
181 // Print available data (HTTP response from server)
182 while (client.available()) {
183 char c = client.read();
184 SerialMon.print(c);
185 timeout = millis();
186 }
187 }
188 SerialMon.println();
189
190 // Close client and disconnect
191 client.stop();
192 SerialMon.println(F("Server disconnected"));
193 modem.gprsDisconnect();
194 SerialMon.println(F("GPRS disconnected"));
195 }
196 }
197 // Put ESP32 into deep sleep mode (with timer wake up)
198 esp_deep_sleep_start();
199}
200