-
Notifications
You must be signed in to change notification settings - Fork 1
/
ESP-Sender.ino
107 lines (88 loc) · 2.34 KB
/
ESP-Sender.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <WiFi.h>
#include <AsyncMqttClient.h>
// Connection timeout;
#define CON_TIMEOUT 10*1000 // milliseconds
// MQTT Broker configuration and Wi-Fi
const char* ssid = "PTCL-Baj";
const char* password = "lenovo123455";
// MQTT Broker configuration
#define MQTT_HOST "farmer.cloudmqtt.com"
#define MQTT_PORT 16930
#define USERNAME "puycbalx"
#define PASSWORD "So2IRqdJlmCg"
// MQTT callback
AsyncMqttClient mqttClient;
TimerHandle_t mqttReconnectTimer;
TimerHandle_t wifiReconnectTimer;
bool published = false;
void sleep()
{
published = false;
esp_sleep_enable_ext0_wakeup(GPIO_NUM_4, 1);
Serial.println("Going to sleep now");
delay(2000);
esp_deep_sleep_start();
Serial.println("This will never be printed");
}
bool connectMQTT() {
Serial.println("Connecting to MQTT...");
mqttClient.connect();
int i = 0;
while ( !mqttClient.connected() && i < 9 )
{
delay(250);
Serial.print(".");
i++;
}
if ( !mqttClient.connected() )
{
Serial.println("Failed to connect to MQTT Broker");
return false;
}
else
{
Serial.println("Connected to MQTT Broker");
return true;
}
}
void onMqttConnect(bool sessionPresent)
{
uint16_t packetIdPubTemp = mqttClient.publish( "takepic", 0, false, "Yes");
if ( !packetIdPubTemp )
{
Serial.println( "Sending Failed! err: " + String( packetIdPubTemp ) );
}
else
{
Serial.println("MQTT Publish succesful");
published = true;
}
}
void setup()
{
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
randomSeed(micros());
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
// COnfigure MQTT Broker and callback
mqttReconnectTimer = xTimerCreate("mqttTimer", pdMS_TO_TICKS(2000), pdFALSE, (void*)0, reinterpret_cast<TimerCallbackFunction_t>(connectMQTT));
mqttClient.setCredentials( USERNAME, PASSWORD );
mqttClient.onConnect (onMqttConnect );
mqttClient.setServer( MQTT_HOST, MQTT_PORT );
String topic = "takepic";
}
void loop()
{
connectMQTT();
if (published)
sleep();
}