commit 5285436956f4e897c02cf7c7d681e5c20b4885a8 Author: Nico Melone Date: Wed Apr 28 22:32:53 2021 -0400 initial diff --git a/AWS_IoT_WiFi_copy.ino b/AWS_IoT_WiFi_copy.ino new file mode 100644 index 0000000..36eaf33 --- /dev/null +++ b/AWS_IoT_WiFi_copy.ino @@ -0,0 +1,220 @@ +#include "arduino_secrets.h" +/* + AWS IoT WiFi + + This sketch securely connects to an AWS IoT using MQTT over WiFi. + It uses a private key stored in the ATECC508A and a public + certificate for SSL/TLS authetication. + + It publishes a message every 5 seconds to arduino/outgoing + topic and subscribes to messages on the arduino/incoming + topic. + + The circuit: + - Arduino MKR WiFi 1010 or MKR1000 + + The following tutorial on Arduino Project Hub can be used + to setup your AWS account and the MKR board: + + https://create.arduino.cc/projecthub/132016/securely-connecting-an-arduino-mkr-wifi-1010-to-aws-iot-core-a9f365 + + This example code is in the public domain. +*/ + +#include +#include +#include +#include // change to #include for MKR1000 +#include + +Servo myservo; // create a servo object +int angle = 0; // variable to hold the angle for the servo motor +int pos; +int burner_status = 1; //assume on for safety +float burner_range = 170.0; +/////// Enter your sensitive data in arduino_secrets.h +const char ssid[] = SECRET_SSID; +const char pass[] = SECRET_PASS; +const char broker[] = SECRET_BROKER; +const char* certificate = SECRET_CERTIFICATE; + +WiFiClient wifiClient; // Used for the TCP socket connection +BearSSLClient sslClient(wifiClient); // Used for SSL/TLS connection, integrates with ECC508 +MqttClient mqttClient(sslClient); + +unsigned long lastMillis = 0; + +void setup() { + Serial.begin(115200); + //while (!Serial); + + if (!ECCX08.begin()) { + Serial.println("No ECCX08 present!"); + while (1); + } + + // Set a callback to get the current time + // used to validate the servers certificate + ArduinoBearSSL.onGetTime(getTime); + + // Set the ECCX08 slot to use for the private key + // and the accompanying public certificate for it + sslClient.setEccSlot(0, certificate); + + // Optional, set the client id used for MQTT, + // each device that is connected to the broker + // must have a unique client id. The MQTTClient will generate + // a client id for you based on the millis() value if not set + // + // mqttClient.setId("clientId"); + + // Set the message callback, this function is + // called when the MQTTClient receives a message + mqttClient.onMessage(onMessageReceived); + myservo.attach(5); // attaches the servo on pin 9 to the servo object + myservo.write(0); //set servo to starting position + burner_status = 0; //should be off now +} + +void loop() { + if (WiFi.status() != WL_CONNECTED) { + connectWiFi(); + } + + if (!mqttClient.connected()) { + // MQTT client is disconnected, connect + connectMQTT(); + } + + // poll for new MQTT messages and send keep alives + mqttClient.poll(); + + // publish a message roughly every 5 seconds. +// if (millis() - lastMillis > 5000) { +// lastMillis = millis(); +// +// publishMessage(); +// } + +} + +unsigned long getTime() { + // get the current time from the WiFi module + return WiFi.getTime(); +} + +void connectWiFi() { + Serial.print("Attempting to connect to SSID: "); + Serial.print(ssid); + Serial.print(" "); + + while (WiFi.begin(ssid, pass) != WL_CONNECTED) { + // failed, retry + Serial.print("."); + delay(5000); + } + Serial.println(); + + Serial.println("You're connected to the network"); + Serial.println(); +} + +void connectMQTT() { + Serial.print("Attempting to MQTT broker: "); + Serial.print(broker); + Serial.println(" "); + + while (!mqttClient.connect(broker, 8883)) { + // failed, retry + Serial.print("."); + delay(5000); + } + Serial.println(); + + Serial.println("You're connected to the MQTT broker"); + Serial.println(); + + // subscribe to a topic + mqttClient.subscribe("arduino/incoming"); +} + +void publishMessage() { + Serial.println("Publishing message"); + + // send message, the Print interface can be used to set the message contents + mqttClient.beginMessage("arduino/outgoing"); + mqttClient.print("hello "); + mqttClient.print(millis()); + mqttClient.endMessage(); +} + +void onMessageReceived(int messageSize) { + // we received a message, print out the topic and contents + Serial.print("Received a message with topic '"); + Serial.print(mqttClient.messageTopic()); + Serial.print("', length "); + Serial.print(messageSize); + Serial.println(" bytes:"); + //pos = mqttClient.parseInt(); + //Serial.println(pos); + //myservo.write(pos); + //set_to_percent(pos); + String message = mqttClient.readString(); + Serial.println("Recieved preset: " + message); + if(messageSize > 1){ //know more than 1 char means either string or number >= 10 + if(message.toInt()){ + set_to_percent(message.toInt()); + }else { + set_to_preset(message); + } + }else { //know that only one char exists meaning a number less than 10 so send to percent + if(!message.toInt()){ + off(); + }else { + set_to_percent(message.toInt()); + } + } + Serial.println(); + + Serial.println(); +} +void off(){ + myservo.write(0); + burner_status = 0; + Serial.println("ALL CLEAR ALL CLEAR ALL CLEAR!!!"); +} + +void ignite(){ + if(!burner_status){ + myservo.write(burner_range); //full open to ignite space + delay(3000);//wait for ignition + myservo.write(burner_range-20.0);//bring back from ignite space to HIGH + burner_status = 1; + Serial.println("BURNER IS ON BE CAREFUL!!!"); + } +} + +void set_to_percent(int percent){ + //percent should be 0 to 100 aka 50% is 50 + float percent_f = percent/100.0; + Serial.print("setting position to: "); + Serial.println(burner_range*percent_f); + if(!burner_status){ + ignite(); + } + myservo.write(burner_range * percent_f); //naivly assume range is 180 for now it's not actually 180 +} + +void set_to_preset(String preset){ + Serial.println(preset); + if (preset == "off"){ + Serial.println("Turning off"); + off(); + }else if(preset.equalsIgnoreCase("low")){ + set_to_percent(20); + }else if(preset.equalsIgnoreCase("medium")){ + set_to_percent(50); + }else if(preset.equalsIgnoreCase("high")){ + set_to_percent(90); + } +} diff --git a/README.md b/README.md new file mode 100644 index 0000000..f26d21b --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# Automatic Stove Burner + diff --git a/Servo_Adapter_v1.stl b/Servo_Adapter_v1.stl new file mode 100644 index 0000000..82b4516 Binary files /dev/null and b/Servo_Adapter_v1.stl differ diff --git a/Servo_Adapter_v6.stl b/Servo_Adapter_v6.stl new file mode 100644 index 0000000..88e874e Binary files /dev/null and b/Servo_Adapter_v6.stl differ diff --git a/arduino_secrets.h b/arduino_secrets.h new file mode 100644 index 0000000..c0df714 --- /dev/null +++ b/arduino_secrets.h @@ -0,0 +1,23 @@ +#define SECRET_SSID "Arduino" +#define SECRET_PASS "arduino1234" +#define SECRET_BROKER "a28odbooub39nj-ats.iot.us-east-1.amazonaws.com" +// Fill in the boards public certificate +const char SECRET_CERTIFICATE[] = R"( +-----BEGIN CERTIFICATE----- +MIICjzCCAXegAwIBAgIVAPF+nrITqEa2WHyqWXkn1CBqY2m6MA0GCSqGSIb3DQEB +CwUAME0xSzBJBgNVBAsMQkFtYXpvbiBXZWIgU2VydmljZXMgTz1BbWF6b24uY29t +IEluYy4gTD1TZWF0dGxlIFNUPVdhc2hpbmd0b24gQz1VUzAeFw0yMTA0MjEyMzM5 +MTdaFw00OTEyMzEyMzU5NTlaMB4xCzAJBgNVBAYTAlVTMQ8wDQYDVQQDEwZEYXND +U1IwWTATBgcqhkjOPQIBBggqhkjOPQMBBwNCAAQy49178G79jZMraB6B/mHL19CX +mhWVgoFB9iyKYBnb4XmNpWSoC4TYeZslGTxaZrq907K8oj7zykQquIT6L++Uo2Aw +XjAfBgNVHSMEGDAWgBSvxvTntexUFrbJt2mPmVUv3NXjmDAdBgNVHQ4EFgQUd5sD +kRMeRgEGsptRt99/IZzlRXEwDAYDVR0TAQH/BAIwADAOBgNVHQ8BAf8EBAMCB4Aw +DQYJKoZIhvcNAQELBQADggEBACen4PvKZbHTa8mgFqikvx9alpNqEwnRFVB4KJn+ +Dcmr5N/t717j/VHDZ+IPXXJ3XQBrTYKIAucn3y/avAtTW5tbsyRQfgLTBJQfQ2xg +tnnYDv3yOAK+RDmdfIB4j4Hyfi6kXHqFo28k2ftvtHbhTEFQWkBjqBKOi+jriISO +jdhjuGwCLzVeJF4LOq1eNf1NKZawK94LWKonce5uGwywvg5hS3njoUg4rzrWmsMt +xcDSYqFMiGKbIv0n+WQulphOVE2oNaSOJyTqF5YWfC49ZTvzPtxPs5xkYRJpAAq3 +GpcY7JS3/fzytI8fIve9ig7hVc9kXjyP6cYSkOCgcq/d17Y= +-----END CERTIFICATE----- + +)";