Skip to content

Commit afe7d4c

Browse files
committed
Atualização do Código do Projeto IoT
1 parent ac6a816 commit afe7d4c

File tree

9 files changed

+3238
-747
lines changed

9 files changed

+3238
-747
lines changed

Código_Projeto_IoT/PROJETO_IoT.ino

+1,499-747
Large diffs are not rendered by default.

PubSubClient/PubSubClient.cpp

+769
Large diffs are not rendered by default.

PubSubClient/PubSubClient.h

+184
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
/*
2+
PubSubClient.h - A simple client for MQTT.
3+
Nick O'Leary
4+
http://knolleary.net
5+
*/
6+
7+
#ifndef PubSubClient_h
8+
#define PubSubClient_h
9+
10+
#include <Arduino.h>
11+
#include "IPAddress.h"
12+
#include "Client.h"
13+
#include "Stream.h"
14+
15+
#define MQTT_VERSION_3_1 3
16+
#define MQTT_VERSION_3_1_1 4
17+
18+
// MQTT_VERSION : Pick the version
19+
//#define MQTT_VERSION MQTT_VERSION_3_1
20+
#ifndef MQTT_VERSION
21+
#define MQTT_VERSION MQTT_VERSION_3_1_1
22+
#endif
23+
24+
// MQTT_MAX_PACKET_SIZE : Maximum packet size. Override with setBufferSize().
25+
#ifndef MQTT_MAX_PACKET_SIZE
26+
#define MQTT_MAX_PACKET_SIZE 256
27+
#endif
28+
29+
// MQTT_KEEPALIVE : keepAlive interval in Seconds. Override with setKeepAlive()
30+
#ifndef MQTT_KEEPALIVE
31+
#define MQTT_KEEPALIVE 15
32+
#endif
33+
34+
// MQTT_SOCKET_TIMEOUT: socket timeout interval in Seconds. Override with setSocketTimeout()
35+
#ifndef MQTT_SOCKET_TIMEOUT
36+
#define MQTT_SOCKET_TIMEOUT 60
37+
#endif
38+
39+
// MQTT_MAX_TRANSFER_SIZE : limit how much data is passed to the network client
40+
// in each write call. Needed for the Arduino Wifi Shield. Leave undefined to
41+
// pass the entire MQTT packet in each write call.
42+
//#define MQTT_MAX_TRANSFER_SIZE 80
43+
44+
// Possible values for client.state()
45+
#define MQTT_CONNECTION_TIMEOUT -4
46+
#define MQTT_CONNECTION_LOST -3
47+
#define MQTT_CONNECT_FAILED -2
48+
#define MQTT_DISCONNECTED -1
49+
#define MQTT_CONNECTED 0
50+
#define MQTT_CONNECT_BAD_PROTOCOL 1
51+
#define MQTT_CONNECT_BAD_CLIENT_ID 2
52+
#define MQTT_CONNECT_UNAVAILABLE 3
53+
#define MQTT_CONNECT_BAD_CREDENTIALS 4
54+
#define MQTT_CONNECT_UNAUTHORIZED 5
55+
56+
#define MQTTCONNECT 1 << 4 // Client request to connect to Server
57+
#define MQTTCONNACK 2 << 4 // Connect Acknowledgment
58+
#define MQTTPUBLISH 3 << 4 // Publish message
59+
#define MQTTPUBACK 4 << 4 // Publish Acknowledgment
60+
#define MQTTPUBREC 5 << 4 // Publish Received (assured delivery part 1)
61+
#define MQTTPUBREL 6 << 4 // Publish Release (assured delivery part 2)
62+
#define MQTTPUBCOMP 7 << 4 // Publish Complete (assured delivery part 3)
63+
#define MQTTSUBSCRIBE 8 << 4 // Client Subscribe request
64+
#define MQTTSUBACK 9 << 4 // Subscribe Acknowledgment
65+
#define MQTTUNSUBSCRIBE 10 << 4 // Client Unsubscribe request
66+
#define MQTTUNSUBACK 11 << 4 // Unsubscribe Acknowledgment
67+
#define MQTTPINGREQ 12 << 4 // PING Request
68+
#define MQTTPINGRESP 13 << 4 // PING Response
69+
#define MQTTDISCONNECT 14 << 4 // Client is Disconnecting
70+
#define MQTTReserved 15 << 4 // Reserved
71+
72+
#define MQTTQOS0 (0 << 1)
73+
#define MQTTQOS1 (1 << 1)
74+
#define MQTTQOS2 (2 << 1)
75+
76+
// Maximum size of fixed header and variable length size header
77+
#define MQTT_MAX_HEADER_SIZE 5
78+
79+
#if defined(ESP8266) || defined(ESP32)
80+
#include <functional>
81+
#define MQTT_CALLBACK_SIGNATURE std::function<void(char*, uint8_t*, unsigned int)> callback
82+
#else
83+
#define MQTT_CALLBACK_SIGNATURE void (*callback)(char*, uint8_t*, unsigned int)
84+
#endif
85+
86+
#define CHECK_STRING_LENGTH(l,s) if (l+2+strnlen(s, this->bufferSize) > this->bufferSize) {_client->stop();return false;}
87+
88+
class PubSubClient : public Print {
89+
private:
90+
Client* _client;
91+
uint8_t* buffer;
92+
uint16_t bufferSize;
93+
uint16_t keepAlive;
94+
uint16_t socketTimeout;
95+
uint16_t nextMsgId;
96+
unsigned long lastOutActivity;
97+
unsigned long lastInActivity;
98+
bool pingOutstanding;
99+
MQTT_CALLBACK_SIGNATURE;
100+
uint32_t readPacket(uint8_t*);
101+
boolean readByte(uint8_t * result);
102+
boolean readByte(uint8_t * result, uint16_t * index);
103+
boolean write(uint8_t header, uint8_t* buf, uint16_t length);
104+
uint16_t writeString(const char* string, uint8_t* buf, uint16_t pos);
105+
// Build up the header ready to send
106+
// Returns the size of the header
107+
// Note: the header is built at the end of the first MQTT_MAX_HEADER_SIZE bytes, so will start
108+
// (MQTT_MAX_HEADER_SIZE - <returned size>) bytes into the buffer
109+
size_t buildHeader(uint8_t header, uint8_t* buf, uint16_t length);
110+
IPAddress ip;
111+
const char* domain;
112+
uint16_t port;
113+
Stream* stream;
114+
int _state;
115+
public:
116+
PubSubClient();
117+
PubSubClient(Client& client);
118+
PubSubClient(IPAddress, uint16_t, Client& client);
119+
PubSubClient(IPAddress, uint16_t, Client& client, Stream&);
120+
PubSubClient(IPAddress, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client);
121+
PubSubClient(IPAddress, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client, Stream&);
122+
PubSubClient(uint8_t *, uint16_t, Client& client);
123+
PubSubClient(uint8_t *, uint16_t, Client& client, Stream&);
124+
PubSubClient(uint8_t *, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client);
125+
PubSubClient(uint8_t *, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client, Stream&);
126+
PubSubClient(const char*, uint16_t, Client& client);
127+
PubSubClient(const char*, uint16_t, Client& client, Stream&);
128+
PubSubClient(const char*, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client);
129+
PubSubClient(const char*, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client, Stream&);
130+
131+
~PubSubClient();
132+
133+
PubSubClient& setServer(IPAddress ip, uint16_t port);
134+
PubSubClient& setServer(uint8_t * ip, uint16_t port);
135+
PubSubClient& setServer(const char * domain, uint16_t port);
136+
PubSubClient& setCallback(MQTT_CALLBACK_SIGNATURE);
137+
PubSubClient& setClient(Client& client);
138+
PubSubClient& setStream(Stream& stream);
139+
PubSubClient& setKeepAlive(uint16_t keepAlive);
140+
PubSubClient& setSocketTimeout(uint16_t timeout);
141+
142+
boolean setBufferSize(uint16_t size);
143+
uint16_t getBufferSize();
144+
145+
boolean connect(const char* id);
146+
boolean connect(const char* id, const char* user, const char* pass);
147+
boolean connect(const char* id, const char* willTopic, uint8_t willQos, boolean willRetain, const char* willMessage);
148+
boolean connect(const char* id, const char* user, const char* pass, const char* willTopic, uint8_t willQos, boolean willRetain, const char* willMessage);
149+
boolean connect(const char* id, const char* user, const char* pass, const char* willTopic, uint8_t willQos, boolean willRetain, const char* willMessage, boolean cleanSession);
150+
void disconnect();
151+
boolean publish(const char* topic, const char* payload);
152+
boolean publish(const char* topic, const char* payload, boolean retained);
153+
boolean publish(const char* topic, const uint8_t * payload, unsigned int plength);
154+
boolean publish(const char* topic, const uint8_t * payload, unsigned int plength, boolean retained);
155+
boolean publish_P(const char* topic, const char* payload, boolean retained);
156+
boolean publish_P(const char* topic, const uint8_t * payload, unsigned int plength, boolean retained);
157+
// Start to publish a message.
158+
// This API:
159+
// beginPublish(...)
160+
// one or more calls to write(...)
161+
// endPublish()
162+
// Allows for arbitrarily large payloads to be sent without them having to be copied into
163+
// a new buffer and held in memory at one time
164+
// Returns 1 if the message was started successfully, 0 if there was an error
165+
boolean beginPublish(const char* topic, unsigned int plength, boolean retained);
166+
// Finish off this publish message (started with beginPublish)
167+
// Returns 1 if the packet was sent successfully, 0 if there was an error
168+
int endPublish();
169+
// Write a single byte of payload (only to be used with beginPublish/endPublish)
170+
virtual size_t write(uint8_t);
171+
// Write size bytes from buffer into the payload (only to be used with beginPublish/endPublish)
172+
// Returns the number of bytes written
173+
virtual size_t write(const uint8_t *buffer, size_t size);
174+
boolean subscribe(const char* topic);
175+
boolean subscribe(const char* topic, uint8_t qos);
176+
boolean unsubscribe(const char* topic);
177+
boolean loop();
178+
boolean connected();
179+
int state();
180+
181+
};
182+
183+
184+
#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
Software serial multple serial test
3+
4+
Receives from the hardware serial, sends to software serial.
5+
Receives from software serial, sends to hardware serial.
6+
7+
The circuit:
8+
* RX is digital pin 10 (connect to TX of other device)
9+
* TX is digital pin 11 (connect to RX of other device)
10+
11+
Note:
12+
Not all pins on the Mega and Mega 2560 support change interrupts,
13+
so only the following can be used for RX:
14+
10, 11, 12, 13, 50, 51, 52, 53, 62, 63, 64, 65, 66, 67, 68, 69
15+
16+
Not all pins on the Leonardo and Micro support change interrupts,
17+
so only the following can be used for RX:
18+
8, 9, 10, 11, 14 (MISO), 15 (SCK), 16 (MOSI).
19+
20+
created back in the mists of time
21+
modified 25 May 2012
22+
by Tom Igoe
23+
based on Mikal Hart's example
24+
25+
This example code is in the public domain.
26+
27+
*/
28+
#include <SoftwareSerial.h>
29+
30+
SoftwareSerial mySerial(10, 11); // RX, TX
31+
32+
void setup() {
33+
// Open serial communications and wait for port to open:
34+
Serial.begin(57600);
35+
while (!Serial) {
36+
; // wait for serial port to connect. Needed for native USB port only
37+
}
38+
39+
40+
Serial.println("Goodnight moon!");
41+
42+
// set the data rate for the SoftwareSerial port
43+
mySerial.begin(4800);
44+
mySerial.println("Hello, world?");
45+
}
46+
47+
void loop() { // run over and over
48+
if (mySerial.available()) {
49+
Serial.write(mySerial.read());
50+
}
51+
if (Serial.available()) {
52+
mySerial.write(Serial.read());
53+
}
54+
}
55+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
Software serial multple serial test
3+
4+
Receives from the two software serial ports,
5+
sends to the hardware serial port.
6+
7+
In order to listen on a software port, you call port.listen().
8+
When using two software serial ports, you have to switch ports
9+
by listen()ing on each one in turn. Pick a logical time to switch
10+
ports, like the end of an expected transmission, or when the
11+
buffer is empty. This example switches ports when there is nothing
12+
more to read from a port
13+
14+
The circuit:
15+
Two devices which communicate serially are needed.
16+
* First serial device's TX attached to digital pin 10(RX), RX to pin 11(TX)
17+
* Second serial device's TX attached to digital pin 8(RX), RX to pin 9(TX)
18+
19+
Note:
20+
Not all pins on the Mega and Mega 2560 support change interrupts,
21+
so only the following can be used for RX:
22+
10, 11, 12, 13, 50, 51, 52, 53, 62, 63, 64, 65, 66, 67, 68, 69
23+
24+
Not all pins on the Leonardo support change interrupts,
25+
so only the following can be used for RX:
26+
8, 9, 10, 11, 14 (MISO), 15 (SCK), 16 (MOSI).
27+
28+
created 18 Apr. 2011
29+
modified 19 March 2016
30+
by Tom Igoe
31+
based on Mikal Hart's twoPortRXExample
32+
33+
This example code is in the public domain.
34+
35+
*/
36+
37+
#include <SoftwareSerial.h>
38+
// software serial #1: RX = digital pin 10, TX = digital pin 11
39+
SoftwareSerial portOne(10, 11);
40+
41+
// software serial #2: RX = digital pin 8, TX = digital pin 9
42+
// on the Mega, use other pins instead, since 8 and 9 don't work on the Mega
43+
SoftwareSerial portTwo(8, 9);
44+
45+
void setup() {
46+
// Open serial communications and wait for port to open:
47+
Serial.begin(9600);
48+
while (!Serial) {
49+
; // wait for serial port to connect. Needed for native USB port only
50+
}
51+
52+
53+
// Start each software serial port
54+
portOne.begin(9600);
55+
portTwo.begin(9600);
56+
}
57+
58+
void loop() {
59+
// By default, the last intialized port is listening.
60+
// when you want to listen on a port, explicitly select it:
61+
portOne.listen();
62+
Serial.println("Data from port one:");
63+
// while there is data coming in, read it
64+
// and send to the hardware serial port:
65+
while (portOne.available() > 0) {
66+
char inByte = portOne.read();
67+
Serial.write(inByte);
68+
}
69+
70+
// blank line to separate data from the two ports:
71+
Serial.println();
72+
73+
// Now listen on the second port
74+
portTwo.listen();
75+
// while there is data coming in, read it
76+
// and send to the hardware serial port:
77+
Serial.println("Data from port two:");
78+
while (portTwo.available() > 0) {
79+
char inByte = portTwo.read();
80+
Serial.write(inByte);
81+
}
82+
83+
// blank line to separate data from the two ports:
84+
Serial.println();
85+
}
86+
87+
88+
89+
90+
91+

SoftwareSerial/keywords.txt

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#######################################
2+
# Syntax Coloring Map for SoftwareSerial
3+
# (formerly NewSoftSerial)
4+
#######################################
5+
6+
#######################################
7+
# Datatypes (KEYWORD1)
8+
#######################################
9+
10+
SoftwareSerial KEYWORD1
11+
12+
#######################################
13+
# Methods and Functions (KEYWORD2)
14+
#######################################
15+
16+
begin KEYWORD2
17+
end KEYWORD2
18+
read KEYWORD2
19+
write KEYWORD2
20+
available KEYWORD2
21+
isListening KEYWORD2
22+
overflow KEYWORD2
23+
flush KEYWORD2
24+
listen KEYWORD2
25+
peek KEYWORD2
26+
27+
#######################################
28+
# Constants (LITERAL1)
29+
#######################################
30+

SoftwareSerial/library.properties

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name=SoftwareSerial
2+
version=1.0
3+
author=Arduino
4+
maintainer=Arduino <info@arduino.cc>
5+
sentence=Enables serial communication on any digital pin.
6+
paragraph=The SoftwareSerial library has been developed to allow serial communication on any digital pin of the board, using software to replicate the functionality of the hardware UART. It is possible to have multiple software serial ports with speeds up to 115200 bps.
7+
category=Communication
8+
url=http://www.arduino.cc/en/Reference/SoftwareSerial
9+
architectures=avr
10+

0 commit comments

Comments
 (0)