Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <PubSubClient.h>
- //The NODEMCU has a button on it - named FLASH - it is connected to D3:
- #define SWITCH_PIN D3
- #define buttonPin D7
- int buttonState = 0;
- int spamFix = 0;
- // Update these with values suitable for your network.
- const char* ssid = "DSB Free Wifi";
- const char* password = "skridspader";
- // for the in-class exercise, we'll use the public broker, but for the actual course project:
- // set-up a private mqtt broker at cloudmqtt.com (see Canvas for a howto video)
- //and uncomment / edit the following: [ you also need to change the 'client.connect' part of the code below in the code ]
- //const char *mqtt_server = "mXX.cloudmqtt.com";
- //const int mqtt_port = 17323;
- const char *mqtt_server = "m21.cloudmqtt.com";
- const int mqtt_port = 14939;
- const char *mqtt_user = "button1";
- const char *mqtt_pass = "hejhejhej";
- //see the actual function below - this is just a placeholder..
- void callback(char* byteArraytopic, byte* byteArrayPayload, unsigned int length);
- //Let's define some objects:
- WiFiClient espClient;
- //and the mqtt client:
- PubSubClient client(mqtt_server,mqtt_port,callback,espClient);
- //THIS IS THE CORE OF THE 'SUBSCRIBE'
- //everytime something happens on the topics we've subscribed [down below in the code]; this function will be fired.
- void callback(char* byteArraytopic, byte* byteArrayPayload, unsigned int length) {
- // it has the 'topic' and the message 'payload'
- //let's convert the topic name [the first array] to a string:
- String topic;
- topic=String(byteArraytopic);
- Serial.print("Message arrived [");
- Serial.print(topic);
- Serial.print("] ");
- // let's convert the incoming message [which arrives as an array of characters] to a string:
- String payload;
- for (int i = 0; i < length; i++) {
- //Serial.print((char)payload[i]);
- payload+=(char)byteArrayPayload[i];
- }
- if (String(topic) == "groupsix/controller_online"){
- client.publish("groupsix/readyCheckBut1", "1");
- }
- }
- // Setup the wifi connection...
- void setup_wifi() {
- delay(10);
- // We start by connecting to a WiFi network
- Serial.println();
- Serial.print("Connecting to ");
- Serial.println(ssid);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("");
- Serial.println("WiFi connected");
- Serial.println("IP address: ");
- Serial.println(WiFi.localIP());
- }
- // A function to connect to the mqtt server, and reconnect if the connection breaks, wifi breaks, server problem etc..
- void reconnect() {
- // Loop until we're reconnected
- while (!client.connected()) {
- Serial.print("Attempting MQTT connection...");
- // Attempt to connect:
- // if you are using cloudmqtt, uncomment the following line, and comment the one below...
- if (client.connect("but1","button1","hejhejhej")){ // make sure 'myclientID' is something unique, e.g. 'groupXX'
- //if (client.connect("myclientID")) { // make sure 'myclientID' is something unique, e.g. 'groupXX'
- Serial.println("connected");
- // Once connected, publish an announcement...
- client.publish("groupsix/readyCheckBut1", "1");
- // ... and resubscribe to all messages coming to the 'OLED' topic...
- client.subscribe("groupsix/#");
- } else {
- Serial.print("failed, rc=");
- Serial.print(client.state());
- Serial.println(" try again in 5 seconds");
- // Wait 5 seconds before retrying
- delay(5000);
- }
- }
- }
- void setup() {
- //NOTE: if the following line does not work - try changing the BUILTIN_LED to LED_BUILTIN
- //if still does not work - attach a real LED to e.g. pin D5..
- pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output
- pinMode(D3, INPUT_PULLUP); // Initialize the on-board button [named FLASH] pin as an output
- pinMode(buttonPin, INPUT);
- Serial.begin(115200);
- setup_wifi(); // self-explanatory.. check the function to see what it does..
- client.setServer(mqtt_server, mqtt_port); // connect to the mqtt server at the specified port [defined at the top]
- // set the 'callback' function, which is defined above.
- // We tell our client to fire the 'callback' function everytime there is a new message on the subscribed topics
- client.setCallback(callback);
- }
- void loop() {
- //if we're not connected to the mqtt broker:
- if (!client.connected()) {
- reconnect();
- }
- client.loop();
- Serial.println(buttonState);
- buttonState = digitalRead(buttonPin);
- if (buttonState==LOW){
- spamFix=0;
- }
- if (buttonState==HIGH){
- if (spamFix==0){
- client.publish("groupsix/b1p", "1");
- spamFix=1;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement