#include "config.h"
#include <AdafruitIO_Feed.h>
#include <ModbusRTU.h>
#include <Wire.h>
#include <Wifi.h>
#include "SSD1306Wire.h"
#define SERIAL_COMMUNICATION_CONTROL_PIN 19
ModbusRTU mb;
#define LED 9
const int ledPin = 2;
uint16_t res[5];
bool cb(Modbus::ResultCode event, uint16_t transactionId, void *data)
{ // Callback to monitor errors
if (event != Modbus::EX_SUCCESS)
{
Serial.println("Request result: 0x");
Serial.println(event, HEX);
}
return true;
}
AdafruitIO_Feed *analog = io.feed("analog");
SSD1306Wire display(0x3c, 22, 23);
void setup()
{
Serial.begin(9600);
Serial2.begin(9600, SERIAL_8N1, 16, 17);
mb.begin(&Serial2, SERIAL_COMMUNICATION_CONTROL_PIN);
mb.master();
while (!Serial)
;
delay(200);
Serial.print("Connecting to IO");
io.connect();
while (io.status() < AIO_CONNECTED)
{
Serial.print("IO Bekleniyor");
delay(500);
}
analog->onMessage(handleMessage);
Serial.println();
Serial.print("ESP Board MAC Address: ");
Serial.println(io.statusText());
pinMode(SERIAL_COMMUNICATION_CONTROL_PIN, OUTPUT);
digitalWrite(SERIAL_COMMUNICATION_CONTROL_PIN, 1);
digitalWrite(SERIAL_COMMUNICATION_CONTROL_PIN, 0);
display.init();
delay(100);
display.flipScreenVertically();
display.setFont(ArialMT_Plain_10);
display.drawString(0, 5, "TEST");
display.display();
delay(200);
}
void loop()
{
io.run();
mb.task();
yield();
}
void handleMessage(AdafruitIO_Data *data)
{
// convert the data to integer
uint16_t reading = data->toInt();
Serial.print("received <- ");
Serial.println(reading);
//mb.writeHreg(1, 2, &reading, 1, cb); // id, StartAdres, Data, count, cb
//mb.writeHreg(1, 3, &reading, 1, cb); // orp
mb.writeHreg(1, 3, &reading, 1, cb); // ph
display.clear();
display.drawString(0, 0, "pH=" + String(reading));
display.display();
delay(100);
}