About This Project
The PLCDuino is a pretty cool piece of kit for only $89! http://www.PLC.us. More deets here: Open Source PLC! - 15 Day Free Trial! - Ships Overnight
The challenge is that the support isn’t that great, and after pulling out bunches of hair, @wmontg5988 sent it to me. I was determined to connect it to Cayenne.
The PLCDuino is basically an Arduino Mega with a bunch of integrated shields and an ESP-02 attached, so it should be easy to get er done using a variation of this WifiShield with arduino uno compiling error - #11 by kreggly.
For the above method to work, you need to make sure the ESP functions like a modem with the AT command set. By default, the ESP is programmed with NodeMCU. No problem, right? Devil is in the details.
The Details
The ESP is soldered to the board, and to program it, you need to analyze the schematic and see how it is connected. The ESP is connected to Serial2 in Arduino-speak. Then we need to know how the control signals are mapped. See schematic. https://www.digital-loggers.com/plcschematics.pdf
Fortunately I found a library, and an esp_flash_helper sketch.
https://github.com/digitalloggers/PLDuino/tree/master/Arduino/libraries
Running the sketch, it is evident that there is a “feature” where the serial DTR line is used to reset the PLC when the baud rate changes. However, when we change baud rate to flash the ESP, the PLC resets, as do our connections to the ESP
I was able to program it by connecting to the ESP programming header at J9 with a USB to Serial dongle, but not everyone has one of those, and I don’t like driving into an output line in the case of two TX lines talking to one RX line.
At that time, I determined that the programming baud rate was also wrong.
Analyzing the schematic, I determined that I could force the Mega reset line high so the helper program would work, and the fruits of my labour are yours.
The Nitty Gritty Details
Give yourself some time. This is going to be a ride.
DANGER WILL ROBINSON! You are about to void your warranty. You are about to touch hardware. Make sure you take ESD precautions. Make sure when powering up the PLC, you check polarities. Make sure you don’t have the PLC powered up when you are attaching the jumper. When the jumper is attached, do not press the reset button. Yadda. Yadda. Yadda. This is completely at your own risk. Have a nice day
-
Install the support libraries from here: https://github.com/digitalloggers/PLDuino/tree/master/Arduino/libraries.
-
You will also need the Cayenne bits from here: WifiShield with arduino uno compiling error - #11 by kreggly
-
Open your PLC by lifting the body away from the tabs on the USB side.
-
Carefully lift the top and remove the twist tie around the speaker wire for extra reach.
-
Now find J7 to the left of the Touchscreen connector on the main board, and attach a jumper between pins 2 and 5. This will allow us to change the baud rate for programming without the PLC resetting!!!
-
Load this sketch and download my simplified helper sketch to the PLC.
#PLCDuino_ESPFlashHelper Sketch
#include <PLDuino.h>
#include <PLDTouch.h>
#include <PLDuinoGUI.h>
#include <using_namespace_PLDuinoGUI.h>
#include <TMRpcm_PLDuino.h>
#include <SPI.h>
#include <SD.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <DS3232RTC.h>
#include <TimeLib.h>
#include <Wire.h>
#include <avr/io.h>
Adafruit_ILI9341 tft = Adafruit_ILI9341(PLDuino::LCD_CS, PLDuino::LCD_DC);
PLDTouch touch(PLDuino::TOUCH_CS, PLDuino::TOUCH_IRQ);
void resetESP()
{
digitalWrite(PLDuino::ESP_RST, LOW);
delay(1000);
digitalWrite(PLDuino::ESP_RST, HIGH);
}
void setupFlashMode()
{
digitalWrite(PLDuino::ESP_GPIO0, LOW);
resetESP();
Serial.begin(74880);
Serial2.begin(74880);
Button btnReset("RESET ESP", ILI9341_RED, ILI9341_MAROON);
btnReset.setPositionAndSize(100, 100, 130, 40);
btnReset.draw(tft);
Label lblNote("Ready to Flash ESP-02 at 74880 baud.", ILI9341_WHITE, ILI9341_BLACK, HORZ_LEFT|VERT_TOP);
lblNote.setPositionAndSize(10, 25, 300, 120);
lblNote.draw(tft);
while(true)
{
if (touch.dataAvailable())
{
Point pt = touch.read();
int x = pt.x;
int y = pt.y;
if (btnReset.isTouched(x, y))
{
resetESP();
}
}
if (Serial2.available()) Serial.write(Serial2.read());
if (Serial.available()) Serial2.write(Serial.read());
}
}
void setup()
{
PLDuino::init();
PLDuino::enableLCD();
PLDuino::enableESP();
tft.begin();
tft.setRotation(3);
touch.init(1);
setupFlashMode();
while(1);
}
void loop() {}
.
7. Load 'er up, and we are ready to flash. Time for some 80s dancing…
8. Clone the NodeMCU Flasher application, or whatever you like. Node flasher is here: GitHub - nodemcu/nodemcu-flasher: A firmware Flash tool for nodemcu
9. Run the exe in the release directory for your OS, 32 or 64 bit.
10. Get the AT firmware. I used this one: http://www.electrodragon.com/w/File:V1.3.0.2_AT_Firmware.bin.zip
11. Now configure based on the pictures, and make sure you use the Arduino serial port (serial monitor closed).
.
12. Now press Flash. The flasher should find the ESP addresses, and the progress bar should slowly start to climb.
13. When done, the bottom left will show ‘Ready’.
14. You just flashed the ESP. Squeee! NOW STOP!
15. Remove power from your PLCDuino and remove that jumper you placed on J7!!!
16. Close up the box and power it back on.
17. You can now drop in your Cayenne sketch.
18. Here you go:
#PLCDuino_Cayenne_Connect
/*
Cayenne ESP8266 Shield WiFi Example
Adapted from Blynk's ESP8266_Shield_HardSer Example
This sketch connects to the Cayenne server using an ESP8266 WiFi module as a shield connected
via a hardware serial to an Arduino.
You should install the ESP8266HardwareSerial.zip library via the Arduino IDE (Sketch->Include Library->Add .ZIP Library)
from the Cayenne extras/libraries folder (e.g. My Documents\Arduino\libraries\Cayenne\extras\libraries) to compile this example.
NOTE: Ensure a stable serial connection to ESP8266!
Firmware version 1.0.0 (AT v0.22) or later is needed.
You can change ESP baud rate. Connect to AT console and call:
AT+UART_DEF=115200,8,1,0,0
For Cayenne Dashboard widgets using digital or analog pins this sketch will automatically
send data on those pins to the Cayenne server. If the widgets use Virtual Channels, data
should be sent to those channels using virtualWrites. Examples for sending and receiving
Virtual Channel data are under the Basics folder.
*/
//#define CAYENNE_DEBUG // Uncomment to show debug messages
#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
#include <CayenneESP8266Shield.h>
#include <PLDuino.h>
#include <PLDuinoGUI.h>
#include <TMRpcm_PLDuino.h>
#include <SPI.h>
#include <SD.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <PLDuino.h>
#include <PLDTouch.h>
#include <PLDuinoGUI.h>
#include <using_namespace_PLDuinoGUI.h>
#include <DS3232RTC.h>
#include <TimeLib.h>
#include <Wire.h>
#include <avr/io.h>
Adafruit_ILI9341 tft = Adafruit_ILI9341(PLDuino::LCD_CS, PLDuino::LCD_DC);
PLDTouch touch(PLDuino::TOUCH_CS, PLDuino::TOUCH_IRQ);
// Cayenne authentication token. This should be obtained from the Cayenne Dashboard.
char token[] = "...";
// Your network name and password.
char ssid[] = "...";
char password[] = "...";
// Set ESP8266 Serial object
#define EspSerial Serial2
ESP8266 wifi(EspSerial);
void setup()
{
PLDuino::init();
PLDuino::enableLCD();
PLDuino::enableESP();
tft.begin();
tft.setRotation(3);
touch.init(1);
Serial.begin(9600);
delay(100);
// Set ESP8266 baud rate
EspSerial.begin(115200);
delay(100);
Cayenne.begin(token, wifi, ssid, password);
// while(touch.dataAvailable()) touch.read();
// while(!touch.dataAvailable()); touch.read();
// tft.fillScreen(ILI9341_BLACK);
}
void loop()
{
Cayenne.run();
}
.
19. Fill in your credentials and your Arduino Cayenne Token, and upload to the PLCDuino.
20. Note, if it doesn’t compile, you probably need to look above and grab the files in the linked forum post.
21. If all’s well, your dashboard should pop up, and you can start adding some stuff to your dashboard.
22. As a start, channels 2-7 are the relay outputs.
What’s Connected
Check out these features:
- ATMega2560 based
- WiFi with external long range antenna.
- 2.4" Color LCD touch screen and speaker.
- Six fused DPST power relays.
- Eight protected digital outputs with 500mA open collector drivers
- Eight protected & isolated digital inputs 3-24V AC/DC
- Eight 10-bit ADC inputs.
- Clock calendar & battery for scheduling.
- RS-232 for long distance wired comm.
- Expanded 128Mb & 4GB FLASH.
- DIN rail or panel mount.
- 12-28 VDC powered.
- Full Arduino compatibility.
Triggers & Alerts
With all this IO. WHOOOSH! Mind blown.
Scheduling
Soon. Soon.
Photos of the Project
Cheers,
Craig