Codes for CupCarbon IoT

CupCarbon

Download starting codes

Code Arduino: ESP32 (MQTT)

Subscribe (independent node)

#include <WiFi.h>
#include <PubSubClient.h>

const char* wifi_ssid = "xxxxxx";
const char* wifi_password = "yyyyyy";

WiFiClient espClient;
PubSubClient client(espClient);

int pin = 2;

void callback(char* topic, byte* payload, unsigned int length) {
   if (payload[0] == '1') {
      digitalWrite(pin, 1);
   }
   if (payload[0] == '0') {
      digitalWrite(pin, 0);
   }
}

void setup() {
   pinMode(pin, OUTPUT);
   WiFi.begin(wifi_ssid, wifi_password);

   while (WiFi.status() != WL_CONNECTED) {
      delay(500);
   }

   client.setServer("mqtt.eclipse.org", 1883);
}

void loop() {
   if (!client.connected()) {
      if (client.connect("CUPCARBON-ID")) {
         client.subscribe("cupcarbon2020/room1/sensor1");
         client.setCallback(callback);
         digitalWrite(pin, 1);
         delay(100);
         digitalWrite(pin, 0);
         delay(100);
         digitalWrite(pin, 1);
         delay(100);
         digitalWrite(pin, 0);
         delay(100);
      } 
   }

   client.loop();
   delay(1);
}

Publish -Blink- (independent node)

#include <WiFi.h>
#include <PubSubClient.h>

const char* wifi_ssid = "xxxxxx";
const char* wifi_password = "yyyyyy";

WiFiClient espClient;
PubSubClient client(espClient);

int pin = 2;

void setup() { 
   pinMode(pin, OUTPUT);
   WiFi.begin(wifi_ssid, wifi_password);

   while (WiFi.status() != WL_CONNECTED) {
      delay(500);
   }

   client.setServer("mqtt.eclipse.org", 1883);
}

void loop() {
   if (!client.connected()) {
      if (client.connect("CUPCARBON-ID")) {
         digitalWrite(pin, 1);
         delay(100);
         digitalWrite(pin, 0);
         delay(100);
         digitalWrite(pin, 1);
         delay(100);
         digitalWrite(pin, 0);
         delay(100);
      } 
   }
   client.publish("cupcarbon2020/room1/sensor1", "1");
   delay(2000);
   client.publish("cupcarbon2020/room1/sensor1", "0");
   delay(2000);

   client.loop();
}

Publish -Sensor- (independent node)

#include <WiFi.h>
#include <PubSubClient.h>

const char* wifi_ssid = "xxxxxx";
const char* wifi_password = "yyyyyy";

WiFiClient espClient;
PubSubClient client(espClient);

int pin = 2;

void setup() { 
   pinMode(pin, OUTPUT);
   WiFi.begin(wifi_ssid, wifi_password);

   while (WiFi.status() != WL_CONNECTED) {
      delay(500);
   }

   client.setServer("mqtt.eclipse.org", 1883);
}

void loop() {
   if (!client.connected()) {
      if (client.connect("CUPCARBON-ID")) {
         digitalWrite(pin, 1);
         delay(100);
         digitalWrite(pin, 0);
         delay(100);
         digitalWrite(pin, 1);
         delay(100);
         digitalWrite(pin, 0);
         delay(100);
      } 
   }
   int v = random(20, 31);
   char s[2] ;
   sprintf(s, "%d", v);
   client.publish("bounceur/sensor", s);
   delay(1000);

   client.loop();
}

Publish (real node - can be visualized in CupCarbon)

#include <WiFi.h>
#include <PubSubClient.h>

const char* ssid = "wifi_ssid";
const char* password = "wifi_passwork";

const char* mqtt_server = "mqtt.eclipse.org";
const char* s_topic = "cupcarbon_sim/.../s/1";
const char* r_topic = "cupcarbon_sim/.../r/1"; 
const char* l_topic = "cupcarbon_sim/.../led/1";

const char* led_topic = "cupcarbon/led"; 
const char* rec_topic = "cupcarbon/rec"; 

WiFiClient espClient;
PubSubClient client(espClient);

boolean simulating = false;

unsigned int pin_led = 2;

void callback(char* topic, byte* payload, unsigned int length) {
   if(strcmp(topic,l_topic)==0) {
      if (payload[0] == '1') {
         digitalWrite(pin_led, HIGH);
         client.publish(s_topic, "mark:1");
      }
      if (payload[0] == '0') {
         digitalWrite(pin_led, LOW);
         client.publish(s_topic, "mark:0");
      }
   }
   if(strcmp(topic, r_topic)==0) {
      if(payload[0]=='H' && payload[1]=='I') {
         client.publish(s_topic, "YES");
         simulating = true;
      }
      if(payload[0]=='0') {
         simulating = false;
      }
   }

   if(strcmp(topic, rec_topic)==0) {
      if (payload[0] == '1') {
         digitalWrite(pin_led, HIGH);
         client.publish(s_topic, "MARK:1");
      }
      if (payload[0] == '0') {
         digitalWrite(pin_led, LOW);
         client.publish(s_topic, "MARK:0");
      }
   }
}

void connect() {
   while (!client.connected()) {
      if (client.connect("BOUNCEUR-ESP1")) {
         client.subscribe(l_topic);
         client.subscribe(r_topic);
         client.publish(s_topic, "YES");
         digitalWrite(2, 1);
         delay(100);
         digitalWrite(2, 0);
         delay(100);
         digitalWrite(2, 1);
         delay(100);
         digitalWrite(2, 0);
         //------
         client.subscribe(rec_topic);
      }
   }
}

void setup() {
   pinMode(pin_led, OUTPUT);

   delay(10);
   WiFi.begin(ssid, password);
   while (WiFi.status() != WL_CONNECTED) {
      delay(500);
   }
   client.setServer(mqtt_server, 1883);
   client.setCallback(callback);
}

void loop() {
   if (!client.connected()) {
      connect();
   }
   client.loop();
   if(simulating) {
      // Write your code here
      client.publish(led_topic, "1");
      delay(2000);
      client.publish(led_topic, "0");
      delay(2000);
   }
   delay(10);
}