Issue
Code backup
This commit is contained in:
Binary file not shown.
@@ -0,0 +1,25 @@
|
||||
// print a msg Hello word
|
||||
|
||||
//#include <iostream>
|
||||
//
|
||||
//int main(){
|
||||
// std::cout << "Hello World!";
|
||||
// return 0;
|
||||
//}
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
vector<string> msg {"Hello", "C++", "World", "from", "VS Code", "and the C++ extension!"};
|
||||
|
||||
for (const string& word : msg)
|
||||
{
|
||||
cout << word << " ";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,22 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
cout << "- Sum of Two integers -" << endl;
|
||||
|
||||
int fistNumber, secondNumber, sumOfTwoNumbers;
|
||||
|
||||
cout << "Int1: ";
|
||||
cin >> fistNumber;
|
||||
cout << "Int2: ";
|
||||
cin >> secondNumber;
|
||||
|
||||
// sum of two numbers in stored in variable sumOfTwoNumbers
|
||||
sumOfTwoNumbers = fistNumber + secondNumber;
|
||||
|
||||
// Print sum
|
||||
cout << fistNumber << " + " << secondNumber << " = " << sumOfTwoNumbers << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,23 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
cout << "- Quotient & Remainder -" << endl;
|
||||
|
||||
int divisor, dividend, quotient, remainder;
|
||||
|
||||
cout << "Enter dividend: ";
|
||||
cin >> dividend;
|
||||
|
||||
cout << "Enter divisor: ";
|
||||
cin >> divisor;
|
||||
|
||||
quotient = dividend / divisor;
|
||||
remainder = dividend % divisor;
|
||||
|
||||
cout << "Quotient = " << quotient << endl;
|
||||
cout << "Remainder = " << remainder << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,30 @@
|
||||
// C++ Swap Number
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
int a = 5, b = 10, c = 5, d = 10, temp;
|
||||
|
||||
cout << "Swap Numbers (Using Temporary Variable)" << "\nBefore swapping." << endl;
|
||||
cout << "a = " << a << ", b = " << b << endl;
|
||||
|
||||
temp = a;
|
||||
a = b;
|
||||
b = temp;
|
||||
|
||||
cout << "\nAfter swapping." << endl;
|
||||
cout << "a = " << a << ", b = " << b << endl;
|
||||
|
||||
cout << "\nSwap Numbers Without Using Temporary Variable" << "\nBefore swapping." << endl;
|
||||
cout << "a = " << c << ", b = " << d << endl;
|
||||
|
||||
c = c + d;
|
||||
d = c - d;
|
||||
c = c - d;
|
||||
|
||||
cout << "\nAfter swapping." << endl;
|
||||
cout << "a = " << c << ", b = " << d << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,10 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
char c;
|
||||
cout << "Enter a character: ";
|
||||
cin >> c;
|
||||
cout << "ASCII Value of " << c << " is " << int(c);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,235 @@
|
||||
#include <esp_now.h>
|
||||
#include <WiFI.h>
|
||||
#include <SimpleDHT.h>
|
||||
#include <WiFiUdp.h>
|
||||
#include <Arduino_SNMP.h>
|
||||
#include <SSD1306.h>
|
||||
#include <Fs.h>
|
||||
#include <SPIFS.h>
|
||||
|
||||
#define DHTPIN 4
|
||||
#define INTERVAL 100
|
||||
#define ESPNOW_CHANNEL 1
|
||||
#define CONFIG_PATH "/conf.bin"
|
||||
#define SSID "SSID"
|
||||
#define PASSWORD "12345678"
|
||||
#define IP "192.168.0.134"
|
||||
|
||||
WiFiUDP udp;
|
||||
SNMPAgent snmp = SNMPAgent("public"); //Inicia o SMMPAgent
|
||||
|
||||
//Referências para o SNMP
|
||||
char* strHumidity;
|
||||
char* strTemperature;
|
||||
|
||||
//Valores caso nada esteja salvo no arquivo de configuração
|
||||
int maxHumidity = 65;
|
||||
int minHumidity = 55;
|
||||
|
||||
//parametros: address,SDA,SCL
|
||||
SSD1306 display(0x3c, 21, 22); //construtor do objeto que controlaremos o display
|
||||
uint8_t slaveMacAddress[] = {0x1A,0xFE,0x34,0xA5,0x90,0x69};
|
||||
// uint8_t slaveMacAddress[] = {0x18,0xFE,0x34,0xA5,0x90,0x69};
|
||||
|
||||
esp_now_peer_info_t slave;
|
||||
|
||||
//Objeto que realiza a leitura da umidade
|
||||
SimpleDHT22 dht;
|
||||
|
||||
//Variável para guardarmos o valor da umidade
|
||||
float humidity = 0;
|
||||
|
||||
//Variável para guardarmos o valor da temperatura
|
||||
float temperature = 0;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
|
||||
strHumidity = (char*)malloc(6);
|
||||
strTemperature = (char*)malloc(6);
|
||||
memset(strHumidity, 0, 6);
|
||||
memset(strTemperature, 0, 6);
|
||||
|
||||
if(SPIFFS.begin(true))
|
||||
{
|
||||
loadConfig();
|
||||
}
|
||||
else
|
||||
{
|
||||
//Se não conseguiu inicializar
|
||||
Serial.println("SPIFFS Mount Failed");
|
||||
}
|
||||
|
||||
setupWiFi();
|
||||
setupSNMP();
|
||||
setupDisplay();
|
||||
setupESPNow();
|
||||
setupSlave();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
readSensor();
|
||||
verifySNMP();
|
||||
showOnDisplay();
|
||||
verifyHumidity();
|
||||
delay(INTERVAL);
|
||||
}
|
||||
|
||||
void setupWiFi()
|
||||
{
|
||||
WiFi.disconnect();
|
||||
WiFi.mode(WIFI_STA);
|
||||
|
||||
WiFi.begin(SSID, PASSWORD);
|
||||
Serial.println("");
|
||||
|
||||
// Wait for connection
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(500);
|
||||
Serial.print(".");
|
||||
}
|
||||
|
||||
Serial.println("");
|
||||
Serial.print("Connected to ");
|
||||
Serial.println(SSID);
|
||||
|
||||
//Configura o IP
|
||||
IPAddress ipAddress;
|
||||
ipAddress.fromString(IP);
|
||||
WiFi.config(ipAddress, WiFi.gatewayIP(), WiFi.subnetMask());
|
||||
|
||||
Serial.print("IP address: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
}
|
||||
|
||||
void setupSNMP()
|
||||
{
|
||||
//Inicializa o snmp
|
||||
snmp.setUDP(&udp);
|
||||
snmp.begin();
|
||||
//Adiciona o OID para umidade (apenas leitura)
|
||||
snmp.addStringHandler(".1.3.6.1.4.1.12345.0", &strHumidity, false);
|
||||
//Adiciona o OID para temperatura (apenas leitura)
|
||||
snmp.addStringHandler(".1.3.6.1.4.1.12345.1", &strTemperature, false);
|
||||
//Adiciona o OID para umidade máxima (leitura e escrita)
|
||||
snmp.addIntegerHandler(".1.3.6.1.4.1.12345.2", &maxHumidity, true);
|
||||
//Adiciona o OID para umidade mínima (leitura e escrita)
|
||||
snmp.addIntegerHandler(".1.3.6.1.4.1.12345.3", &minHumidity, true);
|
||||
}
|
||||
|
||||
void setupDisplay(){
|
||||
display.init(); //inicializa o display
|
||||
display.flipScreenVertically();
|
||||
display.setFont(ArialMT_Plain_10); //configura a fonte
|
||||
}
|
||||
|
||||
void setupESPNow() {
|
||||
//Se a inicialização foi bem sucedida
|
||||
if (esp_now_init() == ESP_OK) {
|
||||
Serial.println("ESPNow Init Success");
|
||||
}
|
||||
//Se houve erro na inicialização
|
||||
else {
|
||||
Serial.println("ESPNow Init Failed");
|
||||
ESP.restart();
|
||||
}
|
||||
}
|
||||
|
||||
void verifySNMP()
|
||||
{
|
||||
//Deve ser sempre chamado durante o loop principal
|
||||
snmp.loop();
|
||||
|
||||
//Se aconteceu alteração de um dos valores
|
||||
if(snmp.setOccurred)
|
||||
{
|
||||
//Salva as os valores
|
||||
saveConfig();
|
||||
//Reseta a flag de alteração
|
||||
snmp.resetSetOccurred();
|
||||
}
|
||||
}
|
||||
|
||||
//Verifica se a umidade está fora dos limites e informa ao ESP8266
|
||||
//se o relê deve ficar ligado ou desligado
|
||||
void verifyHumidity(){
|
||||
if(humidity > maxHumidity)
|
||||
{
|
||||
sendRelayStatus(LOW);
|
||||
}
|
||||
else if(humidity < minHumidity)
|
||||
{
|
||||
sendRelayStatus(HIGH);
|
||||
}
|
||||
}
|
||||
|
||||
//Função responsável por realizar a leitura
|
||||
//da umidade e temperatura
|
||||
void readSensor(){
|
||||
float h, t;
|
||||
int status = dht.read2(DHTPIN, &t, &h, NULL);
|
||||
|
||||
if (status == SimpleDHTErrSuccess) {
|
||||
humidity = h;
|
||||
temperature = t;
|
||||
//Transforma os dados em string
|
||||
String strH = String(humidity);
|
||||
strH.toCharArray(strHumidity, strH.length());
|
||||
String strT = String(temperature);
|
||||
strT.toCharArray(strTemperature, strT.length());
|
||||
}
|
||||
}
|
||||
|
||||
//Mostra a umidade no display
|
||||
void showOnDisplay(){
|
||||
//apaga o conteúdo do display
|
||||
display.clear();
|
||||
display.setTextAlignment(TEXT_ALIGN_LEFT);
|
||||
display.setFont(ArialMT_Plain_16);
|
||||
|
||||
display.drawString(0, 0, "Humidity: ");
|
||||
display.drawString(70, 0, String(humidity));
|
||||
display.drawString(0, 30, "Temperat: ");
|
||||
display.drawString(75, 30, String(temperature));
|
||||
display.display(); //mostra o conteúdo na tela
|
||||
|
||||
}
|
||||
|
||||
void saveConfig()
|
||||
{
|
||||
Serial.println("saveConfig");
|
||||
//Abre o arquivo para escrita
|
||||
File file = SPIFFS.open(CONFIG_PATH, FILE_WRITE);
|
||||
|
||||
//Se não conseguiu abrir/criar o arquivo
|
||||
if(!file)
|
||||
{
|
||||
Serial.println("Failed to open file for writing");
|
||||
return;
|
||||
}
|
||||
|
||||
file.seek(0);
|
||||
file.write((uint8_t*)&maxHumidity, sizeof(maxHumidity));
|
||||
file.write((uint8_t*)&minHumidity, sizeof(minHumidity));
|
||||
//Fecha o arquivo
|
||||
file.close();
|
||||
}
|
||||
|
||||
void loadConfig()
|
||||
{
|
||||
Serial.println("loadConfig");
|
||||
File file = SPIFFS.open(CONFIG_PATH, FILE_READ);
|
||||
|
||||
//Se arquivo não existe
|
||||
if(!file)
|
||||
{
|
||||
//Na primeira vez o arquivo ainda não foi criado
|
||||
Serial.println("Failed to open file for reading");
|
||||
return;
|
||||
}
|
||||
|
||||
file.read((uint8_t*)&maxHumidity, sizeof(maxHumidity));
|
||||
file.read((uint8_t*)&minHumidity, sizeof(minHumidity));
|
||||
//Fecha o arquivo
|
||||
file.close();
|
||||
}
|
||||
Reference in New Issue
Block a user