Skip to content

Commit b9951f4

Browse files
authored
Support for GSM/SMS device
1 parent 93338d6 commit b9951f4

File tree

5 files changed

+143
-1
lines changed

5 files changed

+143
-1
lines changed

.travis.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,4 +503,10 @@ jobs:
503503
- sed -r -i 's/\/\/(SensorMPR121 .+)/\1/' $SKETCH
504504
- sed -r -i 's/\/\/(#include <sensors\/SensorMPR121.h>)/\1/' $SKETCH
505505
- eval $OTA_CONFIGURATION
506+
- eval $COMPILE
507+
- name: "SensorGSM"
508+
script:
509+
- sed -r -i 's/\/\/(SensorGSM .+)/\1/' $SKETCH
510+
- sed -r -i 's/\/\/(#include <sensors\/SensorGSM.h>)/\1/' $SKETCH
511+
- eval $OTA_CONFIGURATION
506512
- eval $COMPILE

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ SensorDSM501A | 1 | Dust sensor module DSM501A for PM1.0 and PM2.
151151
SensorPN532 | 1 | PN532 NFC RFID Module | https://github.com/elechouse/PN532
152152
SensorCCS811 | 1 | CCS811 gas/Air Quality sensor. Measure VOC and eCO2 | https://github.com/adafruit/Adafruit_CCS811
153153
SensorMPR121 | 1 | MPR121-based capacitive touch control sensor | https://github.com/adafruit/Adafruit_MPR121
154+
SensorGSM | 1 | Send SMS through an attached serial modem (e.g. SIM900) | -
154155

155156
Those sensors requiring a pin to operate would take it as an argument in the constructor.
156157
NodeManager automatically creates all the child_ids, assigning an incremental counter. If you need to set your own child_id, pass it as the last argument to the constructor
@@ -977,6 +978,16 @@ Each sensor class may expose additional methods.
977978
bool getCodeIsValid();
978979
~~~
979980

981+
* SensorGSM
982+
~~~c
983+
// set the baud rate of the serial port for connecting to the sensor (default: 115200)
984+
void setBaudRate(uint32_t value);
985+
// [101] set the recipient phone number
986+
void setRecipient(const char* value);
987+
// send the provided text via SMS to the configured recipient
988+
void sendSMS(const char* text);
989+
~~~
990+
980991
### OTA Configuration
981992
982993
When `NODEMANAGER_OTA_CONFIGURATION` is set to ON the API presented above can be also called remotely through `SensorConfiguration`, which is automatically added to NodeManager. SensorConfiguration exposes by default child id 200 that can be used to interact with the service by sending `V_CUSTOM` type of messages and commands within the payload. For each `REQ` message, the node will respond with a `SET` message if successful.

examples/Template/Template.ino

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,9 @@ NodeManager. Just uncomment the settings you need and the sensors you want to ad
364364
//#include <sensors/SensorCCS811.h>
365365
//SensorCCS811 ccs811;
366366

367+
//#include <sensors/SensorGSM.h>
368+
//SensorGSM gsm(6,7);
369+
367370
/***********************************
368371
* Main Sketch
369372
*/

keywords.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,4 +218,5 @@ SensorPca9685W KEYWORD1
218218
SensorDSM501A KEYWORD1
219219
SensorPN532 KEYWORD1
220220
SensorCCS811 KEYWORD1
221-
SensorMPR121 KEYWORD1
221+
SensorMPR121 KEYWORD1
222+
SensorGSM KEYWORD1

sensors/SensorGSM.h

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
* The MySensors Arduino library handles the wireless radio link and protocol
3+
* between your home built sensors/actuators and HA controller of choice.
4+
* The sensors forms a self healing radio network with optional repeaters. Each
5+
* repeater and gateway builds a routing tables in EEPROM which keeps track of the
6+
* network topology allowing messages to be routed to nodes.
7+
*
8+
* Created by Henrik Ekblad <[email protected]>
9+
* Copyright (C) 2013-2017 Sensnology AB
10+
* Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
11+
*
12+
* Documentation: http://www.mysensors.org
13+
* Support Forum: http://forum.mysensors.org
14+
*
15+
* This program is free software; you can redistribute it and/or
16+
* modify it under the terms of the GNU General Public License
17+
* version 2 as published by the Free Software Foundation.
18+
*/
19+
#ifndef SensorGSM_h
20+
#define SensorGSM_h
21+
22+
/*
23+
SensorGSM: send SMS through a serial GSM modem
24+
*/
25+
26+
#include <SoftwareSerial.h>
27+
28+
class SensorGSM: public Sensor {
29+
30+
protected:
31+
SoftwareSerial* _serial;
32+
int8_t _rx_pin;
33+
int8_t _tx_pin;
34+
uint32_t _baud_rate = 115200;
35+
const char* _recipient = "";
36+
37+
public:
38+
SensorGSM(int8_t rxpin, int8_t txpin, uint8_t child_id = 0): Sensor(rxpin) {
39+
_name = "GSM";
40+
_rx_pin = rxpin;
41+
_tx_pin = txpin;
42+
children.allocateBlocks(1);
43+
new Child(this,STRING,nodeManager.getAvailableChildId(child_id), S_CUSTOM, V_CUSTOM, _name);
44+
};
45+
46+
// set the baud rate of the serial port for connecting to the sensor (default: 115200)
47+
void setBaudRate(uint32_t value) {
48+
_baud_rate = value;
49+
};
50+
51+
// [101] set the recipient phone number
52+
void setRecipient(const char* value) {
53+
_recipient = value;
54+
};
55+
56+
// send the provided text via SMS to the configured recipient
57+
void sendSMS(const char* text) {
58+
_serial->println("AT+CMGF=1");
59+
_read();
60+
_serial->print("AT+CMGS=\"");
61+
_serial->print(_recipient);
62+
_serial->println("\"\r");
63+
_read();
64+
_serial->print(text);
65+
_serial->println (char(26));
66+
_read();
67+
children.get()->setValue("OK");
68+
}
69+
70+
// define what to do during setup
71+
void onSetup() {
72+
// report immediately
73+
setReportTimerMode(IMMEDIATELY);
74+
// setup software serial
75+
_serial = new SoftwareSerial(_rx_pin,_tx_pin);
76+
// connect to the sensor
77+
_serial->begin(_baud_rate);
78+
// wait for the SIM card to connect to the carrier
79+
wait(3000);
80+
_read();
81+
// AT handshake
82+
_serial->println("AT");
83+
_read();
84+
// signal quality test
85+
_serial->println("AT+CSQ");
86+
_read();
87+
// read SIM information
88+
_serial->println("AT+CCID");
89+
_read();
90+
// check whether it has registered in the network
91+
_serial->println("AT+CREG?");
92+
_read();
93+
};
94+
95+
// what to do when receiving a message
96+
void onReceive(MyMessage* message) {
97+
Child* child = getChild(message->sensor);
98+
if (child == nullptr) return;
99+
// send the SMS using the text provided in the message payload
100+
if (message->getCommand() == C_SET) sendSMS(message->getString());
101+
};
102+
103+
#if NODEMANAGER_OTA_CONFIGURATION == ON
104+
// define what to do when receiving an OTA configuration request
105+
void onOTAConfiguration(ConfigurationRequest* request) {
106+
switch(request->getFunction()) {
107+
case 101: setRecipient(request->getValueString()); break;
108+
default: return;
109+
}
110+
};
111+
#endif
112+
113+
protected:
114+
// Forward what Software Serial received to Serial Port
115+
void _read() {
116+
wait(500);
117+
String input = _serial->readString();
118+
if (input.length() != 0) debug_verbose(PSTR(LOG_SENSOR "READ %s\n"),const_cast<char*>(input.c_str()));
119+
};
120+
};
121+
#endif

0 commit comments

Comments
 (0)