I want to use an OPTA WiFi/BLE PLC to transfer data from four analog meters to Real Dash.
I reused a basic CAN sketch that works with Industrino via a direct USB connection.
I added a UDP-type WiFi connection, but it doesn’t work. Is it better to use a class-server type: server, client, or UDP?
I’m attaching the sketch for comments/corrections.
Thanks
// shared variables can be accessed with PLCIn.varname and PLCOut.varname
#include <WiFi.h>
int status = WL_IDLE_STATUS;
int E1 = 0;
int E2 = 0;
int E3 = 0;
int E4 = 0;
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid = “APName”; // your network SSID (name)
char pass = “APPassword”; // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0; // your network key index number (needed only for WEP)
unsigned int localPort = 35000; // local port to listen on
char remoteIP = “192.168.5.88”; // a string to send back
WiFiUDP Udp;
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// check for the WiFi module:
if (WiFi.status() == WL_NO_MODULE) {
Serial.println(“Communication with WiFi module failed!”);
// don’t continue
while (true);
}
// attempt to connect to WiFi network:
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open network:
status = WiFi.begin(ssid, pass);
}
Serial.println(“Connected to WiFi”);
printWifiStatus();
Serial.println(“\nStarting connection to server…”);
// if you get a connection, report back via serial:
Udp.begin(localPort);
}
void loop() {
//E1 = int(PLCOut.E1);
//E2 = int(PLCOut.E2);
//E3 = int(PLCOut.E3);
//E4 = int(PLCOut.E4);
//Serial.print(E1); Serial.print(" - “);
//Serial.print(E2); Serial.print(” - “);
//Serial.print(E3); Serial.print(” - ");
//Serial.println(E4);
// void BuildCanFrames(){
byte CanBuffer[8];
// build 1st CAN frame:
memcpy(CanBuffer, &E1, 2);
memcpy(CanBuffer + 2, &E2, 2);
memcpy(CanBuffer + 4, &E3, 2);
memcpy(CanBuffer + 6, &E4, 2);
// write first CAN frame to serial
SendCANFrameToSerial(3200, CanBuffer);
}
void SendCANFrameToSerial(unsigned long canFrameId, const byte* frameData)
{
Udp.beginPacket(remoteIP, localPort);
// the 4 byte identifier at the beginning of each CAN frame
// this is required for RealDash to ‘catch-up’ on ongoing stream of CAN frames
const byte serialBlockTag[4] = { 0x44, 0x33, 0x22, 0x11 };
Udp.write(serialBlockTag, 4);
// the CAN frame id number (as 32bit little endian value)
Udp.write((const byte*)&canFrameId, 4);
// CAN frame payload
Udp.write(frameData, 8);
Udp.endPacket();
}
void printWifiStatus() {
// print the SSID of the network you’re attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your board’s IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print(“signal strength (RSSI):”);
Serial.print(rssi);
Serial.println(" dBm");
}