Skip to content

Commit 954d4d5

Browse files
committed
New method to read the voltage from the AT+CBC command
1 parent c2bf797 commit 954d4d5

3 files changed

Lines changed: 47 additions & 29 deletions

File tree

Http.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,26 @@ void HTTP::wakeUp(){
173173
if (sendATTest() != TRUE) preInit();
174174
}
175175

176+
void HTTP::readVoltage(char *voltage){
177+
char buffer[64];
178+
cleanBuffer(buffer, sizeof(buffer));
179+
cleanBuffer(voltage, sizeof(voltage));
180+
181+
sendCmd(READ_VOLTAGE);
182+
183+
if (readBuffer(buffer, sizeof(buffer)) == TRUE){
184+
char *twoPointsPointer = strchr(buffer, ':');
185+
unsigned int twoPointsIndex = (int)(twoPointsPointer - buffer);
186+
unsigned int voltageOffset = 7;
187+
unsigned int voltageValueStartIndex = twoPointsIndex + voltageOffset;
188+
unsigned int voltageSize = 4;
189+
for (int i = voltageValueStartIndex; i < voltageValueStartIndex + voltageSize; ++i){
190+
voltage[i - voltageValueStartIndex] = buffer[i];
191+
voltage[i - voltageValueStartIndex + 1] = '\0';
192+
}
193+
}
194+
}
195+
176196
Result HTTP::setHTTPSession(const char *uri){
177197

178198
Result result;

Http.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ class HTTP : public SIM800 {
6363
Result post(const char *uri, const char *body, char *response);
6464
void sleep();
6565
void wakeUp();
66+
void readVoltage(char *voltage);
6667

6768
private:
6869
void readResponse(char *response);

HttpExample/WeatherStationExample.ino

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1+
#include <LowPower.h>
12
#include <ArduinoJson.h>
23
#include <Http.h>
34

45
unsigned long lastRunTime = 0;
56
unsigned long waitForRunTime = 0;
7+
unsigned long millisOffset = 0;
8+
unsigned int RX_PIN = 11;
9+
unsigned int TX_PIN = 12;
10+
unsigned int RST_PIN = 10;
11+
HTTP http(9600, RX_PIN, TX_PIN, RST_PIN, true);
612

7-
unsigned int RX_PIN = 7;
8-
unsigned int TX_PIN = 8;
9-
unsigned int RST_PIN = 12;
10-
HTTP http(9600, RX_PIN, TX_PIN, RST_PIN);
1113
/*
1214
* the setup routine runs once when you press reset:
1315
*/
@@ -43,56 +45,51 @@ void print(const __FlashStringHelper *message, int code = -1){
4345
}
4446
}
4547

48+
unsigned long currentMillis(){
49+
return millis() + millisOffset;
50+
}
51+
52+
void sleepEightSeconds(){
53+
LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
54+
/* The 8000 is 8 seconds, the time the clock has been sleeping */
55+
millisOffset += 8000;
56+
}
57+
4658
bool shouldTrackTimeEntry(){
4759
/*
4860
* This calculation uses the max value the unsigned long can store as key. Remember when a negative number
4961
* is assigned or the maximun is exceeded, then the module is applied to that value.
50-
*/
51-
unsigned long elapsedTime = millis() - lastRunTime;
62+
*/
63+
sleepEightSeconds();
64+
unsigned long elapsedTime = currentMillis() - lastRunTime;
5265
print(F("Elapsed time: "), elapsedTime);
5366
return elapsedTime >= waitForRunTime;
5467
}
5568

56-
void readVoltage(char *voltageBuffer){
57-
/*
58-
* We are using a voltage divider, so the voltage in each resistence is calculated by:
59-
* Vn = (Rn / R1 + .. + Rn) * Vt. In our example: V = (270/270 + 270) * 5V = 2.5v
60-
*/
61-
int input = analogRead(0); // Read the input pin. Possible values are betwen 0 (0v) to 1023 (5v)
62-
float maximumBatterVoltage = 5.0;
63-
float maximumAnalogicIput = 1023.0;
64-
float dividedVoltage = maximumBatterVoltage * input / maximumAnalogicIput;
65-
66-
float voltage = dividedVoltage * 2; // We use 2 because of our two resistences are equal
67-
/*
68-
* As the Arduino implementaion of sprintf does not support float values we need to convert it to char
69-
*/
70-
dtostrf(voltage, 4, 2, voltageBuffer);
71-
}
72-
7369
void trackTimeEntry(){
7470

7571
char response[32];
7672
char body[90];
7773
Result result;
7874

79-
print(F("Cofigure bearer: "), http.configureBearer("movistar.es"));
75+
print(F("Cofigure bearer: "), http.configureBearer("your.apn"));
8076
result = http.connect();
8177
print(F("HTTP connect: "), result);
8278

8379
unsigned int moisture = random(1023);
8480
char voltage[6];
85-
readVoltage(voltage);
86-
87-
sprintf(body, "[{\"weatherEntries\":[{\"moisture\": %d, \"currentVoltage\": %s}], \"name\": \"Arduino\"}]", moisture, voltage);
81+
http.readVoltage(voltage);
82+
83+
sprintf(body, "[{\"weatherEntry\":[{\"m\": %d, \"cv\": %s}], \"n\": \"Arduino\"}]", moisture, voltage);
8884
Serial.println(body);
89-
result = http.post("your.domain/api/devices", body, response);
85+
86+
result = http.post("your.api", body, response);
9087
print(F("HTTP POST: "), result);
9188
if (result == SUCCESS) {
9289
Serial.println(response);
9390
StaticJsonBuffer<32> jsonBuffer;
9491
JsonObject& root = jsonBuffer.parseObject(response);
95-
lastRunTime = millis();
92+
lastRunTime = currentMillis();
9693
waitForRunTime = root["waitForRunTime"];
9794

9895
print(F("Last run time: "), lastRunTime);

0 commit comments

Comments
 (0)