What library do you replace WiFiClientSecure with when using the Olimex boards?
This code works with a Wiz850io and the ESP32 using MQTT (non-TLS)
Code: Select all
#include <PubSubClient.h>
#include <SPI.h>
#include <Ethernet2.h>
#include <EthernetUdp2.h>
#include "time.h"
const char* ssid = "xxxxxxxxx";
const char* password = "xxxxxxxxxxx";
const char* mqttServer = "xxxxxxxxxx";
const int mqttPort = 1883;
const char* mqttUser = "xxxxxx";
const char* mqttPassword = "xxxxxxxxxx";
uint8_t baseMac[6];
unsigned int localPort = 8888; // local port to listen for UDP packets
char timeServer[] = "time.nist.gov"; // time.nist.gov NTP server
const int NTP_PACKET_SIZE = 48; // NTP time stamp is in the first 48 bytes of the message
byte packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets
// A UDP instance to let us send and receive packets over UDP
EthernetUDP Udp;
//const int timeZone = -6; // Central Standard Time
const int timeZone = 5; // Central Daylight Time
//const int timeZone = -5; // Eastern Standard Time (USA)
//const int timeZone = -4; // Eastern Daylight Time (USA)
//const int timeZone = -8; // Pacific Standard Time (USA)
//const int timeZone = -7; // Pacific Daylight Time (USA)
#if defined(ESP8266)
// default for ESPressif
#define WIZ_CS 15
#elif defined(ESP32)
#define WIZ_CS 4
#elif defined(ARDUINO_STM32_FEATHER)
// default for WICED
#define WIZ_CS PB4
#elif defined(TEENSYDUINO)
#define WIZ_CS 10
#elif defined(ARDUINO_FEATHER52)
#define WIZ_CS 11
#else // default for 328p, 32u4 and m0
#define WIZ_CS 10
#endif
float rain = 0.00;
String rainStr = "";
float tempT = 35.0;
String tempTStr = "";
byte rainDir = 1;
byte tempTDir = 1;
unsigned long timePub = 0UL;
unsigned long updateNTP = 30000UL;
unsigned long currentTime = 0UL;
EthernetClient espClient;
PubSubClient client(espClient);
void setup() {
Serial.begin(115200);
esp_read_mac(baseMac, ESP_MAC_WIFI_STA);
delay(500);
Serial.print(F("Mac Address: "));
for (byte x = 0; x < 6; x++)
{
if (baseMac[x] < 0x10)
{
Serial.print(F("0"));
Serial.print(baseMac[x]);
}
else
{
Serial.print(baseMac[x], HEX);
}
if (x < 5)
{
Serial.print(F(":"));
}
}
Serial.println(F(""));
Ethernet.init(WIZ_CS);
delay(1000);
if (Ethernet.begin(baseMac) == 0) {
// no point in carrying on, so do nothing forevermore:
while (1) {
Serial.println("Failed to configure Ethernet using DHCP");
delay(10000);
}
}
Serial.print(F("IP number assigned by DHCP is "));
Serial.println(Ethernet.localIP());
Udp.begin(localPort);
delay(1000);
getTime();
currentTime = millis();
client.setServer(mqttServer, mqttPort);
client.setCallback(callback);
while (!client.connected()) {
Serial.println("Connecting to MQTT...");
if (client.connect("ESP32Client", mqttUser, mqttPassword )) {
Serial.println("connected");
} else {
Serial.print("failed with state ");
Serial.print(client.state());
delay(2000);
}
}
client.publish("your pub topic", "Test MQTT Client Starting");
client.subscribe("your sub topic 1");
client.subscribe("your sub topic 2");
timePub = millis();
rainStr = String(rain, 2);
tempTStr = String(tempT, 1);
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived in topic: ");
Serial.println(topic);
Serial.print("Message:");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
Serial.println("-----------------------");
}
time_t prevDisplay = 0; // when the digital clock was displayed
void loop() {
client.loop();
if (millis() < currentTime)
{
currentTime = millis();
}
if (millis() - currentTime > updateNTP)
{
getTime();
currentTime = millis();
}
if (millis() < timePub)//account for millis() rollover
{
timePub = millis() - timePub;
}
if ((millis() - timePub) > 60000UL)
{
if (rainDir == 0)
{
rain -= 0.01;
}
else
{
rain += 0.01;
}
rainStr = String(rain, 2);
if (rain >= 5.00)
{
rainDir = 0;
}
if (rain <= 0.00)
{
rainDir = 1;
}
if (tempTDir == 0)
{
tempT -= 0.1;
}
else
{
tempT += 0.1;
}
tempTStr = String(tempT, 1);
if (tempT >= 40.0)
{
tempTDir = 0;
}
if (tempT <= 32.0)
{
tempTDir = 1;
}
String pubString = String(rainStr + "," + tempTStr);
char message_buff[pubString.length() + 1];
pubString.toCharArray(message_buff, pubString.length() + 1);
client.publish("robe/test", message_buff);
timePub = millis();
}
}
//-------- NTP code ----------
// send an NTP request to the time server at the given address
void sendNTPpacket(const char * address) {
// set all bytes in the buffer to 0
memset(packetBuffer, 0, NTP_PACKET_SIZE);
// Initialize values needed to form NTP request
// (see URL above for details on the packets)
packetBuffer[0] = 0b11100011; // LI, Version, Mode
packetBuffer[1] = 0; // Stratum, or type of clock
packetBuffer[2] = 6; // Polling Interval
packetBuffer[3] = 0xEC; // Peer Clock Precision
// 8 bytes of zero for Root Delay & Root Dispersion
packetBuffer[12] = 49;
packetBuffer[13] = 0x4E;
packetBuffer[14] = 49;
packetBuffer[15] = 52;
// all NTP fields have been given values, now
// you can send a packet requesting a timestamp:
Udp.beginPacket(address, 123); //NTP requests are to port 123
Udp.write(packetBuffer, NTP_PACKET_SIZE);
Udp.endPacket();
}
void getTime()
{
sendNTPpacket(timeServer); // send an NTP packet to a time server
// wait to see if a reply is available
delay(1000);
if ( Udp.parsePacket() ) {
// We've received a packet, read the data from it
Udp.read(packetBuffer, NTP_PACKET_SIZE); // read the packet into the buffer
//the timestamp starts at byte 40 of the received packet and is four bytes,
// or two words, long. First, esxtract the two words:
unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
// combine the four bytes (two words) into a long integer
// this is NTP time (seconds since Jan 1 1900):
unsigned long secsSince1900 = highWord << 16 | lowWord;
Serial.print("Seconds since Jan 1 1900 = " );
Serial.println(secsSince1900);
// now convert NTP time into everyday time:
Serial.print("Unix time = ");
// Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
const unsigned long seventyYears = 2208988800UL;
// subtract seventy years:
unsigned long epoch = secsSince1900 - seventyYears;
// print Unix time:
Serial.println(epoch);
// print the hour, minute and second:
Serial.print("The UTC time is "); // UTC is the time at Greenwich Meridian (GMT)
Serial.print((epoch % 86400L) / 3600); // print the hour (86400 equals secs per day)
Serial.print(':');
if ( ((epoch % 3600) / 60) < 10 ) {
// In the first 10 minutes of each hour, we'll want a leading '0'
Serial.print('0');
}
Serial.print((epoch % 3600) / 60); // print the minute (3600 equals secs per minute)
Serial.print(':');
if ( (epoch % 60) < 10 ) {
// In the first 10 seconds of each minute, we'll want a leading '0'
Serial.print('0');
}
Serial.println(epoch % 60); // print the second
}
}
Other option might be.....
http://wizwiki.net/wiki/doku.php?id=ewb ... t_shield_s