#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include <WiFi.h>
#include <ESPmDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <ArduinoMqttClient.h>
#define LED (2)
#define BUTTONPIN (34) //pin to push the button
#define ADCPIN (25) //adc1 (ADC1 = GPIO25 ADC2 = GPIO26) pin used of ESP32
#define MEASUREResistor (4700) //reference resistor used
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = "Wokwi-GUEST"; // your network SSID (name)
char pass[] = ""; // your network password (use for WPA, or use as key for WEP)
// To connect with SSL/TLS:
// 1) Change WiFiClient to WiFiSSLClient.
// 2) Change port value from 1883 to 8883.
// 3) Change broker value to a server with a known SSL/TLS root certificate
// flashed in the WiFi module.
WiFiClient wifiClient;
MqttClient mqttClient(wifiClient);
const char broker[] = "test.mosquitto.org";
int port = 1883;
const char topic[] = "arduino/simple";
const long interval = 1000;
unsigned long previousMillis = 0;
int count = 0;
#define HSPI_MISO 12 // MISO - Not needed as it is a display
#define HSPI_MOSI 13 // MOSI
#define HSPI_CLK 14 // SCK
#define HSPI_DC 26 // DC
#define HSPI_CS 27 // CS
#define HSPI_RST -1 // No Reset connected
#define USE_HSPI_PORT
#define SUPPORT_TRANSACTIONS
void setup() {
Adafruit_ILI9341 tft = Adafruit_ILI9341(HSPI_CS, HSPI_DC, HSPI_MOSI, HSPI_CLK, HSPI_RST, HSPI_MISO);
tft.begin();
tft.setCursor(15, 100);
tft.setTextColor(ILI9341_RED);
tft.setTextSize(2);
tft.println("Example of SCL");
tft.setCursor(20, 140);
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(2);
tft.println("Press The button");
tft.setCursor(20, 180);
tft.setTextColor(ILI9341_BLUE);
tft.setTextSize(5);
tft.println("See the magic");
//Initialize serial and wait for port to open:
pinMode(LED, OUTPUT);
digitalWrite(LED,HIGH);
pinMode(BUTTONPIN, INPUT);
//pinMode(ADCPIN,INPUT_PULLUP);
pinMode(ADCPIN,INPUT);
Serial.begin(115200);
Serial.println("Booting");
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pass);
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("Connection Failed! Rebooting...");
delay(5000);
ESP.restart();
}
// Port defaults to 3232
// ArduinoOTA.setPort(3232);
// Hostname defaults to esp3232-[MAC]
// ArduinoOTA.setHostname("myesp32");
// No authentication by default
// ArduinoOTA.setPassword("admin");
// Password can be set with it's md5 value as well
// MD5(admin) = 21232f297a57a5a743894a0e4a801fc3
// ArduinoOTA.setPasswordHash("21232f297a57a5a743894a0e4a801fc3");
Serial.println("You're connected to the network");
Serial.println();
// You can provide a unique client ID, if not set the library uses Arduino-millis()
// Each client must have a unique client ID
// mqttClient.setId("clientId");
// You can provide a username and password for authentication
// mqttClient.setUsernamePassword("username", "password");
Serial.print("Attempting to connect to the MQTT broker: ");
Serial.println(broker);
if (!mqttClient.connect(broker, port)) {
Serial.print("MQTT connection failed! Error code = ");
Serial.println(mqttClient.connectError());
while (1);
}
Serial.println("You're connected to the MQTT broker!");
Serial.println();
ArduinoOTA
.onStart([]() {
String type;
if (ArduinoOTA.getCommand() == U_FLASH)
type = "sketch";
else // U_SPIFFS
type = "filesystem";
// NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
Serial.println("Start updating " + type);
})
.onEnd([]() {
Serial.println("\nEnd");
})
.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
})
.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("End Failed");
});
ArduinoOTA.begin();
Serial.println("Ready");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void MeasureResistance() {
if (!digitalRead(BUTTONPIN)){
delay(1000);
digitalWrite(LED, LOW);
delay(1000);
digitalWrite(LED, HIGH);
}
}
void test() {
MeasureResistance();
}
void loop() {
// call poll() regularly to allow the library to send MQTT keep alives which
// avoids being disconnected by the broker
mqttClient.poll();
ArduinoOTA.handle();
test();
// to avoid having delays in loop, we'll use the strategy from BlinkWithoutDelay
// see: File -> Examples -> 02.Digital -> BlinkWithoutDelay for more info
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time a message was sent
previousMillis = currentMillis;
Serial.print("Sending message to topic: ");
Serial.println(topic);
Serial.print("hello ");
Serial.println(count);
// send message, the Print interface can be used to set the message contents
mqttClient.beginMessage(topic);
mqttClient.print("hello ");
mqttClient.print(count);
mqttClient.endMessage();
Serial.println();
count++;
}
}