Blame view

Programmation/RSSI.ino 4.28 KB
3ab1f346   rfoucaul   Modification fina...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
  /*
    WriteSingleField
    
    Description: Writes a value to a channel on ThingSpeak every 20 seconds.
    
    Hardware: Arduino compatible hardware controlling ESP8266 through AT commands. THIS IS CODE FOR THE ARDUINO, NOT THE ESP8266!
    
    !!! IMPORTANT - Modify the secrets.h file for this project with your network connection and ThingSpeak channel details. !!!
    
    Note:
    - Requires Arduino main board connected to ESP8266 (ESP-01) serial pins
    - The ESP8266 device must be running firmware capable of AT commands over TX/RX pins. 
        Details on reflashing and binaries can be found here: https://www.espressif.com/en/support/download/at  
    - Requires WiFiEsp library which is available through the Library Manager. The logging level for the WiFiEsp library is set to INFO.
        To disable logging completely, set _ESPLOGLEVEL_ to 0 in \Arduino\libraries\WiFiEsp\src\utility\debug.h
    - Use TX1/RX1 if present on Arduino header (Mega, Due, etc). If not, then connect TX to pin 7, RX to pin 6.
    - Some boards (Uno, Nano, Leonardo, etc) do not have enough memory to handle writing large strings to ThingSpeak.
        For these boards, keep individual field data lengths 32 characters or less.  
    - This example is written for a network using WPA encryption. For WEP or WPA, change the WiFi.begin() call accordingly.
    
    Wiring diagrams are available at:
    SoftSerial (Uno, Nano, Mini, etc): https://github.com/mathworks/thingspeak-arduino/blob/master/ESP-01_AT_Commands_SoftSerial_Hookup.pdf
    Hardware Serial1 (Mega, Leonardo, Due) - https://github.com/mathworks/thingspeak-arduino/blob/master/ESP-01_AT_Commands_Hardware_Serial_Hookup.pdf
    
      ESP8266 | Arduino without Serial1 | Arduino with Serial1  
      --------------------------------------------------------
         RX   |         pin 7           |        TX1
         TX   |         pin 6           |        RX1
         GND  |          GND            |        GND
         VCC  |          5V             |        5V
        CH_PD |          5V             |        5V
      
    ThingSpeak ( https://www.thingspeak.com ) is an analytic IoT platform service that allows you to aggregate, visualize, and 
    analyze live data streams in the cloud. Visit https://www.thingspeak.com to sign up for a free account and create a channel.  
    
    Documentation for the ThingSpeak Communication Library for Arduino is in the README.md folder where the library was installed.
    See https://www.mathworks.com/help/thingspeak/index.html for the full ThingSpeak documentation.
    
    For licensing information, see the accompanying license file.
    
    Copyright 2019, The MathWorks, Inc.
  */
  
  
  
  #include "WiFiEsp.h"
  
  char ssid[] = SECRET_SSID;    //  your network SSID (name) 
  char pass[] = SECRET_PASS;   // your network password
  
  WiFiEspClient  client;
  
  #include "SoftwareSerial.h"
  SoftwareSerial Serial1(6, 7);
  #define ESP_BAUDRATE  115200
  
  
  void setup() {
    //Initialize serial and wait for port to open
    Serial.begin(9600);  
    
    // initialize serial for ESP module  
    setEspBaudRate(ESP_BAUDRATE);
    
    Serial.print("Searching for ESP8266..."); 
    // initialize ESP module
    WiFi.init(&Serial1);
    
  }
  
  void loop() {
    mesureRSSI();
    delay(5000);
  }
  
  // This function attempts to set the ESP8266 baudrate. Boards with additional hardware serial ports
  // can use 115200, otherwise software serial is limited to 19200.
  void setEspBaudRate(unsigned long baudrate){
    long rates[6] = {115200,74880,57600,38400,19200,9600};
    
    Serial.print("Setting ESP8266 baudrate to ");
    Serial.print(baudrate);
    Serial.println("...");
    
    for(int i = 0; i < 6; i++){
      Serial1.begin(rates[i]);
      delay(100);
      Serial1.print("AT+UART_DEF=");
      Serial1.print(baudrate);
      Serial1.print(",8,1,0,0\r\n");
      delay(100);  
    }
    Serial1.begin(baudrate);
  }
  
  void mesureRSSI(){
    Serial.println("Prise de mesure\n");
    // Connect or reconnect to WiFi
    if(WiFi.status() != WL_CONNECTED){
      //Serial.print("Attempting to connect to SSID: ");
      //Serial.println(SECRET_SSID);
      while(WiFi.status() != WL_CONNECTED){
        WiFi.begin(ssid, pass);  // Connect to WPA/WPA2 network. Change this line if using open or WEP network
        //Serial.print(".");
        delay(100);     
      } 
      //Serial.println("\nConnected");
    }
    
    int rssi = 0;
    while(rssi==0){
      rssi = WiFi.RSSI();
    }
    Serial.print("RSSI : ");
    Serial.println(rssi);
  }