#include /* * Description: * Example for setting the minimal and maximal angle. */ static const int servo1Pin = 13; static const int servo2Pin = 12; Servo_ESP32 servo1; Servo_ESP32 servo2; /* This is a sample template program for ESP32 connecting to AWS IoT over MQTT to make distributed machine device for group project of Machine design and mechanial design in FabAcademy 2020 (students at Fablab Kamakura and Kannai). As this code contains certification information, PLEASE DO NOT UPLOAD THIS SOURCE CODE to ANY SHARABLE PLACE DIRECTLY(including FabAcademy page). If you need to upload this to sharable place, PLEASE MAKE SURE YOU DELETE VALUES OF rootCA, certificate and privateKey CHARACTORS. For preparation, please find PubSubClient.h in youf local library and chhange #define MQTT_MAX_PACKET_SIZE 128 to 512. For using this, please update as follows. 1) Set your ssid and password 2) Configure your topic (or for your use to publish/receive message) 3) Specify your topic name to subscribe in connectAWSIoT() 4) Write your logic on receiving message in mqttCallback() 5) Write your logic for publishing message in mqttLoop() // <-optional */ #include #include #include #include // 1) set your ssid and password ---------- char *ssid = "***"; char *password = "***"; // 1) end --------------------------------- // AWS_IOT endpoint setting (fixed) const char *endpoint = "a2toz7cb5zl4er-ats.iot.ap-northeast-1.amazonaws.com"; const int port = 8883; //char deviceId[4]; // random device ID in 4 digit hexadecimal (max: ffff) byte mac_addr[6]; char deviceId[20]; // 2) configure your topic (or for your use to connect to other people) ----- // Topic name needs to be format in "fa2020jp/topic[*]" // Topic for publishing (if you do not need to publish, you do not need pubTopic. char *pubTopic0 = "fa2020jp/topic0"; // Topic for subscribing char *subTopic0 = "fa2020jp/topic0"; // 2) end ----------------------------------------------------------- const char* rootCA = "-----BEGIN CERTIFICATE-----\n" \ *** "-----END CERTIFICATE-----\n"; const char* certificate = "-----BEGIN CERTIFICATE-----\n" \ *** "-----END CERTIFICATE-----\n"; const char* privateKey = "-----BEGIN RSA PRIVATE KEY-----\n" \ *** "-----END RSA PRIVATE KEY-----\n"; WiFiClientSecure httpsClient; PubSubClient mqttClient(httpsClient); void setup() { Serial.begin(115200); // Start WiFi connection Serial.println("Connecting to "); Serial.print(ssid); WiFi.begin(ssid, password); // wait until WiFi connected while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println("\nWifi Connected."); // Configure certification and MQTT Client httpsClient.setCACert(rootCA); httpsClient.setCertificate(certificate); httpsClient.setPrivateKey(privateKey); mqttClient.setServer(endpoint, port); mqttClient.setCallback(mqttCallback); // Set device Id from Mac Address WiFi.macAddress(mac_addr); sprintf(deviceId, "%02X:%02X:%02X:%02X:%02X:%02X", mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]); connectAWSIoT(); servo1.attach(servo1Pin); servo2.attach(servo2Pin); servo1.write(10); servo2.write(10); } void connectAWSIoT() { while (!mqttClient.connected()) { if (mqttClient.connect(deviceId)) { Serial.print("mqtt Connected - deviceId: "); Serial.println(deviceId); // QoS 0 is sending message only 1 time (the fastest) // QoS 1 is returning puback to publisher after send message successfully // QoS 2 is sending message only 1 time with validation (the slowest) // AWS IoT only allows QoS 0 or 1. int qos = 0; // 3) Specify your topic name to subscribe ----------- mqttClient.subscribe(subTopic0, qos); // 3) end -------------------------------------------- Serial.println("Subscribed."); } else { Serial.print("mqttClient.connect() Failed - deviceId:"); Serial.println(deviceId); Serial.print("Error state="); Serial.println(mqttClient.state()); // Wait every 5 seconds until connect to MQTT broker delay(5000); } } } int t4; int t5; int t6; void mqttCallback(char* topic, byte* payload, unsigned int length) { DynamicJsonDocument doc(256); // Write serial monitor Serial.print("Received. topic="); Serial.println(topic); // deserialize DeserializationError error = deserializeJson(doc, payload); if (error) { Serial.print("deserializeJson() failed with code "); Serial.println(error.c_str()); return; } // 4) Write your logic on received message ----------------- // dump Json in readable format serializeJsonPretty(doc, Serial); Serial.println(); // parse Json (retrieve int value from for each track) int seq = doc["seq"].as(); int interval = doc["interval"].as(); int overhead; // overhead for each beat (returned by pushSolenoids()) // int t1 = doc["t1"].as(); // int t2 = doc["t2"].as(); // int t3 = doc["t3"].as(); // int t4 = doc["t4"].as(); int t5 = doc["t5"].as(); // int t6 = doc["t6"].as(); switch (t5) { case 0: servo1.write(10); servo2.write(10); delay(500); servo1.write(120); servo2.write(120); delay(500); // } break; //for(int posDegrees = 180; posDegrees >= 0; posDegrees--) { case 1: servo1.write(10); servo2.write(10); delay(500); servo1.write(120); servo2.write(120); delay(500); break; } // 4) end ------------------------------------------------- } void mqttLoop() { if (!mqttClient.connected()) { connectAWSIoT(); } mqttClient.loop(); // 5) ----- Write your logic for publishing message from here ----- // (If you are not pubishing anything, you can delete following code) // Publisher publishes folowing element // seq: sequence status (start from 1 when start sound) // count: count (start from 1 when start publisher process (on Node-RED in RaspPi) // t1: melody1 (in MIDI note name) // t2: melody2 (ex. harmony, in MIDI note name) // t3: code, (in MIDI note name) // t4: rhythm1 (8 beat, front) 1, 0, 1, 0, 1, 0, 1, 0 // t5: rhythm2 (8 beat, back) 0, 1, 0, 1, 0, 1, 0, 1 // t6: rhythm3 (8 beat, variation) 1, 1, 0, 1, 0, 1, 1, 0 // interval: interval in delay // mqttClient.disconnect(); // 5) end--------------------------------------------------- } void loop() { // server.handleClient(); mqttLoop(); }