Add "TARGET_RZ_A1H"as target of using Ethernet IF.
Fork of Milkcocoa_EthernetIF by
Milkcocoa.cpp@7:e706a674790f, 2016-07-11 (annotated)
- Committer:
- 1050186
- Date:
- Mon Jul 11 03:14:05 2016 +0000
- Revision:
- 7:e706a674790f
- Parent:
- 1:4a634c06c5dc
Add "TARGET_RZ_A1H"as the target of using Ethernet IF.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jksoft | 0:23e533c4b1ec | 1 | #include "Milkcocoa.h" |
jksoft | 0:23e533c4b1ec | 2 | |
jksoft | 1:4a634c06c5dc | 3 | #if 0 |
jksoft | 1:4a634c06c5dc | 4 | #if 0 |
jksoft | 0:23e533c4b1ec | 5 | #include "SoftSerialSendOnry.h" |
jksoft | 0:23e533c4b1ec | 6 | extern SoftSerialSendOnry pc; |
jksoft | 1:4a634c06c5dc | 7 | #else |
jksoft | 1:4a634c06c5dc | 8 | extern Serial pc; |
jksoft | 1:4a634c06c5dc | 9 | #endif |
jksoft | 0:23e533c4b1ec | 10 | #define DBG(x) x |
jksoft | 0:23e533c4b1ec | 11 | #else |
jksoft | 0:23e533c4b1ec | 12 | #define DBG(x) |
jksoft | 0:23e533c4b1ec | 13 | #endif |
jksoft | 0:23e533c4b1ec | 14 | |
jksoft | 0:23e533c4b1ec | 15 | DataElement::DataElement() { |
jksoft | 0:23e533c4b1ec | 16 | json_msg[0] = '\0'; |
jksoft | 0:23e533c4b1ec | 17 | strcpy(json_msg,"{\"params\":{"); |
jksoft | 0:23e533c4b1ec | 18 | } |
jksoft | 0:23e533c4b1ec | 19 | |
jksoft | 0:23e533c4b1ec | 20 | DataElement::DataElement(char *json_string) { |
jksoft | 0:23e533c4b1ec | 21 | json_msg[0] = '\0'; |
jksoft | 0:23e533c4b1ec | 22 | strcpy(json_msg,json_string); |
jksoft | 0:23e533c4b1ec | 23 | } |
jksoft | 0:23e533c4b1ec | 24 | |
jksoft | 0:23e533c4b1ec | 25 | void DataElement::setValue(const char *key, const char *v) { |
jksoft | 0:23e533c4b1ec | 26 | char json_string[64]; |
jksoft | 0:23e533c4b1ec | 27 | if( json_msg[strlen(json_msg)-1] != '{' ) |
jksoft | 0:23e533c4b1ec | 28 | { |
jksoft | 0:23e533c4b1ec | 29 | strcat(json_msg,","); |
jksoft | 0:23e533c4b1ec | 30 | } |
jksoft | 0:23e533c4b1ec | 31 | sprintf(json_string,"\"%s\":\"%s\"",key,v); |
jksoft | 0:23e533c4b1ec | 32 | strcat(json_msg,json_string); |
jksoft | 0:23e533c4b1ec | 33 | } |
jksoft | 0:23e533c4b1ec | 34 | |
jksoft | 0:23e533c4b1ec | 35 | void DataElement::setValue(const char *key, int v) { |
jksoft | 0:23e533c4b1ec | 36 | char json_string[64]; |
jksoft | 0:23e533c4b1ec | 37 | if( json_msg[strlen(json_msg)-1] != '{' ) |
jksoft | 0:23e533c4b1ec | 38 | { |
jksoft | 0:23e533c4b1ec | 39 | strcat(json_msg,","); |
jksoft | 0:23e533c4b1ec | 40 | } |
jksoft | 0:23e533c4b1ec | 41 | sprintf(json_string,"\"%s\":\"%d\"",key,v); |
jksoft | 0:23e533c4b1ec | 42 | strcat(json_msg,json_string); |
jksoft | 0:23e533c4b1ec | 43 | } |
jksoft | 0:23e533c4b1ec | 44 | |
jksoft | 0:23e533c4b1ec | 45 | void DataElement::setValue(const char *key, double v) { |
jksoft | 0:23e533c4b1ec | 46 | char json_string[64]; |
jksoft | 0:23e533c4b1ec | 47 | if( json_msg[strlen(json_msg)-1] != '{' ) |
jksoft | 0:23e533c4b1ec | 48 | { |
jksoft | 0:23e533c4b1ec | 49 | strcat(json_msg,","); |
jksoft | 0:23e533c4b1ec | 50 | } |
jksoft | 0:23e533c4b1ec | 51 | sprintf(json_string,"\"%s\":\"%f\"",key,v); |
jksoft | 0:23e533c4b1ec | 52 | strcat(json_msg,json_string); |
jksoft | 0:23e533c4b1ec | 53 | } |
jksoft | 0:23e533c4b1ec | 54 | |
jksoft | 0:23e533c4b1ec | 55 | char *DataElement::getString(const char *key) { |
jksoft | 0:23e533c4b1ec | 56 | static char _word[64]; |
jksoft | 0:23e533c4b1ec | 57 | char *p; |
jksoft | 0:23e533c4b1ec | 58 | int i=0; |
jksoft | 0:23e533c4b1ec | 59 | |
jksoft | 0:23e533c4b1ec | 60 | strcpy(_word , "\"\0"); |
jksoft | 0:23e533c4b1ec | 61 | strcat(_word , key ); |
jksoft | 0:23e533c4b1ec | 62 | strcat(_word , "\"" ); |
jksoft | 0:23e533c4b1ec | 63 | |
jksoft | 0:23e533c4b1ec | 64 | p = strstr( (char*)json_msg , _word ) + 2 + strlen(_word); |
jksoft | 0:23e533c4b1ec | 65 | |
jksoft | 0:23e533c4b1ec | 66 | while( (p[i] != ',')&&(p[i] != '\n')&&(p[i] != '\"') ) |
jksoft | 0:23e533c4b1ec | 67 | { |
jksoft | 0:23e533c4b1ec | 68 | _word[i] = p[i]; |
jksoft | 0:23e533c4b1ec | 69 | i++; |
jksoft | 0:23e533c4b1ec | 70 | } |
jksoft | 0:23e533c4b1ec | 71 | _word[i] = '\0'; |
jksoft | 0:23e533c4b1ec | 72 | |
jksoft | 0:23e533c4b1ec | 73 | return _word; |
jksoft | 0:23e533c4b1ec | 74 | } |
jksoft | 0:23e533c4b1ec | 75 | |
jksoft | 0:23e533c4b1ec | 76 | int DataElement::getInt(const char *key) { |
jksoft | 0:23e533c4b1ec | 77 | return atoi(getString(key)); |
jksoft | 0:23e533c4b1ec | 78 | } |
jksoft | 0:23e533c4b1ec | 79 | |
jksoft | 0:23e533c4b1ec | 80 | float DataElement::getFloat(const char *key) { |
jksoft | 0:23e533c4b1ec | 81 | return atof(getString(key)); |
jksoft | 0:23e533c4b1ec | 82 | } |
jksoft | 0:23e533c4b1ec | 83 | |
jksoft | 0:23e533c4b1ec | 84 | char *DataElement::toCharArray() { |
jksoft | 0:23e533c4b1ec | 85 | if( json_msg[strlen(json_msg)-1] != '{' ) |
jksoft | 0:23e533c4b1ec | 86 | { |
jksoft | 0:23e533c4b1ec | 87 | strcat(json_msg,"}"); |
jksoft | 0:23e533c4b1ec | 88 | } |
jksoft | 0:23e533c4b1ec | 89 | strcat(json_msg,"}"); |
jksoft | 0:23e533c4b1ec | 90 | |
jksoft | 0:23e533c4b1ec | 91 | return(json_msg); |
jksoft | 0:23e533c4b1ec | 92 | } |
jksoft | 0:23e533c4b1ec | 93 | |
jksoft | 1:4a634c06c5dc | 94 | Milkcocoa::Milkcocoa(MClient *_client, const char *host, uint16_t port, const char *_app_id, const char *client_id) { |
jksoft | 0:23e533c4b1ec | 95 | |
jksoft | 1:4a634c06c5dc | 96 | client = _client; |
jksoft | 0:23e533c4b1ec | 97 | strcpy(servername,host); |
jksoft | 0:23e533c4b1ec | 98 | portnum = port; |
jksoft | 0:23e533c4b1ec | 99 | app_id = _app_id; |
jksoft | 0:23e533c4b1ec | 100 | strcpy(_clientid,client_id); |
jksoft | 0:23e533c4b1ec | 101 | strcpy(username,"sdammy"); |
jksoft | 0:23e533c4b1ec | 102 | strcpy(password,app_id); |
jksoft | 0:23e533c4b1ec | 103 | |
jksoft | 0:23e533c4b1ec | 104 | } |
jksoft | 0:23e533c4b1ec | 105 | |
jksoft | 1:4a634c06c5dc | 106 | Milkcocoa::Milkcocoa(MClient *_client, const char *host, uint16_t port, const char *_app_id, const char *client_id, char *_session) { |
jksoft | 0:23e533c4b1ec | 107 | |
jksoft | 1:4a634c06c5dc | 108 | client = _client; |
jksoft | 0:23e533c4b1ec | 109 | strcpy(servername,host); |
jksoft | 0:23e533c4b1ec | 110 | portnum = port; |
jksoft | 0:23e533c4b1ec | 111 | app_id = _app_id; |
jksoft | 0:23e533c4b1ec | 112 | strcpy(_clientid,client_id); |
jksoft | 0:23e533c4b1ec | 113 | strcpy(username,_session); |
jksoft | 0:23e533c4b1ec | 114 | strcpy(password,app_id); |
jksoft | 0:23e533c4b1ec | 115 | |
jksoft | 0:23e533c4b1ec | 116 | } |
jksoft | 0:23e533c4b1ec | 117 | |
jksoft | 1:4a634c06c5dc | 118 | Milkcocoa* Milkcocoa::createWithApiKey(MClient *_client, const char *host, uint16_t port, const char *_app_id, const char *client_id, char *key, char *secret) { |
jksoft | 0:23e533c4b1ec | 119 | char session[60]; |
jksoft | 0:23e533c4b1ec | 120 | sprintf(session, "k%s:%s", key, secret); |
jksoft | 1:4a634c06c5dc | 121 | return new Milkcocoa(_client, host, port, _app_id, client_id, session); |
jksoft | 0:23e533c4b1ec | 122 | } |
jksoft | 0:23e533c4b1ec | 123 | |
jksoft | 0:23e533c4b1ec | 124 | void Milkcocoa::connect() { |
jksoft | 0:23e533c4b1ec | 125 | |
jksoft | 1:4a634c06c5dc | 126 | if(client->isConnected()) |
jksoft | 0:23e533c4b1ec | 127 | return; |
jksoft | 1:4a634c06c5dc | 128 | |
jksoft | 1:4a634c06c5dc | 129 | if(client->connect(servername, portnum)!=0) { |
jksoft | 0:23e533c4b1ec | 130 | DBG(pc.printf("Network connect err\r\n");) |
jksoft | 0:23e533c4b1ec | 131 | return; |
jksoft | 0:23e533c4b1ec | 132 | } |
jksoft | 0:23e533c4b1ec | 133 | |
jksoft | 0:23e533c4b1ec | 134 | MQTTPacket_connectData data = MQTTPacket_connectData_initializer; |
jksoft | 0:23e533c4b1ec | 135 | data.keepAliveInterval = 20; |
jksoft | 0:23e533c4b1ec | 136 | data.cleansession = 1; |
jksoft | 0:23e533c4b1ec | 137 | data.MQTTVersion = 4; |
jksoft | 0:23e533c4b1ec | 138 | data.clientID.cstring = _clientid; |
jksoft | 0:23e533c4b1ec | 139 | data.username.cstring = username; |
jksoft | 0:23e533c4b1ec | 140 | data.password.cstring = password; |
jksoft | 0:23e533c4b1ec | 141 | |
jksoft | 1:4a634c06c5dc | 142 | if (client->connect(data) != 0) { |
jksoft | 0:23e533c4b1ec | 143 | DBG(pc.printf("Milkcocoa connect err\r\n");) |
jksoft | 0:23e533c4b1ec | 144 | return; |
jksoft | 0:23e533c4b1ec | 145 | } |
jksoft | 0:23e533c4b1ec | 146 | } |
jksoft | 0:23e533c4b1ec | 147 | |
jksoft | 0:23e533c4b1ec | 148 | bool Milkcocoa::push(const char *path, DataElement dataelement) { |
jksoft | 0:23e533c4b1ec | 149 | char topic[100]; |
jksoft | 0:23e533c4b1ec | 150 | char *buf; |
jksoft | 0:23e533c4b1ec | 151 | MQTT::Message message; |
jksoft | 0:23e533c4b1ec | 152 | |
jksoft | 0:23e533c4b1ec | 153 | sprintf(topic, "%s/%s/push", app_id, path); |
jksoft | 0:23e533c4b1ec | 154 | |
jksoft | 0:23e533c4b1ec | 155 | message.qos = MQTT::QOS0; |
jksoft | 0:23e533c4b1ec | 156 | message.retained = 0; |
jksoft | 0:23e533c4b1ec | 157 | message.dup = false; |
jksoft | 0:23e533c4b1ec | 158 | buf = dataelement.toCharArray(); |
jksoft | 0:23e533c4b1ec | 159 | message.payload = (void*)buf; |
jksoft | 0:23e533c4b1ec | 160 | message.payloadlen = strlen(buf); |
jksoft | 1:4a634c06c5dc | 161 | if(client->publish(topic, message)!=0) |
jksoft | 0:23e533c4b1ec | 162 | return(false); |
jksoft | 0:23e533c4b1ec | 163 | |
jksoft | 0:23e533c4b1ec | 164 | return true; |
jksoft | 0:23e533c4b1ec | 165 | } |
jksoft | 0:23e533c4b1ec | 166 | |
jksoft | 0:23e533c4b1ec | 167 | bool Milkcocoa::send(const char *path, DataElement dataelement) { |
jksoft | 0:23e533c4b1ec | 168 | char topic[100]; |
jksoft | 0:23e533c4b1ec | 169 | char *buf; |
jksoft | 0:23e533c4b1ec | 170 | MQTT::Message message; |
jksoft | 0:23e533c4b1ec | 171 | |
jksoft | 0:23e533c4b1ec | 172 | sprintf(topic, "%s/%s/send", app_id, path); |
jksoft | 0:23e533c4b1ec | 173 | message.qos = MQTT::QOS0; |
jksoft | 0:23e533c4b1ec | 174 | message.retained = 0; |
jksoft | 0:23e533c4b1ec | 175 | message.dup = false; |
jksoft | 0:23e533c4b1ec | 176 | buf = dataelement.toCharArray(); |
jksoft | 0:23e533c4b1ec | 177 | message.payload = (void*)buf; |
jksoft | 0:23e533c4b1ec | 178 | message.payloadlen = strlen(buf); |
jksoft | 1:4a634c06c5dc | 179 | if(client->publish(topic, message)!=0) |
jksoft | 0:23e533c4b1ec | 180 | return false; |
jksoft | 0:23e533c4b1ec | 181 | |
jksoft | 0:23e533c4b1ec | 182 | return true; |
jksoft | 0:23e533c4b1ec | 183 | } |
jksoft | 0:23e533c4b1ec | 184 | |
jksoft | 0:23e533c4b1ec | 185 | void Milkcocoa::loop() { |
jksoft | 0:23e533c4b1ec | 186 | connect(); |
jksoft | 1:4a634c06c5dc | 187 | client->yield(RECV_TIMEOUT); |
jksoft | 0:23e533c4b1ec | 188 | } |
jksoft | 0:23e533c4b1ec | 189 | |
jksoft | 0:23e533c4b1ec | 190 | bool Milkcocoa::on(const char *path, const char *event, GeneralFunction cb) { |
jksoft | 1:4a634c06c5dc | 191 | MilkcocoaSubscriber *sub = new MilkcocoaSubscriber(cb); |
jksoft | 1:4a634c06c5dc | 192 | sprintf(sub->topic, "%s/%s/%s", app_id, path, event); |
jksoft | 0:23e533c4b1ec | 193 | |
jksoft | 1:4a634c06c5dc | 194 | if (client->subscribe(sub->topic, MQTT::QOS0, cb) != 0) { |
jksoft | 0:23e533c4b1ec | 195 | DBG(pc.printf("Milkcocoa subscribe err\r\n");) |
jksoft | 0:23e533c4b1ec | 196 | return false; |
jksoft | 0:23e533c4b1ec | 197 | } |
jksoft | 1:4a634c06c5dc | 198 | for (int i=0; i<MILKCOCOA_SUBSCRIBERS; i++) { |
jksoft | 1:4a634c06c5dc | 199 | if (milkcocoaSubscribers[i] == sub) { |
jksoft | 1:4a634c06c5dc | 200 | return false; |
jksoft | 1:4a634c06c5dc | 201 | } |
jksoft | 1:4a634c06c5dc | 202 | } |
jksoft | 1:4a634c06c5dc | 203 | for (int i=0; i<MILKCOCOA_SUBSCRIBERS; i++) { |
jksoft | 1:4a634c06c5dc | 204 | if (milkcocoaSubscribers[i] == 0) { |
jksoft | 1:4a634c06c5dc | 205 | milkcocoaSubscribers[i] = sub; |
jksoft | 1:4a634c06c5dc | 206 | return true; |
jksoft | 1:4a634c06c5dc | 207 | } |
jksoft | 1:4a634c06c5dc | 208 | } |
jksoft | 0:23e533c4b1ec | 209 | return true; |
jksoft | 0:23e533c4b1ec | 210 | } |
jksoft | 0:23e533c4b1ec | 211 | |
jksoft | 1:4a634c06c5dc | 212 | MilkcocoaSubscriber::MilkcocoaSubscriber(GeneralFunction _cb) { |
jksoft | 1:4a634c06c5dc | 213 | cb = _cb; |
jksoft | 1:4a634c06c5dc | 214 | } |