-
Notifications
You must be signed in to change notification settings - Fork 1
/
ESP-8266
74 lines (66 loc) · 2.1 KB
/
ESP-8266
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
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
// WiFi
const char *ssid = "MarcoBC. TP"; // Enter your WiFi name
const char *password = "carmenr09"; // Enter WiFi password
// MQTT Broker
const char *mqtt_broker = "192.168.0.13";
const char *topic = "esp8266-1/datos";
const char *mqtt_username = "curso_iot";
const char *mqtt_password = "12345";
const int mqtt_port = 1883;
String placa,np1,np2,np3,gl,rpm, vbateria, adblue, ubicacion, mensaje;
WiFiClient espClient;
PubSubClient client(espClient);
void setup() {
// Set software serial baud to 115200;
Serial.begin(115200);
// connecting to a WiFi network
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("Connecting to WiFi..");
}
Serial.println("Connected to the WiFi network");
//connecting to a mqtt broker
client.setServer(mqtt_broker, mqtt_port);
client.setCallback(callback);
while (!client.connected()) {
String client_id = "esp8266-client-";
client_id += String(WiFi.macAddress());
Serial.printf("The client %s connects to the public mqtt broker\n", client_id.c_str());
if (client.connect(client_id.c_str(), mqtt_username, mqtt_password)) {
Serial.println("Public emqx mqtt broker connected");
} else {
Serial.print("failed with state ");
Serial.print(client.state());
delay(2000);
}
}
// publish and subscribe
client.subscribe(topic);
}
void callback(char *topic, byte *payload, unsigned int length) {
Serial.print("Message arrived in topic: ");
Serial.println(topic);
Serial.print("Message:");
for (int i = 0; i < length; i++) {
Serial.print((char) payload[i]);
}
Serial.println();
Serial.println("-----------------------");
}
void loop() {
np1=random(0,9);
np2=random(0,9);
np3=random(0,9);
placa="P"+np1+"F-"+"C"+np2+np3;
gl =random(0,70);
rpm=random(1100,2200);
vbateria=random(0,24);
adblue=random(0,6);
mensaje = "{\"placa\": \""+placa+"\",\"galones\":"+gl+",\"rpm\":"+rpm+",\"bateria\":"+vbateria+",\"adblue\":"+adblue+"}";
client.publish(topic, (char*) mensaje.c_str());
client.loop();
delay(3000);
}