forked from gyassine/Arduino_MQTT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMQTT_Test.ino
237 lines (191 loc) · 7.26 KB
/
MQTT_Test.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/***************************************************
Adafruit MQTT Library WINC1500 Modified by Yassine
Demo written by Limor Fried/Ladyada for Adafruit Industries.
Modified by Yassine Gangat for LE²P.
MIT license, all text above must be included in any redistribution
****************************************************/
#include <SPI.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include <WiFi101.h>
// ---------------------------- GROVE LCD Lib -------------------------
//#include <Wire.h> // i2c LIB to use LCD
#include "rgb_lcd.h" // lcd LIB
#include "arduino_secrets.h"
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
const char ssid[] = SECRET_SSID; // your network SSID (name)
const char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
int status = WL_IDLE_STATUS; // the WiFi radio's status
// int keyIndex = 0; // your network key Index number (needed only for WEP)
// Initialise the Arduino data pins for INPUT/OUTPUT
const int analogInPin = A0;
const int RELAY2 = 8;
const int mVperAmp = 185; // use 100 for 20A Module and 66 for 30A Module
/************************* Broker Setup **********************************/
#define SERVER "192.168.2.2"
#define SERVERPORT 1883
#define USERNAME "SolarPan" // NetwEmul - CtrlLoad - SolarPan
#define PWD "apqm"
/******************************* Global State ****************************/
//Set up the wifi client & Adafruit MQTT Client
WiFiClient client;
Adafruit_MQTT_Client mqtt(&client, SERVER, SERVERPORT, USERNAME, PWD);
#define halt(s) { Serial.println(F( s )); while(1); }
/****************************** Feeds ***************************************/
// Setup a feed called 'photocell' for publishing.
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
Adafruit_MQTT_Publish numbers = Adafruit_MQTT_Publish(&mqtt, USERNAME "/feeds/numbers");
// Setup a feed called 'onoff' for subscribing to changes.
Adafruit_MQTT_Subscribe onoffmsg = Adafruit_MQTT_Subscribe(&mqtt, USERNAME "/feeds/onoff");
/*************************** Sketch Code ************************************/
// --------------------------- Variables & others --------------------------------
rgb_lcd lcd;
byte heart[8] = {
0b00000,
0b01010,
0b11111,
0b11111,
0b11111,
0b01110,
0b00100,
0b00000
};
void setup() {
// ------------------------- Initialization Serial ------------------------------
while (!Serial); // wait for serial port to connect. Needed for native USB port only
Serial.begin(9600);
// Serial.println(F("Adafruit MQTT Library WINC1500 Modified by Yassine"));
// ---------------------- Initialization Wifi WINC1500 --------------------------
// Initialise the Client
// Serial.print(F("\nInit the WiFi module..."));
// check for the presence of the breakout
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println(F("WINC1500 not present"));
// don't continue:
while (true);
}
// Serial.println(F("ATWINC OK!"));
// ------------------------------ Physical part ----------------------------------
// Serial.println(F("LCD Init"));
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
#if 1
lcd.createChar(0, heart);
#endif
lcd.setCursor(0, 0);
lcd.print(F("Made with "));
lcd.write((unsigned char)0);
lcd.setCursor(0, 1);
lcd.print(F(" by Yassine"));
pinMode(RELAY2, OUTPUT);
// --------------------------- Variables & others --------------------------------
mqtt.subscribe(&onoffmsg);
}
// uint32_t x = 0;
void loop() {
// ------------------------------ MQTT Connection --------------------------------
// Ensure the connection to the MQTT server is alive (this will make the first
// connection and automatically reconnect when disconnected).
MQTT_connect();
// lcd.setRGB(255,165,0);
// ------------------------------ Subscription -----------------------------------
Adafruit_MQTT_Subscribe *subscription;
while ((subscription = mqtt.readSubscription(5000))) {
if (subscription == &onoffmsg) {
Serial.print(F("Got: "));
Serial.println((char *)onoffmsg.lastread);
if (0 == strcmp((char *)onoffmsg.lastread, "OFF")) {
lcd.setRGB(255, 0, 0);
lcd.setCursor(0, 1);
lcd.print(F("Closing "));
lcd.print(F(USERNAME));
digitalWrite(RELAY2, LOW); // Turns OFF Relays 1
}
if (0 == strcmp((char *)onoffmsg.lastread, "ON")) {
lcd.setRGB(0, 255, 0);
lcd.setCursor(0, 1);
lcd.print(F("Opening "));
lcd.print(F(USERNAME));
digitalWrite(RELAY2, HIGH); // Turns ON Relays 1
}
}
}
// ------------------------------ Publication ----------------------------------
int32_t val = mesureSimple(analogInPin);
if (val < 0) {
lcd.setRGB(245, 130, 49);
} else {
lcd.setRGB(0, 0, 117);
}
// int32_t val = mesure(analogInPin) * 100;
Serial.print(F("\nSending "));
Serial.print(val);
lcd.setCursor(0, 1);
lcd.print(F(" "));
lcd.setCursor(0, 1);
lcd.print(F(USERNAME));
lcd.setCursor(9, 1);
lcd.print(val);
// Si on met double au lieu de int32_t, plantage !!!
if (! numbers.publish(val)) {
Serial.println(F("...Failed"));
} else {
Serial.println(F("...OK!"));
}
}
// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care if connecting.
void MQTT_connect() {
int8_t ret;
// attempt to connect to Wifi network:
while (WiFi.status() != WL_CONNECTED) {
Serial.print(F("Attempting to connect to SSID: "));
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
uint8_t timeout = 10;
while (timeout && (WiFi.status() != WL_CONNECTED)) {
timeout--;
delay(1000);
}
}
// Stop if already connected.
if (mqtt.connected()) {
return;
}
Serial.print(F("Connecting to MQTT... "));
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
Serial.println(mqtt.connectErrorString(ret));
Serial.println(ret);
Serial.println(F("Retrying MQTT connection in 5 seconds..."));
mqtt.disconnect();
delay(5000); // wait 5 seconds
}
Serial.println(F("MQTT Connected!"));
}
double mesure(int analogPin) {
const int ACSoffset = 2500;
double Amps = 0;
/*double Voltage = 0;
int RawValue= 0;
RawValue = analogRead(analogPin);
/*Voltage = (RawValue / 1024.0) * 5000; // Gets you mV
Amps = ((Voltage - ACSoffset) / mVperAmp);*/
Amps = (((analogRead(analogPin) / 1024.0) * 5000 - ACSoffset) / mVperAmp);
/*Serial.print("Raw Value = " ); // shows pre-scaled value
Serial.print(RawValue);
Serial.print("\t mV = "); // shows the voltage measured
Serial.print(Voltage,3);
// the '3' after voltage allows you to display 3 digits after decimal point
Serial.print("\t Amps = "); // shows the voltage measured
Serial.println(Amps,3);
// the '3' after voltage allows you to display 3 digits after decimal point
delay(2500); */
return (Amps);
}
int mesureSimple(int analogPin) {
int Valeur = analogRead(analogPin);
return (Valeur);
}