0

Arduino Pro Mini wiringARduino uno wiringI have a project where I am getting sensor data and sending it over to an ESP8266-01 to send to ThingSpeak.
After some work I got it to work. So like I real smart person I decided to replace the Arduino UNO I was using (that worked) to an Arduino Pro Mini. Using the same code and connections the String isn't sent to the ESP and then not to the ThingSpeak.

Here is a reduced version of the sending code that I used on the UNO and the Pro.

void setup() {
   Serial.begin(9600);
  pinMode(2,OUTPUT);
  digitalWrite(2,HIGH);
  delay(10000);

  // put your setup code here, to run once:

}

void loop() {
  digitalWrite(2,HIGH);
  float waterSensorOutValue= 50;
  float sensorBatValue = 300;
  String f = String('<')+String("Hi")+String(',')+String(waterSensorOutValue)+String(',')+String(sensorBatValue)+String('>');

  Serial.print (f);
  delay(1000);
  Serial.print (f);

delay(1000); Serial.print (f); delay(1000); Serial.print (f); delay(1000); Serial.print (f); delay(1000); Serial.print (f); delay(1000); Serial.print (f); digitalWrite(2,LOW); // put your main code here, to run repeatedly: delay(20000); }

And this is the code I use on the ESP to receive the String (f) and then upload it to ThingSpeak.

It is a bit involved because I use AutoConnect to first put in my wifi and password as well as ThingSpeak write API and save them into EEPROM for later connections.

After that it is pretty straight forward.

But like I said it works great on the Arduino UNO but not on the Arduino Pro Mini.

#include <FS.h>
#include <ESP8266WiFi.h>          //https://github.com/esp8266/Arduino
#include <EEPROM.h>
//needed for library
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>         //https://github.com/tzapu/WiFiManager
#include <ArduinoJson.h>
//#include <SoftwareSerial.h>

//SoftwareSerial espSerial(A0, D1); // RX | TX
//stuff to bring in string
const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars];        // temporary array for use when parsing
// variables to hold the parsed data
char messageFromPC[numChars] = {0};
float floatFromPC = 0;
float floatFromPC2 = 0;
boolean newData = false;
//end stuff ti bring in string
String f;
String start = "start";
//NEW STUFF START
char Password[36]="";
char apiKey[16]="";
WiFiClient client;
//eeprom new end
char defaultHost[100] = "";  //Thing Speak IP address (sometime the web address causes issues with ESP's :/
    long itt = 500;
    long itt2 = 500;

const byte wifiResetPin = 13;
int interruptPinDebounce = 0;
long debouncing_time = 1000;
volatile unsigned long wifiResetLastMillis = 0;


bool shouldSaveConfig = false;


void saveConfigCallback () {
  Serial.println("Should save config");
  shouldSaveConfig = true;}

  void handleWifiReset(){
    if(millis()<wifiResetLastMillis){
      wifiResetLastMillis = millis();
    }
    if((millis() - wifiResetLastMillis)>= debouncing_time){
      Serial.println("Clearing WiFi data resetting");
      WiFiManager wifiManager;
      wifiManager.resetSettings();
      SPIFFS.format();
      ESP.reset();
      delay(1000);
    }
    wifiResetLastMillis = millis();
  }

int addr = 0; 

void setup() {

  //EEPROM.begin(512);  //Initialize EEPROM
  WiFiManager wifiManager;
    // put your setup code here, to run once:
    Serial.begin(9600);
    //espSerial.begin(115200);




    pinMode(wifiResetPin, INPUT_PULLUP);
    attachInterrupt(digitalPinToInterrupt(wifiResetPin), handleWifiReset,FALLING);







  WiFiManagerParameter customAPIKey("apiKey", "ThingSpeakWriteAPI", apiKey, 16);
//END NEW STUFF
    //WiFiManager
    //Local intialization. Once its business is done, there is no need to keep it around
   //WiFiManager wifiManager;

    //NEW STUFF START 
    //wifiManager.setSaveConfigCallback(saveConfigCallback);

    wifiManager.addParameter(&customAPIKey);
     //END NEW STUFF
    //reset saved settings
 //wifiManager.resetSettings();

    //set custom ip for portal
    //wifiManager.setAPStaticIPConfig(IPAddress(10,0,1,1), IPAddress(10,0,1,1), IPAddress(255,255,255,0));

    //fetches ssid and pass from eeprom and tries to connect
    //if it does not connect it starts an access point with the specified name
    //here  "AutoConnectAP"
    //and goes into a blocking loop awaiting configuration
    wifiManager.autoConnect("AutoConnectAP");
    Serial.println("Connected");

  //NEW STUFF START

  strcpy(apiKey, customAPIKey.getValue());

  if (shouldSaveConfig) {
    Serial.println("saving config");
    DynamicJsonBuffer jsonBuffer;
    JsonObject& json = jsonBuffer.createObject();
    json["defaultHost"] = defaultHost;
    json["apiKey"] = apiKey;
    Serial.println("API");
    Serial.print(apiKey);
    String apiKey2 = String(apiKey);
    File configFile = SPIFFS.open("/config.json", "w");
    if (!configFile) {
      Serial.println("failed to open config file for writing");
    }
json.printTo(configFile);
    json.printTo(Serial);
    delay(1000);
    configFile.close();
    //end save
  }
  Serial.println("local ip");
  Serial.println(WiFi.localIP());
  //END NEW STUFF
    //or use this for auto generated name ESP + ChipID
    //wifiManager.autoConnect();


    //Serial.println("WriteApi");
    //Serial.println(apiKey);


    //if you get here you have connected to the WiFi
    //Serial.println("K)");
     //save the custom parameters to FS

  strcpy(apiKey,customAPIKey.getValue());
   EEPROM.begin(512);  //Initialize EEPROM

  // write appropriate byte of the EEPROM.
  // these values will remain there when the board is
  // turned off.

  EEPROM.write(addr, 'A');    //Write character A
  addr++;                      //Increment address
  EEPROM.write(addr, 'B');    //Write character A
  addr++;                      //Increment address
  EEPROM.write(addr, 'C');    //Write character A

  //Write string to eeprom
  String www = apiKey;
  for(int i=0;i<www.length();i++) //loop upto string lenght www.length() returns length of string
  {
    EEPROM.write(0x0F+i,www[i]); //Write one by one with starting address of 0x0F
  }
  EEPROM.commit();    //Store data to EEPROM


  //Read string from eeprom


}

//callback notifying us of the need to save config


void loop() {

  //new stuff string
recvWithStartEndMarkers();
    if (newData == true) {
        strcpy(tempChars, receivedChars);
            // this temporary copy is necessary to protect the original data
            //   because strtok() used in parseData() replaces the commas with \0
        parseData();
        showParsedData();
        newData = false;
    }
//new stuff string end

 WiFiManager wifiManager;
if (WiFi.status() == WL_DISCONNECTED) {

 wifiManager.autoConnect("AutoConnectAP");}
 delay(1000);
 if (WiFi.status() == WL_CONNECTED) {  Serial.println("Connected");
delay(1000);

WiFiClient client;

 long itt =50;


    long itt2 = 200;
char defaultHost[100] = "api.thingspeak.com";



    EEPROM.begin(512);
    Serial.println(""); //Goto next line, as ESP sends some garbage when you reset it  
  Serial.print(char(EEPROM.read(addr)));    //Read from address 0x00
  addr++;                      //Increment address
  Serial.print(char(EEPROM.read(addr)));    //Read from address 0x01
  addr++;                      //Increment address
  Serial.println(char(EEPROM.read(addr)));    //Read from address 0x02

  //Read string from eeprom
  String www;   
  //Here we dont know how many bytes to read it is better practice to use some terminating character
  //Lets do it manually www.circuits4you.com  total length is 20 characters
  for(int i=0;i<16;i++) 
  {
    www = www + char(EEPROM.read(0x0F+i)); //Read one by one with starting address of 0x0F    
  }  

  Serial.print(www);  //Print the text on serial monitor




    if (client.connect(defaultHost,80))
    { // "184.106.153.149" or api.thingspeak.com
        itt++;  //Replace with a sensor reading or something useful

        String postStr = www;
        postStr +="&field1=";
        postStr += String(floatFromPC);
        postStr +="&field2=";
        postStr += String(floatFromPC2);
        postStr += "\r\n\r\n\r\n";

        client.print("POST /update HTTP/1.1\n");
        client.print("Host: api.thingspeak.com\n");
        client.print("Connection: close\n");
        client.print("X-THINGSPEAKAPIKEY: "+String (www)+"\n");
        client.print("Content-Type: application/x-www-form-urlencoded\n");
        client.print("Content-Length: ");
        client.print(postStr.length());
        client.print("\n\n\n");
        client.print(postStr);

        Serial.println("% send to Thingspeak");
    }

    client.stop();

    Serial.println("Waiting…");

 }

    delay(5000);

}


//more new stuff for string
void recvWithStartEndMarkers() {
    static boolean recvInProgress = false;
    static byte ndx = 0;
    char startMarker = '<';
    char endMarker = '>';
    char rc;


    while (Serial.available() > 0 && newData == false) {
        rc = Serial.read();

        if (recvInProgress == true) {
            if (rc != endMarker) {
                receivedChars[ndx] = rc;
                ndx++;
                if (ndx >= numChars) {
                    ndx = numChars - 1;
                }
            }
            else {
                receivedChars[ndx] = '\0'; // terminate the string
                recvInProgress = false;
                ndx = 0;
                newData = true;
            }
        }

        else if (rc == startMarker) {
            recvInProgress = true;
        }
    }
}

//============

void parseData() {      // split the data into its parts

    char * strtokIndx; // this is used by strtok() as an index

    strtokIndx = strtok(tempChars,",");      // get the first part - the string
    strcpy(messageFromPC, strtokIndx); // copy it to messageFromPC

    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    floatFromPC = atoi(strtokIndx);     // convert this part to an integer

    strtokIndx = strtok(NULL, ",");
    floatFromPC2 = atoi(strtokIndx);     // convert this part to a float

}

//============

void showParsedData() {
    Serial.print("Message ");
    Serial.println(messageFromPC);
    Serial.print("Float ");
    Serial.println(floatFromPC);
    Serial.print("Float ");
    Serial.println(floatFromPC2);
}
//end new stuff string

Sorry for the length but ..... I don't know if I need to modify how I send the string or I have to turn on the TX and RX pins in the Pro Mini. I have tried connecting the RX Arduino to TX ESP and TX Arduino to RX ESP and RX to RX and TX to TX with no results. I know the pins work because I have programed the ESP and the PRO numerous time with the TX and RX pins.

Can anyone help me with this. It is driving me crazy.

  • Are you using the same connections between the ESP and either Arduino? Cross-connecting RX/TX is correct. The Uno is a 5v device; the ESP is a 3.3v device. The Pro-mini could be either. Did you use level shifters or equivalent with the UNO? Does the Pro-mini both need & use them (5v Pro-mini) or not need and not use them (3.3v Pro-mini)? – JRobert Jun 11 at 11:59
  • So I tried some experiments. I had an adapter module attached to the ESP so I removed it and wired directly to the ESP. I went from the Pro's board pins both RX to TX and TX to Rx and the other way around with no upload, no flashing tx led on board I also tried from the header pins at the end of the board both T – user1114881 Jun 11 at 18:09
  • sorry hit return here is the end of the previous comment TX to RX and RX to TX and visa versa. But with no better results. I usually use level shifters but didn't for the experiment on the Arduino Uno and the Arduino Pro Mini. Without the shifter it worked with the UNO and not the Pro Mini. So I don't think it is the pin connections. – user1114881 Jun 11 at 18:20
  • Something about you Pro-min is different from you Uno. (Operating voltage? Frequency? Baud-rate selection?) We don't have enough information (yet) to know what it is, but that's the direction you'll need to take. – JRobert Jun 12 at 11:45
  • So the ESP is running off a separate voltage regulator 3.3v. I have tried all kinds of baud rates on both Pro and ESP. The ESP is supposed to run at 115200 and when I try to reset it it says that the 115200 baud rate is overridden with 115200 but what ever rate I put in the code to override I have to set the serial monitor to that rate to have it read correctly. The Pro says its normal rate is 57600 but I have tried to reset it and nothing seems to help. I need a definitive answer. Is the TXO on the Pro board go to the RX on the ESP and RXI to TX. this is how I have it set up. – user1114881 Jun 12 at 14:39
0

I finally got this to work by repeating the Serial.print (f); and then delay(1000); multiple times. I changed my original post to reflect this. Somehow this gets the string over to the ESP so it can parse it and upload it. Now all I want to do is add a temperature sensor and maybe add an IFTTT to the whole thing so if the low end gets too low or the high end too high (of the water level I can get a notification to check out the whole system. Thanks JRobert for the help I got the power I needed to the board.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.