Arduino NBduino Library
NBduinoLibrary.cpp
Go to the documentation of this file.
1 
12 #include <arduino.h>
13 #include <NBduinoLibrary.h>
14 
15 static SoftwareSerial mySerial(10, 11); //RX, TX
16 
23 {
24 }
25 
35 NBduino::NBduino(const String mqttServer, const int mqttPort, const String mqttUser, const String mqttPassword)
36 {
37  _mqttServer=mqttServer;
38  _mqttPort=mqttPort;
39  _mqttUser=mqttUser;
40  _mqttPassword=mqttPassword;
41  _timeToConn=120000;
42 }
43 
54 NBduino::NBduino(const String mqttServer, const int mqttPort, const String mqttUser, const String mqttPassword, unsigned long timeToConn)
55 {
56  _mqttServer=mqttServer;
57  _mqttPort=mqttPort;
58  _mqttUser=mqttUser;
59  _mqttPassword=mqttPassword;
60  _timeToConn=timeToConn;
61 }
62 
69 NBduino::setAPN(String APN)
70 {
71  wakeup();
72  mySerial.begin(115200);
73  delay(1000);
74  mySerial.println("AT+IPR=4800");
75  delay (500);
76  mySerial.end();
77  mySerial.begin(4800);
78  delay(500);
79  mySerial.print("AT*MCGDEFCONT=");
80  mySerial.print("\"");
81  mySerial.print("IP");
82  mySerial.print("\"");
83  mySerial.print(",");
84  mySerial.print("\"");
85  mySerial.print(APN);
86  mySerial.println("\"");
87  delay(500);
88  mySerial.print("AT+CGDCONT=1,");
89  mySerial.print("\"");
90  mySerial.print("IP");
91  mySerial.print("\"");
92  mySerial.print(",");
93  mySerial.print("\"");
94  mySerial.print(APN);
95  mySerial.print("\"");
96  mySerial.print(",");
97  mySerial.print("\"");
98  mySerial.print("\"");
99  mySerial.println(",0,0,0,,,,,,0,,0");
100  delay(500);
101  mySerial.println("AT+CEREG=5");
102  delay(500);
103  mySerial.println("AT+COPS=?");
104 }
105 
112 {
113  wakeup();
114  mySerial.begin(115200);
115  delay(1000);
116  mySerial.println("AT+IPR=4800");
117  delay (500);
118  mySerial.end();
119  mySerial.begin(4800);
120  delay (500);
121  mySerial.println("ATE0");
122  mySerial.println("AT+CEREG=5"); //restituisce i parametri della cella LTE
123  delay (1500);
124  mySerial.flush();
125  _lastTime = millis();
126  while (1)
127  {
128  if (millis() - _lastTime < _timeToConn) {
129  // get incoming byte:
130  _inChar = mySerial.read();
131  if ( _inChar == '+') {
132  return true;
133  }
134  }
135  else return false;
136  }
137 }
138 
145 {
146  digitalWrite(_reset, LOW);
147  digitalWrite(_pwrkey, HIGH);
148  delay(2000);
149  digitalWrite(_pwrkey, LOW);
150 }
151 
158 {
159  digitalWrite(_pwrkey, HIGH);
160  delay(2000);
161  digitalWrite(_pwrkey, LOW);
162 }
163 
171 NBduino::publish(const String topic, const String value)
172 {
173  char outword[200];
174  String s=value;
175  unsigned int len, i;
176  len = value.length();
177  if(value[len-1]=='\n')
178  s[--len] = '\0';
179 
180  for(i = 0; i<len; i++) {
181  sprintf(outword+i*2, "%02X", s[i]);
182  }
183  digitalWrite( _led, HIGH );
184  delay(2000);
185  mySerial.print("AT+CMQNEW=\"");
186  mySerial.print(_mqttServer);
187  mySerial.print("\",""\"");
188  mySerial.print(_mqttPort);
189  mySerial.println("\",2400,100");
190  delay(2000);
191  mySerial.print("AT+CMQCON=0,3,");
192  mySerial.print("\"");
193  mySerial.print(topic);
194  mySerial.print("\"");
195  mySerial.print(",1200,0,0,");
196  mySerial.print("\"""");
197  mySerial.print(_mqttUser);
198  mySerial.print("\",""\"");
199  mySerial.print(_mqttPassword);
200  mySerial.println("\"");
201  delay(2000);
202  mySerial.print("AT+CMQPUB=0,\"");
203  mySerial.print("NBduino/");
204  mySerial.print(topic);
205  mySerial.print("\",1,0,0,");
206  mySerial.print(value.length());
207  mySerial.print(",");
208  mySerial.print("\"");
209  mySerial.print(outword);
210  mySerial.println("\"");
211  delay(2000);
212  mySerial.println("AT+CMQDISCON=0");
213  delay(2000);
214  digitalWrite(_led, LOW); //LED OFF
215 }
216 
223 {
224  char aMessage[200];
225  unsigned int messageSize;
226  char cha;
227  mySerial.println("AT+GSN");
228  mySerial.flush();
229  if (mySerial.available()) {
230  for (int c = 0; c <= 99; c++) aMessage[c] = 0; // clear aMessage in prep for new message
231  messageSize = 0; // set message size to zero
232 
233  while (mySerial.available()) { // loop through while data is available
234  cha = mySerial.read(); // get character
235  aMessage[messageSize] = cha; // append to aMessage
236  messageSize++; // bump message size
237  delay(20); // just to slow the reads down a bit
238  } // while
239 
240  aMessage[messageSize] = '\0'; // set last character to a null
241 
242  String exString = aMessage;
243  exString.remove(0, 2);
244  exString.remove(15);
245  return exString;
246  }
247 }
setAPN(String APN)
bool begin()
contains the NBduino class definition and the include file that the class implementation needs...
publish(const String topic, const String value)
String reqIMEI()