Go Down

Topic: Using SoftwareSerial.h and Wire.h causing problems (Read 172 times) previous topic - next topic

somearduinoguy

May 16, 2017, 03:28 pm Last Edit: May 16, 2017, 03:56 pm by somearduinoguy
Hello,

I am currently working on a project that incorporates I2C between two Arduinos and uses the ESP8266 for Wifi connectivity on one of these Arduinos.

I am having some trouble making them work together, both of these work separately but adding both to one Arduino causes the ESP8266 module to be unable to connect to the Access Point.

I am using MQTT when after connection is established.

Quote
#include <PubSubClient.h>
#include "WiFiEsp.h"
#include <SoftwareSerial.h>
#include <Servo.h>
#include <Wire.h>
 
SoftwareSerial espSerial(10, 11); // RX, TX
long int baudRate = 9600;
 
char ssid[] = "RPi1";             // your network SSID (name)
char pass[] = "Raspberry";        // your network password
int status = WL_IDLE_STATUS;      // the Wifi radio's status
char server[] = "192.168.1.1";    // IP address of the MQTT server
char topic[] = "MandemOnTheWall";            // Default topic string
char clientId[] = "Mandem1";      // Cliwent id: Must be unique on the broker
 
WiFiEspClient wifi;               // Initialize the Ethernet client object
PubSubClient mqttClient(wifi);    // Initialize the MQTT client
int servoPin = 9;
Servo Servo1;
String startOfPayload = "";
 
void setup() {
  Serial.begin(9600);
  pinMode(servoPin, OUTPUT);
  Servo1.attach(servoPin);
  Wire.begin(8);                // join i2c bus with address #8
  Wire.onReceive(MQTTOnReceiveEvent); // register event
   
  // Set baud rate of ESP8266 to 9600 regardless of original setting
  set_esp8266_baud_rate(baudRate);
  espSerial.begin(baudRate);
  espSerial.print("AT+UART_CUR=");
  espSerial.print(baudRate);
  espSerial.print(",8,1,0,0\r\n");
  WiFi.init(&espSerial);
   
 
  // check for the presence of the shield
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue
    while (true);
  }
 
  // attempt to connect to WiFi network
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network
    status = WiFi.begin(ssid, pass);
  }
 
  // you're connected now, so print out the data
  Serial.println("You're connected to the network");
   
  printWifiStatus();
   
  mqttClient.setServer(server, 1883);
  mqttClient.setCallback(onReceive);
  lock();
}
 
void loop() {
  char message[256];
  int i;
 
  for (i=0; i<256; i++) {
    message = 0;
  }
 
  if (Serial.available()) {
    Serial.readBytesUntil("\r",message, 255);
    mqttClient.publish(topic,message);
  }
   
  if (!mqttClient.connected()) {
    reconnect();
  }
   
  mqttClient.loop();
 
}
 
void printWifiStatus()
{
  // print the SSID of the network you're attached to
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());
 
  // print your WiFi shield'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");
}
 
void onReceive(byte* payload, unsigned int length) {
  if (Wire.available()){
    MQTTOnReceiveEvent();
  }
  else {
    Serial.print("Message arrived: ");
    for (int i=0;i<length;i++) {
      Serial.print((char)payload);
    }
  }
}
 
void reconnect() {
  // Loop until we're reconnected
  while (!mqttClient.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (mqttClient.connect(clientId)) {
      Serial.println("connected");
      // Once connected, publish an announcement...
      mqttClient.publish(topic,"hello world");
      // ... and resubscribe
      mqttClient.subscribe(topic);
    } else {
      Serial.print("failed, rc=");
      Serial.print(mqttClient.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}
 
void set_esp8266_baud_rate(long int baud_rate){
  long int baud_rate_array[] = {1200,2400,4800,9600,19200,38400,57600,74880,115200,230400};
  int i, j, pause=10;
  String response;
 
  Serial.println("Setting ESP8266 baud rate...");
  for (j=0; j<5; j++){
    for (int i=0; i<10; i++){
      espSerial.begin(baud_rate_array);
      espSerial.print("AT\r\n");
      delay(pause);
      if (espSerial.available()) {
        response=espSerial.readString();
        response.trim();
        if (response.endsWith("OK")) {
          espSerial.print("AT+UART_CUR=");
          espSerial.print(baud_rate);
          espSerial.println(",8,1,0,0");
          delay(pause);
          if (espSerial.available()) {
            response=espSerial.readString();
          }
          espSerial.begin(baudRate);
          delay(pause);
          espSerial.println("AT");
          delay(pause);
          if (espSerial.available()) {
            response=espSerial.readString();
            response.trim();
            if (response.endsWith("OK"))
              break;
            else {
              Serial.println("Trying again...");
            }
          }
          else {
            Serial.println("Trying again...");
          }
        }
      }
    }
    if (response.endsWith("OK"))
      break;
  }
  espSerial.begin(baudRate);
  delay(pause);
  espSerial.println("AT");
  delay(pause);
  if (espSerial.available()) {
    response=espSerial.readString();
    response.trim();
    if (response.endsWith("OK")) {
      Serial.print("\r\nBaud rate is now ");
      Serial.println(baudRate);
    }
    else {
      Serial.println("Sorry - could not set baud rate");
      Serial.println("Try powering off and on again");
      Serial.println("Don't try to use 115200");
    }
  }
}
void MQTTOnReceiveEvent() {
  while (1 < Wire.available()) { // loop through all but the last
    char c = Wire.read(); // receive byte as a character
    Serial.print(c);         // print the character
  }
  int x = Wire.read();    // receive byte as an integer
  Serial.println(x);         // print the integer
  if (x == 1){
    unlock();
  }
  if (x == 0){
    lock();
  }
}
void unlock(){
  Servo1.write(0);
}
void lock(){
  Servo1.write(90);
}
Thank you for your help in advance.



Go Up