Subversion Repositories ESP8266_P1_Meter

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 raymond 1
/*
2
 Basic ESP8266 MQTT example
3
 This sketch demonstrates the capabilities of the pubsub library in combination
4
 with the ESP8266 board/library.
5
 It connects to an MQTT server then:
6
  - publishes "hello world" to the topic "outTopic" every two seconds
7
  - subscribes to the topic "inTopic", printing out any messages
8
    it receives. NB - it assumes the received payloads are strings not binary
9
  - If the first character of the topic "inTopic" is an 1, switch ON the ESP Led,
10
    else switch it off
11
 It will reconnect to the server if the connection is lost using a blocking
12
 reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to
13
 achieve the same result without blocking the main loop.
14
 To install the ESP8266 board, (using Arduino 1.6.4+):
15
  - Add the following 3rd party board manager under "File -> Preferences -> Additional Boards Manager URLs":
16
       http://arduino.esp8266.com/stable/package_esp8266com_index.json
17
  - Open the "Tools -> Board -> Board Manager" and click install for the ESP8266"
18
  - Select your ESP8266 in "Tools -> Board"
19
*/
20
 
21
#include <ESP8266WiFi.h>
22
#include <PubSubClient.h>
23
 
24
// Update these with values suitable for your network.
25
 
26
const char* ssid = "........";
27
const char* password = "........";
28
const char* mqtt_server = "broker.mqtt-dashboard.com";
29
 
30
WiFiClient espClient;
31
PubSubClient client(espClient);
32
unsigned long lastMsg = 0;
33
#define MSG_BUFFER_SIZE	(50)
34
char msg[MSG_BUFFER_SIZE];
35
int value = 0;
36
 
37
void setup_wifi() {
38
 
39
  delay(10);
40
  // We start by connecting to a WiFi network
41
  Serial.println();
42
  Serial.print("Connecting to ");
43
  Serial.println(ssid);
44
 
45
  WiFi.mode(WIFI_STA);
46
  WiFi.begin(ssid, password);
47
 
48
  while (WiFi.status() != WL_CONNECTED) {
49
    delay(500);
50
    Serial.print(".");
51
  }
52
 
53
  randomSeed(micros());
54
 
55
  Serial.println("");
56
  Serial.println("WiFi connected");
57
  Serial.println("IP address: ");
58
  Serial.println(WiFi.localIP());
59
}
60
 
61
void callback(char* topic, byte* payload, unsigned int length) {
62
  Serial.print("Message arrived [");
63
  Serial.print(topic);
64
  Serial.print("] ");
65
  for (int i = 0; i < length; i++) {
66
    Serial.print((char)payload[i]);
67
  }
68
  Serial.println();
69
 
70
  // Switch on the LED if an 1 was received as first character
71
  if ((char)payload[0] == '1') {
72
    digitalWrite(BUILTIN_LED, LOW);   // Turn the LED on (Note that LOW is the voltage level
73
    // but actually the LED is on; this is because
74
    // it is active low on the ESP-01)
75
  } else {
76
    digitalWrite(BUILTIN_LED, HIGH);  // Turn the LED off by making the voltage HIGH
77
  }
78
 
79
}
80
 
81
void reconnect() {
82
  // Loop until we're reconnected
83
  while (!client.connected()) {
84
    Serial.print("Attempting MQTT connection...");
85
    // Create a random client ID
86
    String clientId = "ESP8266Client-";
87
    clientId += String(random(0xffff), HEX);
88
    // Attempt to connect
89
    if (client.connect(clientId.c_str())) {
90
      Serial.println("connected");
91
      // Once connected, publish an announcement...
92
      client.publish("outTopic", "hello world");
93
      // ... and resubscribe
94
      client.subscribe("inTopic");
95
    } else {
96
      Serial.print("failed, rc=");
97
      Serial.print(client.state());
98
      Serial.println(" try again in 5 seconds");
99
      // Wait 5 seconds before retrying
100
      delay(5000);
101
    }
102
  }
103
}
104
 
105
void setup() {
106
  pinMode(BUILTIN_LED, OUTPUT);     // Initialize the BUILTIN_LED pin as an output
107
  Serial.begin(115200);
108
  setup_wifi();
109
  client.setServer(mqtt_server, 1883);
110
  client.setCallback(callback);
111
}
112
 
113
void loop() {
114
 
115
  if (!client.connected()) {
116
    reconnect();
117
  }
118
  client.loop();
119
 
120
  unsigned long now = millis();
121
  if (now - lastMsg > 2000) {
122
    lastMsg = now;
123
    ++value;
124
    snprintf (msg, MSG_BUFFER_SIZE, "hello world #%ld", value);
125
    Serial.print("Publish message: ");
126
    Serial.println(msg);
127
    client.publish("outTopic", msg);
128
  }
129
}