/*
This sketch demonstrates how to scan WiFi networks.
The API is almost the same as with the WiFi Shield library,
the most obvious difference being the different file you need to include:
*/
#include "WiFi.h"
#include <ArduinoHttpClient.h>
#include "AsyncTCP.h"
#include "DHT.h"
#include <HTTPClient.h>
#include <Preferences.h>
#define DHTPIN 32
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
Preferences preferences;
const char * ssid = "Wokwi-GUEST";
const char * password = "";
//const char* ssid = "aetos";
//const char* password = "1234567 9";
const char * host = "XXX.XXX.XXX.XXX";
const uint16_t port = XXXX;
const int out1 = 12;
const int out2 = 13;
const int out3 = 14;
const int out4 = 15;
const int out5 = 16;
const int out6 = 17;
const int out7 = 18;
const int out8 = 19;
const int led4 = 26;
const int input1 = 32;
const int input2 = 33;
const int input3 = 34;
const int input4 = 35;
QueueHandle_t xQueuesend, xQueuereceive;
int konektovano = 0;
uint32_t chipId = 0;
char * prefiks = "";
char * temp = "";
char * slanje = "";
char * prethodnoSlanje = "";
char * prvoSlanje = "";
WiFiClient client1;
HttpClient cliente = HttpClient(client1,host,port);
void setup() {
preferences.begin("gpio", false);
pinMode(out1, OUTPUT);
pinMode(out2, OUTPUT);
pinMode(out3, OUTPUT);
pinMode(out4, OUTPUT);
pinMode(out5, OUTPUT);
pinMode(out6, OUTPUT);
pinMode(out7, OUTPUT);
pinMode(out8, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(input1, INPUT);
pinMode(input2, INPUT);
pinMode(input3, INPUT);
pinMode(input4, INPUT);
digitalWrite(out1, preferences.getBool("out1", false));
digitalWrite(out2, preferences.getBool("out2", false));
digitalWrite(out3, preferences.getBool("out3", false));
digitalWrite(out4, preferences.getBool("out4", false));
digitalWrite(out5, preferences.getBool("out5", false));
digitalWrite(out6, preferences.getBool("out6", false));
digitalWrite(out7, preferences.getBool("out7", false));
digitalWrite(out8, preferences.getBool("out8", false));
// task variable to run on core 0 and controls socket communication
TaskHandle_t Task1;
Serial.begin(115200);
// Set WiFi to station mode and disconnect from an AP if it was previously connected
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
Serial.println("Setup done");
// try to connect to wifi, led 4 blinks
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
digitalWrite(led4, HIGH);
delay(200);
digitalWrite(led4, LOW);
Serial.print(".");
delay(200);
}
// wifi connection done, now try to connect to tcp/server
delay(500);
Serial.println("\nConnect done");
digitalWrite(led4, HIGH);
//cliente.get("");
//int statusCode = cliente.responseStatusCode();
//Serial.print("Status: ");
//Serial.println(statusCode);
// create queues for communication between processes
/* Create a queue capable of containing 10 char [50] */
//void *message = malloc( sizeof(char) * 50 );
xQueuesend = xQueueCreate(10, sizeof(char) * 50);
xQueuereceive = xQueueCreate(10, sizeof(char) * 50);
if (xQueuesend == NULL || xQueuereceive == NULL) {
Serial.println("\nError creating queues");
}
xTaskCreatePinnedToCore(
clienttcp, /* Function to implement the task */
"Task1", /* Name of the task */
10000, /* Stack size in words */
NULL, /* Task input parameter */
0, /* Priority of the task */ &
Task1, /* Task handle. */
0); /* Core where the task should run */
dht.begin();
}
void clienttcp(void * c) {
// asyncronous tcp client
AsyncClient client;
client.onError([](void * arg, AsyncClient * c, int8_t error) {
konektovano = 0;
Serial.printf("Error\n\n");
digitalWrite(led4, HIGH);
if (!c->connected()) {
c -> close();
}
});
client.onTimeout([](void * arg, AsyncClient * c, uint32_t time) {
konektovano = 0;
Serial.printf("\nTimeout\n");
if (!c->connected()) {
c -> close();
}
});
client.onDisconnect([](void * arg, AsyncClient * c) {
konektovano = 0;
Serial.printf("\nonDisconnect\n");
if (!c->connected()) {
c -> close();
delay(100);
Serial.printf("\Konektovanje\n");
c -> connect(host, port);
}
});
client.onConnect([](void * arg, AsyncClient * c) {
konektovano = 1;
Serial.println("-");
Serial.printf("Connected\n"); // Sending data.\n");
digitalWrite(led4, LOW);
for (int i = 0; i < 17; i = i + 8) {
chipId |= ((ESP.getEfuseMac() >> (40 - i)) & 0xff) << i;
}
//sprintf(prefiks, "%s %d %s", prefiks, chipId, '|');
//prefiks = realloc(prefiks, sizeof(uint32_t));
//sprintf(prefiks, "%d", chipId);
//sprintf(prefiks+sizeof(uint32_t), "%d", chipId);
char textToWrite[ 16 ];
// as per comment from LS_dev, platform is int 16bits
prefiks = "kontroler|";
sprintf(textToWrite, "%lu", chipId);
asprintf(&prefiks, "%s%s", prefiks, textToWrite);
temp = "|";
asprintf(&prefiks, "%s%s", prefiks, temp);
//sprintf(prefiks, "%s %s", prefiks, textToWrite);
//sprintf(prefiks,"%lu", chipId);
//sprintf(prefiks, "%" PRIu32, chipId);
Serial.printf(prefiks);
asprintf(&prvoSlanje, "%s%s", "kontroler|", textToWrite);
c->write(prvoSlanje);
});
client.onData([](void * arg, AsyncClient * c, void * data, size_t len) {
// Serial.printf("Data received with length: %d\n", len);
//char subbuff[len];
//memcpy( subbuff, &((char*)data)[0], len );
//Serial.printf("%s", (char*)data);
if (xQueueSend(xQueuereceive,
(char * ) data,
(TickType_t) 10) != pdPASS) {
Serial.printf("Greska slanja\n\n");
}
//c->close();
});
client.connect(host, port);
//Serial.println(xPortGetCoreID());
while (true)
{
char missatge[50];
vTaskDelay(10);
if ( xQueueReceive( xQueuesend, missatge, ( TickType_t ) 10 ) )
{
//Serial.printf("SLANJE\n\n");
client.add(missatge, sizeof(missatge));
client.send();
}
}
}
void loop() {
if (konektovano == 1) {
//char * cadena = "";
//SendToServer(cadena);
vTaskDelay(10);
char missatge[50];
if (xQueueReceive(xQueuereceive, missatge, (TickType_t) 10)) {
Serial.printf(missatge);
ProcessMessage(missatge);
}
} else {
vTaskDelay(30);
}
// while (true);//client.connected)
}
void SendToServer(char * value) {
//Serial.println(value);
if (xQueueSend(xQueuesend,
(char * ) value,
(TickType_t) 10) != pdPASS) {
Serial.printf("SLANJE GRESKA\n\n");
}
}
void ProcessMessage(char * value) {
//Serial.printf("Primanje poruke\n\n");
prethodnoSlanje = slanje;
//Serial.printf(prethodnoSlanje);
//Serial.printf(temp);
//Serial.printf("\n\n");
if (strncmp(value, "010", 3) == 0) {
digitalWrite(out1, LOW);
preferences.putBool("out1", LOW);
}
if (strncmp(value, "011", 3) == 0) {
digitalWrite(out1, HIGH);
preferences.putBool("out1", HIGH);
}
if (strncmp(value, "020", 3) == 0) {
digitalWrite(out2, LOW);
preferences.putBool("out2", LOW);
}
if (strncmp(value, "021", 3) == 0) {
digitalWrite(out2, HIGH);
preferences.putBool("out2", HIGH);
}
if (strncmp(value, "030", 3) == 0) {
digitalWrite(out3, LOW);
preferences.putBool("out3", LOW);
}
if (strncmp(value, "031", 3) == 0) {
digitalWrite(out3, HIGH);
preferences.putBool("out3", HIGH);
}
if (strncmp(value, "040", 3) == 0) {
digitalWrite(out4, LOW);
preferences.putBool("out4", LOW);
}
if (strncmp(value, "041", 3) == 0) {
digitalWrite(out4, HIGH);
preferences.putBool("out4", HIGH);
}
if (strncmp(value, "050", 3) == 0) {
digitalWrite(out5, LOW);
preferences.putBool("out5", LOW);
}
if (strncmp(value, "051", 3) == 0) {
digitalWrite(out5, HIGH);
preferences.putBool("out5", HIGH);
}
if (strncmp(value, "060", 3) == 0) {
digitalWrite(out6, LOW);
preferences.putBool("out6", LOW);
}
if (strncmp(value, "061", 3) == 0) {
digitalWrite(out6, HIGH);
preferences.putBool("out6", HIGH);
}
if (strncmp(value, "070", 3) == 0) {
digitalWrite(out7, LOW);
preferences.putBool("out7", LOW);
}
if (strncmp(value, "071", 3) == 0) {
digitalWrite(out7, HIGH);
preferences.putBool("out7", HIGH);
}
if (strncmp(value, "080", 3) == 0) {
digitalWrite(out8, LOW);
preferences.putBool("out8", LOW);
}
if (strncmp(value, "081", 3) == 0) {
digitalWrite(out8, HIGH);
preferences.putBool("out8", HIGH);
}
int stanje = digitalRead(out1);
asprintf(&slanje, "%s%d", prefiks, stanje);
stanje = digitalRead(out2);
asprintf(&slanje, "%s%d", slanje, stanje);
stanje = digitalRead(out3);
asprintf(&slanje, "%s%d", slanje, stanje);
stanje = digitalRead(out4);
asprintf(&slanje, "%s%d", slanje, stanje);
stanje = digitalRead(out5);
asprintf(&slanje, "%s%d", slanje, stanje);
stanje = digitalRead(out6);
asprintf(&slanje, "%s%d", slanje, stanje);
stanje = digitalRead(out7);
asprintf(&slanje, "%s%d", slanje, stanje);
stanje = digitalRead(out8);
asprintf(&slanje, "%s%d", slanje, stanje);
asprintf(&slanje, "%s%s", slanje, "|");
Serial.printf("\n\n");
Serial.printf(slanje);
Serial.printf("\n");
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
// Compute heat index in Fahrenheit (the default)
float hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);
asprintf(&slanje, "%s%f", slanje, t);
asprintf(&slanje, "%s%s", slanje, "|");
asprintf(&slanje, "%s%f", slanje, h);
asprintf(&slanje, "%s%s", slanje, "|");
Serial.print(slanje);
SendToServer(slanje);
}