Hello, I am just trying to simply blink an LED using OpenPLC editor and an ESP8266. I have edited the esp8266.cpp file to add functionality for I2C communication with an AHT20 sensor and modbus protocol so the ESP can be used as an OPCUA connection to an Ignition gateway. Now I am trying to add functionality with the physical pins so I included ladder logic in the OpenPLC Editor but I cannot seem to get a simple LED to flash. I will share my esp8266.cpp file code and a picture of my ladder logic. Any help would be greatly appreciated.
esp8266.cpp file:
#include <stdlib.h>
extern "C" {
#include "openplc.h"
}
#include "Arduino.h"
#include "../examples/Baremetal/defines.h"
#include <Wire.h>
#include <Adafruit_AHTX0.h>
#include <ESP8266WiFi.h>
#include <ModbusIP_ESP8266.h>
const char* ssid = MBTCP_SSID; // Using SSID from defines.h
const char* password = MBTCP_PWD; // Using password from defines.h
#define NODE_PIN_D0 16
#define NODE_PIN_D1 5
#define NODE_PIN_D2 4
#define NODE_PIN_D3 0
#define NODE_PIN_D4 2
#define NODE_PIN_D5 14
#define NODE_PIN_D6 12
#define NODE_PIN_D7 13
#define NODE_PIN_D8 15
uint8_t pinMask_DIN[] = { NODE_PIN_D4, NODE_PIN_D5, NODE_PIN_D6, NODE_PIN_D7 };
uint8_t pinMask_DOUT[] = { NODE_PIN_D0, NODE_PIN_D1, NODE_PIN_D2, NODE_PIN_D3 };
uint8_t pinMask_AIN[] = { A0 };
uint8_t pinMask_AOUT[] = { NODE_PIN_D8 };
Adafruit_AHTX0 aht; // Initialize aht sensor object
ModbusIP mb; // Initialize the modbus object
void hardwareInit() // Set pin functionality for hardware
{
pinMode(NODE_PIN_D0, OUTPUT);
pinMode(NODE_PIN_D1, OUTPUT);
pinMode(NODE_PIN_D2, OUTPUT);
pinMode(NODE_PIN_D3, OUTPUT);
pinMode(NODE_PIN_D4, INPUT);
pinMode(NODE_PIN_D5, INPUT);
pinMode(NODE_PIN_D6, INPUT);
pinMode(NODE_PIN_D7, INPUT);
pinMode(NODE_PIN_D8, OUTPUT);
}
void setup() {
Serial.begin(115200); // Start the serial interface with 115200 baud rate
hardwareInit();
// Set IP protocol information based on OpenPLC communication configuration
IPAddress ip(MBTCP_IP);
IPAddress subnet(MBTCP_SUBNET);
IPAddress gateway(MBTCP_GATEWAY);
// Connect to WiFi with IP configuration
WiFi.config(ip, gateway, subnet);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) { // Keep trying to connect to WiFi until successful
delay(1000);
Serial.println("Connecting to WiFi...");
}
// Print assigned IP address
Serial.println("Connected to WiFi");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
// Initialize Modbus server
mb.server(502);
// Add 2 Analog Input Registers
mb.addIreg(0, 0); // Initialize anglog input register 0 to zero
mb.addIreg(1, 0); // Initialize anglog input register 1 to zero
// Add 1 Holding Register
mb.addHreg(8, 10000); // Initialize holding register 8 to 10000 milliseconds
if (!aht.begin()) { // Check for AHT20 connection
Serial.println("Could not find AHT20 sensor! Check wiring.");
while (1) delay(10);
}
Serial.println("AHT20 sensor initialized successfully!");
updateInputBuffers();
}
void updateInputBuffers() // Get values from AHT20 and store them in the modbus
{
// Read data from the AHT20 sensor
sensors_event_t humidity, temp;
aht.getEvent(&humidity, &temp);
float temperature = temp.temperature * 1.8 + 32; // Convert from Celsius to Fahrenheit
float humidityVal = humidity.relative_humidity;
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °F");
Serial.print("Humidity: ");
Serial.print(humidityVal);
Serial.println(" %");
mb.Ireg(0, (uint16_t)(temperature * 100)); // Multiply by 100 for precision and store in Analog Input register
mb.Ireg(1, (uint16_t)(humidityVal * 100)); // Multiply by 100 for precision and store in Analog Input register
for (int i = 0; i < NUM_DISCRETE_INPUT; i++)
{
if (bool_input[i/8][i%8] != NULL)
*bool_input[i/8][i%8] = digitalRead(pinMask_DIN[i]);
}
for (int i = 0; i < NUM_ANALOG_INPUT; i++)
{
if (int_input[i] != NULL)
*int_input[i] = (analogRead(pinMask_AIN[i]) * 64);
}
}
void updateOutputBuffers()
{
for (int i = 0; i < NUM_DISCRETE_OUTPUT; i++)
{
if (bool_output[i/8][i%8] != NULL)
digitalWrite(pinMask_DOUT[i], *bool_output[i/8][i%8]);
}
for (int i = 0; i < NUM_ANALOG_OUTPUT; i++)
{
if (int_output[i] != NULL)
analogWrite(pinMask_AOUT[i], (*int_output[i] / 64));
}
}
unsigned long previousMillis = 0;
long interval = 0;
int count;
void loop() {
mb.task();
interval = mb.Hreg(8); // Set interval to value sitting in the Holding Regsiter
unsigned long currentMillis = millis();
// Delay loop until interval time has passed
if (currentMillis - previousMillis >= interval) {
// Handle Modbus communication
updateInputBuffers();
updateOutputBuffers();
count = count + 1;
// Count to 20 to ensure that Ignition captures a value
if (count >= 20) {
previousMillis = currentMillis;
count = 0;
}
}
}
OpenPLC Editor Ladder Logic:
![[203128%2F8997817%2Fmceclip0]](https://d28lcup14p4e72.cloudfront.net/203128%2F8997817%2Fmceclip0.png)